Mercurial > repos > yufei-luo > s_mart
comparison commons/launcher/NWalignProgramLauncher.py @ 31:0ab839023fe4
Uploaded
author | m-zytnicki |
---|---|
date | Tue, 30 Apr 2013 14:33:21 -0400 |
parents | 94ab73e8a190 |
children |
comparison
equal
deleted
inserted
replaced
30:5677346472b5 | 31:0ab839023fe4 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 ##@file | |
4 # Launch NWalign (pairwise alignment). | |
5 # | |
6 # options: | |
7 # -h: this help | |
8 # -i: name of the input file (queries, format='fasta') | |
9 # -s: name of the subject file (format='fasta') | |
10 # -p: parameters for 'NWalign' (default='-d 2') | |
11 # -o: name of the output file (format='align', default=inFile+'.align') | |
12 # -c: clean | |
13 # -v: verbosity level (default=0/1) | |
14 | |
15 | |
16 import os | |
17 import sys | |
18 | |
19 from pyRepet.launcher.AbstractProgramLauncher import AbstractProgramLauncher | |
20 from commons.core.seq.AlignedBioseqDB import AlignedBioseqDB | |
21 from commons.core.coord.AlignUtils import AlignUtils | |
22 | |
23 | |
24 class NWalignProgramLauncher( AbstractProgramLauncher ): | |
25 """ | |
26 Launch NWalign (pairwise alignment). | |
27 """ | |
28 | |
29 def __init__( self ): | |
30 """ | |
31 Constructor. | |
32 """ | |
33 AbstractProgramLauncher.__init__( self ) | |
34 self._prgName = "NWalign" | |
35 self._formatInFile = "fasta" | |
36 self._sbjFile = "" | |
37 self._prgParam = "" | |
38 self._cmdLineSpecificOptions = "s:p:o:" | |
39 | |
40 | |
41 def getSpecificHelpAsString( self ): | |
42 """ | |
43 Return the specific help as a string. | |
44 """ | |
45 string = "" | |
46 string += "\nspecific options:" | |
47 string += "\n -s: name of the subject file (format='fasta')" | |
48 string += "\n -p: parameters for '%s'" % ( self.getProgramName() ) | |
49 string += "\n -o: name of the output file (format='align', default=inFile+'.align')" | |
50 return string | |
51 | |
52 | |
53 def setASpecificAttributeFromCmdLine( self, o, a="" ): | |
54 """ | |
55 Set a specific attribute from the command-line arguments. | |
56 """ | |
57 if o =="-s": | |
58 self.setSubjectFile( a ) | |
59 elif o == "-p": | |
60 self.setProgramParameters( a ) | |
61 elif o == "-o": | |
62 self.setOutputFile( a ) | |
63 | |
64 | |
65 def setSubjectFile( self, arg ): | |
66 self._sbjFile = arg | |
67 | |
68 | |
69 def getSubjectFile( self ): | |
70 return self._sbjFile | |
71 | |
72 | |
73 def checkSpecificAttributes( self ): | |
74 """ | |
75 Check the specific attributes before running the program. | |
76 """ | |
77 if self._sbjFile == "": | |
78 string = "ERROR: missing subject file (-s)" | |
79 print string | |
80 print self.getHelpAsString() | |
81 sys.exit(1) | |
82 if self.getOutputFile() == "": | |
83 self.setOutputFile( "%s.align" % ( self.getInputFile() ) ) | |
84 | |
85 | |
86 def setWrapperCommandLine( self ): | |
87 """ | |
88 Set the command-line of the wrapper. | |
89 Required for NWalignClusterLauncher. | |
90 """ | |
91 self._wrpCmdLine = self.getWrapperName() | |
92 self._wrpCmdLine += " -i %s" % ( self.getInputFile() ) | |
93 self._wrpCmdLine += " -s %s" % ( self.getSubjectFile() ) | |
94 if self.getProgramParameters() != "": | |
95 self._wrpCmdLine += " -p '%s'" % ( self.getProgramParameters() ) | |
96 if self.getOutputFile() == "": | |
97 self.setOutputFile( "%s.align" % ( self.getInputFile() ) ) | |
98 self._wrpCmdLine += " -o %s" % ( self.getOutputFile() ) | |
99 if self.getClean(): | |
100 self._wrpCmdLine += " -c" | |
101 self._wrpCmdLine += " -v %i" % ( self.getVerbosityLevel() ) | |
102 | |
103 | |
104 def setProgramCommandLine( self ): | |
105 """ | |
106 Set the command-line of the program. | |
107 """ | |
108 self._prgCmdLine = self.getProgramName() | |
109 if self.getProgramParameters() != "": | |
110 self._prgCmdLine += " %s" % ( self.getProgramParameters() ) | |
111 self._prgCmdLine += " -o %s.afa" % ( self.getInputFile() ) | |
112 self._prgCmdLine += " -v %i" % ( self.getVerbosityLevel() ) | |
113 self._prgCmdLine += " %s" % ( self.getSubjectFile() ) | |
114 self._prgCmdLine += " %s" % ( self.getInputFile() ) | |
115 | |
116 | |
117 def setListFilesToKeep( self ): | |
118 """ | |
119 Set the list of files to keep. | |
120 """ | |
121 if self.getOutputFile() == "": | |
122 self.setOutputFile( "%s.afa" % ( self.getInputFile() ) ) | |
123 self.appendFileToKeep( self.getOutputFile() ) | |
124 | |
125 | |
126 def postprocess( self ): | |
127 absDB = AlignedBioseqDB( "%s.afa" % ( self.getInputFile() ) ) | |
128 lHeaders = absDB.getHeaderList() | |
129 queryHeader = lHeaders[0] | |
130 subjectHeader = lHeaders[1] | |
131 queryLength = absDB.fetch( queryHeader ).getLength() | |
132 subjectLength = absDB.fetch( subjectHeader ).getLength() | |
133 lAligns = absDB.getAlignList( queryHeader, subjectHeader ) | |
134 for i in lAligns: | |
135 if "re-oriented" in i.getQueryName(): | |
136 i.setQueryName( queryHeader.replace(" re-oriented","") ) | |
137 start = i.getQueryStart() | |
138 end = i.getQueryEnd() | |
139 i.setQueryStart( queryLength - end + 1 ) | |
140 i.setQueryEnd( queryLength - start + 1 ) | |
141 if "re-oriented" in i.getSubjectName(): | |
142 i.setSubjectName( subjectHeader.replace(" re-oriented","") ) | |
143 start = i.getSubjectStart() | |
144 end = i.getSubjectEnd() | |
145 i.setSubjectEnd( subjectLength - end + 1 ) | |
146 i.setSubjectStart( subjectLength - start + 1 ) | |
147 if not i.isQueryOnDirectStrand(): | |
148 i.reverse() | |
149 AlignUtils.writeListInFile( lAligns, self.getOutputFile() ) | |
150 os.remove( "%s.afa" % ( self.getInputFile() ) ) | |
151 | |
152 | |
153 def setSummary( self ): | |
154 self._summary = "input file: %s" % ( self.getInputFile() ) | |
155 self._summary += "\nsubject file: %s" % ( self.getSubjectFile() ) | |
156 self._summary += "\nparameters: %s" % ( self.getProgramParameters() ) | |
157 if self.getOutputFile() == "": | |
158 self.setOutputFile( "%s.align" % ( self.getInputFile() ) ) | |
159 self._summary += "\noutput file: %s" % ( self.getOutputFile() ) | |
160 | |
161 | |
162 def run( self ): | |
163 """ | |
164 Run the program. | |
165 """ | |
166 self.start() | |
167 | |
168 self.setProgramCommandLine() | |
169 cmd = self.getProgramCommandLine() | |
170 if self.getVerbosityLevel() > 0: | |
171 print "LAUNCH: %s" % ( cmd ) | |
172 sys.stdout.flush() | |
173 exitStatus = os.system( cmd ) | |
174 if exitStatus != 0: | |
175 string = "ERROR: program '%s' returned exit status '%i'" % ( self.getProgramName(), exitStatus ) | |
176 print string | |
177 sys.exit(1) | |
178 | |
179 self.postprocess() | |
180 | |
181 self.end() | |
182 | |
183 | |
184 if __name__ == "__main__": | |
185 i = NWalignProgramLauncher() | |
186 i.setAttributesFromCmdLine() | |
187 i.run() |