Python Print语句不返回PSL数据

时间:2014-05-26 00:59:09

标签: python bioinformatics

我有一个代码正在产生一个非常奇怪的打印声明。这是我的代码:

import sys
class PSLreader :

    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
            else: 
                break

fileH = ("EEV14-Cb.filtered.psl")
new_psl = PSLreader(fileH)
print (new_psl.readPSLpairs())

我得到了#34;没有&#34;作为我的印刷声明。我想我在最后一段代码中犯了一些错误,但我不确定它是什么。为了给你一些见解,我正在测试我的一些代码并尝试使用&#34; readPSLpars&#34;方法打印出我PSL文档的前20行。 &#34; readPSL&#34;部分代码只选择PSL行代表序列中第一个或最后1000个碱基的对齐,我非常确定代码的一部分是可以的。如果有人能让我知道我的代码的最后几行有什么问题,那就太棒了。

编辑:我应该制作一个看起来像这样的生成器:

gen = new_psl.readPSLpairs()            
for line in gen:
    new_psl = PSLreader("EEV14-Cb.filtered.psl")
    print (new_psl.readPSLpairs()) 

它仍然无法正常工作,但那是因为我的语法总是存在问题

编辑2:我将我的代码更改为此,这会导致错误:

new_psl = PSLreader("EEV14-Cb.filtered.psl")
gen = new_psl.readPSLpairs()            
for line in gen:
    print (new_psl.readPSLpairs())

错误是:&#34; TypeError:&#39; NoneType&#39;对象不可迭代&#34;

编辑3:

new_psl = PSLreader("EEV14-Cb.filtered.psl")
gen = new_psl.readPSLpairs()            
for line in gen:
     print line

&#34;线&#34;在&#34;打印线&#34;语法错误出现时会突出显示。

1 个答案:

答案 0 :(得分:0)

readPSL将创建一个readPSLpairs使用的对象,它不会产生或返回任何内容,因此打印输出是正确的。

代码的最后两行应该如下所示

for line in gen:
    print line

它将允许您查看您询问的前20行。