Mercurial > repos > iuc > data_manager_snpeff
annotate data_manager/data_manager_snpEff_download.py @ 5:3d9dd4b9fb09 draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
author | iuc |
---|---|
date | Tue, 18 Apr 2017 09:40:03 -0400 |
parents | 847b0f43c0e5 |
children | a6400027d849 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
2 import json |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
3 import optparse |
0 | 4 import os |
5 import re | |
6 import subprocess | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
7 import sys |
0 | 8 |
9 | |
10 def stop_err(msg): | |
11 sys.stderr.write(msg) | |
12 sys.exit(1) | |
13 | |
14 | |
5
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
15 def fetch_databases(genome_list=None): |
0 | 16 snpDBs = dict() |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
17 databases_path = 'databases.out' |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
18 databases_output = open(databases_path, 'w') |
5
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
19 args = ['snpEff', 'databases'] |
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
20 return_code = subprocess.call(args=args, shell=False, stdout=databases_output.fileno()) |
0 | 21 if return_code: |
22 sys.exit( return_code ) | |
23 databases_output.close() | |
24 try: | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
25 fh = open(databases_path, 'r') |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
26 for i, line in enumerate(fh): |
0 | 27 fields = line.split('\t') |
28 if len(fields) >= 2: | |
29 genome_version = fields[0].strip() | |
30 if genome_list and genome_version not in genome_list: | |
31 continue | |
32 if genome_version.startswith("Genome") or genome_version.startswith("-"): | |
33 continue | |
34 description = fields[1].strip() | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
35 snpDBs[genome_version] = description |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
36 except Exception as e: |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
37 stop_err( 'Error parsing %s %s\n' % (databases_path, str( e )) ) |
0 | 38 else: |
39 fh.close() | |
40 return snpDBs | |
41 | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
42 |
5
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
43 def getOrganismNames(genomes, organisms): |
0 | 44 genome_list = genomes.split(',') |
45 organism_list = organisms.split(',') if organisms else [] | |
46 if len(genome_list) != len(organism_list): | |
47 descriptions = [] | |
5
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
48 snpDBdict = fetch_databases(genome_list=genome_list) |
0 | 49 for genome in snpDBdict: |
50 descriptions.append(snpDBdict[genome] if genome in snpDBdict else genome) | |
51 return ','.join(descriptions) | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
52 return organisms |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
53 |
0 | 54 |
5
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
55 def getSnpeffVersion(): |
0 | 56 snpeff_version = 'SnpEff ?.?' |
57 stderr_path = 'snpeff.err' | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
58 stderr_fh = open(stderr_path, 'w') |
5
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
59 args = ['snpEff', '-h'] |
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
60 return_code = subprocess.call(args=args, shell=False, stderr=stderr_fh.fileno()) |
0 | 61 if return_code != 255: |
62 sys.exit( return_code ) | |
63 stderr_fh.close() | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
64 fh = open(stderr_path, 'r') |
0 | 65 for line in fh: |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
66 m = re.match('^[Ss]npEff version (SnpEff)\s*(\d+\.\d+).*$', line) |
0 | 67 if m: |
68 snpeff_version = m.groups()[0] + m.groups()[1] | |
69 break | |
70 fh.close() | |
71 return snpeff_version | |
72 | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
73 |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
74 # Download human database 'hg19' |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
75 # java -jar snpEff.jar download -v hg19 |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
76 # |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
77 # <command>java -jar \$SNPEFF_JAR_PATH/snpEff.jar download -c \$JAVA_JAR_PATH/snpEff.config $genomeVersion > $logfile </command> |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
78 # |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
79 # snpEffectPredictor.bin |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
80 # regulation_HeLa-S3.bin |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
81 # regulation_pattern = 'regulation_(.+).bin' |
5
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
82 def download_database(data_manager_dict, target_directory, genome_version, organism): |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
83 # get data_dir from config |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
84 # --- |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
85 # Databases are stored here |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
86 # E.g.: Information for 'hg19' is stored in data_dir/hg19/ |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
87 # |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
88 # Note: Since version 2.1 you can use tilde ('~') as first character to refer to your home directory |
0 | 89 data_dir = target_directory |
5
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
90 args = ['snpEff', 'download', '-dataDir', data_dir, '-v', genome_version] |
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
91 return_code = subprocess.call(args=args, shell=False) |
0 | 92 if return_code: |
93 sys.exit( return_code ) | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
94 # search data_dir/genome_version for files |
0 | 95 regulation_pattern = 'regulation_(.+).bin' |
5
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
96 # annotation files that are included in SnpEff by a flag |
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
97 annotations_dict = {'nextProt.bin': '-nextprot', 'motif.bin': '-motif', 'interactions.bin': '-interaction'} |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
98 genome_path = os.path.join(data_dir, genome_version) |
5
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
99 snpeff_version = getSnpeffVersion() |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
100 key = snpeff_version + '_' + genome_version |
0 | 101 if os.path.isdir(genome_path): |
102 for root, dirs, files in os.walk(genome_path): | |
103 for fname in files: | |
104 if fname.startswith('snpEffectPredictor'): | |
105 # if snpEffectPredictor.bin download succeeded | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
106 name = genome_version + (' : ' + organism if organism else '') |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
107 data_table_entry = dict(key=key, version=snpeff_version, value=genome_version, name=name, path=data_dir) |
0 | 108 _add_data_table_entry( data_manager_dict, 'snpeffv_genomedb', data_table_entry ) |
109 else: | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
110 m = re.match(regulation_pattern, fname) |
0 | 111 if m: |
112 name = m.groups()[0] | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
113 data_table_entry = dict(key=key, version=snpeff_version, genome=genome_version, value=name, name=name) |
0 | 114 _add_data_table_entry( data_manager_dict, 'snpeffv_regulationdb', data_table_entry ) |
115 elif fname in annotations_dict: | |
116 value = annotations_dict[fname] | |
117 name = value.lstrip('-') | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
118 data_table_entry = dict(key=key, version=snpeff_version, genome=genome_version, value=value, name=name) |
0 | 119 _add_data_table_entry( data_manager_dict, 'snpeffv_annotations', data_table_entry ) |
120 return data_manager_dict | |
121 | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
122 |
0 | 123 def _add_data_table_entry( data_manager_dict, data_table, data_table_entry ): |
124 data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} ) | |
125 data_manager_dict['data_tables'][data_table] = data_manager_dict['data_tables'].get( data_table, [] ) | |
126 data_manager_dict['data_tables'][data_table].append( data_table_entry ) | |
127 return data_manager_dict | |
128 | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
129 |
0 | 130 def main(): |
131 parser = optparse.OptionParser() | |
132 parser.add_option( '-g', '--genome_version', dest='genome_version', action='store', type="string", default=None, help='genome_version' ) | |
133 parser.add_option( '-o', '--organism', dest='organism', action='store', type="string", default=None, help='organism name' ) | |
134 (options, args) = parser.parse_args() | |
135 | |
136 filename = args[0] | |
137 | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
138 params = json.loads( open( filename ).read() ) |
0 | 139 target_directory = params[ 'output_data' ][0]['extra_files_path'] |
140 os.mkdir( target_directory ) | |
141 data_manager_dict = {} | |
142 | |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
143 # Create SnpEff Reference Data |
5
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
144 for genome_version, organism in zip(options.genome_version.split(','), getOrganismNames(options.genome_version, options.organism).split(',')): |
3d9dd4b9fb09
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 5c6d595ecbf2e4e39c25662a165c9e475e01ecd1-dirty
iuc
parents:
4
diff
changeset
|
145 download_database( data_manager_dict, target_directory, genome_version, organism ) |
0 | 146 |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
147 # save info to json file |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
148 open( filename, 'wb' ).write( json.dumps( data_manager_dict ) ) |
0 | 149 |
4
847b0f43c0e5
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_snpeff commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
1
diff
changeset
|
150 |
1
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
151 if __name__ == "__main__": |
85a23e2dd92b
planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/data_managers/data_manager_snpeff commit 88c982c5adcd32b11d98428fc554a4fdfcc19584
iuc
parents:
0
diff
changeset
|
152 main() |