예제 #1
0
def iterate( infile ):
    '''iterate over ``samtools pileup -c`` formatted file.

    *infile* can be any iterator over a lines.

    The function yields named tuples of the type :class:`pysam.Pileup.PileupSubstitution`
    or :class:`pysam.Pileup.PileupIndel`.

    .. note:: 
       The parser converts to 0-based coordinates
    '''
    
    conv_subst = (str,lambda x: int(x)-1,str,str,int,int,int,int,str,str)
    conv_indel = (str,lambda x: int(x)-1,str,str,int,int,int,int,str,str,int,int,int)

    for line in infile:
        d = line[:-1].split()
        if d[2] == "*":
            try:
                yield PileupIndel( *[x(y) for x,y in zip(conv_indel,d) ] )
            except TypeError:
                raise pysam.SamtoolsError( "parsing error in line: `%s`" % line)
        else:
            try:
                yield PileupSubstitution( *[x(y) for x,y in zip(conv_subst,d) ] )
            except TypeError:
                raise pysam.SamtoolsError( "parsing error in line: `%s`" % line)
예제 #2
0
    def setUp(self):
        '''setup tests. 

        For setup, all commands will be run before the first test is
        executed. Individual tests will then just compare the output
        files.

        '''
        if BinaryTest.first_time:

            # remove previous files
            if os.path.exists(WORKDIR):
                shutil.rmtree(WORKDIR)
                pass

            # copy the source files to WORKDIR
            os.makedirs(WORKDIR)

            for f in ("ex1.fa", "ex1.sam.gz", "ex1.sam", "ex2.bam", "ex1.bed"):
                shutil.copy(os.path.join(DATADIR, f), os.path.join(WORKDIR, f))

            # cd to workdir
            savedir = os.getcwd()
            os.chdir(WORKDIR)
            for label in self.order:
                sys.stdout.write("preparing test {}".format(label))
                command = self.commands[label]
                # build samtools command and target and run
                samtools_target, samtools_command = command[0]
                runSamtools(" ".join((SAMTOOLS, samtools_command)))
                sys.stdout.write(" samtools ok")
                # get pysam command and run
                try:
                    pysam_target, pysam_command = command[1]
                except ValueError as msg:
                    raise ValueError("error while setting up %s=%s: %s" %
                                     (label, command, msg))

                pysam_method, pysam_options = pysam_command

                try:
                    output = pysam_method(*pysam_options.split(" "),
                                          raw=True,
                                          catch_stdout=True)
                except pysam.SamtoolsError as msg:
                    raise pysam.SamtoolsError(
                        "error while executing %s: options=%s: msg=%s" %
                        (label, pysam_options, msg))

                sys.stdout.write(" pysam ok\n")

                if ">" in samtools_command:
                    with open(pysam_target, "wb") as outfile:
                        if type(output) == list:
                            if IS_PYTHON3:
                                for line in output:
                                    outfile.write(line.encode('ascii'))
                            else:
                                for line in output:
                                    outfile.write(line)
                        else:
                            outfile.write(output)

            os.chdir(savedir)
            BinaryTest.first_time = False

        samtools_version = getSamtoolsVersion()

        def _r(s):
            # patch - remove any of the alpha/beta suffixes, i.e., 0.1.12a ->
            # 0.1.12
            if s.count('-') > 0:
                s = s[0:s.find('-')]
            return re.sub("[^0-9.]", "", s)

        if _r(samtools_version) != _r(pysam.__samtools_version__):
            raise ValueError(
                "versions of pysam/samtools and samtools differ: %s != %s" %
                (pysam.__samtools_version__, samtools_version))