如何在python中使用序列文件创建数据集

时间:2014-07-11 16:38:44

标签: python dataset bioinformatics

我有一个蛋白质序列文件,如下所示:

>102L:A       MNIFEMLRIDEGLRLKIYKDTEGYYTIGIGHLLTKSPSLNAAAKSELDKAIGRNTNGVITKDEAEKLFNQDVDAAVRGILRNAKLKPVYDSLDAVRRAALINMVFQMGETGVAGFTNSLRMLQQKRWDEAAVNLAKSRWYNQTPNRAKRVITTFRTGTWDAYKNL       -------------------------------------------------------------------------------------------------------------------------------------------------------------------XX

第一个是序列的名称,第二个是实际的蛋白质序列,第一个是显示是否有任何缺失坐标的指示器。在这种情况下,请注意有两个" X"到底。这意味着序列的最后两个残留是" NL"在这种情况下缺少坐标。

通过Python编码,我想生成一个表格,如下所示:

  1. 序列名称
  2. 缺失坐标的总数(即X的数量)
  3. 这些缺失坐标的范围(这些X的位置范围) 4)序列的长度 5)实际顺序
  4. 所以最终结果应如下所示:

    >102L:A 2 163-164 164 MNIFEMLRIDEGLRLKIYKDTEGYYTIGIGHLLTKSPSLNAAAKSELDKAIGRNTNGVITKDEAEKLFNQDVDAAVRGILRNAKLKPVYDSLDAVRRAALINMVFQMGETGVAGFTNSLRMLQQKRWDEAAVNLAKSRWYNQTPNRAKRVITTFRTGTWDAYKNL
    

    到目前为止我的代码看起来像这样:

    total_seq = []
    with open('sample.txt') as lines:
        for l in lines:
            split_list = l.split()
    
            # Assign the list number
            header = split_list[0]                                # 1
            seq = split_list[1]                                   # 5
            disorder = split_list[2]
    
            # count sequence length and total residue of missing coordinates
            sequence_length = len(seq)                            # 4
    
            for x in disorder:
                counts = 0
                if x == 'X':
                    counts = counts + 1
    
            total_seq.append([header, seq, str(counts)])   # obviously I haven't finish coding 2 & 3
    
    with open('new_sample.txt', 'a') as f:
        for lol in total_seq:
            f.write('\n'.join(lol))
    

    我是python的新手,有人会帮忙吗?

1 个答案:

答案 0 :(得分:0)

这是您修改后的代码。它现在可以产生您想要的输出。

with open("sample.txt") as infile:
    matrix  = [line.split() for line in infile.readlines()]

    header_list  = [row[0] for row in matrix]
    seq_list = [str(row[1]) for row in matrix]
    disorder_list = [str(row[2]) for row in matrix]

f = open('new_sample.txt', 'a')

for i in range(len(header_list)):
    header = header_list[i]
    seq = seq_list[i]
    disorder = disorder_list[i]

    # count sequence length and total residue of missing coordinates
    sequence_length = len(seq)                            

    # get total number of missing coordinates
    num_missing = disorder.count('X')             

    # get the range of these missing coordinates
    first_X_pos = disorder.find('X')
    last_X_pos = disorder.rfind('X')
    range_missing = '-'.join([str(first_X_pos), str(last_X_pos)])

    reformat_seq=" ".join([header, str(num_missing), range_missing, str(sequence_length), seq, '\n'])  
    f.write(reformat_seq)

f.close()

更多提示:

不要忘记python的字符串函数。他们会自动解决你的很多问题。 documentation非常好。

如果您在问题中搜索了如何仅执行第2部分或仅部分3,您会在其他地方找到结果。