2013-04-05 152 views
2

这将会很长,但我不知道如何有效解释这一点。Python:从字典中追加到列表

所以,我有2个文件,我在正在读,第一个具有characters.The第二文件的列表是3个字符的列表,然后它的匹配标识符字符(由制表符分隔)。

在第二文件I制成的字典的3个字符作为项目与所述一个字符作为对应的按键。 我需要做的是从第一个列表中每次取3个字符并将其与字典进行比较。如果有匹配,我需要采取相应的键并将其附加到我将打印出的新列表中。如果匹配是'*'字符,我需要停止不继续比较列表和字典。

我在与比较,然后通过附加功能使得新名单的麻烦。

这是第一个输入文件的一部分:

Seq0 
ATGGAAGCGAGGATGtGa 

下面是部分第二:

AUU  I 
AUC  I 
AUA  I 
CUU  L 
GUU  V 
UGA  * 

这是到目前为止我的代码:

input = open("input.fasta", "r") 
codons = open("codons.txt", "r") 

counts = 1 
amino_acids = {} 

for lines in codons: 
     lines = lines.strip() 
     codon, acid = lines.split("\t") 
     amino_acids[codon] = acid 
     counts += 1 

count = 1 

for line in input: 
     if count%2 == 0: 
       line = line.upper() 
       line = line.strip() 
       line = line.replace(" ", "") 
       line = line.replace("T", "U") 

       import re 

       if not re.match("^[AUCG]*$", line): 
         print "Error!" 

       if re.match("^[AUCG]*$", line): 
         mrna = len(line)/3 
         first = 0 
         last = 3 

         while mrna != 0: 
           codon = line[first:last] 
           first += 3 
           last += 3 
           mrna -= 1 
           list = [] 

           if codon == amino_acids[codon]: 
             list.append(acid) 

             if acid == "*": 
               mrna = 0 

           for acid in list: 
             print acid 

所以我想我的输出看起来像这样:

M L I V * 

但我还没有接近这一点。 请帮忙!

+0

对于其中一个,你似乎不会增加“计数”。你有没有得到任何输出? – kufudo 2013-04-05 02:14:06

回答

0

以下是纯粹未经测试的代码。检查缩进,语法和逻辑,但应该更接近你想要的。

import re 

codons = open("codons.txt", "r") 
amino_acids = {} 
for lines in codons: 
     lines = lines.strip() 
     codon, acid = lines.split("\t") 
     amino_acids[codon] = acid 

input = open("input.fasta", "r") 
count = 0 
list = [] 
for line in input: 
    count += 1 
    if count%2 == 0: #i.e. only care about even lines 
     line = line.upper() 
     line = line.strip() 
     line = line.replace(" ", "") 
     line = line.replace("T", "U") 

     if not re.match("^[AUCG]*$", line): 
       print "Error!" 
     else: 
      mrna = len(line)/3 
       first = 0 
       while mrna != 0: 
        codon = line[first:first+3] 
        first += 3 
        mrna -= 1 
        if codon in amino_acids: 
         list.append(amino_acids[codon]) 
         if acid == "*": 
          mrna = 0 

for acid in list: 
    print acid 
0

在Python中,通常有一种方法可以避免用计数器等编写显式循环。有一个令人难以置信的强大的列表理解语法,可以让你在一行中构建列表。要晓得,这里是写你的第二个for环的另一种方法:

import re 

def codons_to_acids(amino_acids, sequence): 
    sequence = sequence.upper().strip().replace(' ', '').replace('T', 'U') 
    codons = re.findall(r'...', sequence) 
    acids = [amino_acids.get(codon) for codon in codons if codon in amino_acids] 

    if '*' in acids: 
     acids = acids[:acids.index('*') + 1] 

    return acids 

第一行执行所有的字符串消毒的。将不同方法链接在一起会使代码更易读。你可能会喜欢,也可能不喜欢。第二行使用re.findall以非常棘手的方式每三个字符分割一次字符串。第三行是列表理解,它查找amino_acids字典中的每个密码子并创建结果值列表。

在列表理解中没有简单的方法来打开for循环,因此最后的if语句切断了在*之后发生的任何条目。

你会调用该函数像这样:

amino_acids = { 
    'AUU': 'I', 'AUC': 'I', 'AUA': 'I', 'CUU': 'L', 'GUU': 'V', 'UGA': '*' 
} 

print codons_to_acids(amino_acids, 'ATGGAAGCGAGGATGtGaATT') 
0

如果你能解决这个问题,而正则表达式,最好不要使用它。

with open('input.fasta', 'r') as f1: 
    input = f1.read() 

codons = list() 
with open('codons.txt', 'r') as f2: 
    codons = f2.readlines() 

input = [x.replace('T', 'U') for x in input.upper() if x in 'ATCG'] 
chunks = [''.join(input[x:x+3]) for x in xrange(0, len(input), 3)] 

codons = [c.replace('\n', '').upper() for c in codons if c != '\n'] 

my_dict = {q.split()[0]: q.split()[1] for q in codons } 

result = list() 

for ch in chunks: 
    new_elem = my_dict.pop(ch, None) 
    if new_elem is None: 
     print 'Invalid key!' 
    else: 
     result.append(new_elem) 
     if new_elem == '*': 
      break 

print result