annotate data_manager/data_manager_snpEff_download.py @ 3:c6769a700e55

Update loc data to separate version 4 databases
author Jim Johnson <jj@umn.edu>
date Tue, 21 Oct 2014 14:04:33 -0500
parents bc50aa3b328a
children 78bcf4ac437c
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
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
22 def fetch_databases(jar_path,genome_list=None):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
23 snpDBs = dict()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
24 (snpEff_dir,snpEff_jar) = os.path.split(jar_path)
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
25 databases_path = '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 fh = open(databases_path,'r')
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
40 for i,line in enumerate(fh):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
41 fields = line.split('\t')
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
42 if len(fields) >= 2:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
43 genome_version = fields[0].strip()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
44 if genome_list and genome_version not in genome_list:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
45 continue
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
46 if genome_version.startswith("Genome") or genome_version.startswith("-"):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
47 continue
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
48 description = fields[1].strip()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
49 snpDBs[genome_version] = description;
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
50 except Exception, e:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
51 stop_err( 'Error parsing %s %s\n' % (config,str( e )) )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
52 else:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
53 fh.close()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
54 return snpDBs
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
55
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
56 def getOrganismNames(jar_path,genomes,organisms) :
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
57 genome_list = genomes.split(',')
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
58 organism_list = organisms.split(',') if organisms else []
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
59 if len(genome_list) != len(organism_list):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
60 descriptions = []
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
61 snpDBdict = fetch_databases(jar_path,genome_list=genome_list);
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
62 for genome in snpDBdict:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
63 descriptions.append(snpDBdict[genome] if genome in snpDBdict else genome)
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
64 return ','.join(descriptions)
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
65 return organisms
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
66
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
67 """
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
68 # Download human database 'hg19'
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
69 java -jar snpEff.jar download -v hg19
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
70
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
71 <command>java -jar \$SNPEFF_JAR_PATH/snpEff.jar download -c \$JAVA_JAR_PATH/snpEff.config $genomeVersion > $logfile </command>
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
72
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
73 snpEffectPredictor.bin
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
74 regulation_HeLa-S3.bin
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
75 regulation_pattern = 'regulation_(.+).bin'
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
76 """
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
77 def download_database(data_manager_dict, target_directory, jar_path,config,genome_version,organism):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
78 ## get data_dir from config
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
79 ##---
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
80 ## Databases are stored here
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
81 ## E.g.: Information for 'hg19' is stored in data_dir/hg19/
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
82 ##
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
83 ## Note: Since version 2.1 you can use tilde ('~') as first character to refer to your home directory
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
84 ##---
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
85 #data_dir = ~/snpEff/data/
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
86 data_dir = target_directory
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
87 (snpEff_dir,snpEff_jar) = os.path.split(jar_path)
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
88 args = [ 'java','-jar' ]
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
89 args.append( jar_path )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
90 args.append( 'download' )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
91 args.append( '-c' )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
92 args.append( config )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
93 args.append( '-dataDir' )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
94 args.append( data_dir )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
95 args.append( '-v' )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
96 args.append( genome_version )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
97 proc = subprocess.Popen( args=args, shell=False, cwd=snpEff_dir )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
98 return_code = proc.wait()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
99 if return_code:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
100 sys.exit( return_code )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
101 ## search data_dir/genome_version for files
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
102 regulation_pattern = 'regulation_(.+).bin'
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
103 # annotation files that are included in snpEff by a flag
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
104 annotations_dict = {'nextProt.bin' : '-nextprot','motif.bin': '-motif'}
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
105 genome_path = os.path.join(data_dir,genome_version)
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
106 if os.path.isdir(genome_path):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
107 for root, dirs, files in os.walk(genome_path):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
108 for fname in files:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
109 if fname.startswith('snpEffectPredictor'):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
110 # if snpEffectPredictor.bin download succeeded
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
111 name = genome_version + (' : ' + organism if organism else '')
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
112 data_table_entry = dict(value=genome_version, name=name, path=data_dir)
3
c6769a700e55 Update loc data to separate version 4 databases
Jim Johnson <jj@umn.edu>
parents: 0
diff changeset
113 _add_data_table_entry( data_manager_dict, 'snpeff4_genomedb', data_table_entry )
0
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
114 else:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
115 m = re.match(regulation_pattern,fname)
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
116 if m:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
117 name = m.groups()[0]
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
118 data_table_entry = dict(genome=genome_version,value=name, name=name)
3
c6769a700e55 Update loc data to separate version 4 databases
Jim Johnson <jj@umn.edu>
parents: 0
diff changeset
119 _add_data_table_entry( data_manager_dict, 'snpeff4_regulationdb', data_table_entry )
0
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
120 elif fname in annotations_dict:
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
121 value = annotations_dict[fname]
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
122 name = value.lstrip('-')
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
123 data_table_entry = dict(genome=genome_version,value=value, name=name)
3
c6769a700e55 Update loc data to separate version 4 databases
Jim Johnson <jj@umn.edu>
parents: 0
diff changeset
124 _add_data_table_entry( data_manager_dict, 'snpeff4_annotations', data_table_entry )
0
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
125 return data_manager_dict
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
126
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
127 def _add_data_table_entry( data_manager_dict, data_table, data_table_entry ):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
128 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
129 data_manager_dict['data_tables'][data_table] = data_manager_dict['data_tables'].get( data_table, [] )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
130 data_manager_dict['data_tables'][data_table].append( data_table_entry )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
131 return data_manager_dict
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
132
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
133 def main():
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
134 #Parse Command Line
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
135 parser = optparse.OptionParser()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
136 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
137 parser.add_option( '-c', '--config', dest='config', action='store', type="string", default=None, help='snpEff.config path' )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
138 parser.add_option( '-g', '--genome_version', dest='genome_version', action='store', type="string", default=None, help='genome_version' )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
139 parser.add_option( '-o', '--organism', dest='organism', action='store', type="string", default=None, help='organism name' )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
140 (options, args) = parser.parse_args()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
141
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
142 filename = args[0]
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
143
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
144 params = from_json_string( open( filename ).read() )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
145 target_directory = params[ 'output_data' ][0]['extra_files_path']
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
146 os.mkdir( target_directory )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
147 data_manager_dict = {}
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
148
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
149
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
150 #Create SnpEff Reference Data
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
151 for genome_version, organism in zip(options.genome_version.split(','), getOrganismNames(options.jar_path,options.genome_version,options.organism).split(',')):
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
152 download_database( data_manager_dict, target_directory, options.jar_path, options.config, genome_version, organism )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
153
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
154 #save info to json file
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
155 open( filename, 'wb' ).write( to_json_string( data_manager_dict ) )
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
156
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
157 if __name__ == "__main__": main()
bc50aa3b328a Uploaded
jjohnson
parents:
diff changeset
158