-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCAMMiQ-download
235 lines (222 loc) · 7.33 KB
/
CAMMiQ-download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
Download complete genomes from NCBI RefSeq
System requirements:
python, wget, gzip (all added to $PATH)
Usage:
python CAMMiQ-download <OPTIONS>
Options:
--dir <DATABASE_PATH>
The directory to maintain the genomes
--taxa bacteria|viral|archaea|all
Download all complete bacterial, viral, archaeal genomes
Default: bacteria
--sample none|taxid|species|species_rep
Keep one representative genome per taxid/species
species_rep: Keep ALL reference genomes and representative genomes per species
Default: species
--map_fn <OUTPUT_FILE_NAME>
Specify the output (genome map) file name
Default: genome_map.out
--unzip
Unzip *.gz files
--quiet
Disable wget outputs
Output:
A list of *.fna(.gz) files and a single file "genome_map.out"
(or with user specified file name) in directory DATABASE_PATH
"""
import sys
import os
db_dir = "./"
sample_genomes = "species"
genome_map_fn = "genome_map.out"
unzip_genomes = 0
div = "bacteria"
quiet = 0
fn_map = dict()
download_list = []
download_list_extra = []
taxids = []
taxid_status = []
def parse_arguments():
global db_dir, sample_genomes, genome_map_fn, unzip_genomes, div, quiet
i = 1
while i < len(sys.argv):
if sys.argv[i] == "--dir":
i += 1
db_dir = sys.argv[i]
elif sys.argv[i] == "--taxa":
i += 1
div = sys.argv[i]
elif sys.argv[i] == "--sample":
i += 1
sample_genomes = sys.argv[i]
elif sys.argv[i] == "--map_fn":
i += 1
genome_map_fn = sys.argv[i]
elif sys.argv[i] == "--unzip":
unzip_genomes = 1
elif sys.argv[i] == "--quiet":
quiet = 1
else:
sys.exit("Invalid argument.")
i += 1
def download_assembly_summary(division, q = 0):
assembly_summary_dir = "ftp://ftp.ncbi.nlm.nih.gov/genomes/refseq/" + division + "/assembly_summary.txt"
if q == 1:
os.popen("wget -q " + assembly_summary_dir)
else:
os.popen("wget " + assembly_summary_dir)
os.popen("mv assembly_summary.txt " + db_dir + "assembly_summary_" + division + ".txt")
def parse_assembly_summary(assembly_summary_fn = "assembly_summary_bacteria.txt"):
fp = open(db_dir + assembly_summary_fn, 'r')
for line in fp:
if line[0] == "#":
continue
line = line.strip()
tokens = line.split('\t')
if tokens[11] == "Complete Genome":
fn = tokens[19].split('/')
fn = fn[-1] + "_genomic.fna"
fn_map[fn] = [tokens[5], tokens[7]]
download_list.append(tokens[19] + "/" + fn + ".gz")
fp.close()
def parse_assembly_summary_taxid(assembly_summary_fn = "assembly_summary_bacteria.txt"):
fp = open(db_dir + assembly_summary_fn, 'r')
for line in fp:
if line[0] == "#":
continue
line = line.strip()
tokens = line.split('\t')
if tokens[11] == "Complete Genome":
if tokens[5] not in taxids:
fn = tokens[19].split('/')
fn = fn[-1] + "_genomic.fna"
fn_map[fn] = [tokens[5], tokens[7]]
download_list.append(tokens[19] + "/" + fn + ".gz")
taxids.append(tokens[5])
taxid_status.append(tokens[4])
else:
taxid_idx = taxids.index(tokens[5])
if (taxid_status[taxid_idx] == "na" and \
(tokens[4] == "reference genome" or tokens[4] == "representative genome")) or \
(taxid_status[taxid_idx] == "representative genome" and tokens[4] == "reference genome"):
fn = tokens[19].split('/')
fn = fn[-1] + "_genomic.fna"
fn_map[fn] = [tokens[5], tokens[7]]
fn_old = download_list[taxid_idx].split('/')
fn_old = fn_old[-1][:-3]
del fn_map[fn_old]
download_list[taxid_idx] = tokens[19] + "/" + fn + ".gz"
taxid_status[taxid_idx] = tokens[4]
fp.close()
def parse_assembly_summary_species(assembly_summary_fn = "assembly_summary_bacteria.txt"):
fp = open(db_dir + assembly_summary_fn, 'r')
for line in fp:
if line[0] == "#":
continue
line = line.strip()
tokens = line.split('\t')
if tokens[11] == "Complete Genome":
if tokens[6] not in taxids:
fn = tokens[19].split('/')
fn = fn[-1] + "_genomic.fna"
fn_map[fn] = [tokens[6], tokens[7]]
download_list.append(tokens[19] + "/" + fn + ".gz")
taxids.append(tokens[6])
taxid_status.append(tokens[4])
else:
taxid_idx = taxids.index(tokens[6])
if (taxid_status[taxid_idx] == "na" and \
(tokens[4] == "reference genome" or tokens[4] == "representative genome")) or \
(taxid_status[taxid_idx] == "representative genome" and tokens[4] == "reference genome"):
fn = tokens[19].split('/')
fn = fn[-1] + "_genomic.fna"
fn_map[fn] = [tokens[6], tokens[7]]
fn_old = download_list[taxid_idx].split('/')
fn_old = fn_old[-1][:-3]
del fn_map[fn_old]
download_list[taxid_idx] = tokens[19] + "/" + fn + ".gz"
taxid_status[taxid_idx] = tokens[4]
fp.close()
def parse_assembly_summary_species_rep(assembly_summary_fn = "assembly_summary_bacteria.txt"):
fp = open(db_dir + assembly_summary_fn, 'r')
for line in fp:
if line[0] == "#":
continue
line = line.strip()
tokens = line.split('\t')
if tokens[11] == "Complete Genome":
if tokens[6] not in taxids:
fn = tokens[19].split('/')
fn = fn[-1] + "_genomic.fna"
fn_map[fn] = [tokens[6], tokens[7]]
download_list.append(tokens[19] + "/" + fn + ".gz")
taxids.append(tokens[6])
taxid_status.append(tokens[4])
else:
taxid_idx = taxids.index(tokens[6])
if tokens[4] == "reference genome" or tokens[4] == "representative genome":
if taxid_status[taxid_idx] == "na":
fn = tokens[19].split('/')
fn = fn[-1] + "_genomic.fna"
fn_map[fn] = [tokens[6], tokens[7]]
fn_old = download_list[taxid_idx].split('/')
fn_old = fn_old[-1][:-3]
del fn_map[fn_old]
download_list[taxid_idx] = tokens[19] + "/" + fn + ".gz"
taxid_status[taxid_idx] = tokens[4]
else:
fn = tokens[19].split('/')
fn = fn[-1] + "_genomic.fna"
fn_map[fn] = [tokens[6], tokens[7]]
download_list_extra.append(tokens[19] + "/" + fn + ".gz")
fp.close()
def prepare_assembly_summary(division, q = 0):
download_assembly_summary(division, q)
if sample_genomes == "none":
parse_assembly_summary("assembly_summary_" + division + ".txt")
elif sample_genomes == "taxid":
parse_assembly_summary_taxid("assembly_summary_" + division + ".txt")
elif sample_genomes == "species_rep":
parse_assembly_summary_species_rep("assembly_summary_" + division + ".txt")
else:
parse_assembly_summary_species("assembly_summary_" + division + ".txt")
def download_files(q = 0):
if q == 1:
for file in download_list:
os.popen("wget -q " + file + " -P " + db_dir)
for file in download_list_extra:
os.popen("wget -q " + file + " -P " + db_dir)
else:
for file in download_list:
os.popen("wget " + file + " -P " + db_dir)
for file in download_list_extra:
os.popen("wget " + file + " -P " + db_dir)
def unzip_files():
os.popen("gunzip " + db_dir + "*.gz")
def output_map():
map_fp = open(db_dir + genome_map_fn, 'w')
taxid2gid = dict()
i = 1
for fn in fn_map.keys():
taxid = fn_map[fn][0]
sp_name = fn_map[fn][1]
if taxid not in taxid2gid:
taxid2gid[taxid] = i
map_fp.write("%s\t%d\t%s\t%s\n" %(fn, i, taxid, sp_name))
i += 1
else:
map_fp.write("%s\t%d\t%s\t%s\n" %(fn, taxid2gid[taxid], taxid, sp_name))
map_fp.close()
if __name__ == '__main__':
parse_arguments()
if div == "all":
for division in ["bacteria", "viral", "archaea"]:
prepare_assembly_summary(division, quiet)
else:
prepare_assembly_summary(div, quiet)
download_files(quiet)
if unzip_genomes:
unzip_files()
output_map()