簡體   English   中英

從文件中讀取序列作為字符串而不是單個字母 python

[英]reading in sequences from file as strings not individual letters python

我有三個文件,它們看起來像這樣:

>xx_oneFish |xxx
AAAAAAA
>xx_twoFish |xxx
CCCCCC
>xx_redFish |xxx
TTTTTT
>xx_blueFish |xxx
GGGGGG
>xx_oneFish |xxx
aaaa
>xx_twoFish |xxx
cccc
>xx_redFish |xxx
tt
>xx_blueFish |xxx
gg

我正在嘗試使用 python 讀取這些文件以獲得以下結果:

[[ 'aaaa', 'cccc'], ['tt', 'gg'], [ 'AAAAAAA', 'CCCCCC', 'TTTTTT', 'GGGGGG']]

這是我的代碼:

testNames = []
testSequences = []
counter = 0
for filename in os.listdir("/PATH/TO/FILE"): #go to directory where aligned files are kept
    if filename.endswith(".txt"): #open files which have been aligned with MAFFT
        fastaFile = open(filename, 'r') 
        testNames.append([])
        testSequences.append([])
        for line in fastaFile: 
            line = line.strip() 
            if len(line)>0: 
                if line[0] == '>':  
                    testNames[counter].append(line[1:]) 
                    testSequences.append("") 
                    currentTaxon = len(testSequences)-1 
                else: 
                    testSequences[currentTaxon] += line 
        counter +=1

print testSequences

這給了我這個結果:

[[], 'aaaa', 'cccc', [], 'tt', 'gg', [], 'AAAAAAA', 'CCCCCC', 'TTTTTT', 'GGGGGG']

我試圖通過取出第 14 行將我的代碼更改為但括號內的字符串:

testNames = []
testSequences = []
counter = 0
for filename in os.listdir("/PATH/TO/FILE"): #go to directory where aligned files are kept
    if filename.endswith(".txt"): #open files which have been aligned with MAFFT
        fastaFile = open(filename, 'r') 
        testNames.append([])
        testSequences.append([])
        for line in fastaFile: 
            line = line.strip() 
            if len(line)>0: 
                if line[0] == '>':  
                    testNames[counter].append(line[1:]) 
                    currentTaxon = len(testSequences)-1 
                else: 
                    testSequences[currentTaxon] += line 
        counter +=1

print testSequences

現在我得到這個結果:

[['a', 'a', 'a', 'a', 'c', 'c', 'c', 'c'], ['t', 't', 'g', 'g'], ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'C', 'C', 'C', 'C', 'C', 'C', 'T', 'T', 'T', 'T', 'T', 'T', 'G', 'G', 'G', 'G', 'G', 'G']]

如何修復我的代碼以在嵌套列表中將序列作為字符串讀入?

我想保持列表 testNames 的內容不變:

[['xx_oneFish |xxx', 'xx_twoFish |xxx'], ['xx_redFish |xxx', 'xx_blueFish |xxx'], ['xx_oneFish |xxx', 'xx_twoFish |xxx', 'xx_redFish |xxx', 'xx_blueFish |xxx']]

嘗試這個 :

import os
testSequences = []
testNames = []
for filename in os.listdir("./"): #go to directory where aligned files are kept
    if filename.endswith(".txt"): #open files which have been aligned with MAFFT
        fastaFile = open(filename, 'r') 
        temp_sub_list_names = []
        temp_sub_list_seq = []
        for line in fastaFile:
            line = line.strip()
            if line:
                if not line.startswith('>'):
                    temp_sub_list_seq.append(line)
                else:
                    temp_sub_list_names.append(line)
        testSequences.append(temp_sub_list_seq)
        testNames.append(temp_sub_list_names)

print (testSequences)
print (testNames)

輸出

[['tt', 'gg'], ['AAAAAAA', 'CCCCCC', 'TTTTTT', 'GGGGGG'], ['aaaa', 'cccc']]
[['>xx_redFish |xxx', '>xx_blueFish |xxx'], ['>xx_oneFish |xxx', '>xx_twoFish |xxx', '>xx_redFish |xxx', '>xx_blueFish |xxx'], ['>xx_oneFish |xxx', '>xx_twoFish |xxx']]

注意: 1. 如果您將腳本放在文本文件所在的同一文件夾中,這將起作用。 2. 這不會檢查以'>'開頭的行之后恰好發生的行中的預期值。 話雖如此,如果您的.txt文件之一是這樣的:

>xx_oneFish |xxx
aaaa
bbbb
dddd
>xx_twoFish |xxx
cccc

對於該文件,在testSequences中生成的子列表將是['aaaa', 'bbbb', 'dddd', 'cccc']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM