annotate data_manager/data_manager_snpEff_databases.py @ 6:a329eda0cdff draft default tip

Uploaded
author jjohnson
date Wed, 09 Dec 2015 13:49:55 -0500
parents 78bcf4ac437c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
1 #!/usr/bin/env python
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
2
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
3 import sys
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
4 import os
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
5 import re
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
6 import tempfile
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
7 import subprocess
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
8 import fileinput
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
9 import shutil
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
10 import optparse
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
11 import urllib2
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
12 from ftplib import FTP
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
13 import tarfile
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
14
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
15 from galaxy.util.json import from_json_string, to_json_string
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
16
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
17 def stop_err(msg):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
18 sys.stderr.write(msg)
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
19 sys.exit(1)
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
20
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
21 def fetch_databases(data_manager_dict, target_directory, jar_path):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
22 (snpEff_dir,snpEff_jar) = os.path.split(jar_path)
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
23 if not os.path.exists(target_directory):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
24 os.makedirs(target_directory)
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
25 databases_path = os.path.join( target_directory, 'databases.out' )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
26 databases_output = open(databases_path,'w')
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
27 args = [ 'java','-jar', ]
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
28 args.append( snpEff_jar )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
29 args.append( 'databases' )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
30 # tmp_stderr = tempfile.NamedTemporaryFile( prefix = "tmp-data-manager-snpEff-stderr" )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
31 # databases_output = open(databases_path)
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
32 # proc = subprocess.Popen( args=args, shell=False, cwd=snpEff_dir, stdout=databases_output.fileno(), stderr=tmp_stderr.fileno() )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
33 proc = subprocess.Popen( args=args, shell=False, cwd=snpEff_dir, stdout=databases_output.fileno() )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
34 return_code = proc.wait()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
35 if return_code:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
36 sys.exit( return_code )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
37 databases_output.close()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
38 try:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
39 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
6
a329eda0cdff Uploaded
jjohnson
parents: 5
diff changeset
40 data_manager_dict['data_tables']['snpeff4_databases'] = data_manager_dict['data_tables'].get( 'snpeff4_databases', [] )
0
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
41 data_table_entries = []
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
42 fh = open(databases_path,'r')
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
43 for i,line in enumerate(fh):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
44 fields = line.split('\t')
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
45 if len(fields) >= 2:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
46 genome_version = fields[0].strip()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
47 if genome_version.startswith("Genome") or genome_version.startswith("-"):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
48 continue
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
49 #snpeff test genome
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
50 if genome_version == '30c2c903' or fields[1].strip() == 'TestCase' or fields[1].strip().startswith('Test_'):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
51 continue
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
52 description = fields[1].strip() + ' : ' + genome_version
6
a329eda0cdff Uploaded
jjohnson
parents: 5
diff changeset
53 data_table_entries.append(dict(value=genome_version, name=description))
a329eda0cdff Uploaded
jjohnson
parents: 5
diff changeset
54 data_manager_dict['data_tables']['snpeff4_databases'] = data_table_entries
0
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
55 except Exception, e:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
56 stop_err( 'Error parsing %s %s\n' % (config,str( e )) )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
57 else:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
58 fh.close()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
59 return data_manager_dict
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
60
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
61 def main():
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
62 #Parse Command Line
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
63 parser = optparse.OptionParser()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
64 parser.add_option( '-j', '--jar_path', dest='jar_path', action='store', type="string", default=None, help='snpEff.jar path' )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
65 (options, args) = parser.parse_args()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
66
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
67 filename = args[0]
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
68
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
69 params = from_json_string( open( filename ).read() )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
70 target_directory = params[ 'output_data' ][0]['extra_files_path']
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
71 os.mkdir( target_directory )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
72 data_manager_dict = {}
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
73
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
74
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
75 #Create Defuse Reference Data
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
76 data_manager_dict = fetch_databases( data_manager_dict, target_directory, options.jar_path)
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
77
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
78 #save info to json file
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
79 open( filename, 'wb' ).write( to_json_string( data_manager_dict ) )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
80
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
81 if __name__ == "__main__": main()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
82