正在打印前20行PSL fi

2024-07-02 12:40:40 发布

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

我有一个读取PSL文件的代码。为了清晰起见,我在下面粘贴了整个内容,但我现在真正关注的是readPSLpairs方法(它在最下面)。现在,我只是想让这个方法打印psl文件的前20行,但由于某些原因它不是。。。我没有收到错误信息或任何东西-但程序只是空白。我错过了什么?谢谢。你知道吗

import sys
class PSLreader :
    '''
    Class to provide reading of a file containing psl alignments
    formatted sequences:
    object instantiation:
    myPSLreader = PSLreader(<file name>):

    object attributes:
    fname: the initial file name

    methods:
    readPSL() : reads psl file, yielding those alignments that are within the first or last
                1000 nt

    readPSLpairs() : yields psl pairs that support a circular hypothesis 

    Author: David Bernick
    Date: May 12, 2013
    '''

    def __init__ (self, fname='EEV14-Cb.filtered.psl'):
        '''contructor: saves attribute fname '''

        self.fname = fname

    def doOpen (self):
        if self.fname is '':
            return sys.stdin
        else:
            return open(self.fname)

    def readPSL (self):
        '''
        using filename given in init, returns each filtered psl records
        that contain alignments that are within the terminal 1000nt of
        the target. Incomplete psl records are discarded.
        If filename was not provided, stdin is used.

        This method selects for alignments that could may be part of a
        circle.

        Illumina pairs aligned to the top strand would have read1(+) and read2(-).
        For the bottoms trand, read1(-) and read2(+).

        For potential circularity,
        these are the conditions that can support circularity:
        read1(+) near the 3' terminus
        read1(-) near the 5' terminus
        read2(-) near the 5' terminus
        read2(+) near the 3' terminus

        so...
        any read(+) near the 3', or
        any read(-) near the 5'

        '''

        nearEnd = 1000   # this constant determines "near the end"
        with self.doOpen() as fileH:

            for line in fileH:
                pslList = line.split()
                if len(pslList) < 17:
                    continue
                tSize = int(pslList[14])
                tStart = int(pslList[15])
                strand = str(pslList[8])

                if strand.startswith('+') and (tSize - tStart > nearEnd):
                    continue
                elif strand.startswith('-') and (tStart > nearEnd):
                    continue

                yield line

    def readPSLpairs (self):
        i = 0
        for psl in self.readPSL():
            if i>20:
                print(psl.split())
                i+=1

编辑:所以我试着像这样引用main:

def main():
    new_psl = PSLreader(fname)
    new_psl.readPSLpairs()#creating class objects
    new_psl.output()

main()

但这仍然不能使代码正常工作。。。错误状态为“NameError:未定义全局名称'fname'”


Tags: theselfifthatdeffnamearefile
1条回答
网友
1楼 · 发布于 2024-07-02 12:40:40

首先,在readPSLpairs函数中的check i>20意味着程序只打印第20行之后的行。如果您的文件只有20行长(或者不到20行满足readPSL中的约束条件),这可以解释您的问题。我会检查有多少行通过了readPSL(形式为if condition: continue)中的检查;尝试在readPSL中放入一些print语句。你知道吗

另外,如果这是您的整个程序,那么您没有任何main方法实际运行这个程序;请确保您的代码实际运行的是某些东西!你知道吗

相关问题 更多 >