-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
1256 lines (1110 loc) · 51 KB
/
main.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
from posixpath import basename
from Bio import Entrez
from xml.dom.minidom import parse
from Bio.Align import MultipleSeqAlignment
from Bio.Entrez.Parser import ValidationError
from Bio.Entrez.Parser import CorruptedXMLError
import requests
import io
import pandas as pd
from pandas.errors import EmptyDataError
import re
import os
from copy import deepcopy
import subprocess
import sys
from urllib.error import HTTPError
from urllib.error import URLError
import time
import ftplib
from io import BytesIO
import ftplib
import gzip
import zlib
import json
import argparse
from numpy import nan
from Bio import SeqIO
import glob
import django
import tastypie
from tastypie import http
from urllib.request import urlopen
from socket import timeout
import http
from http import client
from http.client import IncompleteRead
import Bio
import matplotlib.pyplot as plt
import datetime
from subprocess import Popen
from subprocess import TimeoutExpired
import concurrent.futures
import requests
from requests.adapters import HTTPAdapter, Retry
import shutil
########### arguments ###########
parser = argparse.ArgumentParser(description="BGCSniffer")
parser.add_argument("-json", "--jsonfile", dest="jsonfile", help= "Please input the complete path of parameter file (JSON format)")
parser.add_argument("-workdir", "--workdir", dest="workdir", help= "Please input your working directory")
parser.add_argument("-database", "--database",dest="database",help="The directory of the genome database and GBF database")
args = parser.parse_args()
########### arguments ###########
########### functions ###########
def readPara(Parafile):
with open (Parafile) as Para:
parameters = json.load(Para)
return(parameters)
def extract_query_info(query_string):
id_list = []
if ('and' in query_string):
query_list = re.split(' and ', query_string)
for parenthesis_ele in query_list:
parenthesis_ele = parenthesis_ele.replace('(', '')
parenthesis_ele = parenthesis_ele.replace(')', '')
if 'AND' in parenthesis_ele:
for id in re.split(' AND ', parenthesis_ele):
id_list.append(id)
else:
for id in re.split(' OR ', parenthesis_ele):
id_list.append(id)
elif ('or' in query_string):
query_list = re.split(' or ', query_string)
for parenthesis_ele in query_list:
parenthesis_ele = parenthesis_ele.replace('(', '')
parenthesis_ele = parenthesis_ele.replace(')', '')
if 'AND' in parenthesis_ele:
for id in re.split(' AND ', parenthesis_ele):
id_list.append(id)
else:
for id in re.split(' OR ', parenthesis_ele):
id_list.append(id)
else:
query_list = re.split(' or ', query_string)
for parenthesis_ele in query_list:
parenthesis_ele = parenthesis_ele.replace('(', '')
parenthesis_ele = parenthesis_ele.replace(')', '')
if 'AND' in parenthesis_ele:
for id in re.split(' AND ', parenthesis_ele):
id_list.append(id)
else:
for id in re.split(' OR ', parenthesis_ele):
id_list.append(id)
return(id_list)
def get_next_link(headers, re_next_link):
if "Link" in headers:
match = re_next_link.match(headers["Link"])
if match:
return match.group(1)
def get_batch(batch_url, s, re_next_link):
while batch_url:
response = s.get(batch_url)
response.raise_for_status()
total = response.headers["x-total-results"]
yield response, total
batch_url = get_next_link(response.headers, re_next_link)
def searchUniprot(pf_id):
re_next_link = re.compile(r'<(.+)>; rel="next"')
retries = Retry(total=5, backoff_factor=0.25, status_forcelist=[500, 502, 503, 504])
session = requests.Session()
session.mount("https://", HTTPAdapter(max_retries=retries))
BASE = 'http://rest.uniprot.org'
KB_ENDPOINT = '/uniprotkb/'
QUERY_ID = pf_id
UNIPROT_URL = BASE + KB_ENDPOINT + 'search?query=%28' + QUERY_ID + '%20AND%20%28taxonomy_id%3A2%29%29&fields=accession,id,reviewed,protein_name,gene_names,organism_name,organism_id,length,sequence&format=tsv&size=500'
progress = 0
with open('query_res_tmp.tsv', 'w') as f:
for batch, total in get_batch(UNIPROT_URL, session, re_next_link):
lines = batch.text.splitlines()
if not progress:
print(lines[0], file=f)
for line in lines[1:]:
print(line, file=f)
progress += len(lines[1:])
print(f'{progress} / {total}')
uniprot_df = pd.read_csv('query_res_tmp.tsv', sep="\t")
uniprot_df['ProteinFamily'] = pf_id
return(uniprot_df)
# uniprot_result = requests.get(UNIPROT_URL)
# if uniprot_result.ok:
# content = uniprot_result.text
# content = io.StringIO(content)
# uniprot_df = pd.read_csv(content, sep="\t", index_col=0)
# uniprot_df['ProteinFamily'] = pf_id
# return(uniprot_df)
# else:
# return('Something went wrong in Interpro searching ' + uniprot_result.status_code)
def findMutualProtein(queryString, combined_df):
if re.search('and', queryString):
queryString_list = re.split(' and ', queryString)
mutual_species_id = set(combined_df['Organism (ID)'].to_list())
for block in queryString_list:
block = block.replace('(', '')
block = block.replace(')', '')
tmp_block_list = []
for element in re.split(' OR ', block):
tmp_speciesID = combined_df[combined_df['ProteinFamily'] == element]['Organism (ID)'].to_list()
tmp_block_list.extend(tmp_speciesID)
tmp_block_list = list(set(tmp_block_list))
mutual_species_id = mutual_species_id.intersection(tmp_block_list)
filtered_df = combined_df[combined_df['Organism (ID)'].isin(list(mutual_species_id)) & ~combined_df['Organism'].isin(['uncultured bacterium','bioreactor metagenome'])]
else:
filtered_df = combined_df
return(filtered_df)
def downloadGenome(dataframe, work_dir,refdatabase):
species_id_list = []
organism_list = []
protein_family_info = {}
for row in range(0, dataframe.shape[0]):
species_name = dataframe['Organism'].iloc[row,]
try:
genus = re.split(' ', species_name)[0]
species = re.split(' ', species_name)[1]
#query_name = ' '.join([genus,species])
protein_ID = dataframe.iloc[row,].name
#protein_sequence = merged_df['Sequence'].iloc[row,]
organism = dataframe['Organism'].iloc[row,]
organism_id = dataframe['Organism (ID)'].iloc[row,]
## Get protein family information for each protein
proteinfamily_type = dataframe['ProteinFamily'].iloc[row,]
protein_family_info[protein_ID] = proteinfamily_type
species_id_list.append(str(organism_id))
organism_list.append(organism)
except IndexError:
next
# Retrieving genomes from the Entrez databases
if os.path.exists(work_dir +'/Candidate_genomes/'):
os.system("rm -rf " + work_dir + "/Candidate_genomes/*")
os.makedirs(work_dir + "/Candidate_genomes/", exist_ok = True)
else:
os.makedirs(work_dir + "/Candidate_genomes/", exist_ok = True)
content = '\n'.join(list(set(species_id_list)))
with open(os.path.join(work_dir ,'strains_ID.txt'), 'w+') as out:
out.write(content)
## 根据输入文件下载Refseq和genebank
# subprocess.run(['ncbi-genome-download', '--taxids' , os.path.join(work_dir,'strains_ID.txt'),
# 'bacteria' ,'--assembly-levels', 'complete,chromosome',
# '--flat-output','--parallel', '10','-r' , '10', '-o' ,
# work_dir + '/Candidate_genomes/', '-s', 'genbank', '-v',
# '-d'], stdout=subprocess.PIPE)
# subprocess.run(['ncbi-genome-download', '--taxids' , os.path.join(work_dir,'strains_ID.txt'),
# 'bacteria' ,'--assembly-levels', 'complete,chromosome',
# '-F' ,'fasta,genbank' ,'--flat-output','--parallel', '30', '-r' ,
# '50','-o' , work_dir + '/Candidate_genomes/', '-v', '-d'], stdout=subprocess.PIPE)
DatabaseFile = {}
with open(refdatabase,'r') as file:
for line in file:
(taxID,Genome,Gbff) = line.strip().split('\t')
if taxID in DatabaseFile.keys():
DatabaseFile[taxID].append(Genome)
DatabaseFile[taxID].append(Gbff)
else:
DatabaseFile[taxID] = [Genome,Gbff]
CandidateDir = os.path.join(work_dir,'Candidate_genomes/')
for target in list(set(species_id_list)):
if target in DatabaseFile.keys():
print('--' + str(target))
for gzfile in DatabaseFile[target]:
#shutil.copy(gzfile, CandidateDir)
if 'fna.gz' in gzfile:
ExtractPath = os.path.join(CandidateDir,os.path.basename(gzfile)[:-3])
with gzip.open(gzfile, 'rb') as gz_file, open(ExtractPath, 'wb') as output_file:
time.sleep(2)
shutil.copyfileobj(gz_file, output_file)
else:
time.sleep(2)
shutil.copy(gzfile, CandidateDir)
## 解压基因组fasta文件
# for gz_file in glob.glob(work_dir + '/Candidate_genomes/*genomic.fna.gz'):
# unzip_status = subprocess.run(['gunzip','-f', gz_file], stdout=subprocess.PIPE)
# if unzip_status.returncode:
# print(gz_file + ' unzip failed!!')
# subprocess.run(["rm", work_dir + "/Candidate_genomes/" + gz_file], shell=True)
# next
# else:
# pass
## 检查是否每一个解压后的基因组都有Refseq和对应genebank文件
# for refseq in glob.glob(work_dir + '/Candidate_genomes/*genomic.fna'):
# file_name = os.path.basename(refseq)
# Refseq_ID = re.split('_',file_name)[0:2]
# Refseq_ID = '_'.join(Refseq_ID)
# pattern = work_dir + '/Candidate_genomes/' + Refseq_ID + '*gz'
# file_list = glob.glob(pattern)
# if len(file_list) == 1 :
# pass
# else:
# subprocess.run(['rm', work_dir + '/Candidate_genomes/' + Refseq_ID + '*'])
return [species_id_list, organism_list, protein_family_info]
def downloadProtein(dataframe, work_dir):
dataframe = dataframe.drop_duplicates()
if os.path.exists(work_dir + '/All_proteins.fasta'):
os.system('rm '+ work_dir + '/All_proteins.fasta')
else:
pass
id_list = []
seq_list = []
content_list = []
for tmp_id in dataframe.index.values:
if tmp_id not in id_list:
id_list.append(tmp_id)
try:
tmp_seq = dataframe.loc[tmp_id,'Sequence'].to_list()[0]
seq_list.append(tmp_seq)
content ='>' + tmp_id + '\n' + tmp_seq
content_list.append(content)
except KeyError:
tmp_seq = dataframe.loc[tmp_id,'Sequence'].to_list()
seq_list.append(tmp_seq)
content ='>' + tmp_id + '\n' + tmp_seq
content_list.append(content)
except AttributeError:
tmp_seq = dataframe.loc[tmp_id,'Sequence']
seq_list.append(tmp_seq)
content ='>' + tmp_id + '\n' + tmp_seq
content_list.append(content)
else:
next
proteins = '\n'.join(content_list)
with open (work_dir + '/All_proteins.fasta', 'w+') as p_seq:
p_seq.write(proteins)
def sortDownloadedData(work_dir):
ID_dict = {}
datafolder = os.path.join(work_dir,'Candidate_genomes')
all_fna_files = glob.glob(os.path.join(datafolder, '*fna'))
ID_list = []
for fna_file in all_fna_files:
refseq_file_name = os.path.basename(fna_file)
tmp_folder = os.path.dirname(fna_file)
ID = re.split('_', refseq_file_name)[1]
ID = re.split('.fna', ID)[0]
gbff_file_pattern = os.path.join(tmp_folder, '*'+ ID + '*.gbff.gz')
## Check if relevant gbff file exists, jump to next id if not.
for gbff_file in glob.glob(gbff_file_pattern):
if os.path.isfile(gbff_file):
flag = 1
else:
flag = 0
if flag :
ID_list.append(ID)
else:
next
print(fna_file + ' 无对应gbff文件')
ID_dict['accessionIDnum'] = ID_list
all_strains_id = pd.DataFrame.from_dict(ID_dict)
all_strains_id.to_csv(work_dir + '/All_Strains_for_Antismash.xls', sep = '\t')
return(all_strains_id)
def buildHMMprofile(filtered_query, work_dir, query_string):
if 'and' in query_string or 'or' in query_string:
blocks = re.split(r"\s+(and|or)\s+", query_string)
blocks = [item for item in blocks if item not in ['and', 'or']]
else:
blocks = [query_string]
n=1
for block in blocks:
block = block.replace("(", "").replace(")", "")
pf_list = re.split(' OR ', block)
tmp_filtered_result = filtered_query[filtered_query['ProteinFamily'].isin(pf_list)]
tmp_block_fasta_file_name = work_dir + 'ProteinFamily' + str(n) + '.fasta'
with open(tmp_block_fasta_file_name, 'w') as fasta_file:
for index, row in tmp_filtered_result.iterrows():
entry = row['Entry']
sequence = row['Sequence']
fasta_file.write(f'>{entry}\n')
fasta_file.write(f'{sequence}\n')
n+=1
## 将所有蛋白整合到一起
# complete_fasta = work_dir + '/All_proteins.fasta'
#
# if ' and ' in query_string:
# blocks = re.split(' and ', query_string)
# n = 1
# for block in blocks:
# block = block.replace('(', '')
# block = block.replace(')', '')
# pf_list = re.split(' OR ', block)
# tmp_filtered_result = filtered_query[filtered_query['ProteinFamily'].isin(pf_list)]
# block_protein_list = tmp_filtered_result['Entry'].to_list()
#
# ## 从下载的蛋白序列中抓取block中包含的蛋白序列
#
# tmp_block_fasta_file_name = work_dir + 'ProteinFamily' + str(n) + '.fasta'
# n = n +1
#
# content_list = []
# for record in SeqIO.parse(complete_fasta, "fasta"):
# id = str(record.id)
# if id in block_protein_list:
# content = '>' + id + '\n' + str(record.seq)
# content_list.append(content)
# with open(tmp_block_fasta_file_name, 'w+') as out:
# out.write('\n'.join(content_list))
# out.close()
# else:
# next
#
#
# elif ' or ' in query_string:
# blocks = re.split(' or ', query_string)
# n = 1
# for block in blocks:
# block = block.replace('(', '')
# block = block.replace(')', '')
# pf_list = re.split(' OR ', block)
# tmp_filtered_result = filtered_query[filtered_query['ProteinFamily'].isin(pf_list)]
# block_protein_list = tmp_filtered_result['Entry'].to_list()
# print(pf_list)
# print(tmp_filtered_result)
# print(block_protein_list)
# ## 从下载的蛋白序列中抓取block中包含的蛋白序列
#
# tmp_block_fasta_file_name = work_dir + 'ProteinFamily' + str(n) + '.fasta'
# n = n +1
# content_list = []
# for record in SeqIO.parse(complete_fasta, "fasta"):
# id = str(record.id)
# if id in block_protein_list:
# content = '>' + id + '\n' + str(record.seq)
# content_list.append(content)
# with open(tmp_block_fasta_file_name, 'w+') as out:
# out.write('\n'.join(content_list))
# out.close()
# else:
# next
#
# else:
# n = 1
# query_string = query_string.replace('(', '')
# query_string = query_string.replace(')', '')
# pf_list = re.split(' OR ', query_string)
# tmp_filtered_result = filtered_query[filtered_query['ProteinFamily'].isin(pf_list)]
# block_protein_list = tmp_filtered_result['Entry'].to_list()
#
# ## 从下载的蛋白序列中抓取block中包含的蛋白序列
#
# tmp_block_fasta_file_name = work_dir + 'ProteinFamily' + str(n) + '.fasta'
#
# content_list = []
# for record in SeqIO.parse(complete_fasta, "fasta"):
# id = str(record.id)
# if id in block_protein_list:
# content = '>' + id + '\n' + str(record.seq)
# content_list.append(content)
# with open(tmp_block_fasta_file_name, 'w+') as out:
# out.write('\n'.join(content_list))
# out.close()
# else:
# next
## Reabundance
## Multiple Seq Alignment and Build HMM profile
for block_fasta in glob.glob(work_dir + 'ProteinFamily*.fasta'):
basename = os.path.basename(block_fasta)
dirname = os.path.dirname(block_fasta)
basename = re.split('.fasta',basename)[0]
subprocess.run(['cd-hit','-i',block_fasta,'-o',block_fasta + '.reabun','-T','100','-M','20000','-n','3'])
subprocess.run(['FAMSA/famsa', block_fasta + '.reabun', os.path.join(work_dir, basename + '.st'),'-t','200'])
os.system('hmmbuild --amino ' + os.path.join(dirname, basename + '.hmm ') + ' ' + work_dir + basename + '.st')
def execute_command(command):
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
return command, result.stdout, result.stderr
def run_commands(commands_list, max_workers):
results = []
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = {executor.submit(execute_command, command): command for command in commands_list}
for future in concurrent.futures.as_completed(futures):
command = futures[future]
try:
command, stdout, stderr = future.result()
results.append((command, stdout, stderr))
except Exception as e:
print(f"Error executing command: {command}, {e}")
return results
def runAntismash(query_string, work_dir, space_len,neighbour, n_threads):
## Prepare input files
block_hmm_list = []
hmm_file_name_list =[]
hmm_file_list = []
for block_hmm in glob.glob(os.path.join(work_dir, '*.hmm')):
## Copy hmm file to antismash folder
hmm_file_name = os.path.basename(block_hmm)
hmm_file_list.append(hmm_file_name)
hmm_file_name = re.split('.hmm',hmm_file_name)[0]
#os.system('cp ' + block_hmm + ' ' + hmm_folder + '/data/')
block_hmm_list.append(block_hmm)
hmm_file_name_list.append(hmm_file_name)
## Define rules in txt files
# RULE loose_test
# CATEGORY user_define
# COMMENT test for pilin and sortaseC
# CUTOFF 20
# NEIGHBOURHOOD 8
# CONDITIONS sortaseC and pilin
if 'and' in query_string:
# Loose rules file
content = "RULE" + " " + "loose_rules" + "\n\t"
content = content + "CATEGORY" + " " + "user_defined" + "\n\t"
content = content + "COMMENT" + " " + "user-defined BGC region" + "\n\t"
content = content + "CUTOFF" + " " + str(space_len) + "\n\t"
content = content + "NEIGHBOURHOOD" + " " + str(neighbour) + "\n\t"
content = content + "CONDITIONS" + " " + " and ".join(hmm_file_name_list)
with open (work_dir + '/loose.txt', 'w+') as out:
out.write(content)
# Relaxed rules file
content = "RULE" + " " + "relaxed_rules" + "\n\t"
content = content + "CATEGORY" + " " + "user_defined" + "\n\t"
content = content + "COMMENT" + " " + "user-defined BGC region" + "\n\t"
content = content + "CUTOFF" + " " + str(space_len) + "\n\t"
content = content + "NEIGHBOURHOOD" + " " + str(neighbour) + "\n\t"
content = content + "CONDITIONS" + " " + " and ".join(hmm_file_name_list)
with open (work_dir + '/relaxed.txt', 'w+') as out:
out.write(content)
# Stricted rules file
content = "RULE" + " " + "strict_rules" + "\n\t"
content = content + "CATEGORY" + " " + "user_defined" + "\n\t"
content = content + "COMMENT" + " " + "user-defined BGC region" + "\n\t"
content = content + "CUTOFF" + " " + str(space_len) + "\n\t"
content = content + "NEIGHBOURHOOD" + " " + str(neighbour) + "\n\t"
content = content + "CONDITIONS" + " " + " and ".join(hmm_file_name_list)
with open (work_dir + '/strict.txt', 'w+') as out:
out.write(content)
else:
# Loose rules file
content = "RULE" + " " + "loose_rules" + "\n\t"
content = content + "CATEGORY" + " " + "user_defined" + "\n\t"
content = content + "COMMENT" + " " + "user-defined BGC region" + "\n\t"
content = content + "CUTOFF" + " " + str(space_len) + "\n\t"
content = content + "NEIGHBOURHOOD" + " " + str(neighbour) + "\n\t"
content = content + "CONDITIONS" + " " + " or ".join(hmm_file_name_list)
with open (work_dir + '/loose.txt', 'w+') as out:
out.write(content)
# Relaxed rules file
content = "RULE" + " " + "relaxed_rules" + "\n\t"
content = content + "CATEGORY" + " " + "user_defined" + "\n\t"
content = content + "COMMENT" + " " + "user-defined BGC region" + "\n\t"
content = content + "CUTOFF" + " " + str(space_len) + "\n\t"
content = content + "NEIGHBOURHOOD" + " " + str(neighbour) + "\n\t"
content = content + "CONDITIONS" + " " + " or ".join(hmm_file_name_list)
with open (work_dir + '/relaxed.txt', 'w+') as out:
out.write(content)
# Stricted rules file
content = "RULE" + " " + "strict_rules" + "\n\t"
content = content + "CATEGORY" + " " + "user_defined" + "\n\t"
content = content + "COMMENT" + " " + "user-defined BGC region" + "\n\t"
content = content + "CUTOFF" + " " + str(space_len) + "\n\t"
content = content + "NEIGHBOURHOOD" + " " + str(neighbour) + "\n\t"
content = content + "CONDITIONS" + " " + " or ".join(hmm_file_name_list)
with open (work_dir + '/strict.txt', 'w+') as out:
out.write(content)
## Get path of antismash
antismash = subprocess.run(['which', 'antismash'], stdout=subprocess.PIPE)
if not antismash.returncode:
antismash= antismash.stdout.decode('utf-8')
antismash = antismash.replace('\n', '')
antismash = os.path.dirname(antismash)
path = subprocess.run(['find', '/opt/', '-name', 'hmm_detection'], stdout=subprocess.PIPE)
if not path.returncode:
path = path.stdout.decode('utf-8')
path = path.replace('\n', '')
else:
print("Error found when looking for hmm_detection folder in your docker image, please make sure it's installed correctedly!!")
else:
print("Error found when looking for antismash in your docker image, please make sure it's installed correctedly!!")
if os.path.isdir(os.path.join(os.path.dirname(path), 'hmm_detection_backup/')):
pass
else:
os.system("cp -r {0} {1}".format(path, os.path.join(os.path.dirname(path), 'hmm_detection_backup/')))
## Write hmmdetails file
content_list = []
for proteinfamily in hmm_file_name_list:
content = proteinfamily + "\t" + proteinfamily + "\t" + str(space_len) + "\t" + proteinfamily + '.hmm'
content_list.append(content)
all_content = '\n'.join(content_list)
with open(os.path.join(path, 'data','hmmdetails.txt'), 'a+') as HMMDETAIL:
HMMDETAIL.write(all_content)
os.system('cp ' + os.path.join(path, 'data','hmmdetails.txt') + " " + work_dir)
## Write category json file
category_dict = {
"user_defined": {
"description": "user-defined BGC region",
"version": 1}
}
with open(work_dir + "/categories.json","w+") as f:
json.dump(category_dict,f,indent = 4)
## Copy files
os.system('cp ' + work_dir + "/categories.json " + os.path.join(path, 'data'))
os.system('cp ' + work_dir + "strict.txt " + os.path.join(path, 'cluster_rules'))
os.system('cp ' + work_dir + "relaxed.txt " + os.path.join(path, 'cluster_rules'))
os.system('cp ' + work_dir + "loose.txt " + os.path.join(path, 'cluster_rules'))
os.system('cp ' + work_dir + "/*hmm " + os.path.join(path, 'data'))
print('Check param files in antismash')
os.system('ls ' + os.path.join(path, 'data','*hmm'))
os.system('ls ' + os.path.join(path, 'cluster_rules','*'))
print("yes")
## run antismash
ID_list = []
candidate_gbk_file_list = glob.glob(os.path.join(work_dir,'Candidate_genomes','*.gbff.gz'))
for candidate_gbk_file in candidate_gbk_file_list:
file_name = os.path.basename(candidate_gbk_file)
ID = re.split('_', file_name)[0:2]
ID = '_'.join(ID)
refseq_accession = 'GCF_'+str(ID)
os.makedirs(os.path.join(work_dir, 'Anitismash_Result', refseq_accession), exist_ok = True)
ID_list.append(ID)
input = zip(ID_list, candidate_gbk_file_list)
command_list = []
for ID, input_file in input:
refseq_accession = 'GCF_'+str(ID)
command = ' '.join(['antismash', '--hmmdetection-strictness','strict' , '--genefinding-tool prodigal', '-c', '10', '--taxon', 'bacteria', input_file , '--output-dir', os.path.join(work_dir, 'Anitismash_Result', refseq_accession)])
with open (os.path.join(work_dir, 'antismash.sh' ), 'a') as file:
file.write(command + '\n')
command_list.append(command)
max_parallel_tasks = 40 # 设置最大并行任务数
results = run_commands(command_list, max_parallel_tasks)
with open(os.path.join(work_dir,'Antismash.log'),'a') as file:
for command, stdout, stderr in results:
file.write(f"Command: {command}\n")
file.write(f"Standard Output:\n{stdout}\n")
file.write(f"Standard Error:\n{stderr}\n")
file.write("=" * 30)
for i in range(0, len(command_list), n_threads):
tmp_command_list = command_list[i:i+n_threads]
procs = [ Popen(i, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) for i in tmp_command_list]
for p in procs:
try:
outs, errs = p.communicate(timeout = 400)
if errs:
print(errs.decode('ascii'))
except TimeoutExpired:
p.kill()
outs, errs = p.communicate()
print(errs.decode('ascii'))
def summaryAntismashResult(Result_folder, work_dir):
all_strain_dict = {}
strainID2ncID = {}
assemblyID2ncID = {}
assemblyIDnumer = {}
n=0
for root, subdir, files in os.walk(Result_folder):
n+=1
print(str(n) + '\r', end='', flush=True)
for file in files:
if re.search('region', file):
if file.endswith('gbk'):
region_id = os.path.basename(file)
strain_id = os.path.basename(root)
region_id = re.split('.gbk',region_id )[0]
region_dict = {}
file_path = os.path.join(root,file)
for seq_record in SeqIO.parse(file_path, "genbank"):
organism = seq_record.description
organism = re.split(',', organism)[0]
organism = organism.replace(' plasmid', '')
organism = organism.replace(' chromosome', '')
## 获得taxonomy信息
try:
if len(seq_record.annotations['taxonomy']) < 6:
genus = seq_record.annotations['taxonomy'][4] ## Genus level,某些物种缺失某些层级信息
else:
genus = seq_record.annotations['taxonomy'][5] ## Genus level
except IndexError:
genus = seq_record.annotations['taxonomy'][-1]
print(file_path)
print('文件物种层级存在一些问题,请检查')
print(seq_record.annotations['taxonomy'])
species = ' '.join(re.split(' ', organism)[0:2])
ID = seq_record.id
for block_hmm in glob.glob(os.path.join(work_dir, '*.hmm')):
block_name_num = 0
block_name = os.path.basename(block_hmm)
block_name = re.split('.hmm', block_name)[0]
for feature in seq_record.features:
if feature.type == "CDS":
try:
for function in feature.qualifiers['gene_functions']:
if re.search(block_name, function):
block_name_num = block_name_num + 1
else:
next
except KeyError:
next
else:
next
region_dict[block_name] = block_name_num
region_dict['organism'] = organism
region_dict['genus'] = genus
region_dict['ID'] = ID
region_dict['species'] = species
#region_dict['StrainID']= strain_id
region_dict['regionID'] = region_id
all_strain_dict[region_id] = region_dict
else:
next
else:
if file.endswith('gbk'):
AssemblyID_backup = re.split('_',file)
AssemblyID_number = AssemblyID_backup[1]
AssemblyID_backup = AssemblyID_backup[0:2]
AssemblyID_backup = '_'.join(AssemblyID_backup)
#AssemblyID_backup = AssemblyID_backup.replace('.gbk', '')
file_path = os.path.join(root,file)
for seq_record in SeqIO.parse(file_path, "genbank"):
ID = seq_record.id
for feature in seq_record.features:
try:
content = ''.join(feature.qualifiers.get("db_xref"))
if re.search('taxon', content):
taxon = content
taxonID = re.split(':',taxon )[-1]
else:
next
except TypeError:
next
strainID2ncID[ID] = taxonID
Assembly_ID = AssemblyID_backup
assemblyID2ncID[ID] = Assembly_ID
assemblyIDnumer[ID] = AssemblyID_number
else:
next
## 获得物种基因组序列ID对应的taxaID
Stat_df = pd.DataFrame(all_strain_dict).T
Stat_df['TaxonID'] = nan
Stat_df['AssemblyID'] = nan
Stat_df['AssemblyIDnum'] = nan
Stat_df['Genus_branches'] = nan
## 增加taxonID和AssemblyID信息
Assembly_id_list = []
for key in strainID2ncID.keys():
Stat_df[Stat_df['ID'] == key] = Stat_df[Stat_df['ID'] == key].assign(TaxonID = strainID2ncID[key])
Stat_df[Stat_df['ID'] == key] = Stat_df[Stat_df['ID'] == key].assign(AssemblyID = assemblyID2ncID[key])
Stat_df[Stat_df['ID'] == key] = Stat_df[Stat_df['ID'] == key].assign(AssemblyIDnum = assemblyIDnumer[key])
Assembly_id_list.append(assemblyID2ncID[key])
Assembly_id_list = list(set(Assembly_id_list))
# with open ('strain_list.txt', 'w') as strain:
# content = '\n'.join(Assembly_id_list)
# strain.write(content)
# strain.close
## 获取构建进化树的AssemblyID
Tree_genome_list = []
Tree_genome_dict = {}
tree_content = []
for Genus in list(set(Stat_df['genus'].to_list())):
tmp_df = Stat_df[Stat_df['genus'] == Genus]
species_num = len(set(tmp_df['species'].to_list()))
species_num = int(species_num)
Stat_df[Stat_df['genus'] == Genus] = Stat_df[Stat_df['genus'] == Genus].assign(**{"Genus_branches" : species_num})
ref_assembly_id = tmp_df['AssemblyID'].to_list()[0]
Tree_genome_list.append(ref_assembly_id)
Tree_genome_dict[ref_assembly_id] = Genus
Stat_df.to_csv(os.path.join(work_dir,'BGC_stat.xls'), sep = '\t')
for key in Tree_genome_dict.keys():
line = key + '\t' + Tree_genome_dict[key]
tree_content.append(line)
with open (os.path.join(work_dir, 'tree_genomeIDs.txt'), 'w') as genomes:
content = '\n'.join(tree_content)
genomes.write(content)
genomes.close
def StrainClassification(BGC_stat, db_file, work_dir):
df = BGC_stat
db = pd.read_csv(db_file,sep='\t',encoding = "ISO-8859-1",dtype=str)
df = df.drop_duplicates()
df = df.reset_index()
df['Type'] = nan
for assemblyid in list(set(df['AssemblyID'].to_list())):
assemblyid_trans = assemblyid.replace('GCF_', '')
strain_name = df[df['AssemblyID'] == assemblyid]['organism'].to_list()[0]
strain_name = re.sub('genome*assembly','',strain_name)
strain_name = re.sub('complete*genome','',strain_name)
strain_name = re.sub('DNA','',strain_name)
strain_name = re.sub('draft*genome','',strain_name)
strain_name = re.sub('genome','',strain_name)
strain_name = re.sub('assembly','',strain_name)
strain_name = strain_name.rstrip()
strain_name = re.sub(' +', ' ', strain_name)
strain_name = re.sub('\(','.',strain_name)
strain_name = re.sub('\)','.',strain_name)
strain_name = re.sub('[-_]','.',strain_name)
strain_name = re.sub(r'\s+','.',strain_name)
taxid = df[df['AssemblyID'] == assemblyid]['TaxonID'].to_list()[0]
## 根据assemblyID注释
if db['assembly_accession'].str.contains(assemblyid_trans, na=False).values.any():
df[df['AssemblyID'] == assemblyid] = df[df['AssemblyID'] == assemblyid].assign(**{'Type':db[db['assembly_accession'].str.contains(assemblyid_trans, na=False)]['Type'].to_list()[0]})
elif db[db['genome_name'].str.contains(strain_name, regex=True,case=False) == True].shape[0] > 0:
## 根据菌株名字注释
type = db[db['genome_name'].str.contains(strain_name, regex=True,case=False) == True]['Type'].to_list()[0]
df[df['AssemblyID'] == assemblyid] = df[df['AssemblyID'] == assemblyid].assign(**{'Type':type})
else:
df[df['AssemblyID'] == assemblyid] = df[df['AssemblyID'] == assemblyid].assign(**{'Type':'Others'})
df['Pathogen'] = ''
df['Industrial workhorse'] = ''
df['others'] = ''
for row in range(0,df.shape[0]):
if df.loc[row,'Type'] == 'Pathogen':
df.loc[row,'Pathogen'] = 'yes'
df.loc[row,'Industrial workhorse'] = 'no'
df.loc[row,'others'] = 'no'
elif df.loc[row,'Type'] == 'Industrial workhorse':
df.loc[row,'Pathogen'] = 'no'
df.loc[row,'Industrial workhorse'] = 'yes'
df.loc[row,'others'] = 'no'
else:
df.loc[row,'others'] = 'yes'
df.loc[row,'Pathogen'] = 'no'
df.loc[row,'Industrial workhorse'] = 'no'
df.to_csv(os.path.join(work_dir, 'Strain_classification.xls'), sep = '\t')
Block_names = []
for col in list(df.columns):
if 'ProteinFamily' in col:
Block_names.append(col)
else:
next
for row in range(0,df.shape[0]):
Patter_num_list = []
for tmp_name in Block_names:
tmp_num = df.loc[row, tmp_name]
Patter_num_list.append(str(tmp_num))
pattern = ','.join(Patter_num_list)
df.loc[row,'BGCPattern'] = pattern
df.to_csv(os.path.join(work_dir, 'Strain_classification.xls'), sep = '\t')
d = {}
count = 1
d['BGCPattern_Name'] = []
d['Pattern'] = []
d['BGCPattern number'] = []
for temp_str in list(set(df['BGCPattern'])):
d['BGCPattern_Name'].append('BGCPattern'+str(count))
d['Pattern'].append(temp_str)
d['BGCPattern number'].append(df[df['BGCPattern'] == temp_str].shape[0])
count = count + 1
pattern_df = pd.DataFrame(d)
pattern_df.to_csv(work_dir + 'Pattern_stat.xls', sep= '\t')
## 绘制pielplot
labels = list(set(df['Type'].to_list()))
sizes = [(len(df[df['Type'] == x]['AssemblyID'].drop_duplicates().to_list())/(len(df['AssemblyID'].drop_duplicates().to_list()))) * 100 for x in labels]
#fig, ax = plt.subplots()
#ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
#patches, texts = plt.pie(sizes, startangle=90)
colours = {
'Pathogen':'C0',
'Industrial workhorse':'C1',
'Others':'C2'
}
patches, texts, _ = plt.pie(sizes, autopct='%1.1f%%', colors=[colours[key] for key in labels], startangle=90)
texts[0].set_fontsize(4)
plt.legend(patches, labels, loc="best")
plt.axis('equal')
plt.savefig(work_dir + 'classification.png', format = 'png', dpi = 250)
# Construct tree metadata
for row in range(0,df.shape[0]):
pattern = df.loc[row,'BGCPattern']
for p in pattern_df['BGCPattern_Name']:
tmp_pattern = pattern_df[pattern_df['BGCPattern_Name'] == p]['Pattern'].to_list()[0]
if pattern == tmp_pattern:
df.loc[row,'BGCPatternID'] = p
else:
next
for i in pattern_df['BGCPattern_Name']:
df[i] = nan
for assemblyid in list(set(df['AssemblyID'].to_list())):
tmp_df = df[df['AssemblyID'] == assemblyid]
tmp_pattern_lsit = tmp_df['BGCPatternID'].to_list()
for i in pattern_df['BGCPattern_Name']:
count = tmp_pattern_lsit.count(i)
df[df['AssemblyID'] == assemblyid] = df[df['AssemblyID'] == assemblyid].assign(**{i:count})
df.to_csv(os.path.join(work_dir, 'Tree_metadata.xls'), sep = '\t')
def buildPhylogenetic_tree(work_dir, input_genomes_folder_name):
classification_file = os.path.join(work_dir, 'Strain_classification.xls')
classification_df = pd.read_csv(classification_file, index_col=0, sep = '\t', dtype=str)
#annotated_df = classification_df
annotated_df = classification_df[classification_df['others'] != 'yes']
tree_strain_list = list(set(annotated_df['AssemblyIDnum'].to_list()))
if os.path.isdir(os.path.join(work_dir,'StrainPhylogeneticTree/Input')):
pass
else:
subprocess.run(['mkdir','-p',os.path.join(work_dir,'StrainPhylogeneticTree/Input/')])
## Look for input genomes and copy them into folder
input_genomes_folder = subprocess.run(['find', work_dir, '-name', input_genomes_folder_name], stdout=subprocess.PIPE)
if not input_genomes_folder.returncode:
input_genomes_folder = input_genomes_folder.stdout.decode('utf-8')
input_genomes_folder = input_genomes_folder.replace('\n', '')
else:
print('Can\'t find input genomes folder in' + input_genomes_folder_name)
input_tree_list_fna = glob.glob(os.path.join(input_genomes_folder, '*fna'))
input_tree_list_fasta = glob.glob(os.path.join(input_genomes_folder, '*fasta'))
input_tree_list = input_tree_list_fna + input_tree_list_fasta
for input_tree_file in input_tree_list:
print(input_tree_file)
# 获取Input Genomes的NC ID
with open(input_tree_file) as handle:
nc_id_list = []
for record in SeqIO.parse(handle, "fasta"):
if record.id in annotated_df['ID'].to_list():
next
else:
subprocess.run(['cp', input_tree_file, os.path.join(work_dir,'StrainPhylogeneticTree/Input/')])
for strain_id in tree_strain_list:
for fasta_file in glob.glob(os.path.join(work_dir, 'Candidate_genomes') + '/*' + str(strain_id) + '*'):
if fasta_file.endswith('fasta') or fasta_file.endswith('fna'):
file_size = os.path.getsize(fasta_file)
if file_size == 0:
next
else:
subprocess.run(['cp', fasta_file, os.path.join(work_dir,'StrainPhylogeneticTree/Input/')])
else:
next
## Create strain tree file
subprocess.run(['JolyTree.sh', '-i', os.path.join(work_dir,'StrainPhylogeneticTree/Input/'),'-b', os.path.join(work_dir,'StrainPhylogeneticTree/','Strain_tree'), '-t', '200'])
def Infer_candidate_strains(work_dir):
annotated_df = pd.read_table(os.path.join(work_dir,'Strain_classification.xls'), sep = '\t',dtype=str)
# 去掉ID栏多余的前缀
newID_list = []
for id in annotated_df['ID'].to_list():
new_id = re.sub(r'.+_', '',id)
newID_list.append(new_id)
annotated_df['ID'] = newID_list
distance_file = glob.glob(work_dir + '/StrainPhylogeneticTree/' + 'Strain_tree' + '.d')
distance_file = distance_file[0]
distance_matrtix = pd.read_csv(distance_file, skiprows = 1, index_col = 0,sep ='\s+',header=None)
newname = []
for i in distance_matrtix.index.values:
newname.append(re.split('_', i)[1])
name = pd.Series(newname)
distance_matrtix = distance_matrtix.set_index(name)
distance_matrtix.columns = newname
for input_tree_file in glob.glob(os.path.join(work_dir,'Input_genomes/*')):
if input_tree_file.endswith('fasta') or input_tree_file.endswith('fna'):
# 获取Input Genomes的NC ID
with open(input_tree_file) as handle:
Assemlbly_ID_list = []
for record in SeqIO.parse(handle, "fasta"):
tmp_id = re.split('_',record.id)[1]
if tmp_id in annotated_df['ID'].to_list():
Assemlbly_ID_num = annotated_df[annotated_df['ID'] == tmp_id]['AssemblyID'].to_list()[0]
Assemlbly_ID_num = re.split('_',Assemlbly_ID_num)[1]
Assemlbly_ID_list.append(Assemlbly_ID_num)
else:
next
else:
next
content = '候选菌株信息:\n'
if len(Assemlbly_ID_list) == 0:
content = '未能找到候选工程菌'
else:
pass
candidate_dict = {}
for pathogen in set(Assemlbly_ID_list):
tmp_pathogen = annotated_df[annotated_df['AssemblyIDnum'] == pathogen]['organism'].to_list()[0]
#tmp_df = annotated_df
tmp_df = annotated_df[annotated_df['Type'] == 'Industrial workhorse']
content = content + tmp_pathogen + '的候选工程菌为:\n'
tmp_distance_series = distance_matrtix.loc[pathogen,]
tmp_distance_series = tmp_distance_series.sort_values(ascending = True)
tmp_candidate = tmp_distance_series.rename('ID')
## 只挑选前10的物种输出到候选菌株信息.txt中