view commons/launcher/BlatProgramLauncher.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents
children
line wrap: on
line source

#!/usr/bin/env python

##@file
# Launch Blat (pairwise alignment).
#
# options:
#      -h: this help
#      -i: name of the input file (queries, format='fasta')
#      -s: name of the subject file (format='fasta')
#      -p: parameters for 'blat' (default='-d 2')
#      -o: name of the output file (format='align', default=inFile+'.align')
#      -c: clean
#      -v: verbosity level (default=0/1)


import os
import sys

from commons.pyRepetUnit.components.AbstractProgramLauncher import  AbstractProgramLauncher


class BlatProgramLauncher( AbstractProgramLauncher ):
    """
    Launch Blat (pairwise alignment).
    """
    
    def __init__( self ):
        """
        Constructor.
        """
        AbstractProgramLauncher.__init__( self )
        self._prgName = "blat"
        self._formatInFile = "fasta"
        self._sbjFile = ""
        self._prgParam = ""
        self._allByAll = False
        
        
    def getHelpAsString( self ):
        string = AbstractProgramLauncher.getHelpAsString(self)
        string += "\nspecific options:"
        string += "\n     -s: name of the subject file (database, format='fasta')"
        string += "\n     -p: parameters for '%s'" % ( self.getProgramName() )
        string += "\n     -A: same sequences (all-by-all)"
        string += "\n     -o: name of the output file (format='align', default=inFile+'.align')"
        return string
    
    def getCmdLineOptions(self):
        return AbstractProgramLauncher.getCmdLineOptions(self) + "s:p:Ao:"

    def setAttributesFromCmdLine( self, o, a = "" ):
        AbstractProgramLauncher.setAttributesFromCmdLine(self, o, a)
        if o == "-s":
            self.setSubjectFile( a )
        elif o == "-p":
            self.setProgramParameters( a )
        elif o == "-A":
            self.setAllByAll()
        elif o == "-o":
            self.setOutputFile( a )
            
            
    def setSubjectFile( self, arg ):
        self._sbjFile = arg
        
        
    def getSubjectFile( self ):
        return self._sbjFile
    
    
    def setAllByAll( self ):
        self._allByAll = True
        
        
    def getAllByAll( self ):
        return self._allByAll
    
    
    def check( self ):
        """
        Check the specific attributes before running the program.
        """
        AbstractProgramLauncher.check(self)
        if self._sbjFile == "":
            string = "ERROR: missing subject file (-s)"
            print string
            print self.getHelpAsString()
            sys.exit(1)
        if self.getOutputFile() == "":
            self.setOutputFile( "%s.align" % ( self.getInputFile() ) )
            
            
    def setWrapperCommandLine( self ):
        """
        Set the command-line of the wrapper.
        Required for BlatClusterLauncher.
        """
        self._wrpCmdLine = self.getWrapperName()
        self._wrpCmdLine += " -i %s" % ( self.getInputFile() )
        self._wrpCmdLine += " -s %s" % ( self.getSubjectFile() )
        if self.getProgramParameters() != "":
            self._wrpCmdLine += " -p '%s'" % ( self.getProgramParameters() )
        if self.getAllByAll():
            self._wrpCmdLine += " -A"
        if self.getOutputFile() == "":
            self.setOutputFile( "%s.align" % ( self.getInputFile() ) )
        self._wrpCmdLine += " -o %s" % ( self.getOutputFile() )
        if self.getClean():
            self._wrpCmdLine += " -c"
        self._wrpCmdLine += " -v %i" % ( self.getVerbosityLevel() )
        
        
    def setProgramCommandLine( self ):
        """
        Set the command-line of the program.
        """
        self._prgCmdLine = self.getProgramName()
        self._prgCmdLine += " %s" % ( self.getSubjectFile() )
        self._prgCmdLine += " %s" % ( self.getInputFile() )
        if self.getProgramParameters() != "":
            self._prgCmdLine += " %s" % ( self.getProgramParameters() )
        self._prgCmdLine += " -out=blast8"
        self._prgCmdLine += " %s.blast" % ( self.getInputFile() )
        
        
    def setListFilesToKeep( self ):
        """
        Set the list of files to keep.
        """
        if self.getOutputFile() == "":
            self.setOutputFile( "%s.align" % ( self.getInputFile() ) )
        self.appendFileToKeep( self.getOutputFile() )
        
        
    def setListFilesToRemove( self ):
        """
        Set the list of files to remove.
        """
        self.appendFileToRemove( "%s.blast" % ( self.getInputFile() ) )
        
        
    def convertBlastIntoAlign( self ):
        """
        Convert a 'blast' file into the 'align' format.
        """
        cmd = os.environ["REPET_PATH"] + "/bin/blast2align.py"
        cmd += " -i %s.blast" % ( self.getInputFile() )
        cmd += " -o %s" % ( self.getOutputFile() )
        exitStatus = os.system( cmd )
        if exitStatus != 0:
            string = "ERROR while converting 'blast' file into 'align' format"
            print string
            sys.exit(1)
            
            
    def setSummary( self ):
        self._summary = "input file: %s" % ( self.getInputFile() )
        self._summary += "\nsubject file: %s" % ( self.getSubjectFile() )
        self._summary += "\nparameters: %s" % ( self.getProgramParameters() )
        if self.getAllByAll():
            self._summary += "\nall-by-all"
        if self.getOutputFile() == "":
            self.setOutputFile( "%s.align" % ( self.getInputFile() ) )
        self._summary += "\noutput file: %s" % ( self.getOutputFile() )
        
        
    def run( self ):
        """
        Run the program.
        """
        self.start()
        
        self.setProgramCommandLine()
        cmd = self.getProgramCommandLine()
        if self.getVerbosityLevel() > 0:
            print "LAUNCH: %s" % ( cmd )
            sys.stdout.flush()
        exitStatus = os.system( cmd )
        if exitStatus != 0:
            string = "ERROR: program '%s' returned exit status '%i'" % ( self.getProgramName(), exitStatus )
            print string
            sys.exit(1)
            
        self.convertBlastIntoAlign()
        
        self.end()
        
        
if __name__ == "__main__":
    i = BlatProgramLauncher()
    i.checkAttributesFromCmdLine()
    i.run()