view commons/launcher/YassProgramLauncher.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 Yass (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 'yass' (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 pyRepet.launcher.AbstractProgramLauncher import AbstractProgramLauncher


class YassProgramLauncher( AbstractProgramLauncher ):
    """
    Launch Yass (pairwise alignment).
    """
    
    def __init__( self ):
        """
        Constructor.
        """
        AbstractProgramLauncher.__init__( self )
        self._prgName = "yass"
        self._formatInFile = "fasta"
        self._sbjFile = ""
        self._prgParam = ""
        self._allByAll = False
        self._cmdLineSpecificOptions = "s:p:Ao:"
        
        
    def getSpecificHelpAsString( self ):
        """
        Return the specific help as a string.
        """
        string = ""
        string += "\nspecific options:"
        string += "\n     -s: name of the subject file (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 setASpecificAttributeFromCmdLine( self, o, a="" ):
        """
        Set a specific attribute from the command-line arguments.
        """
        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 checkSpecificAttributes( self ):
        """
        Check the specific attributes before running the program.
        """
        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 YassClusterLauncher.
        """
        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 += " -d 2"
        if self.getProgramParameters() != "":
            self._prgCmdLine += " %s" % ( self.getProgramParameters() )
        self._prgCmdLine += " -o %s.blast" % ( self.getInputFile() )
        self._prgCmdLine += " %s" % ( self.getInputFile() )
        self._prgCmdLine += " %s" % ( self.getSubjectFile() )
        
        
    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.
        """
        pass
    
    
    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 = YassProgramLauncher()
    i.setAttributesFromCmdLine()
    i.run()