1

所以我有一个从文件(查询)中读取的字符串列表。我想将所有以“>”开头的行附加到一个名为 name_list 的列表中,并将其后面的所有字母字符(但在下一个“>”行之前)附加到一个列表中。这是我之前关于集合论必须提出的非常相似的问题,但是当我尝试操纵while 循环时,它陷入了无限反馈循环......

这是字符串列表的示例

query = [">mm10_refGene_NM_001011532 range=chr2:86084810-86085854 5'pad=0 3'pad=0 strand=- repeatMasking=none", 'caatgcctttgcctcactgataatttctattagtcttatcttatttcatt', 'ttactttgcagctgttaagacttgatgaaATGGCTGGAAGCAATGCCACT', 'GGTGTGACAGAATTCATTCTCTTGGGGTTTGCAGTCCAGAGAGAGGTAGA',">mm10_refGene_NM_001011534 range=chr2:85352995-85353924 5'pad=0 3'pad=0 strand=- repeatMasking=none", 'ATGGAACAAAGTAATGACACCAAAGTGACTGAATTCATTCTTCTGGGATT', 'TTCCGGACAGCACAAATCTTGGCACATTCTGTTCATAATATTTCTAATGA', 'TCTATGTTGTCACACTCATGGGTAACATTGGAATGATCGTACTCATCAAA']

这是我一直在使用的代码:

name_list = []
seq_list = []

for line in query:

    while line.startswith(">"):
        name=line
        temp_seq=[]

        for line in query:
            if line.isalpha()==True:
                temp_seq.append(line)


            else:
                break
        name_list.append(name)
        seq_list.append(''.join(temp_seq))

输出数据示例:

name_list = [">mm10_refGene_NM_001011532 range=chr2:86084810-86085854 5'pad=0 3'pad=0 strand=- repeatMasking=none",">mm10_refGene_NM_001011534 range=chr2:85352995-85353924 5'pad=0 3'pad=0 strand=- repeatMasking=none"]

seq_list = ['caatgcctttgcctcactgataatttctattagtcttatcttatttcattttactttgcagctgttaagacttgatgaaATGGCTGGAAGCAATGCCACTGGTGTGACAGAATTCATTCTCTTGGGGTTTGCAGTCCAGAGAGAGGTAGA','ATGGAACAAAGTAATGACACCAAAGTGACTGAATTCATTCTTCTGGGATTTTCCGGACAGCACAAATCTTGGCACATTCTGTTCATAATATTTCTAATGATCTATGTTGTCACACTCATGGGTAACATTGGAATGATCGTACTCATCAAA']

抱歉,如果这类似于(在行(从行列表)中搜索单词(从单词列表)并将值附加到新列表。Python)并且在任何方面都是多余的,但我认为这是一个帮助人们的好问题谁处理这类数据。

4

3 回答 3

0
name_list = []
seq_list = []

lines = iter(query)
for line in lines:
    while line.startswith(">"):
        name = line
        temp_seq = []
        for line in lines:
            if line.isalpha():
                temp_seq.append(line)
            else:
                break
        name_list.append(name)
        seq_list.append(''.join(temp_seq))
于 2013-03-21T22:32:11.317 回答
0

你可以很直接地做到这一点itertools.groupby

from itertools import groupby

def name_seq_chunks(seq): 
    isheader = lambda l:l.startswith('>')
    header = None
    for startgroup, dataiter in groupby(seq, isheader):
        if startgroup is True:
            header = list(dataiter)[-1]
        elif startgroup is False:
            yield header, ''.join(dataiter)

print list(name_seq_chunks(query))

这将产生一个元组列表,如[('>header', 'caatgccttt...'), ...]. 如果您真的希望它们分开,您可以重新压缩列表:

names, seqs = zip(*name_seq_chunks(query))
于 2013-03-21T23:33:32.910 回答
0

Here's a modification of your code that loops through the elements of query one at a time:

name_list = []
seq_list = []

seq = ""
for line in query:
    if line.startswith('>'):
        if seq:
            seq_list.append(seq)
            seq = ""
        name_list.append(line)
    elif line.isalpha():
        seq = seq + line
seq_list.append(seq)

But, in the example you provided, query has a consistent pattern of "name", followed by 3 "sequences". If your data always follows this consistent pattern, then here's another way to do it. You can define a function called grouper (taken from the itertools docs), which will let you read 4 elements of query at a time.

from itertools import izip_longest

def grouper(n, iterable, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

Now, for each 4 element "chunk" of query, you append the 1st element to "names", and concatenate the last 3 to append to "sequences":

names = []
sequences = []

for chunk in grouper(4, query):
    names.append(chunk[0])
    sequences.append(''.join(chunk[1:]))

print names
print sequences

Output:

[">mm10_refGene_NM_001011532 range=chr2:86084810-86085854 5'pad=0 3'pad=0 strand=- repeatMasking=none", ">mm10_refGene_NM_001011534 range=chr2:85352995-85353924 5'pad=0 3'pad=0 strand=- repeatMasking=none"]
['caatgcctttgcctcactgataatttctattagtcttatcttatttcattttactttgcagctgttaagacttgatgaaATGGCTGGAAGCAATGCCACTGGTGTGACAGAATTCATTCTCTTGGGGTTTGCAGTCCAGAGAGAGGTAGA', 'ATGGAACAAAGTAATGACACCAAAGTGACTGAATTCATTCTTCTGGGATTTTCCGGACAGCACAAATCTTGGCACATTCTGTTCATAATATTTCTAATGATCTATGTTGTCACACTCATGGGTAACATTGGAATGATCGTACTCATCAAA']
于 2013-03-21T23:18:54.640 回答