Mercurial > repos > urgi-team > teiso
comparison TEisotools-1.0/commons/core/checker/CheckerUtils.py @ 6:20ec0d14798e draft
Uploaded
author | urgi-team |
---|---|
date | Wed, 20 Jul 2016 05:00:24 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5:4093a2fb58be | 6:20ec0d14798e |
---|---|
1 # Copyright INRA (Institut National de la Recherche Agronomique) | |
2 # http://www.inra.fr | |
3 # http://urgi.versailles.inra.fr | |
4 # | |
5 # This software is governed by the CeCILL license under French law and | |
6 # abiding by the rules of distribution of free software. You can use, | |
7 # modify and/ or redistribute the software under the terms of the CeCILL | |
8 # license as circulated by CEA, CNRS and INRIA at the following URL | |
9 # "http://www.cecill.info". | |
10 # | |
11 # As a counterpart to the access to the source code and rights to copy, | |
12 # modify and redistribute granted by the license, users are provided only | |
13 # with a limited warranty and the software's author, the holder of the | |
14 # economic rights, and the successive licensors have only limited | |
15 # liability. | |
16 # | |
17 # In this respect, the user's attention is drawn to the risks associated | |
18 # with loading, using, modifying and/or developing or reproducing the | |
19 # software by the user in light of its specific status of free software, | |
20 # that may mean that it is complicated to manipulate, and that also | |
21 # therefore means that it is reserved for developers and experienced | |
22 # professionals having in-depth computer knowledge. Users are therefore | |
23 # encouraged to load and test the software's suitability as regards their | |
24 # requirements in conditions enabling the security of their systems and/or | |
25 # data to be ensured and, more generally, to use and operate it in the | |
26 # same conditions as regards security. | |
27 # | |
28 # The fact that you are presently reading this means that you have had | |
29 # knowledge of the CeCILL license and that you accept its terms. | |
30 | |
31 | |
32 import os | |
33 import sys | |
34 import re | |
35 import glob | |
36 from ConfigParser import NoOptionError | |
37 from ConfigParser import NoSectionError | |
38 from commons.core.checker.CheckerException import CheckerException | |
39 | |
40 | |
41 ## A set of static methods used to perform checks. | |
42 # | |
43 # | |
44 class CheckerUtils( object ): | |
45 | |
46 ## Check if blastName param is in ["blastn", "blastp", "blastx", "tblastn", "tblastx"] | |
47 # | |
48 # @param blastName name to check | |
49 # @return True if name is in list False otherwise | |
50 # | |
51 def isBlastNameNotInBlastValues( blastName ): | |
52 blastValuesSet = set( ["blastn", "blastp", "blastx", "tblastn", "tblastx"] ) | |
53 blastNameSet = set( [ blastName ] ) | |
54 return not blastNameSet.issubset( blastValuesSet ) | |
55 | |
56 isBlastNameNotInBlastValues = staticmethod( isBlastNameNotInBlastValues ) | |
57 | |
58 | |
59 ## Check if param is NOT "TRUE" and NOT false "FALSE" | |
60 # | |
61 # @param param str to check | |
62 # @return True if param is not eq to "TRUE" AND not eq to "FALSE", false otherwise | |
63 # | |
64 def isNotTRUEisNotFALSE( param ): | |
65 return param != "TRUE" and param != "FALSE" | |
66 | |
67 isNotTRUEisNotFALSE = staticmethod( isNotTRUEisNotFALSE ) | |
68 | |
69 | |
70 ## Check if resource (file or dir) do NOT exists | |
71 # | |
72 # @param resource file or dir to check | |
73 # @return True if resource exists False otherwise | |
74 # | |
75 def isRessourceNotExits( resource ): | |
76 return not os.path.exists( resource ) | |
77 | |
78 isRessourceNotExits = staticmethod( isRessourceNotExits ) | |
79 | |
80 | |
81 ## Check a specific E-value format: de-dd | |
82 # | |
83 # @param param E-value to check | |
84 # @return True if format is de-dd False otherwise | |
85 # | |
86 def isNotAeValueWithOneDigit2DecimalsAtLeast( param ): | |
87 # \d\d stands for 2 digits and more ??? | |
88 return not re.match( "\de\-\d\d", param ) | |
89 | |
90 isNotAeValueWithOneDigit2DecimalsAtLeast = staticmethod( isNotAeValueWithOneDigit2DecimalsAtLeast ) | |
91 | |
92 | |
93 ## Check a number format | |
94 # | |
95 # @param param value to check | |
96 # @return True if param is a number (d+) False otherwise | |
97 # | |
98 def isNotANumber( param ): | |
99 return not re.match( "\d+", param ) | |
100 | |
101 isNotANumber = staticmethod( isNotANumber ) | |
102 | |
103 | |
104 ## Check if an executable is in the user's PATH | |
105 # | |
106 # @param exeName name of the executable | |
107 # @return True if executable in user's PATH, False otherwise | |
108 # | |
109 def isExecutableInUserPath( exeName ): | |
110 dirPathList = os.environ["PATH"].split(":") | |
111 for dirPath in dirPathList: | |
112 if os.path.isdir( dirPath ): | |
113 try: | |
114 binPathList = glob.glob( dirPath + "/*" ) | |
115 except OSError, e: | |
116 continue | |
117 for binPath in binPathList: | |
118 bin = os.path.basename( binPath ) | |
119 if bin == exeName: | |
120 return True | |
121 return False | |
122 | |
123 isExecutableInUserPath = staticmethod( isExecutableInUserPath ) | |
124 | |
125 | |
126 ## Return the full path of a given executable | |
127 # | |
128 def getFullPathFromExecutable( exeName ): | |
129 lDirFromUserPath = os.environ["PATH"].split(":") | |
130 for dir in lDirFromUserPath: | |
131 if os.path.isdir( dir ): | |
132 try: | |
133 lExecutables = glob.glob( "%s/*" % ( dir ) ) | |
134 except OSError, e: | |
135 continue | |
136 for exe in lExecutables: | |
137 path, exe = os.path.split( exe ) | |
138 if exe == exeName: | |
139 return path | |
140 return "" | |
141 | |
142 getFullPathFromExecutable = staticmethod( getFullPathFromExecutable ) | |
143 | |
144 | |
145 #TODO: to remove ? | |
146 ## Check if a queue Name is valid. Warning: Only with the queue manager SGE | |
147 # | |
148 # @param fullQueueName name of the queue to test (with or without parameters) | |
149 # @return True if queue name is valid, False otherwise | |
150 # | |
151 def isQueueNameValid( fullQueueName ): | |
152 queueName = fullQueueName.split()[0] | |
153 if queueName == "none": | |
154 return True | |
155 queueFile = "queueName.txt" | |
156 if not CheckerUtils.isExecutableInUserPath( "qconf" ): | |
157 msg = "executable 'qconf' can't be found" | |
158 sys.stderr.write( "%s\n" % ( msg ) ) | |
159 return False | |
160 cmd = "qconf -sql > " + queueFile | |
161 os.system( cmd ) | |
162 queueFileHandler = open( queueFile, "r" ) | |
163 lQueueNames = queueFileHandler.readlines() | |
164 queueFileHandler.close() | |
165 os.remove( queueFile ) | |
166 queueNameValid = False | |
167 for qName in lQueueNames: | |
168 qName = qName.strip() | |
169 if qName == queueName: | |
170 queueNameValid = True | |
171 break | |
172 return queueNameValid | |
173 | |
174 isQueueNameValid = staticmethod( isQueueNameValid ) | |
175 | |
176 | |
177 ## Check if a string length is lower or equal than 15 | |
178 # | |
179 # @param strName any string | |
180 # @return True if string length is <= 15, False otherwise | |
181 # | |
182 def isMax15Char( strName ): | |
183 return (len(strName) <= 15 ) | |
184 | |
185 isMax15Char = staticmethod( isMax15Char ) | |
186 | |
187 | |
188 ## Check if a string is made with only alphanumeric or underscore character | |
189 # | |
190 # @param strName any string | |
191 # @return True if string is with alphanumeric or underscore, False otherwise | |
192 # | |
193 def isCharAlphanumOrUnderscore( strName ): | |
194 # authorized ALPHABET [a-z,A-Z,0-9,_] | |
195 p = re.compile('\W') | |
196 errList=p.findall(strName) | |
197 if len( errList ) > 0 : | |
198 return False | |
199 else: | |
200 return True | |
201 | |
202 isCharAlphanumOrUnderscore = staticmethod( isCharAlphanumOrUnderscore ) | |
203 | |
204 | |
205 ## Check if sectionName is in the configuration file | |
206 # | |
207 # @param config filehandle of configuration file | |
208 # @param sectionName string of section name to check | |
209 # @exception NoSectionError: if section not found raise a NoSectionError | |
210 # | |
211 def checkSectionInConfigFile( config, sectionName ): | |
212 if not (config.has_section(sectionName)): | |
213 raise NoSectionError(sectionName) | |
214 | |
215 checkSectionInConfigFile = staticmethod( checkSectionInConfigFile ) | |
216 | |
217 | |
218 ## Check if an option is in a specified section in the configuration file | |
219 # | |
220 # @param config filehandle of configuration file | |
221 # @param sectionName string of section name | |
222 # @param optionName string of option name to check | |
223 # @exception NoOptionError: if option not found raise a NoOptionError | |
224 # | |
225 def checkOptionInSectionInConfigFile( config, sectionName, optionName ): | |
226 config.get( sectionName, optionName ) | |
227 | |
228 checkOptionInSectionInConfigFile = staticmethod( checkOptionInSectionInConfigFile ) | |
229 | |
230 | |
231 ## Check version number coherency between configFile and CHANGELOG | |
232 # | |
233 # @param config ConfigParser Instance of configuration file | |
234 # @param changeLogFileHandle CHANGELOG file handle | |
235 # @exception NoOptionError: if option not found raise a NoOptionError | |
236 # | |
237 def checkConfigVersion( changeLogFileHandle, config ): | |
238 line = changeLogFileHandle.readline() | |
239 while not line.startswith("REPET release "): | |
240 line = changeLogFileHandle.readline() | |
241 numVersionChangeLog = line.split()[2] | |
242 | |
243 numVersionConfig = config.get("repet_env", "repet_version") | |
244 | |
245 if not numVersionChangeLog == numVersionConfig: | |
246 message = "*** Error: wrong config file version. Expected version num is " + numVersionChangeLog + " but actual in config file is " + numVersionConfig | |
247 raise CheckerException(message) | |
248 | |
249 checkConfigVersion = staticmethod( checkConfigVersion ) | |
250 | |
251 | |
252 ## Get version number from CHANGELOG | |
253 # | |
254 # @param changeLogFile CHANGELOG file name | |
255 # | |
256 def getVersionFromChangelogFile(changeLogFileName): | |
257 with open(changeLogFileName) as changeLogFileHandle: | |
258 line = changeLogFileHandle.readline() | |
259 while not line.startswith("REPET release "): | |
260 line = changeLogFileHandle.readline() | |
261 numVersionChangeLog = line.split()[2] | |
262 return numVersionChangeLog | |
263 | |
264 | |
265 getVersionFromChangelogFile = staticmethod( getVersionFromChangelogFile ) | |
266 | |
267 | |
268 ## Check if headers of an input file contain only alpha numeric characters and "_ : . -" | |
269 # | |
270 # @param fileHandler file handle | |
271 # @exception CheckerException if bad header raise a CheckerException | |
272 # | |
273 def checkHeaders( fileHandler ): | |
274 lHeaders = CheckerUtils._getHeaderFromFastaFile(fileHandler) | |
275 p = re.compile('[^a-zA-Z0-9_:\.\-]', re.IGNORECASE) | |
276 lWrongHeaders = [] | |
277 for header in lHeaders: | |
278 errList=p.findall(header) | |
279 if len( errList ) > 0 : | |
280 lWrongHeaders.append(header) | |
281 if lWrongHeaders != []: | |
282 exception = CheckerException() | |
283 exception.setMessages(lWrongHeaders) | |
284 raise exception | |
285 | |
286 checkHeaders = staticmethod( checkHeaders ) | |
287 | |
288 | |
289 def _getHeaderFromFastaFile( inFile ): | |
290 lHeaders = [] | |
291 while True: | |
292 line = inFile.readline() | |
293 if line == "": | |
294 break | |
295 if line[0] == ">": | |
296 lHeaders.append( line[1:-1] ) | |
297 return lHeaders | |
298 | |
299 _getHeaderFromFastaFile = staticmethod( _getHeaderFromFastaFile ) | |
300 | |
301 | |
302 ## Return True if an option is in a specified section in the configuration file, False otherwise | |
303 # | |
304 # @param config handler of configuration file | |
305 # @param sectionName string of section name | |
306 # @param optionName string of option name to check | |
307 # | |
308 def isOptionInSectionInConfig( configHandler, section, option ): | |
309 try: | |
310 CheckerUtils.checkOptionInSectionInConfigFile( configHandler, section, option ) | |
311 except NoOptionError: | |
312 return False | |
313 return True | |
314 | |
315 isOptionInSectionInConfig = staticmethod( isOptionInSectionInConfig ) |