【发布时间】:2020-11-14 22:54:18
【问题描述】:
我一直在研究这个问题很长时间,但我似乎无法正确解决这个问题。我已经尝试了我所知道的一切,我真的需要帮助找出问题所在。
我有两个文件: 文件 1:序列的名称和数量列表,即:
name,AGAT,AATG,TATC
Jake,28,42,14
Chris,17,22,19
Anne,36,18,25
和文件二:一串DNA "GCTAAATTTGTTCAGCCAGATGTAGGCTTACAAATCAAGCTGTCCGCTCGGCACGGCCTACACACGT..."
这个想法是实施一个程序,该程序可以根据他们的 DNA 来识别一个人。遍历文件 2,并计算文件 1 中提供的序列的出现次数。如果两个文件中出现的次数匹配,则返回名称。不幸的是,我似乎无法获得第二个文件的正确“总数”。
这是我目前所拥有的:
Python:
with open(argv[1], 'r') as csvfile:
csvfile_data = csv.reader(csvfile)
next(csvfile_data) #skip first line
for row in csvfile_data:
list_temp = row
# copy elements into a new list
temp = []
temp.extend(list_temp)
# remove the first element, because its the name
name = temp.pop(0)
# the values attached to the name
csvlist = temp
#change strings in list to integers
csvlist = [int(i) for i in csvlist]
# open dna sequence also
with open(argv[2], 'r') as dnafile:
dnafile_data = dnafile.read()
#use regular expressions to find each sequence's occurence in the file
patterns = re.compile(r'AGATC|TTTTTTCT|AATG|TCTAG|GATA|TATC|GAAA|TCTG')
result = re.findall(patterns, dnafile_data)
#count each sequence's occurence
dictionary = Counter(result)
#split the key sand values into a new list
dnalist = dictionary.values()
print(dnalist)
if collections.Counter(csvlist) == collections.Counter(dnalist):
print(name)
else:
print("No match")
```
【问题讨论】:
-
我认为这需要一个(简短的)示例来说明输入数据是什么,才能开始回答。我也不清楚您期望重叠序列会发生什么。
-
@DavidW 感谢您的回复,我对此有点陌生。我将编辑输入数据的示例并将其添加到问题中。
-
不知道我害怕。它的后半部分看起来应该可以工作(假设序列不重叠)。
csvlist仅用于您的第一个文件的最后一行。
标签: python csv dna-sequence