首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正在尝试打印PSL文件的前二十行

问正在尝试打印PSL文件的前二十行
EN

Stack Overflow用户
提问于 2014-05-24 05:40:05
回答 1查看 137关注 0票数 0

我有这段读取PSL文件的代码。为了清楚起见,我在下面粘贴了整个内容,但我现在真正关注的是readPSLpairs方法(它在最底层)。现在,我只是尝试让该方法打印psl文件的前20行,但由于某种原因,它没有...我没有收到错误消息或其他任何东西--但是程序是空白的。我遗漏了什么?谢谢。

代码语言:javascript
复制
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,如下所示:

代码语言:javascript
复制
def main():
    new_psl = PSLreader(fname)
    new_psl.readPSLpairs()#creating class objects
    new_psl.output()

main()

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

EN

回答 1

Stack Overflow用户

发布于 2014-05-24 05:49:16

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23838693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档