Mercurial > repos > jjohnson > defuse
comparison datamanager_create_reference.py @ 11:b22f8634ff84 draft
planemo upload for repository https://github.com/jj-umn/galaxytools/tree/master/defuse commit 23b94b5747c6956360cd2eca0a07a669929ea141-dirty
author | jjohnson |
---|---|
date | Sun, 17 Jan 2016 14:11:06 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
10:f65857c1b92e | 11:b22f8634ff84 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import sys | |
4 import os | |
5 import re | |
6 import tempfile | |
7 import subprocess | |
8 import fileinput | |
9 import shutil | |
10 import optparse | |
11 import urllib2 | |
12 from ftplib import FTP | |
13 import tarfile | |
14 | |
15 from galaxy.util.json import from_json_string, to_json_string | |
16 | |
17 | |
18 def stop_err(msg): | |
19 sys.stderr.write(msg) | |
20 sys.exit(1) | |
21 | |
22 def get_config_dict(config,dataset_directory=None): | |
23 keys = ['dataset_directory','ensembl_organism','ensembl_prefix','ensembl_version','ensembl_genome_version','ucsc_genome_version','ncbi_organism','ncbi_prefix','chromosomes','mt_chromosome','gene_sources','ig_gene_sources','rrna_gene_sources'] | |
24 pat = '^([^=]+?)\s*=\s*(.*)$' | |
25 config_dict = {} | |
26 try: | |
27 fh = open(config) | |
28 for i,l in enumerate(fh): | |
29 line = l.strip() | |
30 if line.startswith('#'): | |
31 continue | |
32 m = re.match(pat,line) | |
33 if m and len(m.groups()) == 2: | |
34 (k,v) = m.groups() | |
35 if k in keys: | |
36 config_dict[k] = v | |
37 except Exception, e: | |
38 stop_err( 'Error parsing %s %s\n' % (config,str( e )) ) | |
39 else: | |
40 fh.close() | |
41 if dataset_directory: | |
42 config_dict['dataset_directory'] = dataset_directory | |
43 return config_dict | |
44 | |
45 def run_defuse_script(data_manager_dict, params, target_directory, dbkey, description, config, script): | |
46 if not os.path.isdir(target_directory): | |
47 os.makedirs(target_directory) | |
48 ## Name the config consistently with data_manager_conf.xml | |
49 # copy the config file to the target_directory | |
50 # when DataManager moves files to there tool-data location, the config will get moved as well, | |
51 # and the value_translation in data_manager_conf.xml will tell us the new location | |
52 # defuse.xml will use the path to this config file to set the dataset_directory | |
53 config_name = '%s.config' % dbkey | |
54 defuse_config = os.path.join( target_directory, config_name) | |
55 shutil.copyfile(config,defuse_config) | |
56 cmd = "/bin/bash %s %s" % (script,target_directory) | |
57 # Run | |
58 try: | |
59 tmp_out = tempfile.NamedTemporaryFile().name | |
60 tmp_stdout = open( tmp_out, 'wb' ) | |
61 tmp_err = tempfile.NamedTemporaryFile().name | |
62 tmp_stderr = open( tmp_err, 'wb' ) | |
63 proc = subprocess.Popen( args=cmd, shell=True, cwd=".", stdout=tmp_stdout, stderr=tmp_stderr ) | |
64 returncode = proc.wait() | |
65 tmp_stderr.close() | |
66 # get stderr, allowing for case where it's very large | |
67 tmp_stderr = open( tmp_err, 'rb' ) | |
68 stderr = '' | |
69 buffsize = 1048576 | |
70 try: | |
71 while True: | |
72 stderr += tmp_stderr.read( buffsize ) | |
73 if not stderr or len( stderr ) % buffsize != 0: | |
74 break | |
75 except OverflowError: | |
76 pass | |
77 tmp_stdout.close() | |
78 tmp_stderr.close() | |
79 if returncode != 0: | |
80 raise Exception, stderr | |
81 | |
82 # TODO: look for errors in program output. | |
83 except Exception, e: | |
84 stop_err( 'Error creating defuse reference:\n' + str( e ) ) | |
85 config_dict = get_config_dict(config, dataset_directory=target_directory) | |
86 data_table_entry = dict(value=dbkey, dbkey=dbkey, name=description, path=config_name) | |
87 _add_data_table_entry( data_manager_dict, data_table_entry ) | |
88 def _add_data_table_entry( data_manager_dict, data_table_entry ): | |
89 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} ) | |
90 data_manager_dict['data_tables']['defuse_reference'] = data_manager_dict['data_tables'].get( 'defuse_reference', [] ) | |
91 data_manager_dict['data_tables']['defuse_reference'].append( data_table_entry ) | |
92 return data_manager_dict | |
93 | |
94 def main(): | |
95 #Parse Command Line | |
96 parser = optparse.OptionParser() | |
97 parser.add_option( '-k', '--dbkey', dest='dbkey', action='store', type="string", default=None, help='dbkey' ) | |
98 parser.add_option( '-d', '--description', dest='description', action='store', type="string", default=None, help='description' ) | |
99 parser.add_option( '-c', '--defuse_config', dest='defuse_config', action='store', type="string", default=None, help='defuse_config' ) | |
100 parser.add_option( '-s', '--defuse_script', dest='defuse_script', action='store', type="string", default=None, help='defuse_script' ) | |
101 (options, args) = parser.parse_args() | |
102 | |
103 filename = args[0] | |
104 | |
105 params = from_json_string( open( filename ).read() ) | |
106 target_directory = params[ 'output_data' ][0]['extra_files_path'] | |
107 os.mkdir( target_directory ) | |
108 data_manager_dict = {} | |
109 | |
110 | |
111 #Create Defuse Reference Data | |
112 run_defuse_script( data_manager_dict, params, target_directory, options.dbkey, options.description,options.defuse_config,options.defuse_script) | |
113 | |
114 #save info to json file | |
115 open( filename, 'wb' ).write( to_json_string( data_manager_dict ) ) | |
116 | |
117 if __name__ == "__main__": main() | |
118 |