【问题标题】:Counting genetic mutations in dictionary using python使用python计算字典中的基因突变
【发布时间】:2012-09-28 10:05:45
【问题描述】:

我有这种格式的数据:

>abc12
ATCGACAG

>def34
ACCGACG

等等

我已将每个基因存储到字典中,其中以 > 开头的行作为值。所以字典类似于{'abc12':'ATCGACAG'等}

现在我希望能够比较每个基因,以便计算每个位点的 A、T、C 或 G 的数量。

我唯一能想到的是将字典分解为每个核苷酸位点的列表,并使用带有计数器的 zip()。这是最好的方法吗?如果是,我如何将字典分成每个站点的列表?

【问题讨论】:

    标签: python list dictionary bioinformatics


    【解决方案1】:

    使用collections.Counter:

    >>> from collections import Counter
    >>> Counter('ATCGACAG')
    Counter({'A': 3, 'C': 2, 'G': 2, 'T': 1})
    >>> Counter('ACCGACG')
    Counter({'C': 3, 'A': 2, 'G': 2})
    

    【讨论】:

    • 我希望能够像对齐一样计数。例如。在位点一,两个基因都有一个 A,但在位点二,一个有一个 C,一个有一个 T。所以输出会是这样的:1:5 个 A,2 个 C 2:3 个 G,4 个 A感觉?
    【解决方案2】:

    有理由不使用 Biopython 吗?

    from Bio import AlignIO
    alignment =AlignIO.read("alignment.fas", "fasta")
    n=0
    while n<len(alignment[0]):
        A=alignment[:,n].count('A')
        C=alignment[:,n].count('C')
        G=alignment[:,n].count('G')
        T=alignment[:,n].count('T')
        gap=alignment[:,n].count('-')
    
        print "at position %s there are %s A's, %s C's, %s G's, %s T's and %s gaps" % (n, A, C, G, T, gap)
        n=n+1
    

    确保您具有真正的对齐方式(即序列长度相同)。
    p.s.对于打印语句的丑陋格式,我深表歉意...

    返回

    at position 0 there are 2 A's, 0 C's, 0 G's, 0 T's and 0 gaps
    at position 1 there are 0 A's, 1 C's, 0 G's, 1 T's and 0 gaps
    at position 2 there are 0 A's, 2 C's, 0 G's, 0 T's and 0 gaps
    at position 3 there are 0 A's, 0 C's, 2 G's, 0 T's and 0 gaps
    at position 4 there are 2 A's, 0 C's, 0 G's, 0 T's and 0 gaps
    at position 5 there are 0 A's, 2 C's, 0 G's, 0 T's and 0 gaps
    at position 6 there are 1 A's, 0 C's, 0 G's, 0 T's and 1 gaps
    at position 7 there are 0 A's, 0 C's, 2 G's, 0 T's and 0 gaps
    

    【讨论】:

      【解决方案3】:
      s1 = "ATCGACAG"
      s2 = "ACCGACG"   
      alignment = [s1[i] if s1[i] == s2[i] else "-" for i in range(len(min([s1,s2],key=len)))]
      print "".join(alignment)
      A-CGAC-
      

      【讨论】:

        猜你喜欢
        • 2017-03-07
        • 1970-01-01
        • 1970-01-01
        • 2013-11-19
        • 1970-01-01
        • 2015-01-28
        • 2013-05-24
        • 2019-10-31
        • 1970-01-01
        相关资源
        最近更新 更多