annotate uniqprimer-0.5.0/utils.py @ 6:8218425fda1d draft default tip

Uploaded
author dereeper
date Tue, 04 Jan 2022 16:05:55 +0000
parents 3249d78ecfc2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
1 '''
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
2 Created on Jan 1, 2011
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
3
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
4 @author: John L. Herndon
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
5 @contact: herndon@cs.colostate.edu
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
6 @organization: Colorado State University
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
7 @group: Computer Science Department, Asa Ben-Hur's laboratory
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
8 '''
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
9
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
10
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
11 import exceptions
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
12 import time
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
13 import os.path
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
14 from os import pathsep
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
15 from string import split
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
16 import tempfile
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
17 import shutil
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
18
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
19 def getTimeStamp( ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
20 return time.strftime('%d%m%Y-%H%M%S')
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
21
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
22
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
23 class Match( object ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
24 '''
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
25 record where two genomes line up. stores only alignments for one part of the genome
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
26 '''
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
27 def __init__( self, start, end, seqID ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
28 self.seqID = seqID
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
29 self.start = start
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
30 self.end = end
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
31
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
32 def __repr__( self ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
33 return "Start: {0}, End:{1}, SeqID:{2}".format( self.start, self.end, self.seqID )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
34
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
35 class PrimerSet( object ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
36
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
37 def __init__( self, id ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
38 self.id = id
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
39 self.productSize = 0
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
40 self.forwardPrimer = ""
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
41 self.forwardMeltTemp = ""
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
42 self.reversePrimer = ""
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
43 self.reverseMeltTemp = ""
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
44
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
45
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
46 def setProductSize( self, productSize):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
47 self.productSize = productSize
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
48
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
49 def setForwardPrimerData( self, sequence, temp ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
50 self.forwardPrimer = sequence
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
51 self.forwardMeltTemp = temp
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
52
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
53 def setReversePrimerData( self, sequence, temp):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
54 self.reversePrimer = sequence
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
55 self.reverseMeltTemp = temp
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
56
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
57
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
58 #search function from http://code.activestate.com/recipes/52224-find-a-file-given-a-search-path/
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
59 def search_file( filename ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
60 """ find file
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
61 """
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
62 search_path = os.getenv( 'PATH' )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
63 logMessage( "utils::search_file", "Path: {0}".format( search_path ) )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
64 file_found = 0
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
65 paths = split( search_path, pathsep )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
66 for path in paths:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
67 if os.path.exists( os.path.join( path, filename ) ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
68 file_found = 1
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
69 break
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
70 if file_found:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
71 return os.path.abspath( os.path.join( path, filename ) )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
72 else:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
73 return None
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
74
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
75 tempDir = ""
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
76 removeTemp = True
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
77
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
78 verbose = False
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
79
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
80
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
81 def initialize( isVerbose, cleanup, lf): ##Mau: added lf
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
82 global removeTemp
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
83 global tempDir
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
84 global verbose
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
85 global logFile ##Mau: added logFile variable
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
86
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
87 logFile = lf #:Mau add line
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
88
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
89 verbose = isVerbose
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
90 tempDir = tempfile.mkdtemp( dir="" )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
91 initializeLogging()
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
92 removeTemp = cleanup
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
93 logMessage( "utils::Initialize( )", "Initialization complete. Temporary directory: {0}".format( tempDir ) )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
94
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
95 logFile = None
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
96
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
97
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
98 def printProgressMessage( message ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
99 global verbose
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
100 if verbose == True:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
101 print message
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
102
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
103 def getTemporaryDirectory( ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
104 global tempDir
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
105 return tempDir
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
106
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
107 def initializeLogging():
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
108 global logFile
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
109 #logFileName = "uniqprimer_{0}.log".format( getTimeStamp( ) )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
110 #logFileName = "log_uniqprimer.txt" ##Mau: commented out
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
111 logFileName = logFile ##Mau: changed
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
112 logFile = open( logFileName, 'w' )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
113
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
114 def shutdown( ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
115 global removeTemp
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
116 global tempDir
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
117 shutdownLogging( )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
118 if removeTemp == True:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
119 print "*** Removing temporary directory ***"
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
120 shutil.rmtree( tempDir )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
121
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
122 def shutdownLogging( ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
123 global logFile
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
124 if logFile != None:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
125 logFile.close( )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
126
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
127 def logList( method, list ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
128
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
129 message = reduce( lambda x,y: str( x ) + " " + str( y ) , list )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
130 logMessage(method, message)
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
131
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
132
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
133 def logMessage( method, message ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
134 global logFile
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
135 if logFile == None:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
136 return
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
137 log = "{0} - {1}".format( method, message )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
138
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
139 logFile.write( log + "\n" )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
140 logFile.flush( )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
141
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
142 class EPrimerOptions( object ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
143
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
144 def __init__( self ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
145
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
146 self.minPrimerSize = 18
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
147 self.maxPrimerSize = 27
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
148 self.primerSize = 20
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
149 self.productRange = "200-250"
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
150
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
151 def setPrimerSize( self, size ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
152
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
153 size = int( size )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
154 if size > 35:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
155 size = 35
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
156
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
157 self.primerSize = size
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
158 if self.primerSize < self.minPrimerSize:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
159 self.maxPrimerSize = self.primerSize
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
160 elif self.primerSize > self.maxPrimerSize:
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
161 self.maxPrimerSize = self.primerSize
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
162
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
163 def getPrimerSize( self ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
164 return self.primerSize
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
165
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
166 def setMinPrimerSize( self, minSize):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
167 self.minPrimerSize = minSize
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
168
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
169 def getMinPrimerSize( self ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
170 return self.minPrimerSize
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
171
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
172 def setMaxPrimerSize( self, size ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
173 self.maxPrimerSize = size
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
174
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
175 def getMaxPrimerSize( self ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
176 return self.maxPrimerSize
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
177
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
178 def setProductRange( self, range ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
179 self.productRange = range
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
180
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
181 def getProductRange( self ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
182 return self.productRange
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
183
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
184 class NoPrimersExistException( exceptions.BaseException ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
185
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
186 def __init__( self ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
187 exceptions.BaseException( self )
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
188
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
189 class ProgramNotFoundException( exceptions.BaseException ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
190
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
191 def __init__( self, programName, details ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
192 exceptions.BaseException.__init__(self)
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
193 self.programName = programName
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
194 self.details = details
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
195
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
196 class NoFileFoundException( exceptions.BaseException ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
197
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
198 def __init__( self, filename ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
199 exceptions.BaseException.__init__(self)
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
200 self.filename = filename
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
201
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
202
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
203 class ModuleNotInitializedException( exceptions.BaseException ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
204
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
205 def __init__( self, moduleName, reason ):
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
206 self.moduleName = moduleName
3249d78ecfc2 Uploaded
dereeper
parents:
diff changeset
207 self.reason = reason