私の最後の質問の続き(Matching two string lists that partially match into another list)別の問題がつまずいた。おそらく、私は最初に、これまでのコードを投稿する必要があります:私はトラブルは何辞書で検索し、キーと値をマージしてリストに入れる
#!/usr/bin/env python3.4
# -*- coding: utf-8 -*-
import random
import timeit
def generateSequences(n):
RandomSequences = []
dna = ["A","G","C","T"]
for i in range(int(n)):
randseq=''
for i in range(50):
randseq+=random.choice(dna)
RandomSequences.append(randseq)
return RandomSequences
def generatePrefixes(p, RandomSequences):
First20Chars = [x[:20] for x in RandomSequences]
RandomChoices = []
for i in range(p):
randomPrefix = random.choice(First20Chars)
RandomChoices.append(randomPrefix)
return First20Chars, RandomChoices
def searchReadsInList(RandomSequences, RandomChoices):
start_time = timeit.default_timer()
Matches_RS_RC = []
for i in RandomChoices:
for j in RandomSequences:
if i in j:
Matches_RS_RC.append(j)
elapsed = timeit.default_timer() - start_time
return Matches_RS_RC, elapsed
def makeSuffixDict(reads, extSize = 30, verbose = True):
"""
Generates a dictionary with read-suffixes as values from a list of reads.
Arguments:
reads: list of str, reads for generating the dictionary
extSize: int, length of suffixes stored as values in dict
verbose: bool, whether to print feedback about results
Returns:
suffixDict: dict, dictionary with read-suffixes as values
"""
dict = {}
if not verbose:
for i, read in enumerate(reads):
prefix = read[0:-extSize]
suffix = read[-extSize:]
if prefix not in dict:
dict[prefix] = [suffix]
else:
dict[prefix].append(suffix)
else:
unambiguous = set()
ambiguous = set()
for i, read in enumerate(reads):
prefix = read[0:-extSize]
suffix = read[-extSize:]
if prefix not in dict:
dict[prefix] = [suffix]
unambiguous.add(prefix)
else:
dict[prefix].append(suffix)
if suffix in unambiguous:
unambiguous.remove(suffix)
ambiguous.add(prefix)
print("Reads: ", len(reads), "\n",
"Keys: ", len(dict), "\n",
"Unambiguous: ", len(unambiguous), "\n",
"Ambiguous: ", len(ambiguous), sep = "")
return(dict)
def searchReadsInDict(RandomSequences, RandomChoices):
makeSuffixDict(RandomSequences)
Matches_RC_Dict = []
for i in RandomChoices:
for j in dict:
if i in j:
Matches_RC_Dict.append(j)
return Matches_RC_Dict
if __name__ == "__main__":
RandomSequences = generateSequences(15)
print ("genseq", RandomSequences)
First20Chars, RandomChoices = generatePrefixes(5, RandomSequences)
print ("genpre1", First20Chars)
print ("genpre2", RandomChoices)
Matches_RS_RC, elapsed = searchReadsInList(RandomSequences, RandomChoices)
print ("searchList", Matches_RS_RC)
print ("Time elapsed", elapsed)
Matches_RC_Dict = searchReadsInDict (RandomSequences, RandomChoices)
print ("SearchDict", Matches_RC_Dict)
はsearchReadsInDict
です。 searchReadsInDict
辞書を作成するには、付属のmakeSuffixDict
を使用する必要があります。 generatePrefixes
でランダムに選択した文字列を辞書のキーと照合し、キーと値を文字列に結合してリストに入れる必要があります。キーと値を文字列にまとめるためには、いくつかのメソッドがありましたが、それ自体は機能していましたが、それらを関数に統合すると常にエラーが発生しました。 Matches_RC_Dict
の出力と同じように実行しても、[]
という問題があります。 誰かが助けてくれることを願っています。私は他人のために不要な作業を防ぐために、彼の偉大な答えを投稿したいと思いfreenodeの上#pythonチャンネルから「cyphase」の許可を得て
あなたがより良い変数名を使用する場合、これは明確になります大文字より:) – Will
私はコードを編集しました。今はっきりしていることを願っています。 – grindbert