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