亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 FASTA 文件中提取基因序列?

從 FASTA 文件中提取基因序列?

肥皂起泡泡 2021-11-02 16:10:17
我有以下代碼讀取包含 10 個基因序列的 FASTA 文件并將每個序列作為矩陣返回。然而,代碼似乎在最后一個序列中丟失了,我想知道為什么?file=open('/Users/vivianspro/Downloads/rosalind_cons (5).txt', 'r')line=file.readline()strings = []sequence=''while line:    #line=line.rstrip('\n')    line = line.strip() #empty () automatically strips the \n    if '>' in line:        if sequence != "":            strings.append(sequence)            sequence = ""        #sequence=line    else:        sequence+=line    line=file.readline()for s in strings:    print(s)Motifs = []for seq in strings:    Motifs.append(list(seq))#make every symbol into an element in the list separated by ,for s in Motifs:    print(s) ````
查看完整描述

2 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

只有strings當您看到一個新的時才追加到,>但在最后一個序列之后沒有。


這是一個重構,希望它也更加地道。


strings = []

sequence=''


with open('/Users/vivianspro/Downloads/rosalind_cons (5).txt', 'r') as file:

    for line in file:

        line = line.rstrip('\n')

        if line.startswith('>'):

            if sequence != "":

                strings.append(sequence)

            sequence = ""

        else:

            sequence+=line

    # After the last iteration, append once more if we have something to append

    if sequence:

        strings.append(sequence)


查看完整回答
反對 回復 2021-11-02
?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

由于 FASTA 文件包含以下格式的數據:


>ID1

seq_1

>ID2

seq_2

...

根據您的代碼,如果您的行>只包含一個,那么您嘗試追加序列。這意味著,當您迭代 ID_2 時,您正在添加 ID_1 的序列。


要解決此問題,您可以執行以下操作:


for line in file:

    line = line.strip()

    if '>' in line: # Line 1

        line = file.readline().strip()

        # print(line)

        strings.append(line)

上面的示例使用了這樣一個事實,即在 FASTA 文件中,序列直接出現在包含>字符的 ID 之后(您可以更改第 1 行,以便它只檢查第一個字符, line[0] == ">")。


查看完整回答
反對 回復 2021-11-02
  • 2 回答
  • 0 關注
  • 336 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號