3

我正在编写一个 python 程序,它必须计算一组字符串的突变残基和位置的数字编码。这些字符串是蛋白质序列。这些序列存储在 fasta 格式文件中,每个蛋白质序列用逗号分隔。序列不同蛋白质的长度可能不同。在此我试图找到突变的位置和序列。
我使用以下代码来获取它。

a = 'AGFESPKLH'
b = 'KGFEHMKLH'
for i in range(len(a)):
  if a[i] != b[i]:
     print i, a[i], b[i]

但是我想要序列文件作为输入文件。下图将讲述我的项目。在这个图中,第一个框代表输入文件序列的对齐。最后一个框代表输出文件。我怎样才能在 Python 中做到这一点?请帮我。谢谢大家的时间。

例子:

input file

MTAQDD,MTAQDD,MTSQED,MTAQDD,MKAQHD




        positions  1  2  3  4  5  6                         1  2  3  4  5  6

protein sequence1  M  T  A  Q  D  D                            T  A     D

protein sequence2  M  T  A  Q  D  D                            T  A     D

protein sequence3  M  T  S  Q  E  D                            T  S     E

protein sequence4  M  T  A  Q  D  D                            T  A     D

protein sequence5  M  K  A  Q  H  D                            K  A     H


     PROTEIN SEQUENCE ALIGNMENT                          DISCARD NON-VARIABLE REGION

        positions  2  2  3  3  5  5  5

protein sequence1  T     A     D   

protein sequence2  T     A     D   

protein sequence3  T        S     E

protein sequence4  T     A     D   

protein sequence5     K  A           H

   MUTATED RESIDUE IS SPLITED TO SEPARATE COLUMN

输出文件应该是这样的:

position+residue   2T  2K  3A  3S  5D  5E  5H

       sequence1   1   0   1   0   1   0   0

       sequence2   1   0   1   0   1   0   0

       sequence3   1   0   0   1   0   1   0

       sequence4   1   0   1   0   1   0   0

       sequence5   0   1   1   0   0   0   1

    (RESIDUES ARE CODED 1 IF PRESENT, 0 IF ABSENT)
4

4 回答 4

1

像这样的东西?

ls = 'MTAQDD,MTAQDD,MTSQED,MTAQDD,MKAQHD'.split(',')

pos = [set(enumerate(x, 1)) for x in ls]
alle = sorted(set().union(*pos))

print '\t'.join(str(x) + y for x, y in alle)
for p in pos:
    print '\t'.join('1' if key in p else '0' for key in alle)
于 2013-01-24T10:28:12.693 回答
1

如果您要使用表格数据,请考虑pandas

from pandas import *

data = 'MTAQDD,MTAQDD,MTSQED,MTAQDD,MKAQHD'

df = DataFrame([list(row) for row in data.split(',')])

print DataFrame({str(col)+val:(df[col]==val).apply(int) 
        for col in df.columns for val in set(df[col])})

输出:

  0M  1K  1T  2A  2S  3Q  4D  4E  4H  5D
0   1   0   1   1   0   1   1   0   0   1
1   1   0   1   1   0   1   1   0   0   1
2   1   0   1   0   1   1   0   1   0   1
3   1   0   1   1   0   1   1   0   0   1
4   1   1   0   1   0   1   0   0   1   1

如果要删除所有列:

print df.select(lambda x: not df[x].all(), axis = 1)    

   1K  1T  2A  2S  4D  4E  4H
0   0   1   1   0   1   0   0
1   0   1   1   0   1   0   0
2   0   1   0   1   0   1   0
3   0   1   1   0   1   0   0
4   1   0   1   0   0   0   1
于 2013-01-24T11:23:21.433 回答
0
protein_sequence = "MTAQDDSYSDGKGDYNTIYLGAVFQLN,MTAQDDSYSDGRGDYNTIYLGAVFQLN,MTSQEDSYSDGKGNYNTIMPGAVFQLN,MTAQDDSYSDGRGDYNTIMPGAVFQLN,MKAQDDSYSDGRGNYNTIYLGAVFQLQ,MKSQEDSYSDGRGDYNTIYLGAVFQLN,MTAQDDSYSDGRGDYNTIYPGAVFQLN,MTAQEDSYSDGRGEYNTIYLGAVFQLQ,MTAQDDSYSDGKGDYNTIMLGAVFQLN,MTAQDDSYSDGRGEYNTIYLGAVFQLN"

#Parse the file
proteins = protein_sequence.split(",")
#For each protein sequence remove the duplicates
proteins = map(lambda x:"".join(set(list(x))), proteins)

#Create result
result = []
key_set = ['T', 'K', 'A', 'S', 'D', 'E', 'K', 'R', 'D', 'N', 'E', 'Y', 'M', 'L', 'P', 'N', 'Q']
for protein in proteins:
    local_dict = dict(zip(key_set, [0] * len(key_set)))
    #Split the protein in amino acid
    components = list(protein)
    for amino_acid in components:
        local_dict[amino_acid] = 1
    result.append((protein, local_dict))
于 2013-01-30T12:31:22.187 回答
0

您可以使用pandas函数get_dummies来完成大部分繁重的工作:

In [11]: s # a pandas Series (DataFrame's column)
Out[11]: 
0    T
1    T
2    T
3    T
4    K
Name: 1

In [12]: pd.get_dummies(s, prefix=s.name, prefix_sep='')
Out[12]: 
   1K  1T
0   0   1
1   0   1
2   0   1
3   0   1
4   1   0

要将数据放入 DataFrame 中,您可以使用:

df = pd.DataFrame(map(list, 'MTAQDD,MTAQDD,MTSQED,MTAQDD,MKAQHD'.split(',')))

In [20]: df
Out[20]: 
   0  1  2  3  4  5
0  M  T  A  Q  D  D
1  M  T  A  Q  D  D
2  M  T  S  Q  E  D
3  M  T  A  Q  D  D
4  M  K  A  Q  H  D

并找到那些具有不同值的列:

In [21]: (df.ix[0] != df).any()
Out[21]: 
0    False
1     True
2     True
3    False
4     True
5    False

把这一切放在一起:

In [31]: I = df.columns[(df.ix[0] != df).any()]

In [32]: J = (pd.get_dummies(df[i], prefix=df[i].name, prefix_sep='') for i in I)

In [33]: df[[]].join(J)
Out[33]: 
   1K  1T  2A  2S  4D  4E  4H
0   0   1   1   0   1   0   0
1   0   1   1   0   1   0   0
2   0   1   0   1   0   1   0
3   0   1   1   0   1   0   0
4   1   0   1   0   0   0   1
于 2013-02-01T15:36:33.247 回答