2

これは長くなりますが、これを効果的に説明する方法が他にわかりません。

最初のファイルには文字のリストがあり、2 番目のファイルは 3 文字のリストで、一致する識別子文字 (タブで区切られています) です。

2 番目のファイルでは、3 文字をアイテムとして、1 文字を対応するキーとして辞書を作成しました。私がする必要があるのは、最初のリストから一度に 3 文字を取り出し、それを辞書と比較することです。一致する場合は、対応するキーを取得して、印刷する新しいリストに追加する必要があります。一致が「*」文字の場合、リストと辞書の比較を続行しないようにする必要があります。

追加機能を使用して比較してから新しいリストを作成するのに問題があります。

最初の入力ファイルの一部を次に示します。

Seq0
ATGGAAGCGAGGATGtGa

これが2番目の部分です:

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    *

しかし、私はこれにさえ近づいていません。助けてください!

4

3 に答える 3

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
于 2013-04-05T02:20:46.303 に答える
0

Python では通常、カウンタなどを使用して明示的なループを記述しないようにする方法があります。リストを 1 行で作成できる非常に強力なリスト内包表記があります。forつまり、2 番目のループを記述する別の方法を次に示します。

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

最初の行は、すべての文字列サニタイズを実行します。さまざまなメソッドを連鎖させることで、コードが読みやすくなります。あなたはそれが好きかもしれませんし、そうでないかもしれません。2 行目re.findallでは、文字列を 3 文字ごとに分割するというトリッキーな方法を使用しています。amino_acids3 行目は、 dict内の各コドンを検索し、結果の値のリストを作成するリスト内包表記です。

forリスト内包表記内でループを抜け出す簡単な方法はないため、最後のifステートメントは . の後に出現するすべてのエントリを切り取ります*

この関数を次のように呼び出します。

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

print codons_to_acids(amino_acids, 'ATGGAAGCGAGGATGtGaATT')
于 2013-04-05T02:21:25.460 に答える
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
于 2013-04-05T03:11:32.950 に答える