comparison uniqprimer-0.5.0/uniqprimer.py @ 0:cdd8f911ad91 draft

Uploaded
author dereeper
date Fri, 07 Oct 2016 04:18:11 -0400
parents
children 2321523366d7
comparison
equal deleted inserted replaced
-1:000000000000 0:cdd8f911ad91
1 #!/usr/bin/python
2
3 '''
4 Created on Jan 1, 2011
5
6 @author: John L. Herndon
7 @contact: herndon@cs.colostate.edu
8 @organization: Colorado State University
9 @group: Computer Science Department, Asa Ben-Hur's laboratory
10 '''
11
12 import exceptions
13 import sys
14 import time
15 import os ## added by Alexis
16
17 import getopt
18 from primertools import *
19
20 version="0.5.0"
21
22
23 class UniqPrimerFinder( object ):
24
25 def __init__( self, includeFiles, excludeFiles, crossValidate, eprimerOptions):
26
27 utils.logMessage( "UniqPrimerFinder::__init__()", "Initializing UniqPrimerFinder" )
28 self.includeFiles = includeFiles
29 self.includeFileManager = includefilemanager.IncludeFileManager( )
30
31 self.excludeFiles = excludeFiles
32 self.excludeFileManager= excludefilemanager.ExcludeFileManager( )
33
34 self.primerManager = primermanager.PrimerManager( eprimerOptions )
35
36 self.crossValidate = crossValidate
37
38
39 utils.logMessage( "UniqPrimerFinder::__init__()", "Initializing UniqPrimerFinder - complete" )
40
41 def writeOutputFile( self, primers, outputFileName, maxresults = 100 ):
42 '''
43 primers: a list of PrimerSet obs
44 '''
45 ##outputFileName = uPrimer ##Mau: defined this..
46 outputFile = open( outputFileName, 'w' )
47
48 i = 0
49 for primer in primers:
50 i += 1
51
52 outputFile.write( "{0}\t{1}\t{2}\t{3}\n".format( i, primer.forwardPrimer, primer.reversePrimer, primer.productSize ) )
53
54 if i > maxresults:
55 break
56
57 utils.logMessage( "UniqPrimerFinder::writeOutputFile()", "output file written." )
58
59
60 def findPrimers( self, outputFile = "uPrimer.txt" ):
61 outputFile = uPrimer ## Mau adds to overwrite the above value
62
63
64 utils.logMessage( "UniqPrimerFinder::findPrimers()", "Finding primers for include files" )
65 startTime = time.time( )
66 #generate the combined sequence fasta file for all exclude sequences
67 utils.printProgressMessage( "*** Creating Combined Fasta File for Exclude Files ***" )
68 for excludeFile in self.excludeFiles:
69 self.excludeFileManager.addExcludeFile( excludeFile )
70
71 self.excludeFileManager.exportSequences( )
72
73 self.includeFileManager.setExcludeFile( self.excludeFileManager.getOutputFileName( ) )
74
75 utils.printProgressMessage( "*** Finding Sequences Unique to Target Genome ***" )
76
77 #run nucmer program on all include files
78 for includeFile in self.includeFiles:
79 self.includeFileManager.processIncludeFile( includeFile )
80
81 #get the sequences found in include files, but no the exclude file.
82 uniqueSequences = self.includeFileManager.getUniqueSequences( )
83
84 utils.printProgressMessage( "*** Finding Primers ***" )
85
86 primers = self.primerManager.getPrimers( uniqueSequences )
87
88 if self.crossValidate == True:
89 utils.printProgressMessage( "*** Cross Validating Primers ***" )
90 primers = self.primerManager.crossValidatePrimers( primers, self.excludeFileManager.getOutputFileName( ) )
91 # added by Alexis, primersearch also against all include files
92 #run primersearch program on all include files
93 j=0
94 for includeFile in self.includeFiles: # added by Alexis
95 j = j + 1
96 primers = self.primerManager.crossValidatePrimers2( primers, includeFile, j) # added by Alexis
97
98
99 utils.logMessage( "UniqPrimerFinder::findPrimers( )", "found {0} unique sequences".format( len( primers ) ) )
100
101 self.writeOutputFile( primers, outputFile )
102
103 utils.logMessage( "UniqPrimerFinder::findPrimers()", "Finished finding primers" )
104 endTime = time.time()
105 elapsedMinutes = int( ( endTime - startTime ) / 60 )
106 elapsedSeconds = int( ( endTime - startTime ) % 60 )
107 print "*** Time Elapsed: {0} minutes, {1} seconds ***".format( elapsedMinutes, elapsedSeconds )
108 print "*** Output Written to {0} ***".format( outputFile )
109
110
111 def printUsageAndQuit( ):
112 global version
113 print "uniqprimer - finds primers unique to a genome"
114 print "Version: " + str( version )
115 print "Summary of Options."
116 print "Required Arguments:"
117 print " -i <filename>: use <filename> as an include file. Primers will be identified for this genome"
118 print " -x <filename>: use <filename> as an exclude file. Primers for this genome will be excluded"
119 print " -o <filename>: specify the name of the unique primer output file (default is uPrimer.txt)" ## Mau added..
120 print " -l <filename>: specify the name of the log output file" ## Mau added..
121 print " -f <filename>: specify the name of the Fasta of differential sequences" ## Alexis added..
122
123 print "\nOptional Arguments:"
124 print " --productsizerage: set a range for the desired size of PCR product (default=200-250). Example: ./uniqprimer -productsizerage 100-150"
125 print " --primersize: set the desired primer size (default=20)"
126 print " --minprimersize: set the minimum primer size (default=27)"
127 print " --maxprimersize: set the maximum primer size (default=18)"
128 print " --crossvalidate: force the program to cross validate primers against exclude files for extra certainty"
129 print " --keeptempfiles: force the program to keep temporary files"
130
131 print "\n\nExample:"
132 print "uniqprimer -i <includefile1> -i <includefile2> ... -i <includefileN> -x <excludefile1> -x <excludefile2> ... -x <excludefileN> -o primers.txt -l logfile.txt -f seqForPrimer3.fa"
133 utils.shutdownLogging( )
134 sys.exit( )
135
136
137 opts = 'i:x:h:o:l:f:' # Mau added :o & :l for outfile specification, Alexis added :f
138 longopts=[ "productsizerange=", "primersize=", "minprimersize=", "maxprimersize=", "crossvalidate", "keeptempfiles" ]
139
140 def parseArgs( args ):
141
142
143 global uPrimer ## Mau added lf, brute force...
144 global lf # Mau added lf, brute force...
145 global fastaDiff # Alexis added fastaDiff
146 #uPrimer = "uPrimer.txt" ##the default value...
147
148 crossValidate = False
149 cleanup = True
150 optlist, args = getopt.getopt( args, opts, longopts )
151
152 includeFiles = [ ]
153 excludeFiles = [ ]
154 eprimerOptions = utils.EPrimerOptions( )
155
156 verbose = False
157 for opt in optlist:
158 if opt[ 0 ] == '-i':
159 includeFiles.append( opt[ 1 ] )
160 elif opt[ 0 ] == '-x':
161 excludeFiles.append( opt[ 1] )
162 elif opt[ 0 ] == '-v':
163 verbose = True
164 elif opt[ 0 ] == '-o': ## Mau added, if -o...
165 uPrimer = str(opt[1]) ## Mau added, then get filename for outfile after -o
166 elif opt[ 0 ] == '-l': ## Mau added, if -l...
167 lf = str(opt[1]) ## Mau added, then get filename for logfile after -l
168 elif opt[ 0 ] == '-f': ## Alexis added, if -f
169 fastaDiff = str(opt[1]) ## Alexis added, then get filename for fasta file after -f
170 elif opt[ 0 ] == '--productsizerange':
171 eprimerOptions.setProductRange( opt[ 1 ] )
172 productsizerange = opt[ 1 ]
173 elif opt[ 0 ] == '--primersize':
174 eprimerOptions.setPrimerSize( opt[1 ] )
175 elif opt[ 0 ] == '--minprimersize':
176 eprimerOptions.setMinPrimerSize( opt[1 ] )
177 elif opt[ 0 ] == '--maxprimersize':
178 eprimerOptions.setMaxPrimerSize( opt[1 ] )
179 elif opt[ 0 ] == '--crossvalidate':
180 crossValidate = True
181 elif opt[ 0 ] == '--crossvalidate':
182 crossValidate = True
183 elif opt[ 0 ] == '--keeptempfiles':
184 cleanup = False
185 elif opt[ 0 ] == '-h':
186 printUsageAndQuit( )
187 else:
188 print "Unknown option: " + str( opt[ 0 ] )
189 printUsageAndQuit( )
190 #print "uPrimer: " + uPrimer + " log file name: " + lf + "\n"
191 if len( includeFiles ) == 0 or len( excludeFiles ) == 0:
192
193 print "You must specify at least one include file and at least one exclude file"
194 printUsageAndQuit( )
195
196 return includeFiles, excludeFiles, crossValidate, cleanup, verbose, eprimerOptions, lf , uPrimer, fastaDiff #Mau: add lf, uPrime
197
198 def main( args, debug = False):
199 #parse the command line arguments for include and exclude files
200
201 includeFiles, excludeFiles, crossValidate, cleanup, verbose, eprimerOptions, lf, uPrimer, fastaDiff = parseArgs( args ) ##Mau add: lf
202 utils.initialize( True, cleanup, lf) ##Mau: add lf
203 #find primers for the include sequences
204
205 tmpdir = utils.getTemporaryDirectory() ## added by Alexis
206 command = "cp -rf " + tmpdir + "/sequenceForEprimer.fasta" + " " + fastaDiff
207
208 try:
209 utils.logMessage( "uniqprimer::Main( )", "Logging include files: " )
210 utils.logList( "uniqprimer::Main( )", includeFiles )
211 utils.logMessage( "uniqprimer::Main( )", "Logging exclude files: " )
212 utils.logList( "uniqprimer::Main( )", excludeFiles)
213 print "*** Finding Primers ***"
214 uniqPrimer = UniqPrimerFinder( includeFiles, excludeFiles, crossValidate, eprimerOptions)
215 uniqPrimer.findPrimers( )
216 except utils.NoFileFoundException as nfe:
217 print "File not found: " + str( nfe.filename )
218 printUsageAndQuit( )
219 except utils.ProgramNotFoundException as pnfe:
220 print str( pnfe.programName ) + ": program is not installed or is not in your path."
221 print str( pnfe.details )
222 except utils.NoPrimersExistException as npe:
223 print "Failure: No unique primers exist for this combination"
224 except exceptions.BaseException as e:
225 print "It appears that an unknown sequence of events has resulted in the internal explosion of this program. Please send the file called \'log_uniqprimer.txt\' to herndon@cs.colostate.edu and tell that bonehead John to fix it!"
226 print "Details:"
227 print e
228
229 os.system("cp -rf " + tmpdir + "/sequenceForEprimer.fasta" + " " + fastaDiff)
230 utils.shutdown( )
231
232 print "*** Finished ***"
233
234 if __name__ == '__main__':
235
236 #temp_args = "-i data/testdata/smallinclude.ffn -x data/testdata/smallexclude.ffn".split( )
237
238 #temp_args = "-i data/XOO_MAI1_scaffolds.fas -x data/KACC.ffn".split( )
239 if len( sys.argv ) == 1:
240 printUsageAndQuit( )
241 main( sys.argv[ 1: ], debug = True )
242
243
244
245
246
247
248
249
250
251
252
253
254