5

我正在尝试创建遗传特征。我有一个充满 DNA 序列的文本文件。我想从文本文件中读取每一行。然后将 4 个碱基的 4mers 添加到字典中。例如:样本序列

ATGATATATCTATCAT

我要添加的是 ATGA、TGAT、GATA 等。在添加 4mer 时,ID 仅增加 1 的字典。

所以字典会保留...

Genetic signatures, ID
ATGA,1
TGAT, 2
GATA,3

这是我到目前为止...

import sys  

def main ():
    readingFile = open("signatures.txt", "r")
    my_DNA=""

    DNAseq = {} #creates dictionary 

    for char in readingFile:
        my_DNA = my_DNA+char

    for char in my_DNA:             
        index = 0
        DnaID=1
        seq = my_DNA[index:index+4]         

        if (DNAseq.has_key(seq)): #checks if the key is in the dictionary
            index= index +1
        else :
            DNAseq[seq] = DnaID
            index = index+1
            DnaID= DnaID+1

    readingFile.close()

if __name__ == '__main__':
    main()

这是我的输出:

ACTC
ACTC
ACTC
ACTC
ACTC
ACTC

此输出表明它没有遍历字符串中的每个字符......请帮助!

4

5 回答 5

6

您需要在循环之前移动您的indexDnaID声明,否则它们将在每次循环迭代时被重置:

index = 0
DnaID=1
for char in my_DNA:             
    #... rest of loop here

进行更改后,您将获得以下输出:

ATGA 1
TGAT 2
GATA 3
ATAT 4
TATA 5
ATAT 6
TATC 6
ATCT 7
TCTA 8
CTAT 9
TATC 10
ATCA 10
TCAT 11
CAT 12
AT 13
T 14

为了避免最后 3 个项目的长度不正确,您可以修改循环:

for i in range(len(my_DNA)-3):
    #... rest of loop here

这不会遍历最后 3 个字符,从而产生输出:

ATGA 1
TGAT 2
GATA 3
ATAT 4
TATA 5
ATAT 6
TATC 6
ATCT 7
TCTA 8
CTAT 9
TATC 10
ATCA 10
TCAT 11
于 2013-04-05T02:26:28.750 回答
2

这应该会给你想要的效果。

from collections import defaultdict

readingFile = open("signatures.txt", "r").read()
DNAseq      = defaultdict(int)
window      = 4

for i in xrange(len(readingFile)):
    current_4mer = readingFile[i:i+window]
    if len(current_4mer) == window:
        DNAseq[current_4mer] += 1

print DNAseq
于 2013-04-05T02:41:54.743 回答
1

index每次通过以 开头的循环都被重置为 0 for char in my_DNA:

另外,我认为循环条件应该while index < len(my_DNA)-4:与循环体一致。

于 2013-04-05T02:27:32.677 回答
1

您的索引计数器会自行重置,因为它们处于 for 循环中。

我可以提出一些进一步的建议吗?我的解决方案如下所示:

readingFile = open("signatures.txt", "r")
my_DNA=""

DNAseq = {} #creates dictionary 

for line in readingFile:    
    line = line.strip()
    my_DNA = my_DNA + line

ID = 1
index = 0
while True:

    try:
        seq = my_DNA[index:index+4]
        if not seq in my_DNA:
            DNAseq[ID] = my_DNA[index:index+4]
        index += 4
        ID += 1
    except IndexError:
        break

readingFile.close()

但是你想对重复项做什么?例如,如果像 ATGC 这样的序列出现两次?例如,两者都应该添加到不同的 ID 下,{...1:'ATGC', ... 200:'ATGC',...}还是应该省略?

于 2013-04-05T02:28:30.540 回答
0

如果我理解正确,您正在计算每个连续的 4 个碱基串出现的频率?试试这个:

def split_to_4mers(filename):
    dna_dict = {}
    with open(filename, 'r') as f:
        # assuming the first line of the file, only, contains the dna string
        dna_string = f.readline();
        for idx in range(len(dna_string)-3):
            seq = dna_string[idx:idx+4]
            count = dna_dict.get(seq, 0)
            dna_dict[seq] = count+1
    return dna_dict

在仅包含“ATGATATATCTATCAT”的文件上输出:

{'TGAT': 1, 'ATCT': 1, 'ATGA': 1, 'TCAT': 1, 'TATA': 1, 'TATC': 2, 'CTAT': 1, 'ATCA': 1, 'ATAT': 2, 'GATA': 1, 'TCTA': 1}
于 2013-04-05T02:34:56.940 回答