Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7-识别致病岛-计算字符串各部分的GC含量_Python_Python 2.7_Bioinformatics_Biopython - Fatal编程技术网

Python 2.7-识别致病岛-计算字符串各部分的GC含量

Python 2.7-识别致病岛-计算字符串各部分的GC含量,python,python-2.7,bioinformatics,biopython,Python,Python 2.7,Bioinformatics,Biopython,我正在尝试编写一个函数,它查看较长字符串的一段,计算GC内容,然后继续下一段,依此类推 我已经有了一个计算GC内容的函数。我在编写函数中隔离较长字符串段的部分时遇到问题 例如:我有字符串“tatagcatcgatctctgacgtagatcgatcgatctata”,我希望函数查看前5个索引,调用现有函数计算GC内容,然后继续下5个索引,依此类推,直到字符串结束 这是我用来计算GC内容的函数 def GCcont(DNA): '''calculate GC content'''

我正在尝试编写一个函数,它查看较长字符串的一段,计算GC内容,然后继续下一段,依此类推

我已经有了一个计算GC内容的函数。我在编写函数中隔离较长字符串段的部分时遇到问题

例如:我有字符串“tatagcatcgatctctgacgtagatcgatcgatctata”,我希望函数查看前5个索引,调用现有函数计算GC内容,然后继续下5个索引,依此类推,直到字符串结束

这是我用来计算GC内容的函数

def GCcont(DNA):
    '''calculate GC content'''
    counter=0
    for nuc in DNA:
        if nuc=='G' or nuc=='C':
            counter=counter+1
    return counter/float(len(DNA))

有人有什么建议吗?

我会制作一个发生器,将你的DNA序列块吐出来:

def section(dna, blocksize):
    start = 0
    while True:
        end = start + blocksize
        yield dna[start:end]
        if end > len(dna):
            break
        start = end
它是这样工作的:

>>> dna = 'TATAGCATCGATCTCTGACGTATCGATCGATCGTCTATATA'
>>> list(section(dna, 5))
['TATAG', 'CATCG', 'ATCTC', 'TGACG', 'TATCG', 'ATCGA', 'TCGTC', 'TATAT', 'A']
然后,计算每个块的GC内容非常简单:

>>> [GCcont(block) for block in section(dna, 5)]
[0.2, 0.6, 0.4, 0.6, 0.4, 0.4, 0.6, 0.0, 0.0]
还有一种方法:

def get_gc_across_sections(s):
    sections =  [s[i:i+5] for i in range(0, len(s), 5)]
    return [GCcont(section) for section in sections]

顺便说一句,Python中的函数名通常使用snake-case,而不是camel-case。

感谢大家对snake-vs-camel案例的介绍。我发现这与PEP 0008样式指南中的内容类似。我正在使用的这本书一直使用驼峰案例惯例。没问题!是的,没错。我要说的是,大多数Python程序员要么遵循pep-8要么遵循Google Python风格指南,这两个指南都提倡这种命名约定。不过,无论你选择什么风格,保持一致可能更重要。