使用函数来生成dict是行不通的,但是函数之外的函数可以

2024-05-28 18:15:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有个问题,似乎找不到也解决不了。你知道吗

FASTA = >header1
         ATCGATCGATCCCGATCGACATCAGCATCGACTAC
         ATCGACTCAAGCATCAGCTACGACTCGACTGACTACGACTCGCT
        >header2
         ATCGATCGCATCGACTACGACTACGACTACGCTTCGTATCAGCATCAGCT
         ATCAGCATCGACGACGACTAGCACTACGACTACGACGATCCCGATCGATCAGCT

def dnaSequence():
    '''
    This function makes a dict called DNAseq by reading the fasta file 
    given as first argument on the command line
    INPUT: Fasta file containing strings
    OUTPUT: key is header and value is sequence
    '''

    DNAseq = {}
    for line in FASTA:
        line = line.strip()
        if line.startswith('>'):
            header = line
            DNAseq[header] = ""
        else:
            seq = line
            DNAseq[header] = seq

    return DNAseq



def digestFragmentsWithOneEnzyme(dnaSequence):
    '''
    This function digests the sequence from DNAseq into smaller parts
    by using the enzymes listed in the MODES.
    INPUT: DNAseq and the enzymes from sys.argv[2:]
    OUTPUT: The DNAseq is updated with the segments gained from the
    digesting
    '''
    enzymes = sys.argv[2:]

    updated_list = []
    for enzyme in enzymes:
        pattern = MODES(enzyme)
        p = re.compile(pattern)
        for dna in DNAseq.keys():
            matchlist = re.findall(p,dna)
            updated_list = re.split(MODES, DNAseq)
            DNAseq.update((key, updated_list.index(k)) for key in
            d.iterkeys())
    return DNAseq


def getMolecularWeight(dnaSequence):
    '''
    This function calculates the molWeight of the sequence in DNAseq
    INPUT: the updated DNAseq from the previous function as a dict
    OUTPUT: The DNAseq is updated with the molweight of the digested fragments
    '''

    results = []
    for seq in DNAseq.keys():
        results = sum((dnaMass[base]) for base in DNAseq[seq])
        DNAseq.update((key, results.index(k)) for key in
        d.iterkeys())
    return DNAseq


def main(argv=None):
    '''
    This function prints the results of the digested DNA sequence on in the terminal.
    INPUT: The DNAseq from the previous function as a dict
    OUTPUT: name     weight weight weight
            name2    weight weight weight
    '''
    if argv == None:
        argv = sys.argv
    if len(argv) <2:
        usage()
        return 1

    digestFragmentsWithOneEnzyme(dnaSequence())
    Genes = getMolecularWeight(digestFragmentsWithOneEnzyme())
    print ({header},{seq}).format(**DNAseq)
    return 0



if __name__ == '__main__':
    sys.exit(main())

在第一个函数中,我尝试从fasta文件生成一个dict,在第二个函数中使用相同的dict,其中序列由regex切片,最后计算molweight。你知道吗

我的问题是,由于某种原因,Python无法识别我的dict,我得到了一个错误:

name error DNAseq is not defined

如果我把dict放在函数之外,那么我就有了dict。你知道吗


Tags: thekeyinforreturnislinefunction
1条回答
网友
1楼 · 发布于 2024-05-28 18:15:51

您将dict作为dnaSequence传递给这两个函数,而不是DNAseq。你知道吗

注意这是一种非常奇怪的函数调用方式。当您将序列传递给digestFragmentsWithOneEnzyme时,您完全忽略了第一次调用的结果,然后再次尝试调用它以将结果传递给getMolecularWeight,但是您在该调用中没有实际传递序列,因此如果您达到了这个程度,那么实际上会出错。你知道吗

我想你想做的是:

sequence = dnaSequence()
fragments = digestFragmentsWithOneEnzyme(sequence)
genes = getMolecularWeight(fragments)

并且应该避免将参数调用到两个具有相同名称的函数作为单独的函数,因为这样会隐藏函数名称。相反,请选择一个新名称:

def digestFragmentsWithOneEnzyme(sequence):
    ...
    for dna in sequence:

(您不需要调用keys()—在dict上迭代总是在键上进行的。)

相关问题 更多 >

    热门问题