comparison data_manager/data_manager_snpEff_databases.py @ 0:bc50aa3b328a

Uploaded
author jjohnson
date Thu, 26 Jun 2014 09:13:02 -0400
parents
children c6769a700e55
comparison
equal deleted inserted replaced
-1:000000000000 0:bc50aa3b328a
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 def stop_err(msg):
18 sys.stderr.write(msg)
19 sys.exit(1)
20
21 def fetch_databases(data_manager_dict, target_directory, jar_path):
22 (snpEff_dir,snpEff_jar) = os.path.split(jar_path)
23 if not os.path.exists(target_directory):
24 os.makedirs(target_directory)
25 databases_path = os.path.join( target_directory, 'databases.out' )
26 databases_output = open(databases_path,'w')
27 args = [ 'java','-jar', ]
28 args.append( snpEff_jar )
29 args.append( 'databases' )
30 # tmp_stderr = tempfile.NamedTemporaryFile( prefix = "tmp-data-manager-snpEff-stderr" )
31 # databases_output = open(databases_path)
32 # proc = subprocess.Popen( args=args, shell=False, cwd=snpEff_dir, stdout=databases_output.fileno(), stderr=tmp_stderr.fileno() )
33 proc = subprocess.Popen( args=args, shell=False, cwd=snpEff_dir, stdout=databases_output.fileno() )
34 return_code = proc.wait()
35 if return_code:
36 sys.exit( return_code )
37 databases_output.close()
38 try:
39 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
40 data_manager_dict['data_tables']['snpeff_databases'] = data_manager_dict['data_tables'].get( 'snpeff_databases', [] )
41 data_table_entries = []
42 fh = open(databases_path,'r')
43 for i,line in enumerate(fh):
44 fields = line.split('\t')
45 if len(fields) >= 2:
46 genome_version = fields[0].strip()
47 if genome_version.startswith("Genome") or genome_version.startswith("-"):
48 continue
49 #snpeff test genome
50 if genome_version == '30c2c903' or fields[1].strip() == 'TestCase' or fields[1].strip().startswith('Test_'):
51 continue
52 description = fields[1].strip() + ' : ' + genome_version
53 data_table_entries.append(dict(value=genome_version, name=description))
54 data_manager_dict['data_tables']['snpeff_databases'] = data_table_entries
55 except Exception, e:
56 stop_err( 'Error parsing %s %s\n' % (config,str( e )) )
57 else:
58 fh.close()
59 return data_manager_dict
60
61 def main():
62 #Parse Command Line
63 parser = optparse.OptionParser()
64 parser.add_option( '-j', '--jar_path', dest='jar_path', action='store', type="string", default=None, help='snpEff.jar path' )
65 (options, args) = parser.parse_args()
66
67 filename = args[0]
68
69 params = from_json_string( open( filename ).read() )
70 target_directory = params[ 'output_data' ][0]['extra_files_path']
71 os.mkdir( target_directory )
72 data_manager_dict = {}
73
74
75 #Create Defuse Reference Data
76 data_manager_dict = fetch_databases( data_manager_dict, target_directory, options.jar_path)
77
78 #save info to json file
79 open( filename, 'wb' ).write( to_json_string( data_manager_dict ) )
80
81 if __name__ == "__main__": main()
82