-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathartspipeline1.py
1104 lines (1026 loc) · 53.8 KB
/
artspipeline1.py
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# Copyright (C) 2015,2016 Mohammad Alanjary
# University of Tuebingen
# Interfaculty Institute of Microbiology and Infection Medicine
# Lab of Nadine Ziemert, Div. of Microbiology/Biotechnology
# Funding by the German Centre for Infection Research (DZIF)
#
# This file is part of ARTS
# ARTS is free software. you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version
#
# License: You should have received a copy of the GNU General Public License v3 with ARTS
# A copy of the GPLv3 can also be found at: <http://www.gnu.org/licenses/>.
import argparse, tempfile, os, shutil, pickle, sys, re, glob, json, subprocess, shlex, time, multiprocessing as mp
import parsegbk, makeseqsql, makehmmsql, seqsql2fa, extractdbgenes, getrnagenes, setlog
import ete3methods, rangerdtl
from combine_results import *
from subprocess import Popen, PIPE
from threading import Timer
from distutils.dir_util import copy_tree
def makequerydb(infasta,tdir,fname,orgname,idprfx=""):
makeseqsql.runlist([tdir+infasta],tdir+fname+".db",transonly=True,orgname=orgname)
seqsql2fa.writefasta(tdir+fname+".db",tdir+fname+".faa",idprfx=idprfx)
seqsql2fa.writefasta(tdir+fname+".db",tdir+fname+".fna",nuc=True,idprfx=idprfx)
def trimal(infil,outfile):
cmd = ["trimal","-automated1","-in",infil,"-out",outfile]
with open(os.devnull,"w") as devnull:
try:
subprocess.call(cmd,stdout=devnull)
log.debug("TrimAl: finished %s"%outfile)
return True
except subprocess.CalledProcessError as e:
log.error("TrimAl: error, could not process %s - %s"%(outfile,e))
return False
# def getgenes(infile,tdir,outfile):
# #Use gbk only for now
# foundorg, genus = parsegbk.convertgenes(infile,tdir,plasmid=True,userecnum=True,clust=True,rename=outfile)
# return foundorg, genus
def raxmlEPA(outdir,inalgn,reftree):
#Run raxml EPA
cmd="raxmlHPC-SSE3 -f v -m GTRGAMMA -p 12345 -w %s -t %s -s %s -n %s"%(outdir,reftree,inalgn,os.path.split(reftree)[-1])
try:
with open(os.devnull,"w") as devnull:
subprocess.call(cmd.split(),stdout=devnull)
log.debug("RAxML-EPA: finished %s"%inalgn)
return True
except subprocess.CalledProcessError as e:
log.error("RAxML-EPA: error, could not process %s - %s"%(inalgn,e))
return False
def mergealign(refalgn,newseqs,outfile,cpu=1):
#ofil = tempfile.NamedTemporaryFile(dir=os.path.split(outfile)[0])
cmd = ["mafft","--quiet","--add",newseqs,refalgn]
if cpu>1:
cmd[1:1] = ["--thread","%s"%cpu]
# if os.path.isfile(outfile+".tmafft"):
# cmd[1:1] = ["--treein", outfile+".tmafft"]
try:
# ofil = tempfile.NamedTemporaryFile(dir=os.path.split(newseqs)[0])
with open(outfile,"w") as ofil:
subprocess.call(cmd,stdout=ofil)
log.debug("MAFFT: finished %s"%newseqs)
return True
except subprocess.CalledProcessError as e:
log.error("MAFFT: error, failed to align %s"%outfile)
return False
def getcutvals(fname):
cut_vals={}
with open(fname,"r") as fil:
non_decimal = re.compile(r'[^\d.]+')
for line in fil:
if line.startswith("NAME"):
modelname=line.split()[1]
if modelname not in cut_vals:
cut_vals[modelname]={}
if line.startswith("GA ") or line.startswith("TC ") or line.startswith("NC "):
cv=line.split()
cut_vals[modelname][cv[0]]=[float(non_decimal.sub('',cv[1])),float(non_decimal.sub('',cv[2]))]
return cut_vals
def converthmm(hmmfile,outfile):
if hmmfile and os.path.exists(hmmfile):
with open(outfile,"w") as ofil:
status = subprocess.call(['hmmconvert',str(hmmfile)],stdout=ofil)
if not status:
return outfile
return False
def validatehmms(hmmfile):
if hmmfile and os.path.exists(hmmfile):
with open(os.devnull,"w") as devnull:
status = subprocess.call(['hmmstat',hmmfile],stderr=devnull,stdout=devnull)
if status==0:
return True
return False
def renamehmms(hmmdbs,tdir=None):
if os.path.exists(hmmdbs):
tf = tempfile.NamedTemporaryFile(prefix="hmmdb_",suffix=".hmm",dir=tdir,delete=False)
def concathmms(hmmdbs,tdir=None):
if type(hmmdbs) is list:
flist=[x for x in hmmdbs if os.path.isfile(x)]
elif type(hmmdbs) is str and "," in hmmdbs:
flist=[x for x in hmmdbs.split(",") if os.path.isfile(x)]
else:
return False
if len(flist):
tf = tempfile.NamedTemporaryFile(prefix="hmmdb_",suffix=".hmm",dir=tdir,delete=False)
for fname in flist:
if validatehmms(fname):
with open(fname,"rb") as fil:
shutil.copyfileobj(fil, tf)
tf.close()
return tf.name
else:
return False
def parsegmatrix(fname,avgs=True):
temp={}
with open(fname,"r") as fil:
for line in fil:
if line.startswith("#Gene"):
temp["_orgs"]=line.strip().split("\t")[5:]
if line.startswith("#Singles"):
if "," in line:
temp["_singles"]=set(line.strip().split("\t")[1].split(","))
elif not line.startswith("#"):
x = line.strip().split("\t")
if avgs:
temp[x[0]]=[float(v) for v in x[1:5]]
# temp[x[0]].append(float(x[4])/len(x[5:]))
temp[x[0]].extend([float(v) for v in x[5:]])
else:
temp[x[0]]=[float(x[1])]
return temp
def getdupgenes(refgm,genematrix,maxcount=3,minsr=0.2,minubiq=0.2):
glist = list(set(refgm.keys())&set(genematrix.keys()))
duplist = []
for k in glist:
if k.startswith("_"):
continue
# Get counts that are > Avg + STDev of ref genes
# ubiq = float(refgm[k][-2])/refgm[k][-1] #ubiquity numorgs with gene / total orgs
genematrix[k] = list(genematrix[k])
genematrix[k].extend(refgm[k])
# if genematrix[k][0] > refgm[k][0]+refgm[k][1] and refgm[k][0] <= maxcount and refgm[k][2] <= maxrsd and refgm[k][3] >= minubiq:
if genematrix[k][0] > refgm[k][0]+refgm[k][1] and refgm[k][0] <= maxcount and refgm[k][2] >= minsr:
duplist.append(k)
return duplist
def runhmmer(fname,hmmdb,tdir,cut=None,mcpu=1):
tf = tempfile.NamedTemporaryFile(prefix="domrslt_",suffix=".domhr",dir=tdir,delete=False)
tf.close()
with open(tf.name+".log","w") as logfil:
cmd=["hmmsearch", "--domtblout", tf.name, "--noali", "--notextw", hmmdb, fname]
if mcpu>1:
cmd[1:1] = ["--cpu", str(mcpu)]
if cut and cut.lower() in ("ga","tc","nc"):
cmd[1:1] = ["--cut_%s"%cut.lower()]
else:
cmd[1:1] = ["-E","0.01"]
subprocess.call(cmd, stdout=logfil, stderr=logfil)
return tf.name
def buildtrees(refdir, tdir, fname, cpu=1):
trimm = False
rxml = False
algn = False
# global log
# log = setlog.init(tdir + "arts-query.log")
if os.path.isfile(refdir+fname):
algn = mergealign(refdir+fname, tdir+"coregenes/"+fname,tdir+"alignedcore/"+fname,cpu)
if algn:
trimm = trimal(tdir+"alignedcore/"+fname, tdir+"trimmedcore/"+fname)
if trimm:
reftree = refdir+"trees/"+os.path.splitext(fname)[0]+".tree"
rxml = raxmlEPA(os.path.realpath(tdir+"raxml/"),tdir+"trimmedcore/"+fname,reftree)
labledtree = tdir+"raxml/RAxML_labelledTree.%s.tree"%os.path.splitext(fname)[0]
if rxml and os.path.exists(labledtree):
with open(labledtree,"r") as ifil, open(tdir+"trees/%s.tree"%os.path.splitext(fname)[0],"w") as ofil:
x = ifil.readline() #x = ifil.next()
ofil.write(re.sub("\[I\d+?\]|\"|'|QUERY___","",x))
log.info("BuildTree: Finished %s"%os.path.split(fname)[-1])
return True
else:
log.error("BuildTree Failed: %s"%fname)
return False
def catTrees(flist,outfile):
with open(outfile,"w") as ofil:
for fname in flist:
if os.path.exists(fname):
with open(fname,"r") as ifil:
x = ifil.readline() #x = ifil.next()
ofil.write(re.sub("\|[A-Z0-9][0-9]*","",x).replace("|","_")) #write line and remove seq id, ensure "|" is removed
# shutil.copyfileobj(ifil,ofil)
else:
log.warning("Tree file not found for MLST: %s"%fname)
return outfile
def makesptree(tlist,outdir,astjar=False):
if not outdir.endswith("/"):
outdir += "/"
outfile = catTrees(tlist,outdir+"allmlst_cat.nwk")
dfltjar = glob.glob(os.path.join(os.path.dirname(os.path.realpath(__file__)),"astral","*.jar"))
if astjar and type(astjar) is str and os.path.exists(astjar):
astjar = os.path.realpath(str(astjar))
elif dfltjar:
astjar = dfltjar[0]
log.debug("Using default astral %s"%dfltjar[0])
else:
log.warning("cannot find astral jar file")
return False
#Call Astral:
log.info("Started building ASTRAL coalescent MLST tree...")
if os.path.exists(outfile):
if astjar and os.path.exists(astjar):
cmd = ["java","-Xmx3000M","-jar",astjar,"-i",outfile,"-o",outdir+"astMLST.tree","-t","0"]
else:
log.warning("attempting to launch astral as 'astral'")
cmd = ["astral","-i",outfile,"-o",outdir+"astMLST.tree","-t","0"]
try:
with open(outdir+"astral.log","w") as fil:
subprocess.call(cmd,stderr=fil)
log.info("Finished coalescent tree: %s"%outdir+"astMLST.tree")
return outdir+"astMLST.tree"
except Exception as e:
log.error("Could not build coalescent tree: %s"%outdir+"astMLST.tree")
log.exception("exception")
raise
return False
def checkbgcprox(clusters,gtypes=[],glist=[],modeldata={}):
bgchits = {"cluster-%s"%bgc[0]:{"row":["cluster-%s"%bgc[0],bgc[1],bgc[2],"%s - %s"%(bgc[3],bgc[4])],"hits":[]} for bgc in clusters}
corebgchits = {}
corehitnum = 0
for i,ghits in enumerate(glist):
if "seqs" in ghits.keys():
for seqid,gene in ghits['seqs'].items():
#check all start end overlap gene[2]=scaffold gene[3]=start gene[4]=end bgc[2..3..4]
for bgc in clusters:
clustid = "cluster-%s"%bgc[0]
desc = func = ""
if gene[2]==bgc[2] and not ((int(gene[4]) < int(bgc[3])) or (int(bgc[4]) < int(gene[3]))):
if gene[1] in modeldata.keys():
desc = modeldata[gene[1]][0]+": "+modeldata[gene[1]][1]
func = modeldata[gene[1]][2]
if gene[1] not in corebgchits.keys():
corebgchits[gene[1]] = {}
if gtypes[i] == "Core":
corehitnum+=1
corebgchits[gene[1]][seqid] = [str(x) for x in bgc[:5]]+[gene[3],gene[4]]+["Core"]
bgchits[clustid]["hits"].append([seqid,gene[1],gene[3],gene[4],gtypes[i],desc,func])
break
return bgchits, corebgchits, corehitnum
def checkgenus(qgenus, qorg, dtlrow):
#check if genus is not same within LCA pair and direct doner/recp
lp = dtlrow[3].split(", ")
if qorg in lp[0]:
lp = lp[1] #pick unmatching pair
else:
lp = lp[0]
if qgenus.lower() in dtlrow[2].lower() or qgenus.lower() in lp.lower():
return False
return True
def checkphylogeny(orgrecs,qorg,qgenus):
hits={}
i=0
for v in orgrecs[qorg]:
if checkgenus(qgenus,qorg,v[2:]):
if v[0] not in hits:
hits[v[0]]={}
if v[1] not in hits[v[0]]:
hits[v[0]][v[1]]=[]
i+=1
hits[v[0]][v[1]].append(v[2:])
return hits,i
#use this if local antismash is not run, turns gbk file into AS results webpage
#def makeantismashresults(aspath,infile,outdir):
# ashelpdir = os.path.join(os.path.dirname(os.path.realpath(__file__)),"antismash_helper")
# #This hard code needs to be changed for users that will locally install arts, maybe make another argument with different antismash path?
# aspath = "/home/ubuntu/disk/antismash_3/antismash-3.0.5.1/run_antismash.py"
# if not aspath:
# log.warning("No antismash installation found")
# else:
# aspath = os.path.split(aspath)[0]
# sys.path.insert(0, aspath)
# try:
# from Bio import SeqIO
# from antismash.config import load_config
# from antismash.config import set_config
# from antismash.output_modules import html
# import straight.plugin
#
# shutil.copytree(os.path.join(ashelpdir,"output_template"),outdir)
# try:
# seqrecs = list(SeqIO.parse(infile,'genbank'))
# except ValueError:
# log.error("Biopython could not parse genbank. please ensure no external sequence references are present")
# log.exception("exception")
# with open(os.path.join(ashelpdir,"defaultopts.pkl"),"r") as fil:
# opts = pickle.load(fil)
#
# opts.full_outputfolder_path = opts.outputfoldername = os.path.realpath(outdir)
# opts.sep='/'
# opts.plugins=list(straight.plugin.load('antismash.specific_modules'))
# opts.smcogs=False
#
# load_config(opts)
# set_config(opts)
# html.generator.generate_webpage(seqrecs,opts)
# except ImportError:
# log.error("Error importing antismash library, check antismash configuration")
#
def makeantismashresults_json(aspath,infile,outdir,mcpu):
cmd = ["python3", aspath, "--reuse-results", infile, "--output-dir", outdir,"--cpus",str(mcpu)]
if os.path.exists(outdir) != True:
os.mkdir(outdir)
if os.path.exists(os.path.join(outdir, "knownclusterblast")) != True:
os.mkdir(os.path.join(outdir, "knownclusterblast"))
result_gbk_file = ""
try:
aslog_dir= os.path.abspath(os.path.join(outdir, os.pardir))
with open(aslog_dir+"/aslog.txt",'w') as aslog:
cmd = " ".join(cmd)
subprocess.call(cmd,stderr=aslog,stdout=aslog, shell=True)
result_gbk_file = os.path.join(outdir, os.path.basename(infile)[0:-5]+".gbk")
log.info(result_gbk_file)
except:
log.error("Error running Antismash reuse-results")
return result_gbk_file
def runantismash(aspath,infile,outdir,mcpu):
asdir = os.path.join(outdir,"antismash")
if os.path.exists(aspath) and os.path.exists(infile):
##hard-coded quick solution for new version##
python_version = "python3"
output_command = "--output-dir"
# with open("/home/ubuntu/disk/antismash/run_antismash.py", "r") as run_antismash_file:
# run_antismash_lines = run_antismash_file.readlines()
#find antismash version
# if "python3" in run_antismash_lines[0]:
# python_version = "python3"
# output_command = "--output-dir"
# else:
# python_version = "python"
# output_command = "--outputfolder"
cmd = python_version+ " " + aspath + " " +infile + " --minimal " +output_command + " " + asdir + " -v --cpus " + str(mcpu)
cmd2 = python_version+ " " + aspath + " " +infile + " --minimal " +output_command + " " + asdir + " -v --cpus " + str(mcpu) + " --genefinding-tool prodigal"
with open(outdir+"/aslog.txt",'w') as aslog:
proc = Popen(shlex.split(cmd) ,stderr=aslog,stdout=aslog)
#kill antismash after 3 hours
timeout = 10800
timer = Timer(timeout, proc.kill)
try:
start = time.time()
timer.start()
stdout, stderr = proc.communicate()
finally:
end = time.time()
if end - start >= timeout:
log.error("Antismash taking too long, please use antismash servers for job and try again with json file")
timer.cancel()
log.info("first as done")
for line in open(outdir+"/aslog.txt", "r"):
#antismash 5 doesnt accept WGS record without sequences so download them
if "whole genome shotgun records are not supported" in line:
log.info("wgs")
wgs_dir = os.path.join(outdir,"wgs_record")
os.mkdir(wgs_dir)
wgs_gbk_path = os.path.join(wgs_dir, os.path.basename(infile))
with open(outdir + "/wgs_dl_log.txt", 'w') as dllog:
#another package with pip (one of Kai's useful tools), but this hard code needs to be changed as well
cmd_ncbi = ["/home/ubuntu/.local/bin/ncbi-acc-download --recursive " + os.path.splitext(os.path.split(infile)[-1])[0] + " -o " + wgs_gbk_path]
p = Popen(cmd_ncbi, stdout=dllog, stderr=dllog, shell=True)
out, err = p.communicate()
log.info(os.listdir(wgs_dir))
wgs_cmd = python_version+ " " + aspath + " " +wgs_gbk_path + " --minimal " +output_command + " " + asdir + " -v --cpus " + str(mcpu)
wgs_cmd2 = python_version+ " " + aspath + " " +wgs_gbk_path + " --minimal " +output_command + " " + asdir + " -v --cpus " + str(mcpu) + " --genefinding-tool prodigal"
with open(outdir+"/aslog.txt",'w') as aslog:
proc = Popen(shlex.split(wgs_cmd2) ,stderr=aslog,stdout=aslog)
timer = Timer(timeout, proc.kill)
try:
start = time.time()
timer.start()
stdout, stderr = proc.communicate()
finally:
end = time.time()
if end - start >= timeout:
log.error("Antismash taking too long, please use antismash servers for job and try again with json file")
timer.cancel()
break
#If antismash cant find any genes it gives an error
if "ValueError: Called find_genes, but genefinding disabled" in line or "contains no genes and no genefinding tool specified" in line:
with open(outdir+"/aslog.txt",'w') as aslog:
proc = Popen(shlex.split(cmd2) ,stderr=aslog,stdout=aslog)
timer = Timer(timeout, proc.kill)
try:
start = time.time()
timer.start()
stdout, stderr = proc.communicate()
finally:
end = time.time()
if end - start >= timeout:
log.error("Antismash taking too long, please use antismash servers for job and try again with json file")
timer.cancel()
break
if python_version == "python3":
fil = [x for x in os.listdir(asdir) if x.endswith(".gbk") and "region" not in x and "cluster" not in x]
else:
fil = [x for x in os.listdir(asdir) if x.endswith(".final.gbk")]
if len(fil):
return os.path.join(asdir,fil[0])
else:
log.error("Could not find Antismash output")
return infile
else:
log.error("Could not find Antismash executable and/or input file")
return infile
def writecoretable(corelist,gm,krlist,modeldata,outfile):
summary = {"data":[],"seqs":corelist["seqs"]}
tablestr = "#Core_gene\tDescription\tFunction\tDuplication\tBGC_Proximity\tPhylogeny\tKnown_target\t[Hits_listed]\n"
funcstats = {}
if "matrix" in gm.keys():
for k in gm["matrix"].keys():
if k not in modeldata:
log.debug("%s has no model metadata!"%k)
metadata = modeldata.get(k,["N/A","N/A","N/A","N/A","N/A","N/A","N/A"])
if metadata[2]:
if metadata[2] not in funcstats:
funcstats[metadata[2]] = 0
funcstats[metadata[2]] += 1
temp = {"coregene":k,"TC":metadata[3],"dNdS":metadata[4],"SC":metadata[5],"Ubiq":metadata[6],"description":"%s: %s"%(metadata[0],metadata[1]),"func":metadata[2],"duplicate":"N/A","proximity":"N/A",
"phylogeny":"N/A","hits":corelist["core"].get(k,{}).get("seqs",[]),"proxhits":[],"known_hit":"N/A"}
if "duplicates" in gm.keys():
if k in gm["duplicates"]:
temp["duplicate"] = "Yes"
else:
temp["duplicate"] = "No"
if "proximity" in gm.keys():
if k in gm["proximity"].keys():
temp["proximity"] = "Yes"
temp["proxhits"] = gm["proximity"][k]
else:
temp["proximity"] = "No"
if "phylogeny" in gm.keys():
if k in gm["phylogeny"].keys():
temp["phylogeny"] = "Yes"
temp["phylhits"] = gm["phylogeny"][k]
else:
temp["phylogeny"] = "No"
if "seqs" in krlist:
temp["known_hit"] = "No"
for seqid in temp["hits"]:
if seqid in krlist["seqs"].keys():
temp["known_hit"] = "Yes"
summary["data"].append(temp)
temp["allhits"] = "[" + "; ".join([str(corelist["seqs"][seqid][-2]).replace(";","") for seqid in temp["hits"]]) + "]"
tablestr += "{coregene}\t{description}\t{func}\t{duplicate}\t{proximity}\t{phylogeny}\t{known_hit}\t{allhits}\n".format(**temp)
summary["funcstats"]=funcstats
if outfile:
with open(outfile+".json","w") as jfil, open(outfile+".tsv","w") as tfil:
json.dump(summary,jfil,indent=2)
tfil.write(tablestr)
else:
return summary
#reformat duplicate table and write to file
def writeduptable(matrix,dups,corelist,modeldata,outfile):
# metadata = {k:"%s: %s"%tuple(modeldata.get(k,["n/a","n/a"])[:2]) for k in dups}
hitlist = {k:[str(corelist["seqs"].get(seqid,["n/a","n/a"])[-2]).replace(";","") for seqid in corelist["core"].get(k,{}).get("seqs",[])] for k in dups}
dupmat = [[x]+matrix.get(x,["n/a"])[:5]+["["+"; ".join(hitlist.get(x,""))+"]"]+["%s: %s"%tuple(modeldata.get(x,["n/a","n/a"])[:2])] for x in dups]
with open(outfile+".json","w") as jfil, open(outfile+".tsv","w") as tfil:
temp = {"data":dupmat[:-1],"hits":hitlist}
json.dump(temp,jfil,indent=2)
tfil.write("\t".join(["#Core_gene","Count","Ref_median","Ref_stdev","Ref_RSD","Ref_ubiquity","[Hits_listed]","Description"])+"\n")
for x in dupmat:
tfil.write("\t".join([str(y) for y in x])+"\n")
#reformat bgc table and write
def writebgctable(temp,outfile):
data=[]
for key,x in temp.items():
if key.startswith("cluster-"):
corehits = len([hit for hit in x["hits"] if hit[4] == "Core"])
data.append(x["row"]+[str(corehits),str(len(x["hits"])-corehits),x["hits"]])
temp["data"] = data
with open(outfile+".json","w") as jfil, open(outfile+".tsv","w") as tfil:
json.dump(temp,jfil,indent=2)
tfil.write("\t".join(["#Cluster","Type","Source","Location","Core hits","Other hits","Genelist"])+"\n")
for x in temp["data"]:
tfil.write("\t".join([str(y) for y in x])+"\n")
#get model metadata from json, tsv or hmm file
def getmodeldata(fname="",hmmname=""):
modeldata = {}
log.debug("Getting model metadata...")
if ".json" in fname.lower() and os.path.exists(fname):
with open(fname,"r") as fil:
modeldata = json.load(fil)
elif ".tsv" in fname.lower() and os.path.exists(fname):
with open(fname,"r") as fil:
for line in fil:
if not line.startswith("#"):
x = line.strip().split("\t")
if x[0] not in modeldata:
modeldata[x[0]] = x[1:]
elif ".hmm" in hmmname.lower() and os.path.exists(hmmname):
with open(hmmname,"r") as fil:
x = ["N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A"]
for line in fil:
if line.startswith("ACC"):
x[0] = " ".join(line.split()[1:])
if line.startswith("NAME"):
x[1] = " ".join(line.split()[1:])
if line.startswith("DESC"):
x[2] = " ".join(line.split()[1:])
if line.startswith("TC"):
x[4] = line.split()[1]
if line.startswith("HMM "):
if x[0] not in modeldata:
modeldata[x[0]] = x[1:]
x = ["N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A"]
else:
log.error("Could not get model metadata, ensure file is .json, .tsv, or .hmm")
return False
return modeldata
#simple sub strings from location text
def parsesourcelocation(x):
x=x.strip()
source = ""
loc = ["","",""]
if "|source|" in x:
idx1 = x.index("|source|")
if "|" in x[idx1+8:]:
idx2 = x.index("|",idx1+8)
source = x[idx1+8:idx2]
else:
source = x[idx1+8:]
if "|loc|" in x:
idx1 = x.index("|loc|")
if "|" in x[idx1+5:]:
idx2 = x.index("|",idx1+5)
loc = x[idx1:idx2].split("_")
else:
loc = x[idx1+5:].split("_")
return source,loc
def startquery(infile=None,refdir=None,td=None,rd=None,hmmdbs=None,rnahmm=None,cut=None,
astjar=False,toconsole=False,mcpu=1,asrun=False,knownhmms=False,dufhmms=False,
custcorehmms=False,custhmms=False,aspath=None,options="phyl,kres,duf",custorgname=None,prebuilttrees=False,bcp=None, run_bsc=False):
try:
#Set Working directory
if type(rd) is str and not rd.endswith("/"):
rd+="/"
if type(rd) is str and os.path.exists(rd):
tdir = os.path.realpath(rd)
else:
tdir=tempfile.mkdtemp(prefix="arts-query-",dir=td)
if not tdir.endswith("/"):
tdir+="/"
if not refdir.endswith("/"):
refdir+="/"
try:
mcpu = int(mcpu)
except ValueError:
mcpu = mp.cpu_count()
oldfname = os.path.splitext(os.path.split(infile)[-1])[0]
#Start log
global log
log = setlog.init(tdir+"arts-query.log",toconsole=toconsole)
#Set folders if not created already
if not os.path.exists(tdir+"coregenes"):
os.mkdir(tdir+"coregenes")
os.mkdir(tdir+"alignedcore")
os.mkdir(tdir+"trimmedcore")
os.mkdir(tdir+"raxml")
os.mkdir(tdir+"trees")
os.mkdir(tdir+"tables")
log.info("Init folders complete")
log.debug("Starting query: dir=%s; pid=%s; args=%s"%(tdir,os.getpid(),locals()))
except Exception as e:
log.error("Problem with folder creation / disk error (%s)"%e)
log.exception("exception")
raise e
#Start Antismash run if needed
if asrun:
try:
os.mkdir(tdir+"antismash")
log.info("Starting Antismash job...")
infile = runantismash(aspath,os.path.realpath(infile),tdir,mcpu)
log.info("Finished Antismash")
except Exception as e:
log.error("Antismash failed to execute (%s)"%e)
log.exception("exception")
else:
if infile.endswith(".gbk"):
makeantismashresults(aspath, os.path.realpath(infile), os.path.realpath(tdir + "antismash"))
elif infile.endswith(".json"):
infile = makeantismashresults_json(aspath, os.path.realpath(infile), os.path.realpath(tdir + "antismash"), mcpu)
try:
### PREP SQL
if not os.path.isfile(infile):
log.error("Error: No such input path %s"%infile)
raise IOError
try:
queryorg, querygenus, clusters, locus_names = parsegbk.convertgenes(infile,tdir,plasmid=True,userecnum=True,clust=True,rename=oldfname)
with open(os.path.join(tdir,"locus_to_region.pickle"), "wb") as locus_file:
pickle.dump(locus_names, locus_file)
if custorgname:
#remove any bad chars
#if the orgname has characters like :/# then the astral-rangerdtl part of phylogeny also fails so maybe we can change the line below?
# queryorg = ''.join(ch for ch in custorgname.replace(".","").replace("#","").replace(":","").replace("/","").replace(",","").replace("(","_").replace(")","_").replace("-","_").replace(" ","_") if ch.isalnum() or "_")
queryorg = ''.join(ch for ch in custorgname.replace(".","").replace(",","").replace("(","_").replace(")","_").replace("-","_").replace(" ","_") if ch.isalnum() or "_")
querygenus = queryorg.replace("_"," ").split()[0]
except ValueError:
log.error("Biopython could not parse genbank. please ensure no external sequence references are present")
log.exception("exception")
infasta = oldfname+".fna"
makequerydb(infasta,tdir,"queryseqs",orgname=queryorg)
log.info("query: org=%s"%queryorg)
log.info("query: genus=%s"%querygenus)
#use list of hmmmodels if directory is specified
# if hmmdbs and os.path.isdir(hmmdbs):
# if not hmmdbs.endswith("/"):
# hmmdbs+="/"
# refmodels = [os.path.splitext(os.path.split(x)[1])[0] for x in glob.glob(refdir+"*.fna")]
# hmmdbs = [hmmdbs+x for x in os.listdir(hmmdbs) if os.path.splitext(x)[0] in refmodels and "hmm" in x.lower()]
# hmmdb = concathmms(hmmdbs,tdir)
# elif hmmdbs:
# hmmdb = hmmdbs
if type(hmmdbs) is list and all([validatehmms(x) for x in hmmdbs]):
hmmdb = concathmms(hmmdbs,tdir)
modeldata = getmodeldata(hmmname=hmmdb)
else:
hmmdb = os.path.join(refdir,"coremodels.hmm")
if "expert" in options and os.path.exists(os.path.join(refdir,"coremodels_exp.hmm")):
hmmdb = os.path.join(refdir,"coremodels_exp.hmm")
modeldata = getmodeldata(fname=os.path.join(refdir,"model_metadata.json"),hmmname=hmmdb)
if custcorehmms and validatehmms(custcorehmms):
hmmdb = concathmms([converthmm(hmmdb,os.path.join(tdir,"core.hmm")),converthmm(custcorehmms,os.path.join(tdir,"custcore.hmm"))],tdir)
modeldata.update(getmodeldata(hmmname=custcorehmms))
# with open(tdir+"model_metadata.json","w") as expfil:
# json.dump(modeldata,expfil,indent=2)
except Exception as e:
log.error("Problem init database")
log.exception("exception")
raise e
defaultcut="tc"
try:
#Set cutoff thresholds for core search
cut_vals = getcutvals(hmmdb)
log.debug("Setting thresholds...")
if cut and cut.upper() in ("GA","NC"):
log.debug("Setting %s cutoff..."%cut)
cut = {k:x.get(cut.upper(),[0])[0] for k,x in cut_vals.items()}
defaultcut = None
elif cut and cut.upper()[0] in "E" and cut.upper()[1] in "123":
try:
factors = {"1":0.90,"2":0.75,"3":0.50,"4":0.30}
f = factors[cut[1]]
cut = {k:float(x.get("NC",[0])[0])*f for k,x in cut_vals.items()}
defaultcut = None
log.debug("Setting %s NC cutoff..."%f)
except ValueError:
cut = None
log.warning("Warning: invalid value for threshold, using defaults")
else:
cut = None
except Exception as e:
log.warning("Could not set thresholds using default trusted cutoffs")
cut = None
try:
### RUN RNA Search and add to sql
if rnahmm and os.path.exists(rnahmm):
log.info("Starting RNA hmmsearch...")
hmmdomrslt=runhmmer(tdir+"queryseqs.fna",rnahmm,tdir,cut="tc",mcpu=mcpu)
log.info("Adding RNA hmmresults...")
makehmmsql.run(hmmdomrslt,tdir+"queryseqs.db",ev=0.1,rna=True)
### Extract RNA hits
getrnagenes.runfile(tdir+"queryseqs.db",outdir=tdir+"coregenes")
except Exception as e:
log.warning("Could not get RNA hits")
log.exception("exception")
try:
#Run Known resistance models and custom supplied models
knownhits={}
if knownhmms and os.path.exists(knownhmms) and "kres" in options.lower():
#Confirm and add custom hmm models
log.info("Checking customhmms %s..."%custhmms)
if validatehmms(custhmms):
knownhmms = concathmms([converthmm(knownhmms,os.path.join(tdir,"kres.hmm")),converthmm(custhmms,os.path.join(tdir,"custres.hmm"))],tdir)
log.info("Combined knownhmms = %s"%knownhmms)
modeldata.update(getmodeldata(hmmname=knownhmms))
log.info("Start known resistance search...")
hmmdomrslt=runhmmer(tdir+"queryseqs.faa",knownhmms,tdir,cut="tc",mcpu=mcpu)
os.rename(hmmdomrslt,tdir+"knownhits.domhr")
#write results to json file
knownhits = {"data":[],"seqs":{}}
with open(os.path.join(tdir,"knownhits.domhr"),"r") as fil, open(os.path.join(tdir,"tables","knownhits.json"),"w") as ofil, open(os.path.join(tdir,"tables","knownhits.tsv"),"w") as tfil:
tfil.write("#Model\tDescription\tSequence id\tevalue\tbitscore\tSequence description\n")
for line in fil:
if not line.startswith("#"):
x = line.strip().split()
x[-1].replace("'","")
source,loc = parsesourcelocation(line)
seqid = x[0].split("|")[-1]
if seqid not in knownhits["seqs"]:
knownhits["data"].append([x[4],x[3],seqid,x[6],x[7],x[-1]])
tfil.write("\t".join([x[4],x[3],seqid,x[6],x[7],x[-1]])+"\n")
# [Seqid, modelname, scaffold_source, loc_start, loc_end, strand, evalue, bitscore]
knownhits["seqs"][seqid] = [seqid,x[4],source]+loc+[x[6],x[7],x[-1]]
elif float(knownhits["seqs"][seqid][5]) < float(x[7]):
knownhits["seqs"][seqid] = [seqid,x[4],source]+loc+[x[6],x[7],x[-1]]
if x[4] not in knownhits:
knownhits[x[4]] = {}
knownhits[x[4]][seqid] = [x[4],x[3],seqid,x[6],x[7],x[-1]]
json.dump(knownhits,ofil,indent=2)
log.info("Known Resistance Hits: %s"%len(knownhits["seqs"].keys()))
except Exception as e:
log.warning("Could not get known resistance hits")
log.exception("exception")
try:
#Run DUF models
dufhits={}
if dufhmms and os.path.exists(dufhmms) and "duf" in options.lower():
log.info("Start DUF search...")
hmmdomrslt=runhmmer(tdir+"queryseqs.faa",dufhmms,tdir,cut="tc",mcpu=mcpu)
os.rename(hmmdomrslt,tdir+"dufhits.domhr")
#write results to json file
dufhits = {"data":[],"seqs":{}}
with open(os.path.join(tdir,"dufhits.domhr"),"r") as fil, open(os.path.join(tdir,"tables","dufhits.json"),"w") as ofil:
for line in fil:
if not line.startswith("#"):
x = line.split()[:8]
source,loc = parsesourcelocation(line)
seqid = x[0].split("|")[-1]
if seqid not in dufhits["seqs"]:
dufhits["data"].append([x[4],x[3],seqid,x[6],x[7]])
# [Seqid, modelname, scaffold_source, loc_start, loc_end, strand, evalue, bitscore]
dufhits["seqs"][seqid] = [seqid,x[4],source]+loc+[x[6],x[7]]
elif float(dufhits["seqs"][seqid][5]) < float(x[7]):
dufhits["seqs"][seqid] = [seqid,x[4],source]+loc+[x[6],x[7]]
if x[4] not in dufhits:
dufhits[x[4]] = {}
dufhits[x[4]][seqid] = [x[4],x[3],seqid,x[6],x[7]]
json.dump(dufhits,ofil,indent=2)
log.info("DUF Hits: %s"%len(dufhits["seqs"].keys()))
except Exception as e:
log.warning("Could not get DUF hits")
log.exception("exception")
log.info("Milestone_1_complete")
### RUN CORE Search and add to sql
if hmmdb:
log.info("Starting Core Gene hmmsearch... (%s)"%(hmmdb))
hmmdomrslt=runhmmer(tdir+"queryseqs.faa",hmmdb,tdir,cut=defaultcut,mcpu=mcpu)
log.info("Adding hmmresults...")
makehmmsql.run(hmmdomrslt,tdir+"queryseqs.db",ev=0.1)
else:
log.error("No hmm models found")
raise IOError
### EXTRACT Core and Count duplicates and check proximity
log.info("Extracting core genes...")
# corelist = getcoregenes.writeall(tdir+"coregenes/",tdir+"queryseqs.db")
rslt = {}
corelist = {}
try:
# corelist = getcoregenes.writeall(tdir+"coregenes/",tdir+"queryseqs.db",filt2=cut)
corelist = extractdbgenes.writeall(tdir+"coregenes/",tdir+"queryseqs.db",filt2=cut)
refgm = parsegmatrix(refdir + "genematrix.txt")
log.debug("refgm::::%s"%refgm)
genematrix = parsegmatrix(tdir+"coregenes/"+"genematrix.txt",avgs=False)
log.debug("refgm::::%s"%genematrix)
rslt["singles"] = list(refgm.get("_singles",set())&genematrix.get("_singles",set()))
log.info("Found %s single copy genes for MLST: %s"%(len(rslt["singles"]),rslt["singles"]))
rslt["duplicates"] = list(getdupgenes(refgm,genematrix))
rslt["orgs"] = list(genematrix["_orgs"])+list(refgm["_orgs"])
log.info("Found %s duplicate genes"%len(rslt["duplicates"]))
if "_singles" in genematrix:
del genematrix["_singles"]
if "_orgs" in genematrix:
del genematrix["_orgs"]
rslt["matrix"] = genematrix
#check core gene and known resistance factor proximity to BGC clusters
log.info("Writing duplicates table...")
#write out display tables
writeduptable(rslt["matrix"],rslt["duplicates"],corelist,modeldata,os.path.join(tdir,"tables","duptable"))
except Exception as e:
log.error("Could not extract coregenes")
log.exception("exception")
try:
log.info("Writing bgc and core tables...")
bgchits,rslt["proximity"],numhits = checkbgcprox(clusters,gtypes=["Core","ResModel","DUF"],glist=[corelist,knownhits,dufhits],modeldata=modeldata)
log.info("Proximity hits found: %s"%numhits)
writebgctable(bgchits,os.path.join(tdir,"tables","bgctable"))
writecoretable(corelist,rslt,knownhits,modeldata,os.path.join(tdir,"tables","coretable"))
log.info("Milestone_2_complete")
except Exception as e:
log.error("Could not write output tables")
log.exception("exception")
try:
## Phylogeny check
if refdir and "phyl" in options.lower() and "singles" in rslt and len(rslt["singles"]):
if prebuilttrees and os.path.isdir(prebuilttrees):
for pbt in glob.glob(os.path.join(os.path.realpath(prebuilttrees),"*.tree")):
with open(pbt,"r") as ifil, open(tdir+"trees/%s.tree"%os.path.splitext(os.path.split(pbt)[-1])[0],"w") as ofil:
x = ifil.readline() #x = ifil.next()
ofil.write(re.sub("\[I\d+?\]|\"|'|QUERY___","",x))
else:
### Build trees
flist = [os.path.split(x)[-1] for x in glob.glob(tdir+"coregenes/*.fna") if not x.endswith("_rRNA.fna") or x.endswith("RNA_16S_rRNA.fna")] #include 16s seqs
if mcpu > 1:
pool = mp.Pool(mcpu)
for fname in flist:
pool.apply_async(buildtrees, args=(refdir, tdir, fname))
pool.close()
pool.join()
else:
for fname in flist:
buildtrees(refdir, tdir, fname)
log.info("Milestone_3_complete")
### Make Species tree (Astral Coalescent)
tlist = [tdir+"trees/%s.tree"%x for x in rslt["singles"]]
if os.path.isfile(tdir+"trees/RNA_16S_rRNA.tree"):
tlist.append(tdir+"trees/RNA_16S_rRNA.tree")
sptree = makesptree(tlist,tdir,astjar=astjar)
log.info("Milestone_4_complete")
if not sptree:
log.error("No species tree, terminating tree comparison")
log.info("Milestone_5_incomplete")
else:
try:
#Get RANGER-DTL results
log.info("Starting RangerDTL comparison for %s..."%queryorg)
tlist = [os.path.realpath(x) for x in glob.glob(tdir+"trees/*.tree")]
treedict,sptree = ete3methods.mergetrees(sptree,tlist,queryorg)
orgrecs = rangerdtl.runallrdtl(sptree,queryorg,treedict,mcpu)
with open(tdir+"dtlresults.json","w") as fil:
json.dump(orgrecs,fil,indent=2)
#Update genematrix with list of incongruent phylogeny
rslt["phylogeny"], numhits = checkphylogeny(orgrecs,queryorg,querygenus)
log.info("Phylogeny hits found: %s"%numhits)
# with open(tdir+"dtl_orgrecs.json","w") as fil:
# json.dump(orgrecs,fil,indent=2)
# with open(tdir+"resultsummary.json","w") as fil:
# json.dump(rslt,fil,indent=2)
#
# log.info("Rendering trees...")
# for x in os.listdir(tdir+"trees"):
# ete3methods.renderpng(os.path.join(tdir+"trees",x),800,queryorg)
except Exception as e:
log.error("Ranger-dtl failed, phylogeny check unreliable")
log.exception("exception in rangerdtl step")
else:
log.info("No single genes found or phylogeny option unset, skipping phylogeny Check...")
except Exception as e:
log.error("Problem with phylogeny check")
log.exception("exception")
log.info("Milestone_5_complete")
log.info("Exporting results...")
try:
if os.path.exists(tdir+"astMLST.tree"):
os.rename(tdir+"astMLST.tree",tdir+"trees/SpeciesMLST.tree")
if len(glob.glob(os.path.join(tdir,"trees","*.tree"))):
shutil.make_archive(tdir+"alltrees","zip",tdir+"trees")
shutil.rmtree(os.path.join(tdir,"raxml")) #Save disk space
if len(glob.glob(os.path.join(tdir,"trimmedcore","*.fna"))):
shutil.make_archive(tdir+"aligned_core_genes","zip",tdir+"alignedcore")
shutil.rmtree(os.path.join(tdir,"alignedcore")) #Save disk space
shutil.rmtree(os.path.join(tdir,"trimmedcore")) #Save disk space
for x in [f for f in os.listdir(tdir) if f.startswith("queryseqs") or f.startswith("domrslt") or f.endswith(".hmm")]:
os.remove(os.path.join(tdir,x)) #Save disk space
except Exception as e:
log.error("Problem with export")
log.exception("exception")
#count multi criteria hits
twoplus = set()
threeplus = set()
if "phylogeny" in rslt and "proximity" in rslt:
twoplus |= set(rslt["phylogeny"].keys())&set(rslt["proximity"])
if "phylogeny" in rslt and "duplicates" in rslt:
twoplus |= set(rslt["phylogeny"].keys())&set(rslt["duplicates"])
if "proximity" in rslt and "duplicates" in rslt:
twoplus |= set(rslt["proximity"])&set(rslt["duplicates"])
if "phylogeny" in rslt and "proximity" in rslt and "duplicates" in rslt:
threeplus = set(rslt["phylogeny"].keys())&set(rslt["proximity"])&set(rslt["duplicates"])
log.info("Hits with two or more criteria: %s : %s"%(len(twoplus),twoplus))
log.info("Hits with three or more criteria: %s : %s"%(len(threeplus),threeplus))
#update table
writecoretable(corelist,rslt,knownhits,modeldata,os.path.join(tdir,"tables","coretable"))
log.info("SUCCESS! job finished")
return True
def parse_input_orgs(input):
c = 0
final_input_list = []
if "," in input:
input_list = input.split(",")
for i in input_list:
if i in final_input_list:
c += 1
i_final = i + "_" + str(c)
final_input_list.append(i_final)
else:
final_input_list.append(i)
return final_input_list
else:
final_input_list = [input]
return final_input_list
def run_bigscape(antismash_result_directories, bspath, maindir):
combined_log.info("bigscape run start")
all_antismash_results = os.path.join(maindir , "all_antismash")
result_directories = antismash_result_directories
regions_to_filename_dict = {}
if os.path.exists(all_antismash_results) != True:
os.mkdir(all_antismash_results)
for directory in result_directories:
antismash_results_path = os.path.join(directory, "antismash")
copy_tree(antismash_results_path, os.path.join(all_antismash_results, os.path.basename(directory)))
for file in os.listdir(antismash_results_path):
if (".region" in file or ".cluster" in file) and ".gbk" in file:
full_file_path = os.path.join(os.path.join(directory, "antismash"), file)
regions_to_filename_dict[full_file_path] = result_directories[directory]
bigscape_result_folder = os.path.join(maindir , "antismash_bigscape_result")
cmd = "python3 %s -i %s -o %s --mix" % (bspath, all_antismash_results, bigscape_result_folder)
try:
subprocess.call([cmd], shell=True)
except Exception as e:
combined_log.error("Bigscape couldn't run")
combined_log.info("bigscape run saved to: %s"%bigscape_result_folder)
return bigscape_result_folder, regions_to_filename_dict
# def create_symlinks(result_dict, maindir):
# for result_path in result_dict:
# os.symlink(result_path, os.path.join(os.path.abspath(os.path.join(maindir, os.pardir)), os.path.basename(result_path)))
def write_paths_to_file(result_dict,maindir):
path_file = open(os.path.join(maindir,"all_paths"), "w")
path_file.write("#Result Directory\tInput File\n")
for result_path in result_dict:
input = result_dict[result_path]
path_file.write(result_path + "\t" + input + "\n")
path_file.close()
def call_startquery(args):
#parse input function expects a string of comma seperated organisms as in "org1,org2,org3" or if its just one organism "org1"
input_list = parse_input_orgs(args.input)