Mercurial > repos > yufei-luo > s_mart
comparison commons/launcher/BlatProgramLauncher.py @ 18:94ab73e8a190
Uploaded
author | m-zytnicki |
---|---|
date | Mon, 29 Apr 2013 03:20:15 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
17:b0e8584489e6 | 18:94ab73e8a190 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 ##@file | |
4 # Launch Blat (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 'blat' (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 commons.pyRepetUnit.components.AbstractProgramLauncher import AbstractProgramLauncher | |
20 | |
21 | |
22 class BlatProgramLauncher( AbstractProgramLauncher ): | |
23 """ | |
24 Launch Blat (pairwise alignment). | |
25 """ | |
26 | |
27 def __init__( self ): | |
28 """ | |
29 Constructor. | |
30 """ | |
31 AbstractProgramLauncher.__init__( self ) | |
32 self._prgName = "blat" | |
33 self._formatInFile = "fasta" | |
34 self._sbjFile = "" | |
35 self._prgParam = "" | |
36 self._allByAll = False | |
37 | |
38 | |
39 def getHelpAsString( self ): | |
40 string = AbstractProgramLauncher.getHelpAsString(self) | |
41 string += "\nspecific options:" | |
42 string += "\n -s: name of the subject file (database, format='fasta')" | |
43 string += "\n -p: parameters for '%s'" % ( self.getProgramName() ) | |
44 string += "\n -A: same sequences (all-by-all)" | |
45 string += "\n -o: name of the output file (format='align', default=inFile+'.align')" | |
46 return string | |
47 | |
48 def getCmdLineOptions(self): | |
49 return AbstractProgramLauncher.getCmdLineOptions(self) + "s:p:Ao:" | |
50 | |
51 def setAttributesFromCmdLine( self, o, a = "" ): | |
52 AbstractProgramLauncher.setAttributesFromCmdLine(self, o, a) | |
53 if o == "-s": | |
54 self.setSubjectFile( a ) | |
55 elif o == "-p": | |
56 self.setProgramParameters( a ) | |
57 elif o == "-A": | |
58 self.setAllByAll() | |
59 elif o == "-o": | |
60 self.setOutputFile( a ) | |
61 | |
62 | |
63 def setSubjectFile( self, arg ): | |
64 self._sbjFile = arg | |
65 | |
66 | |
67 def getSubjectFile( self ): | |
68 return self._sbjFile | |
69 | |
70 | |
71 def setAllByAll( self ): | |
72 self._allByAll = True | |
73 | |
74 | |
75 def getAllByAll( self ): | |
76 return self._allByAll | |
77 | |
78 | |
79 def check( self ): | |
80 """ | |
81 Check the specific attributes before running the program. | |
82 """ | |
83 AbstractProgramLauncher.check(self) | |
84 if self._sbjFile == "": | |
85 string = "ERROR: missing subject file (-s)" | |
86 print string | |
87 print self.getHelpAsString() | |
88 sys.exit(1) | |
89 if self.getOutputFile() == "": | |
90 self.setOutputFile( "%s.align" % ( self.getInputFile() ) ) | |
91 | |
92 | |
93 def setWrapperCommandLine( self ): | |
94 """ | |
95 Set the command-line of the wrapper. | |
96 Required for BlatClusterLauncher. | |
97 """ | |
98 self._wrpCmdLine = self.getWrapperName() | |
99 self._wrpCmdLine += " -i %s" % ( self.getInputFile() ) | |
100 self._wrpCmdLine += " -s %s" % ( self.getSubjectFile() ) | |
101 if self.getProgramParameters() != "": | |
102 self._wrpCmdLine += " -p '%s'" % ( self.getProgramParameters() ) | |
103 if self.getAllByAll(): | |
104 self._wrpCmdLine += " -A" | |
105 if self.getOutputFile() == "": | |
106 self.setOutputFile( "%s.align" % ( self.getInputFile() ) ) | |
107 self._wrpCmdLine += " -o %s" % ( self.getOutputFile() ) | |
108 if self.getClean(): | |
109 self._wrpCmdLine += " -c" | |
110 self._wrpCmdLine += " -v %i" % ( self.getVerbosityLevel() ) | |
111 | |
112 | |
113 def setProgramCommandLine( self ): | |
114 """ | |
115 Set the command-line of the program. | |
116 """ | |
117 self._prgCmdLine = self.getProgramName() | |
118 self._prgCmdLine += " %s" % ( self.getSubjectFile() ) | |
119 self._prgCmdLine += " %s" % ( self.getInputFile() ) | |
120 if self.getProgramParameters() != "": | |
121 self._prgCmdLine += " %s" % ( self.getProgramParameters() ) | |
122 self._prgCmdLine += " -out=blast8" | |
123 self._prgCmdLine += " %s.blast" % ( self.getInputFile() ) | |
124 | |
125 | |
126 def setListFilesToKeep( self ): | |
127 """ | |
128 Set the list of files to keep. | |
129 """ | |
130 if self.getOutputFile() == "": | |
131 self.setOutputFile( "%s.align" % ( self.getInputFile() ) ) | |
132 self.appendFileToKeep( self.getOutputFile() ) | |
133 | |
134 | |
135 def setListFilesToRemove( self ): | |
136 """ | |
137 Set the list of files to remove. | |
138 """ | |
139 self.appendFileToRemove( "%s.blast" % ( self.getInputFile() ) ) | |
140 | |
141 | |
142 def convertBlastIntoAlign( self ): | |
143 """ | |
144 Convert a 'blast' file into the 'align' format. | |
145 """ | |
146 cmd = os.environ["REPET_PATH"] + "/bin/blast2align.py" | |
147 cmd += " -i %s.blast" % ( self.getInputFile() ) | |
148 cmd += " -o %s" % ( self.getOutputFile() ) | |
149 exitStatus = os.system( cmd ) | |
150 if exitStatus != 0: | |
151 string = "ERROR while converting 'blast' file into 'align' format" | |
152 print string | |
153 sys.exit(1) | |
154 | |
155 | |
156 def setSummary( self ): | |
157 self._summary = "input file: %s" % ( self.getInputFile() ) | |
158 self._summary += "\nsubject file: %s" % ( self.getSubjectFile() ) | |
159 self._summary += "\nparameters: %s" % ( self.getProgramParameters() ) | |
160 if self.getAllByAll(): | |
161 self._summary += "\nall-by-all" | |
162 if self.getOutputFile() == "": | |
163 self.setOutputFile( "%s.align" % ( self.getInputFile() ) ) | |
164 self._summary += "\noutput file: %s" % ( self.getOutputFile() ) | |
165 | |
166 | |
167 def run( self ): | |
168 """ | |
169 Run the program. | |
170 """ | |
171 self.start() | |
172 | |
173 self.setProgramCommandLine() | |
174 cmd = self.getProgramCommandLine() | |
175 if self.getVerbosityLevel() > 0: | |
176 print "LAUNCH: %s" % ( cmd ) | |
177 sys.stdout.flush() | |
178 exitStatus = os.system( cmd ) | |
179 if exitStatus != 0: | |
180 string = "ERROR: program '%s' returned exit status '%i'" % ( self.getProgramName(), exitStatus ) | |
181 print string | |
182 sys.exit(1) | |
183 | |
184 self.convertBlastIntoAlign() | |
185 | |
186 self.end() | |
187 | |
188 | |
189 if __name__ == "__main__": | |
190 i = BlatProgramLauncher() | |
191 i.checkAttributesFromCmdLine() | |
192 i.run() |