Mercurial > repos > yufei-luo > s_mart
comparison commons/tools/srptGameXmlMaker.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 import user, os, sys, getopt, ConfigParser | |
4 from os import listdir | |
5 | |
6 def setup_env(): | |
7 if "REPET_PATH" in os.environ.keys(): | |
8 sys.path.append( os.environ["REPET_PATH"] ) | |
9 else: | |
10 print "*** Error: no environment variable REPET_PATH ***" | |
11 sys.exit(1) | |
12 setup_env() | |
13 | |
14 from pyRepet.sql.RepetDBMySQL import * | |
15 from pyRepet.gamexml.Xml_writer import * | |
16 from pyRepet.gamexml.computational import * | |
17 | |
18 #------------------------------------------------------------------------------ | |
19 | |
20 def help(): | |
21 | |
22 print "" | |
23 print "usage:",sys.argv[0],"[options]" | |
24 print "options:" | |
25 print " -h: this help" | |
26 print " -f: fasta file (required to generate new '.gamexml' files)" | |
27 print " -n: annotation tier_name" | |
28 print " -g: gamexml file (for Apollo). If it's not mentionned, all '.gamexml' files will be updated with the result file" | |
29 print " -l: light gameXML file (without sequence)" | |
30 print " -r: result files (require -n)" | |
31 print " -R: reverse the query and subject of Blaster results" | |
32 print " -s: tier_name of an annotation to remove from a gameXML file" | |
33 print " -t: file of table name to use to create the gamexml files (tier name 'tab' format 'tab' table name)" | |
34 print " -c: configuration file from TEdenovo or TEannot pipeline" | |
35 print " -H: MySQL host (if no configuration file)" | |
36 print " -U: MySQL user (if no configuration file)" | |
37 print " -P: MySQL password (if no configuration file)" | |
38 print " -D: MySQL database (if no configuration file)" | |
39 print " -v: verbose (default=0/1/2)" | |
40 print "" | |
41 | |
42 #------------------------------------------------------------------------------ | |
43 | |
44 def automatisation( result_file, tier_name, reverse, comput ): | |
45 | |
46 if verbose > 1: | |
47 print "Auto update"; sys.stdout.flush() | |
48 writer = Xml_writer() | |
49 file_liste = [] | |
50 liste_comp = [] | |
51 liste_comp = listdir('./') | |
52 | |
53 if result_file != "": | |
54 for j in liste_comp: | |
55 if writer.file_in_keys( j, comput ): | |
56 file_liste = file_liste + [j] | |
57 | |
58 for i in file_liste: | |
59 writer.update_gamexml( i, result_file, tier_name, comput ) | |
60 | |
61 else: | |
62 for j in liste_comp: | |
63 if j.find( "gamexml" ) != -1: | |
64 writer.parse_gamexml( j ) | |
65 writer.verif_name_prog( tier_name ) | |
66 writer.write( j ) | |
67 if verbose > 1: | |
68 print tier_name + " program from " +j +" removed" | |
69 | |
70 #------------------------------------------------------------------------------ | |
71 | |
72 def main(): | |
73 | |
74 f_result = "" | |
75 f_gamexml = "" | |
76 f_fasta = "" | |
77 f_table = "" | |
78 tier_name = "" | |
79 substract_name = "" | |
80 no_seq = 0 | |
81 configFileName = "" | |
82 host = "" | |
83 user = "" | |
84 passwd = "" | |
85 dbname = "" | |
86 verbose = 0 | |
87 | |
88 try: | |
89 options,arguments=getopt.getopt(sys.argv[1:],"hn:f:g:r:s:lRt:c:H:U:P:D:v:",["help","tier_name=","fasta","gamexml","result","substract_program","light","reverse_result","table"]) | |
90 except getopt.GetoptError: | |
91 help() | |
92 sys.exit(1) | |
93 if options == []: | |
94 help() | |
95 sys.exit(1) | |
96 for o,a in options: | |
97 if o == "-h" or o == "--help": | |
98 help() | |
99 sys.exit(0) | |
100 elif o == "-f" or o == "--fasta": | |
101 f_fasta = a | |
102 elif o == "-g" or o == "--gamexml": | |
103 f_gamexml = a | |
104 elif o == "-n" or o == "--tier_name": | |
105 tier_name = a | |
106 elif o == "-r" or o == "--result": | |
107 f_result = a | |
108 elif o == "-s" or o == "--subtract_program": | |
109 substract_name = a | |
110 elif o == "-l" or o == "--light": | |
111 no_seq = 1 | |
112 elif o == "-R" or o == "--reverse_result": | |
113 writer.set_reverse() | |
114 elif o == "-t" or o == "--table": | |
115 f_table = a | |
116 elif o == "-c": | |
117 configFileName = a | |
118 elif o == "-H": | |
119 host = a | |
120 elif o == "-U": | |
121 user = a | |
122 elif o == "-P": | |
123 passwd = a | |
124 elif o == "-D": | |
125 dbname = a | |
126 elif o == "-v": | |
127 verbose = int(a) | |
128 | |
129 if tier_name == "" and substract_name == "" and f_result != "": | |
130 print "*** Error: option -n required" | |
131 help() | |
132 sys.exit(1) | |
133 | |
134 if f_fasta == "" and f_gamexml == "": | |
135 print "*** Error: options -g or -f required" | |
136 help() | |
137 sys.exit(1) | |
138 | |
139 if substract_name!="" and f_result!="" : | |
140 print "Error: option -s and -r together" | |
141 help() | |
142 sys.exit(1) | |
143 | |
144 | |
145 if verbose > 0: | |
146 print "\nbeginning of %s" % (sys.argv[0].split("/")[-1]) | |
147 sys.stdout.flush() | |
148 | |
149 if configFileName != "": | |
150 config = ConfigParser.ConfigParser() | |
151 config.readfp( open(configFileName) ) | |
152 host = config.get("repet_env","repet_host") | |
153 user = config.get("repet_env","repet_user") | |
154 passwd = config.get("repet_env","repet_pw") | |
155 dbname = config.get("repet_env","repet_db") | |
156 | |
157 if host == "" and os.environ.get( "REPET_HOST" ) != None: | |
158 host = os.environ.get( "REPET_HOST" ) | |
159 if user == "" and os.environ.get( "REPET_USER" ) != None: | |
160 user = os.environ.get( "REPET_USER" ) | |
161 if passwd == "" and os.environ.get( "REPET_PW" ) != None: | |
162 passwd = os.environ.get( "REPET_PW" ) | |
163 if dbname == "" and os.environ.get( "REPET_DB" ) != None: | |
164 dbname = os.environ.get( "REPET_DB" ) | |
165 | |
166 writer = Xml_writer() | |
167 | |
168 # create the dico | |
169 comput = computational() | |
170 | |
171 # create all the ".gamexml" files (option '-f') | |
172 if f_fasta != "": | |
173 writer.create_gamexml( f_fasta, f_result, tier_name, comput, no_seq ) | |
174 | |
175 # | |
176 if f_result != "": | |
177 if f_gamexml != "": | |
178 key = ".".join( f_gamexml.split(".")[:-1] ) | |
179 else: | |
180 key = "" | |
181 format = writer.find_type_file( f_result ) | |
182 resFile = open( f_result ) | |
183 if format == "path": | |
184 comput.load_dico_path_from_file( key, f_result ) | |
185 | |
186 if f_table != "": | |
187 if verbose > 1: | |
188 print "parsing file %s... " % ( f_gamexml ); sys.stdout.flush() | |
189 writer.parse_gamexml( f_gamexml ) | |
190 | |
191 if f_gamexml != "": | |
192 ## key=".".join(f_gamexml.split(".")[:-1]) | |
193 key = f_gamexml.split(".")[0] | |
194 else: | |
195 key = "" | |
196 | |
197 tfile = open( f_table ) | |
198 lines = tfile.readlines() | |
199 for l in lines: | |
200 if l[0] == "#": | |
201 continue | |
202 tok = l.split() | |
203 #print tok | |
204 if len(tok) == 0: | |
205 break | |
206 tier_name = tok[0] | |
207 format = tok[1] | |
208 table = tok[2] | |
209 alias = "" | |
210 if verbose > 1: | |
211 print "table: " + table + " (format=" + format + ")" | |
212 if len(tok) > 3: | |
213 alias = tok[3] | |
214 if verbose > 1: | |
215 print " alias=" + alias | |
216 | |
217 if host == "" or user == "" or passwd == "" or dbname == "": | |
218 print "*** Error: missing information about MySQL connection" | |
219 sys.exit(1) | |
220 db = RepetDB( user, host, passwd, dbname ) | |
221 | |
222 if format == "path": | |
223 comput.load_dico_path_from_table( db, key, table, alias ) | |
224 writer.update_gamexml_comput( tier_name, comput ) | |
225 elif format == "rpath": | |
226 comput.load_dico_rpath_from_table( db, key, table, alias ) | |
227 elif format == "ipath": | |
228 comput.load_dico_ipath_from_table( db, key, table, alias ) | |
229 writer.update_gamexml_comput( tier_name, comput ) | |
230 elif format == "align": | |
231 comput.load_dico_align_from_table( db, key, table, alias ) | |
232 writer.update_gamexml_comput( tier_name, comput ) | |
233 elif format == "map": | |
234 comput.load_dico_map_from_table( db, key, table, alias ) | |
235 writer.update_gamexml_comput( tier_name, comput ) | |
236 elif format == "rmap": | |
237 comput.load_dico_rmap_from_table( db, key, table, alias ) | |
238 writer.update_gamexml_comput( tier_name, comput ) | |
239 elif format == "set": | |
240 comput.load_dico_set_from_table( db, key, table, alias ) | |
241 writer.update_gamexml_comput( tier_name, comput ) | |
242 elif format == "annot": | |
243 comput.load_dico_annot_from_table( db, key, table, alias ) | |
244 writer.update_gamexml_annot( table, comput ) | |
245 elif format == "annot_set": | |
246 comput.load_dico_annotset_from_table( db, key, table, alias ) | |
247 writer.update_gamexml_annot( table, comput ) | |
248 else: | |
249 print "*** Error: unknown format '%s'" % ( format ) | |
250 sys.exit(1) | |
251 writer.write(f_gamexml) | |
252 | |
253 db.close() | |
254 | |
255 # | |
256 if f_gamexml == "" and f_result != "" and f_fasta == "": | |
257 automatisation( f_result, tier_name, writer.get_reverse(), comput ) | |
258 | |
259 # update a ".gamexml" file (options '-g' and '-t') | |
260 if f_gamexml != "" and f_result != "": | |
261 writer.update_gamexml( f_gamexml, f_result, tier_name, comput ) | |
262 | |
263 # remove a comput | |
264 if substract_name != "" and tier_name == "": | |
265 if f_gamexml != "": | |
266 writer.parse_gamexml( f_gamexml ) | |
267 writer.verif_name_prog( substract_name ) | |
268 writer.write( f_gamexml ) | |
269 if verbose > 1: | |
270 print substract_name + " program from " + f_gamexml +" removed" | |
271 else: | |
272 automatisation( "", substract_name, 0, None ) | |
273 | |
274 if verbose > 0: | |
275 print "%s finished successfully\n" % (sys.argv[0].split("/")[-1]) | |
276 sys.stdout.flush() | |
277 | |
278 return 0 | |
279 | |
280 #------------------------------------------------------------------------------ | |
281 | |
282 if __name__ == '__main__': | |
283 main() |