Mercurial > repos > yufei-luo > s_mart
diff commons/launcher/NWalignProgramLauncher.py @ 18:94ab73e8a190
Uploaded
author | m-zytnicki |
---|---|
date | Mon, 29 Apr 2013 03:20:15 -0400 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/commons/launcher/NWalignProgramLauncher.py Mon Apr 29 03:20:15 2013 -0400 @@ -0,0 +1,187 @@ +#!/usr/bin/env python + +##@file +# Launch NWalign (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 'NWalign' (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 +from commons.core.seq.AlignedBioseqDB import AlignedBioseqDB +from commons.core.coord.AlignUtils import AlignUtils + + +class NWalignProgramLauncher( AbstractProgramLauncher ): + """ + Launch NWalign (pairwise alignment). + """ + + def __init__( self ): + """ + Constructor. + """ + AbstractProgramLauncher.__init__( self ) + self._prgName = "NWalign" + self._formatInFile = "fasta" + self._sbjFile = "" + self._prgParam = "" + self._cmdLineSpecificOptions = "s:p:o:" + + + 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 -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 == "-o": + self.setOutputFile( a ) + + + def setSubjectFile( self, arg ): + self._sbjFile = arg + + + def getSubjectFile( self ): + return self._sbjFile + + + 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 NWalignClusterLauncher. + """ + 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.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() + if self.getProgramParameters() != "": + self._prgCmdLine += " %s" % ( self.getProgramParameters() ) + self._prgCmdLine += " -o %s.afa" % ( self.getInputFile() ) + self._prgCmdLine += " -v %i" % ( self.getVerbosityLevel() ) + self._prgCmdLine += " %s" % ( self.getSubjectFile() ) + self._prgCmdLine += " %s" % ( self.getInputFile() ) + + + def setListFilesToKeep( self ): + """ + Set the list of files to keep. + """ + if self.getOutputFile() == "": + self.setOutputFile( "%s.afa" % ( self.getInputFile() ) ) + self.appendFileToKeep( self.getOutputFile() ) + + + def postprocess( self ): + absDB = AlignedBioseqDB( "%s.afa" % ( self.getInputFile() ) ) + lHeaders = absDB.getHeaderList() + queryHeader = lHeaders[0] + subjectHeader = lHeaders[1] + queryLength = absDB.fetch( queryHeader ).getLength() + subjectLength = absDB.fetch( subjectHeader ).getLength() + lAligns = absDB.getAlignList( queryHeader, subjectHeader ) + for i in lAligns: + if "re-oriented" in i.getQueryName(): + i.setQueryName( queryHeader.replace(" re-oriented","") ) + start = i.getQueryStart() + end = i.getQueryEnd() + i.setQueryStart( queryLength - end + 1 ) + i.setQueryEnd( queryLength - start + 1 ) + if "re-oriented" in i.getSubjectName(): + i.setSubjectName( subjectHeader.replace(" re-oriented","") ) + start = i.getSubjectStart() + end = i.getSubjectEnd() + i.setSubjectEnd( subjectLength - end + 1 ) + i.setSubjectStart( subjectLength - start + 1 ) + if not i.isQueryOnDirectStrand(): + i.reverse() + AlignUtils.writeListInFile( lAligns, self.getOutputFile() ) + os.remove( "%s.afa" % ( self.getInputFile() ) ) + + + 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.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.postprocess() + + self.end() + + +if __name__ == "__main__": + i = NWalignProgramLauncher() + i.setAttributesFromCmdLine() + i.run()