Mercurial > repos > jjohnson > data_manager_snpeff
annotate data_manager/data_manager_snpEff_databases.py @ 5:78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
author | Jim Johnson <jj@umn.edu> |
---|---|
date | Tue, 13 Jan 2015 12:54:20 -0600 |
parents | c6769a700e55 |
children | a329eda0cdff |
rev | line source |
---|---|
0 | 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 | |
5
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
21 def getSnpeffVersion(jar_path): |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
22 snpeff_version = 'SnpEff ?.?' |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
23 (snpEff_dir,snpEff_jar) = os.path.split(jar_path) |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
24 stderr_path = 'snpeff.err' |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
25 stderr_fh = open(stderr_path,'w') |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
26 args = [ 'java','-jar', ] |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
27 args.append( snpEff_jar ) |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
28 args.append( '-h' ) |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
29 proc = subprocess.Popen( args=args, shell=False, cwd=snpEff_dir, stderr=stderr_fh.fileno() ) |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
30 return_code = proc.wait() |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
31 if return_code != 255: |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
32 sys.exit( return_code ) |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
33 stderr_fh.close() |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
34 fh = open(stderr_path,'r') |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
35 for line in fh: |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
36 m = re.match('^[Ss]npEff version (SnpEff)\s*(\d+\.\d+).*$',line) |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
37 if m: |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
38 snpeff_version = m.groups()[0] + m.groups()[1] |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
39 break |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
40 fh.close() |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
41 return snpeff_version |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
42 |
0 | 43 def fetch_databases(data_manager_dict, target_directory, jar_path): |
44 (snpEff_dir,snpEff_jar) = os.path.split(jar_path) | |
45 if not os.path.exists(target_directory): | |
46 os.makedirs(target_directory) | |
47 databases_path = os.path.join( target_directory, 'databases.out' ) | |
48 databases_output = open(databases_path,'w') | |
49 args = [ 'java','-jar', ] | |
50 args.append( snpEff_jar ) | |
51 args.append( 'databases' ) | |
52 # tmp_stderr = tempfile.NamedTemporaryFile( prefix = "tmp-data-manager-snpEff-stderr" ) | |
53 # databases_output = open(databases_path) | |
54 # proc = subprocess.Popen( args=args, shell=False, cwd=snpEff_dir, stdout=databases_output.fileno(), stderr=tmp_stderr.fileno() ) | |
55 proc = subprocess.Popen( args=args, shell=False, cwd=snpEff_dir, stdout=databases_output.fileno() ) | |
56 return_code = proc.wait() | |
57 if return_code: | |
58 sys.exit( return_code ) | |
59 databases_output.close() | |
5
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
60 snpeff_version = getSnpeffVersion(jar_path) |
0 | 61 try: |
62 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} ) | |
5
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
63 data_manager_dict['data_tables']['snpeffv_databases'] = data_manager_dict['data_tables'].get( 'snpeffv_databases', [] ) |
0 | 64 data_table_entries = [] |
65 fh = open(databases_path,'r') | |
66 for i,line in enumerate(fh): | |
67 fields = line.split('\t') | |
68 if len(fields) >= 2: | |
69 genome_version = fields[0].strip() | |
70 if genome_version.startswith("Genome") or genome_version.startswith("-"): | |
71 continue | |
72 #snpeff test genome | |
73 if genome_version == '30c2c903' or fields[1].strip() == 'TestCase' or fields[1].strip().startswith('Test_'): | |
74 continue | |
75 description = fields[1].strip() + ' : ' + genome_version | |
5
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
76 key = snpeff_version + '_' + genome_version |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
77 data_table_entries.append(dict(key=key, version=snpeff_version, value=genome_version, name=description)) |
78bcf4ac437c
Use tool_data_table with key and version columns added to allow for multiple versions in a .loc file
Jim Johnson <jj@umn.edu>
parents:
3
diff
changeset
|
78 data_manager_dict['data_tables']['snpeffv_databases'] = data_table_entries |
0 | 79 except Exception, e: |
80 stop_err( 'Error parsing %s %s\n' % (config,str( e )) ) | |
81 else: | |
82 fh.close() | |
83 return data_manager_dict | |
84 | |
85 def main(): | |
86 #Parse Command Line | |
87 parser = optparse.OptionParser() | |
88 parser.add_option( '-j', '--jar_path', dest='jar_path', action='store', type="string", default=None, help='snpEff.jar path' ) | |
89 (options, args) = parser.parse_args() | |
90 | |
91 filename = args[0] | |
92 | |
93 params = from_json_string( open( filename ).read() ) | |
94 target_directory = params[ 'output_data' ][0]['extra_files_path'] | |
95 os.mkdir( target_directory ) | |
96 data_manager_dict = {} | |
97 | |
98 | |
99 #Create Defuse Reference Data | |
100 data_manager_dict = fetch_databases( data_manager_dict, target_directory, options.jar_path) | |
101 | |
102 #save info to json file | |
103 open( filename, 'wb' ).write( to_json_string( data_manager_dict ) ) | |
104 | |
105 if __name__ == "__main__": main() | |
106 |