Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python拼接文本文件的一行_Python_Bioinformatics - Fatal编程技术网

使用python拼接文本文件的一行

使用python拼接文本文件的一行,python,bioinformatics,Python,Bioinformatics,我正在努力创造基因特征。我有一个装满DNA序列的文本文件。我想读入文本文件中的每一行。然后在字典中添加4个字母,它们是4个碱基。 例如:样本序列 ATGATATCATCAT 我想补充的是ATGA、TGAT、GATA等。。添加到一个ID为的字典中,在添加4mers时只增加1 所以这本字典可以 Genetic signatures, ID ATGA,1 TGAT, 2 GATA,3 这是我到目前为止所拥有的 import sys def main (): readingFile =

我正在努力创造基因特征。我有一个装满DNA序列的文本文件。我想读入文本文件中的每一行。然后在字典中添加4个字母,它们是4个碱基。 例如:样本序列

ATGATATCATCAT

我想补充的是ATGA、TGAT、GATA等。。添加到一个ID为的字典中,在添加4mers时只增加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

此输出表明它没有遍历字符串中的每个字符。。。请帮忙

您需要在循环之前移动
索引
DnaID
声明,否则每次循环迭代都会重置它们:

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

索引
每次都会通过以my_DNA中字符的
开头的循环重置为0:


此外,我认为循环条件应该类似于
,而索引
,以与循环体一致。

由于索引计数器在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',…}
还是应该省略它们?

如果我理解正确,您正在计算每个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
仅包含“ATGATATCATCAT”的文件上的输出:


这会给你想要的效果

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

谢谢你们,我成功了。。简单解决方案..@蓝木。。。我包含了一个if-else语句来检查字典中的序列(键),如果它已经在字典中,我不会将它添加到字典中。如果它不在字典中,那么我添加它为什么while循环为true?这不是一个条件吗?我认为这是一个很好的方法来循环具有可变长度的内容,而while循环似乎比for循环更可行。在这种情况下,循环将一直运行到最后一个可能的4merMuchas gracias!非常感谢你的摇滚乐@布鲁克林契克:很高兴能帮忙。我建议您选择最有帮助的答案,并勾选该答案以表明您已接受该答案。如果你不确定该选择哪一个,我建议你选择c4p的答案,因为它对我来说似乎是最完整的。谢谢你,西蒙。我怎么打勾?这是我第一次问一个关于stackoverflow的问题。@brooklynchick:每个答案下面都应该有一个复选框提纲。单击该选项接受您喜欢的答案。请参阅“我该如何在这里提问?”下的内容,以了解更多信息和一个示意图,该示意图向您展示了如何提问。谢谢,我现在就收到了:)谢谢您的好意!
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