Skip to content

Commit 88466f7

Browse files
committed
Merge branch 'master' of github.com:FofanovLab/MTSv
2 parents 9c12234 + d434b29 commit 88466f7

2 files changed

Lines changed: 52 additions & 34 deletions

File tree

mtsv/mtsv_prep/MTSv_prune.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -399,27 +399,27 @@ def arg_unwrappers(args, arguments=None):
399399

400400
def oneclickjson(path):
401401
arguments = []
402-
for fh in os.listdir(os.path.join(path, "artifacts/")):
402+
for fh in os.listdir(os.path.join(path, "artifacts")):
403403
if fnmatch.fnmatch(fh, "*.fas"):
404404
arguments.append({})
405405
fh = fh.split(".")[0]
406-
arguments[-1]['serialization-path'] = os.path.abspath( os.path.join(path, "artifacts/{0}.p".format(fh)))
406+
arguments[-1]['serialization-path'] = os.path.abspath( os.path.join(path, "artifacts","{0}.p".format(fh)))
407407

408-
arguments[-1]['fasta-path'] = os.path.abspath(os.path.join(path, "artifacts/{0}.fas".format(fh)))
408+
arguments[-1]['fasta-path'] = os.path.abspath(os.path.join(path, "artifacts", "{0}.fas".format(fh)))
409409

410410
arguments[-1]['minimum-length'] = 0
411411

412412
arguments[-1]['maximum-length'] = float('inf')
413-
arguments[-1]['taxdump-path'] = os.path.abspath(os.path.join(path, "artifacts/taxdump.tar.gz"))
413+
arguments[-1]['taxdump-path'] = os.path.abspath(os.path.join(path, "artifacts","taxdump.tar.gz"))
414414

415415
arguments[-1]['acc-to-taxid-paths'] = []
416-
for fp in os.listdir(os.path.join(path,"artifacts/")):
416+
for fp in os.listdir(os.path.join(path,"artifacts")):
417417
if fnmatch.fnmatch(fp, "*accession2taxid*"):
418-
arguments[-1]['acc-to-taxid-paths'].append(os.path.abspath(os.path.join(path,"artifacts/",fp)))
418+
arguments[-1]['acc-to-taxid-paths'].append(os.path.abspath(os.path.join(path,"artifacts",fp)))
419419

420420
arguments[-1]['rollup-rank'] = "species"
421421

422-
with open(os.path.abspath(os.path.join(path,"artifacts/{0}.json".format(fh))), "w") as file:
422+
with open(os.path.abspath(os.path.join(path,"artifacts", "{0}.json".format(fh))), "w") as file:
423423
json.dump(arguments[-1], file, sort_keys=True, indent=4)
424424

425425
return arguments
@@ -474,7 +474,10 @@ def ftp_dl(x):
474474
connection.login()
475475

476476
while x:
477-
fp_path = x.pop()
477+
try:
478+
fp_path = x.pop()
479+
except IndexError:
480+
break
478481
raw_path = fp_path[0]
479482
fp_path = fp_path[1]
480483
try:
@@ -582,17 +585,18 @@ def pull(path="",thread_count=1,databases ={"genbank"} ):
582585
continue
583586
except:
584587
pass
585-
if line[11].strip().decode().lower().replace(" ", "_") in databases:
588+
db = line[11].strip().decode().lower().replace(" ", "_")
589+
if db in databases:
586590
try:
587591
temp = line[19].split(ftp_path.encode(),1)[1].decode()
588592
temp_path = "{0}/{1}_genomic.gbff.gz".format(temp, os.path.basename(temp))
589593
except:
590594
continue
591595
try:
592-
level2path[line[11].strip()].append(temp_path)
596+
level2path[db.encode()].append(temp_path)
593597
except:
594-
level2path[line[11].strip()] = [temp_path]
595-
to_download.append((os.path.join(raw_path,"flat_files/"),temp_path))
598+
level2path[db.encode()] = [temp_path]
599+
to_download.append((os.path.join(raw_path,"flat_files"),temp_path))
596600

597601
reader = BytesIO()
598602
connection.retrbinary("RETR {0}{1}".format(assembly_gb,assembly_gb_summary) ,reader.write)
@@ -609,32 +613,32 @@ def pull(path="",thread_count=1,databases ={"genbank"} ):
609613
continue
610614
except:
611615
pass
612-
613-
if line[11].strip().decode().lower().replace(" ", "_") in databases:
616+
db = line[11].strip().decode().lower().replace(" ", "_")
617+
if db in databases:
614618
try:
615619
temp = line[19].split(ftp_path.encode(),1)[1].decode()
616620
temp_path = "{0}/{1}_genomic.gbff.gz".format(temp, os.path.basename(temp))
617621
except:
618622
continue
619623
try:
620-
level2path[line[11].strip()].append(temp_path)
624+
level2path[db.encode()].append(temp_path)
621625
except:
622-
level2path[line[11].strip()] = [temp_path]
626+
level2path[db.encode()] = [temp_path]
623627

624628
to_download.append((os.path.join(raw_path,"flat_files/"), temp_path) )
625629
artifacts = [(os.path.join(raw_path,"artifacts/"),"/pub/taxonomy/taxdump.tar.gz")]
626630
tax_path = "/pub/taxonomy/accession2taxid/"
627631
for file in connection.nlst(tax_path):
628632
if not fnmatch.fnmatch(os.path.basename(file), 'dead*') and not fnmatch.fnmatch(file, '*md5'):
629-
artifacts.append((os.path.join(raw_path,"artifacts/"),file))
633+
artifacts.append((os.path.join(raw_path, "artifacts"), file))
630634

631635
man = Manager()
632636
connection.quit()
633637
to_download += artifacts
634638
to_download = man.list(to_download)
635639

636640
proc = [Process(target = ftp_dl, args=(to_download,)) for i in range(thread_count)]
637-
with open(os.path.join(raw_path,"artifacts/ftp_dl.log"), "w"):
641+
with open(os.path.join(raw_path,"artifacts", "ftp_dl.log"), "w"):
638642
pass
639643
for p in proc:
640644
p.start()
@@ -647,7 +651,7 @@ def pull(path="",thread_count=1,databases ={"genbank"} ):
647651
fp = "{0}_ff.txt".format(i.decode().replace(" ","_"))
648652
with open(os.path.join(raw_path,"artifacts/",fp), "w") as out_file:
649653
for line in level2path[i]:
650-
out_file.write("{0}\n".format(os.path.join(os.path.abspath(raw_path),"flat_files/",os.path.basename(line))))
654+
out_file.write("{0}\n".format(os.path.join(os.path.abspath(raw_path),"flat_files/", os.path.basename(line))))
651655
return string_date
652656

653657
if __name__ =="__main__":

mtsv/mtsv_prep/main.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ def decompression(x, path):
7777
return x.strip(".gz")
7878

7979
def oneclickbuild(args):
80-
with open(os.path.join(args.path, "artifacts/decompression.log"), "w" ):
80+
with open(os.path.join(args.path, "artifacts","decompression.log"), "w" ):
8181
pass
8282
pool = Pool(args.threads)
8383
pool.starmap(decompression, [(os.path.abspath(x),
84-
args.path) for x in iglob(os.path.join(args.path,"flat_files/*.gz"))])
85-
for fp in iglob(os.path.join(args.path,"artifacts/","*_ff.txt")):
84+
args.path) for x in iglob(os.path.join(args.path,"flat_files", "*.gz"))])
85+
for fp in iglob(os.path.join(args.path,"artifacts","*_ff.txt")):
8686
db = list(os.path.split(fp))
8787
db[1] = db[1].strip().replace("_ff.txt",".fas")
8888
db = os.path.abspath("{0}{1}{2}".format(db[0],os.sep,db[1]))
@@ -100,14 +100,18 @@ def oneclickbuild(args):
100100
build_db(os.path.abspath(fp), db, os.devnull, os.devnull, args.threads, os.devnull)
101101

102102
arguments = oneclickjson(args.path)
103-
103+
for argument in arguments:
104+
pool.apply_async(tree_make, (argument['taxdump-path'], ))
105+
break
104106
pool.starmap(acc_serialization, [(argument['acc-to-taxid-paths'], argument['fasta-path'],
105107
argument['taxdump-path']) for argument in arguments ])
106108

109+
pool.close()
107110
# shutil.rmtree(os.path.join(args.path, "flat_files" ))
108111

109-
def mapper(x):
110-
return clip(*x)
112+
def tree_make(in_path):
113+
out_path = os.path.abspath(os.path.join(os.path.dirname(in_path), "tree.index"))
114+
subprocess.run("{0} --index {1} --dump {2}".format(bin_path('mtsv-tree-build'), out_path, os.path.abspath(in_path) ).split())
111115

112116
def partition(args):
113117
partition_list = []
@@ -247,6 +251,14 @@ def json_updater(args):
247251
params['partition-path'].append(os.path.abspath(path))
248252

249253
params['tree-index'] = os.path.abspath(os.path.join(args.path, "artifacts","tree.index"))
254+
params['serialization-path'] = os.path.abspath(os.path.join(args.path, "artifacts","{0}.p".format(base)))
255+
params['taxdump-path'] = os.path.abspath(os.path.join(args.path, "artifacts","taxdump.tar.gz"))
256+
params['fasta-path'] = os.path.abspath(os.path.join(args.path, "artifacts","{0}.fas".format(base)))
257+
258+
params['acc-to-taxid-paths'] = []
259+
for path in iglob(os.path.join(args.path, "artifacts","*taxid.gz")):
260+
params['acc-to-taxid-paths'].append(os.path.abspath(path))
261+
250262
with open(json_path, "w") as file:
251263
json.dump(params, file, sort_keys=True, indent=4)
252264

@@ -340,43 +352,45 @@ def setup_and_run(parser):
340352
args = parser.parse_known_args()[0]
341353

342354
try:
343-
344355
if args.cmd_class == Database:
356+
for i, val in enumerate(args.includedb):
357+
args.includedb[i] = val.strip().replace(" ","_").lower()
345358
if args.download_only:
346359
args.path = os.path.abspath(oneclickdl(args))
347-
348360
elif args.build_only:
349361
if args.path and os.path.isdir(args.path):
350362
oneclickbuild(args)
351363
json_updater(args)
352364
make_json_abs(args)
353-
354365
else:
355-
print("A valid path was not specified")
366+
print("A valid directory path was not specified")
356367
else:
357368
args.path = os.path.abspath(oneclickdl(args))
358369
oneclickbuild(args)
359370
json_updater(args)
360371
make_json_abs(args)
361-
362372
elif args.cmd_class == CustomDB:
373+
for i, val in enumerate(args.customdb):
374+
args.includedb[i] = val.strip().replace(" ","_").lower()
363375
oneclickfmbuild(args, args.partitions == DEFAULT_PARTITIONS)
364376
json_updater(args)
365377
make_json_abs(args)
366378

367-
# try:
368-
# make_json_rel(args)
369-
# except:
370-
# pass
371379

372380
except AttributeError:
373381
sys.argv[1] = "database"
374382
args = parser.parse_known_args()[0]
383+
for i, val in enumerate(args.includedb):
384+
args.includedb[i] = val.strip().replace(" ", "_").lower()
375385
args.path = os.path.abspath(oneclickdl(args))
376386
oneclickbuild(args)
377387
path = args.path
388+
json_updater(args)
389+
make_json_abs(args)
378390
sys.argv[1] = "custom_db"
379391
args = parser.parse_known_args()[0]
392+
for i, val in enumerate(args.customdb):
393+
args.customdb[i] = val.strip().replace(" ", "_").lower()
380394
args.path = path
381395
oneclickfmbuild(args, args.partitions == DEFAULT_PARTITIONS)
382396
json_updater(args)

0 commit comments

Comments
 (0)