-
Notifications
You must be signed in to change notification settings - Fork 5
/
MYB_annotator.py
1349 lines (1154 loc) · 55.1 KB
/
MYB_annotator.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
### Boas Pucker ###
### b.pucker@tu-bs.de ###
### some functions were copied from KIPEs: https://doi.org/10.3390/plants9091103 and MaMYB: https://doi.org/10.1371/journal.pone.0239275 ###
### WARNING: do not use underscores in the bait MYB IDs ###
__version__ = "v1.0.1"
__usage__ = """
python3 MYB_annotator.py
--baits <MYB_SEQ_FILE>
--info <MYB_CLASSIFICATION_FILE>
--out <OUTPUT_DIR>
--subject <SUBJECT_FILE (peptide,transcript,genomic sequences)> | --subjectdir <SUBJECT_FOLDER_WITH_SEQ_FILES>
optional:
--search <INITIAL_SEARCH_TOOL>(blast|hmmer)[blast]
--mode <TREE_BUILDER>(fasttree|raxml)[fasttree]
--hmm <MYB_BAIT_HMM_FILE>
--refmybs <REF_MYB_FILE>
--ath <ATH_MYB_FILE_FOR_FINAL_TREE>
--name <STRING_USED_AS_PREFIX_IN_FILENAMES>
--collapse <REDUCES IN-PARALOGS_TO_ONE_REPRESENTATIVE>
--motifs <MOTIFS_TO_CHECK_FILE>
--cpu <NUMBER_OF_THREADS>[4]
--cpub <CPUs_TO_USE_FOR_BLASTp>[cpu]
--cpur <CPUs_TO_USE_FOR_RAxML>[cpu]
--cdsinput <CHANGES_EXPECTED_INPUT_TO_CDS>
--keepnames <PREVENTS_CUTTING_OF_NAMES_AT_FIRST_SPACE>
--mafft <PATH_TO_MAFFT>[mafft]
--blastp <PATH_TO_AND_INCLUDING_BINARY>[blastp]
--hmmsearch <PATH_TO_HMMSEARCH>[hmmsearch]
--makeblastdb <PATH_TO_AND_INCLUDING_BINARY>[makeblastdb]
--fasttree <PATH_TO_FASTTREE>[fasttree]
--raxml <PATH_TO_RAXML>[raxml-ng]
--simcutp <BLASTP_SIMILARITY_CUTOFF>[60]
--poscutp <BLASTP_POSSIBLE_HIT_NUMBER_PER_BAIT_CUTOFF>[100]
--lencutp <BLASTP_MIN_LENGTH_CUTOFF>[50]
--numneighbours <NUMBER_OF_NEIGHBOURS_FOR_CLASSIFICATION> [10]
--neighbourdist <NEIGHBOUR_DISTANCE> [3]
--minneighbours <MINIMAL_NUMBER_OF_NEIGHBOURS> [0]
--paralogdist <DISTANCE_OF_PARALOGS_IN_MASKING_STEP> [10]
bug reports and feature requests: b.pucker@tu-bs.de
"""
import os, glob, sys, re, subprocess, dendropy
from operator import itemgetter
try:
import hashlib
except ImportError:
pass
# --- end of imports --- #
def load_BLAST_results( blast_result_file, similarity_cutoff, possibility_cutoff, length_cutoff ):
"""! @brief load BLAST results """
valid_blast_hits = {} #bait IDs as key
with open( blast_result_file, "r" ) as f:
line = f.readline()
while line:
parts = line.strip().split('\t') #qseqid sseqid pident length mismatch gapopen qstart qend sstart send evalue bitscore
if float( parts[2] ) > similarity_cutoff: #similarity is sufficient
if float( parts[3] ) > length_cutoff: #substantial part of query is matched
try:
valid_blast_hits[ parts[1] ].append( { 'gene': parts[0], 'score': float( parts[-1] ) } )
except KeyError:
valid_blast_hits.update( { parts[1]: [ { 'gene': parts[0], 'score': float( parts[-1] ) } ] } )
line = f.readline()
# --- reduce BLAST hit number to given number of candidate possibilities ---- #
final_valid_blast_hits = {}
for key in list(valid_blast_hits.keys()):
hits = sorted( valid_blast_hits[ key ], key=itemgetter( 'score' ) )[::-1]
genes = []
for hit in hits:
if hit['gene'] not in genes:
if len( genes ) < possibility_cutoff:
genes.append( hit['gene'] )
final_valid_blast_hits.update( { key: genes } )
return final_valid_blast_hits
def load_alignment( aln_file, tmp_mapping ):
"""! @brief load alignment and replace query IDs by real sequence names """
sequences = {}
with open( aln_file ) as f:
header = f.readline()[1:].strip()
try:
header = tmp_mapping[ header ]
except KeyError:
pass
seq = []
line = f.readline()
while line:
if line[0] == '>':
sequences.update( { header: "".join( seq ) } )
header = line.strip()[1:]
try:
header = tmp_mapping[ header ]
except KeyError:
pass
seq = []
else:
seq.append( line.strip() )
line = f.readline()
sequences.update( { header: "".join( seq ) } )
return sequences
def alignment_trimming( aln_file, cln_aln_file, occupancy ):
"""! @brief remove all alignment columns with insufficient occupancy """
alignment = load_alignment( aln_file, {} )
# --- if there is an alignment (expected case)
if len( list(alignment.keys()) ) > 0:
# --- identify valid residues in aligned sequences (columns with sufficient occupancy) --- #
valid_index = []
for idx, aa in enumerate( list(alignment.values())[0] ):
counter = 0
for key in list(alignment.keys()):
if alignment[ key ][ idx ] != "-":
counter += 1
if counter / float( len( list(alignment.keys()) ) ) > occupancy:
valid_index.append( idx )
# --- generate new sequences --- #
with open( cln_aln_file, "w" ) as out:
for key in list(alignment.keys()):
seq = alignment[ key ]
new_seq = []
for idx in valid_index:
new_seq.append( seq[ idx ] )
out.write( ">" + key + '\n' + "".join( new_seq ) + '\n' )
# --- just in case the alignment file is empyt (is this possible?) ---#
else:
with open( cln_aln_file, "w" ) as out:
out.write( "" )
def split_into_ingroup_and_outgroup( tree_file, in_list, out_list, neighbour_cutoff, mean_factor_cutoff, min_neighbour_cutoff ):
"""! @brief split subject sequences into intgroup and outgroup based on reference MYBs and MYB-like sequences """
# --- preparation of data structure --- #
groups_around_ref_gene = {}
for gene in ( in_list+out_list ):
groups_around_ref_gene.update( { gene: [] } )
# --- find node objects of reference genes --- #
tree = dendropy.Tree.get_from_path( tree_file, "newick" )
pdm = dendropy.PhylogeneticDistanceMatrix.from_tree( tree )
my_mean_nearest_taxon_distance = pdm.mean_nearest_taxon_distance()
ref_node_objects = {}
for node in tree.taxon_namespace:
try:
groups_around_ref_gene[ node.label ]
ref_node_objects.update( { node.label: node } )
except KeyError:
pass
ref_gene_nodes = []
ref_gene_nodes_dict_to_check = {}
for gene in ( in_list+out_list ):
ref_gene_nodes.append( ref_node_objects[ gene ] )
ref_gene_nodes_dict_to_check.update( { ref_node_objects[ gene ]: None } )
results = {}
for i, t1 in enumerate( tree.taxon_namespace ):
try:
ref_gene_nodes_dict_to_check[ t1 ]
except KeyError: #only run analysis for non-reference sequences
path_distances = []
patristic_distances = {}
for t2 in ref_gene_nodes: #calculate distance to all other sequences in tree
path_distance = pdm.path_edge_count( t1, t2)
patr_distance = pdm.patristic_distance( t1, t2 )
path_distances.append( { 'key': t2.label, 'val': path_distance } )
patristic_distances.update( { t2.label: patr_distance } )
in_counter = 0
out_counter = 0
sorted_distances = sorted( path_distances, key=itemgetter('val') )
for each in sorted_distances[ : min( [ len( path_distances ), neighbour_cutoff ] ) ]:
patr = patristic_distances[ each['key'] ]
if patr < mean_factor_cutoff*my_mean_nearest_taxon_distance: #exclude outliers on extremely long branches
if each['key'] in in_list: #check if smalles path_distances are to in- or outgroup baits
in_counter += 1
else:
out_counter += 1
if in_counter+out_counter > min_neighbour_cutoff:
results.update( { t1.label: { 'score': float( in_counter ) / ( in_counter + out_counter ), 'in': in_counter, 'out': out_counter } } )
else:
results.update( { t1.label: { 'score': 0.0, 'in': in_counter, 'out': out_counter } } )
#score ranges from 0 (non-MYB) to 1 (MYB)
return results
def load_sequences( fasta_file ):
"""! @brief load candidate gene IDs from file """
sequences = {}
with open( fasta_file ) as f:
header = f.readline()[1:].strip()
seq = []
line = f.readline()
while line:
if line[0] == '>':
sequences.update( { header: "".join( seq ) } )
header = line.strip()[1:]
seq = []
else:
seq.append( line.strip() )
line = f.readline()
sequences.update( { header: "".join( seq ) } )
return sequences
def load_bait_MYB_anno( info_file ):
"""! @brief load MYB IDs into two list (in and out) """
in_list, out_list = [], []
with open( info_file, "r" ) as f:
line = f.readline()
while line:
parts = line.strip().split('\t')
if parts[1] == "in":
in_list.append( parts[0] )
else:
out_list.append( parts[0] )
line = f.readline()
return in_list, out_list
def translate( seqs ):
"""! @brief translates the given nucleotide sequence into peptide and splits at each star (stop codon) """
genetic_code = { 'CTT': 'L', 'ATG': 'M', 'AAG': 'K', 'AAA': 'K', 'ATC': 'I',
'AAC': 'N', 'ATA': 'I', 'AGG': 'R', 'CCT': 'P', 'ACT': 'T',
'AGC': 'S', 'ACA': 'T', 'AGA': 'R', 'CAT': 'H', 'AAT': 'N',
'ATT': 'I', 'CTG': 'L', 'CTA': 'L', 'CTC': 'L', 'CAC': 'H',
'ACG': 'T', 'CCG': 'P', 'AGT': 'S', 'CAG': 'Q', 'CAA': 'Q',
'CCC': 'P', 'TAG': '*', 'TAT': 'Y', 'GGT': 'G', 'TGT': 'C',
'CGA': 'R', 'CCA': 'P', 'TCT': 'S', 'GAT': 'D', 'CGG': 'R',
'TTT': 'F', 'TGC': 'C', 'GGG': 'G', 'TGA': '*', 'GGA': 'G',
'TGG': 'W', 'GGC': 'G', 'TAC': 'Y', 'GAG': 'E', 'TCG': 'S',
'TTA': 'L', 'GAC': 'D', 'TCC': 'S', 'GAA': 'E', 'TCA': 'S',
'GCA': 'A', 'GTA': 'V', 'GCC': 'A', 'GTC': 'V', 'GCG': 'A',
'GTG': 'V', 'TTC': 'F', 'GTT': 'V', 'GCT': 'A', 'ACC': 'T',
'TTG': 'L', 'CGT': 'R', 'TAA': '*', 'CGC': 'R'
}
final_peptide_seqs = {}
for key in seqs.keys():
seq = seqs[ key ].upper()
peptide = []
for i in range( int( len( seq ) / 3.0 ) ):
codon = seq[i*3:i*3+3]
try:
peptide.append( genetic_code[ codon ] )
except:
peptide.append( "*" )
final_peptide_seqs.update( { key: "".join( peptide ) } )
return final_peptide_seqs
def clean_input_FASTA_file( raw_subject_file, subject_file, mapping_table, cds_input, trim_names ):
"""! @brief clean input FASTA file """
forbidden_characters = [ ";", ":", "(", ")", "_", "=" ]
tab_replacement = False
with open( mapping_table, "w" ) as out:
out.write( "InitialID\tCleanID\n" )
sequences = {}
with open( raw_subject_file ) as f:
header = f.readline()[1:].strip()
if trim_names:
if " " in header:
header = header.split(' ')[0]
if "\t" in header:
header = header.split('\t')[0]
if "\t" in header:
tab_replacement = True #set variable to true to trigger warning
out.write( header.replace("\t", " ") + "\t" )
if " " in header:
header = header.split(' ')[0]
if "\t" in header:
header = header.split('\t')[0]
for each in forbidden_characters:
header = header.replace( each, "-" )
header = header.encode("ascii", "ignore").decode() #removal of non-ASCII characters
out.write( header + "\n" )
seq = []
line = f.readline()
while line:
if line[0] == '>':
sequences.update( { header: "".join( seq ) } )
header = line.strip()[1:]
if trim_names:
if " " in header:
header = header.split(' ')[0]
if "\t" in header:
header = header.split('\t')[0]
if "\t" in header:
tab_replacement = True #set variable to true to trigger warning
out.write( header.replace("\t", " ") + "\t" )
if " " in header:
header = header.split(' ')[0]
if "\t" in header:
header = header.split('\t')[0]
for each in forbidden_characters:
header = header.replace( each, "-" )
header = header.encode("ascii", "ignore").decode()
out.write( header + "\n" )
seq = []
else:
seq.append( line.strip() )
line = f.readline()
sequences.update( { header: "".join( seq ) } )
if cds_input:
cds_file = subject_file.replace( ".pep.fasta", ".cds.fasta" )
pep_sequences = translate( sequences )
with open( cds_file, "w" ) as out: #construct file with clean CDS
for key in sequences.keys():
out.write( '>' + key + "\n" + sequences[ key ] + "\n" )
with open( subject_file, "w" ) as out: #construct file with clean PEPs
for key in pep_sequences.keys():
out.write( '>' + key + "\n" + pep_sequences[ key ] + "\n" )
else:
with open( subject_file, "w" ) as out: #construct file with clean PEPs
for key in sequences.keys():
out.write( '>' + key + "\n" + sequences[ key ] + "\n" )
if tab_replacement: #show warning that tabs have been replaced by three spaces
sys.stdout.write( "WARNING: tabs in input sequence names have been replaced by three spaces. Please provide input sequences without tabs or spaces in their names.\n" )
sys.stdout.flush()
def load_ref_mybs( ref_mybs_file ):
"""! @brief load IDs from given file """
refmybs = {}
with open( ref_mybs_file, "r" ) as f:
line = f.readline()
while line:
if "\t" in line:
parts = line.strip().split("\t")
if len( parts ) == 2:
refmybs.update( { parts[0]: { 'id': parts[0], 'name': parts[1], 'function': "n/a", 'group': "" } } )
elif len( parts ) == 3:
refmybs.update( { parts[0]: { 'id': parts[0], 'name': parts[1], 'function': parts[2], 'group': "" } } )
elif len( parts ) == 4:
refmybs.update( { parts[0]: { 'id': parts[0], 'name': parts[1], 'function': parts[2], 'group': parts[3] } } )
else:
refmybs.update( { line.strip(): { 'id': parts[0], 'name': line.strip(), 'function': "n/a" } } )
line = f.readline()
return refmybs
def myb_group_assignment( ref_mybs, tree_file, myb_candidates ):
"""! @brief assign new MYBs to reference MYBs e.g. the A.thaliana MYBs """
new2ref_mapping_table, new_per_ref_myb = {}, {}
#new2ref_mapping_table = { candiate1: ref1, candidate2: ref1, candiate3: ref14, ... }
#new_per_ref_myb = { ref1: [ candidate1, candidate2 ], ref2: [candidate15], ref3: [], ... }
# --- preparation of data structure --- #
my_ref_mybs = list( sorted( ref_mybs.keys() ) )
for gene in my_ref_mybs: #reference MYBs
new_per_ref_myb.update( { gene: [] } )
for gene in myb_candidates: #candidate genes of new species
new2ref_mapping_table.update( { gene: None } )
# --- find node objects of reference genes --- #
tree = dendropy.Tree.get_from_path( tree_file, "newick" )
pdm = dendropy.PhylogeneticDistanceMatrix.from_tree( tree )
my_mean_nearest_taxon_distance = pdm.mean_nearest_taxon_distance()
ref_node_objects = {}
new_node_objects = {}
for node in tree.taxon_namespace:
try:
new_per_ref_myb[ node.label ]
ref_node_objects.update( { node.label: node } )
except KeyError:
try:
new2ref_mapping_table[ node.label ]
new_node_objects.update( { node.label: node } )
except KeyError:
pass
ref_gene_nodes = []
ref_gene_nodes_dict_to_check = {}
candidate_gene_nodes = []
canidate_gene_nodes_dict_to_check = {}
for gene in my_ref_mybs:
ref_gene_nodes.append( ref_node_objects[ gene ] )
ref_gene_nodes_dict_to_check.update( { ref_node_objects[ gene ]: None } )
for gene in myb_candidates:
candidate_gene_nodes.append( new_node_objects[ gene ] )
canidate_gene_nodes_dict_to_check.update( { new_node_objects[ gene ]: None } )
for i, t1 in enumerate( candidate_gene_nodes ):
edge_distances = []
patr_distances = []
for t2 in ref_gene_nodes: #calculate distance to all other sequences in tree
edge_distances.append( pdm.path_edge_count( t1, t2) )
patr_distances.append( pdm.patristic_distance( t1, t2 ) )
ref_myb = my_ref_mybs[ edge_distances.index( min( edge_distances ) ) ]
new2ref_mapping_table[ t1.label ] = { 'label': ref_myb, 'edges': min( edge_distances ), 'patr': patr_distances[ edge_distances.index( min( edge_distances ) ) ] }
new_per_ref_myb[ ref_myb ].append( t1.label )
return new2ref_mapping_table, new_per_ref_myb
def load_myb_classification_from_file( tmp_result_table ):
"""! @brief load MYB classification from file """
myb_classification = {}
with open( tmp_result_table, "r" ) as f:
f.readline() #remove header
line = f.readline()
while line:
parts = line.strip().split('\t')
myb_classification.update( { parts[1]: float( parts[2] ) } )
line = f.readline()
return myb_classification
def MYB_domain_check( seqs ):
"""! @brief screen sequences for R2R3-MYB domain """
domain_status = {}
R1 = "\w{3,4}W\w{17,21}W\w{17,21}W\w{5,8}"
R2 = "\w{5}[WF]{1}\w{18,21}W\w{15,27}[WY]{1}\w{4}" #F at pos1 and Y at pos3 are very rare
R3 = "\w{5}[WLIMF]{1}\w{14,21}W\w{17,21}[WYF]{1}\w{4}" #diversity at pos1 is high, but po2 and pos3 are conserved
#MYB domain patterns based on Feng et al., 2017 (doi: 10.1093/gbe/evx056) and Du et al., 2015 (doi: 10.1038/srep11037)
#banana MYB paper pattern: "\w{5}W\w{85,100}W\w{7}"
for key in sorted( seqs.keys() ):
seq = seqs[ key ]
# --- check for more than 3 MYB repeats --- #
# --- 3R MYBs (R1+R2+R3) --- #
try:
match = re.findall( R1 + "\w{0,3}" + R2 + "\w{0,3}" + R3, seq )[0] #R1R2R3 domains present
domain_status.update( { key: { 'domain': "3R", 'seq': match } } )
except:
try:
match = re.findall( R2 + "\w{0,3}" + R3, seq )[0] #R2R3 domains present
domain_status.update( { key: { 'domain': "R2R3", 'seq': match } } )
except:
try:
match = re.findall( R1, seq )[0] #R1 domain present
domain_status.update( { key: { 'domain': "R1", 'seq': match } } )
except:
domain_status.update( { key: { 'domain': "pseudo", 'seq': seq } } ) #no MYB domain detected
return domain_status
def check_MYB_IDs_across_files( bait_seq_file, info_file, ref_mybs_file ):
"""! @brief check MYB IDs across the different files """
myb_status = True
# --- check FASTA file for forbidden characters --- #
forbidden_characters = [ ";", ":", "(", ")", "_" ]
seqs = load_sequences( bait_seq_file )
header_string = "".join( seqs.keys() )
for each in forbidden_characters:
if each in header_string:
sys.stderr.write( "Forbidden character detected in MYB IDs (bait FASTA file): " + each + "(occurrences:" + str( header_string.count( each ) ) + ")\n" )
sys.stderr.flush()
myb_status = False
# --- check structure of info file --- #
info_myb_IDs = {}
with open( info_file, "r" ) as f:
line = f.readline()
while line:
parts = line.strip().split('\t')
if len( parts ) < 2:
sys.stderr.write( "Issue in MYB info file (number of columns not 2): " + line + "\n" )
sys.stderr.flush()
else:
info_myb_IDs.update( { parts[0]: None } )
if parts[1] not in [ "in", "out" ]:
sys.stderr.write( "Issue in MYB info file (unexpected status; only 'in' and 'out' permitted): " + line + "\n" )
sys.stderr.flush()
line = f.readline()
# --- compare IDs between bait and info file --- #
missing_in_fasta = []
for myb in info_myb_IDs.keys():
try:
seqs[ myb ]
except KeyError:
missing_in_fasta.append( myb )
if len( missing_in_fasta ) > 0:
sys.stderr.write( "Unmatched MYB IDs (missing in bait FASTA file) :" + ";".join( missing_in_fasta ) + "\n" )
sys.stderr.flush()
myb_status = False
missing_in_info = []
for myb in seqs.keys():
try:
info_myb_IDs[ myb ]
except KeyError:
missing_in_info.append( myb )
if len( missing_in_info ) > 0:
sys.stderr.write( "Unmatched MYB IDs (missing in info file) :" + ";".join( missing_in_info ) + "\n" )
sys.stderr.flush()
myb_status = False
if len( ref_mybs_file ) > 0: #only try to check if file actually exists
missing_ref_mybs = []
with open( ref_mybs_file, "r" ) as f:
line = f.readline()
while line:
x = line.strip()
if "\t" in x:
x = line.split('\t')[0]
try:
seqs[ x ]
except KeyError:
missing_ref_mybs.append( x )
line = f.readline()
if len( missing_ref_mybs ) > 0:
sys.stderr.write( "Reference MYB IDs missing in FASTA and/or info file) :" + ";".join( missing_ref_mybs ) + "\n" )
sys.stderr.flush()
myb_status = False
return myb_status
def md5_calculator( input_file ):
"""! @brief calculate md5sum of given file """
with open( input_file, "rb" ) as f:
content = f.read()
try:
return hashlib.md5( content ).hexdigest()
except NameError:
return "n/a"
def generate_documentation_file( doc_file, bait_seq_file, info_file, output_folder, raw_subject_file,
search, mode, blastp, makeblastdb, mafft, cpub, cpur, raxml, fasttree, ref_mybs_file,
similarity_cutoff_p, possibility_cutoff_p, length_cutoff_p, cds_input, hmmsearch,
neighbour_cutoff,mean_factor_cutoff,min_neighbour_cutoff,dist_cutoff_factorB
):
"""! @brief write documentation file with specified inputs and parameters """
with open( doc_file, "w" ) as out:
out.write( "Please cite 'Pucker B (2022). Automatic identification and annotation of MYB gene family members in plants. BMC Genomics 23, 220 (2022). doi: 10.1186/s12864-022-08452-5' when using MYB_annotator.py.\n\n" )
out.write( "MYB_annotator.py version: " + __version__ + "\n" )
bait_seq_file_md5 = md5_calculator( bait_seq_file )
out.write( "MYB bait file: " + bait_seq_file + "\t" + bait_seq_file_md5 + "\n" )
info_file_md5 = md5_calculator( info_file )
out.write( "MYB info file: " + info_file + "\t" + info_file_md5 + "\n" )
raw_subject_file_md5 = md5_calculator( raw_subject_file )
out.write( "Subject FASTA file: " + raw_subject_file + "\t" + raw_subject_file_md5 + "\n" )
out.write( "Output folder: " + output_folder + "\n" )
#--- optional --- #
out.write( "Tool for initial candidate selection: " + search + "\n" )
out.write( "Tool for tree construction: " + mode + "\n" )
out.write( "CPUs for BLASTp: " + str( cpub ) + "\n" )
out.write( "CPUs for RAxML: " + str( cpur ) + "\n" )
if len( ref_mybs_file ) > 0:
ref_myb_file_md5 = md5_calculator( ref_mybs_file )
out.write( "Reference MYB file: " + ref_mybs_file + "\t" + ref_myb_file_md5 + "\n" )
else:
out.write( "Reference MYB file: n/a\n" )
if cds_input:
out.write( "Type of input: CDS\n" )
else:
out.write( "Type of input: PEP\n" )
# --- paths to tools --- #
out.write( "blastp path: " + blastp + "\n" )
out.write( "makeblastdb path: " + makeblastdb + "\n" )
out.write( "mafft path: " + mafft + "\n" )
out.write( "raxml path: " + raxml + "\n" )
out.write( "fasttree path: " + fasttree + "\n" )
# ---- BLAST filter criteria --- #
out.write( "Minimal BLASTp hit similarity cutoff: " + str( similarity_cutoff_p ) + "\n" )
out.write( "Maximal number of BLASTp hits per bait: " + str( possibility_cutoff_p ) + "\n" )
out.write( "Minimal BLASTp hit alignment length: " + str( length_cutoff_p ) + "\n" )
# --- tree analysis settings --- #
out.write( "Number of neighbours to consider in classification: " + str( neighbour_cutoff ) + "\n" )
out.write( "Factor for branch length cutoff in identification of neighbours: " + str( mean_factor_cutoff ) + "\n" )
out.write( "Minimal number of neighbours required for classification as MYB: " + str( min_neighbour_cutoff ) + "\n" )
out.write( "Distance cutoff for paralog clade masking: " + str( dist_cutoff_factorB ) + "\n" )
# --- add tool versions --- #
try:
mafft_version_raw = subprocess.Popen( args=mafft + " --version", stderr=subprocess.PIPE, shell=True )
mafft_version = mafft_version_raw.stderr.read()
out.write ( "MAFFT version: " + str( mafft_version )[2:-3] + "\n" ) #remove characters introduced through binary
except:
out.write ( "MAFFT version detection failed.\n" ) #if no MAFFT installation was detected
try:
fasttree_version_raw = subprocess.Popen( args=fasttree + " -help", stderr=subprocess.PIPE, shell=True )
fasttree_version = fasttree_version_raw.stderr.read()
out.write ( "FastTree version: " + str( fasttree_version )[10:18] + "\n" ) #remove characters introduced through binary
except:
out.write ( "FastTree version detection failed.\n" ) #version not available via command
try:
raxml_version_raw = subprocess.Popen( args=raxml + " --version", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True )
raxml_version = str( raxml_version_raw.stdout.read() ).strip()
out.write ( "RAxML version: " + ( raxml_version[4:65]) + "...\n" ) #remove characters introduced through binary
except:
out.write ( "RAxML version detection failed.\n" ) #if no RAxML installation was detected
try:
hmmsearch_version_raw = subprocess.Popen( args=hmmsearch + " -h", stdout=subprocess.PIPE, shell=True )
hmmsearch_version = str( hmmsearch_version_raw.stdout.read() ).strip().split('\n')[1]
out.write ( "hmmsearch version: " + ( hmmsearch_version ) + "...\n" ) #remove characters introduced through binary
except:
out.write ( "hmmsearch version detection failed.\n" ) #if no hmmsearch installation was detected
def load_subject_name_mapping_table( mapping_table_file ):
"""! @brief load subject name mapping table """
mapping_table = {}
with open( mapping_table_file, "r" ) as f:
line = f.readline()
while line:
parts = line.strip().split('\t')
mapping_table.update( { parts[1]: parts[0] } ) #clean ID back to original one
line = f.readline()
return mapping_table
def load_motifs_from_file( motifs_file ):
"""! @brief load motifs from file """
motifs = {}
with open( motifs_file, "r" ) as f:
line = f.readline()
while line:
if "\t" in line:
parts = line.strip().split()
motifs.update( { parts[0]: parts[1] } )
else:
motifs.update( { "motif-" + str( len( motifs.keys() ).zfill(3) +1 ): line.strip() } )
line = f.readline()
return motifs
def motif_check( seqs, motifs ):
"""! @brief screen sequences for motifs """
results = {}
for key in sorted( seqs.keys() ):
results.update( { key: {} } )
seq = seqs[ key ]
for ID in motifs.keys():
try:
match = re.findall( motifs[ ID ], seq )[0]
results[ key ].update( { key: match } )
except:
results[ key ].update( { key: "" } )
return results
def establish_paralog_groups( tree_file, myb_candidates, dist_cutoff_factorB ):
"""! @brief construct paralog groups """
candidate_mapping_table = {}
for gene in myb_candidates: #candidate genes of new species
candidate_mapping_table.update( { gene: None } )
# --- find node objects of reference genes --- #
tree = dendropy.Tree.get_from_path( tree_file, "newick" )
pdm = dendropy.PhylogeneticDistanceMatrix.from_tree( tree )
my_mean_nearest_taxon_distance = pdm.mean_nearest_taxon_distance()
new_node_objects = {} #get new MYB candidate node objects
for node in tree.taxon_namespace:
try:
candidate_mapping_table[ node.label ]
new_node_objects.update( { node.label: node } )
except KeyError:
pass
candidate_gene_nodes = []
canidate_gene_nodes_dict_to_check = {}
for gene in myb_candidates:
candidate_gene_nodes.append( new_node_objects[ gene ] )
canidate_gene_nodes_dict_to_check.update( { new_node_objects[ gene ]: None } )
black_list = {}
paralog_collection = []
for i, t1 in enumerate( candidate_gene_nodes ):
try:
black_list[ t1.label ]
except KeyError:
paralogs = [ t1.label ]
edge_distances = []
patr_distances = {}
for t2 in tree.taxon_namespace: #calculate distance to all other sequences in tree
try:
black_list[ t2.label ]
except KeyError:
if t1.label != t2.label:
edge_distances.append( { 'id': t2.label, 'dist': pdm.path_edge_count( t1, t2) } )
patr_distances.update( { t2.label: pdm.patristic_distance( t1, t2 ) } )
for each in list( sorted( edge_distances, key=itemgetter('dist') ) ):
try:
candidate_mapping_table[ each['id'] ]
if patr_distances[ each['id'] ] < ( my_mean_nearest_taxon_distance*dist_cutoff_factorB ):
paralogs.append( each['id'] )
black_list.update( { each['id']: None } )
except KeyError:
break #next neighbour is not a new candidate MYB => break extension of paralog group
paralog_collection.append( paralogs )
black_list.update( { t1.label: None } )
return paralog_collection
def get_represenative_paralog_per_group( paralog_groups, clean_mybs, repr_clean_myb_file ):
"""! @brief select longest sequence as representative per paralog group """
paralog_representatives = {}
with open( repr_clean_myb_file, "w" ) as out:
for group in paralog_groups:
if len( group ) == 1:
out.write( '>' + group[0] + "\n" + clean_mybs[ group[0] ] + "\n" )
paralog_representatives.update( { group[0]: clean_mybs[ group[0] ] } )
else:
seqs_len_sorting = []
for each in group:
seqs_len_sorting.append( { 'id': each, 'len': len( clean_mybs[ each ] ), 'seq': clean_mybs[ each ] } )
representative = list( sorted( seqs_len_sorting, key=itemgetter('len', 'id') ) )[-1]
out.write( '>' + representative['id'] + "\n" + representative['seq'] + "\n" )
paralog_representatives.update( { representative['id']: representative['seq'] } )
return paralog_representatives
def MYB_domain_check_wrapper( clean_mybs_file, myb_domain_check_file, myb_domain_fasta, myb_domain_doc, subject_name_mapping_table ):
"""! @brief check sequences for MYB domains """
clean_candidate_myb_sequences = load_sequences( clean_mybs_file )
myb_domains = MYB_domain_check( clean_candidate_myb_sequences ) #based on banana MYB paper: https://doi.org/10.1371/journal.pone.0239275
R1_MYB_counter = 0
R2R3_MYB_counter = 0
R1R2R3_MYB_counter = 0
pseudo_MYB_counter = 0
with open( myb_domain_check_file, "w" ) as out:
with open( myb_domain_fasta, "w" ) as out2:
out.write( "OriginalGeneID\tCleanGeneID\tR2R3-MYB domain status\tR2R3-MYB domain\n" )
candidates = list( sorted( clean_candidate_myb_sequences.keys() ) )
for candidate in candidates:
dom = myb_domains[ candidate ]['domain']
out.write( "\t".join( [ subject_name_mapping_table[ candidate ], candidate, dom, myb_domains[ candidate ]['seq'] ] ) + "\n" )
# Y_seq_ID = subject_name_mapping_table[ candidate ]
# if " " in Y_seq_ID:
# Y_seq_ID = Y_seq_ID.split(' ')[0] #cut name at first space
out2.write( '>' + subject_name_mapping_table[ candidate ]+ "\n" + myb_domains[ candidate ]['seq'] + "\n" )
if dom == "R1":
R1_MYB_counter += 1
elif dom == "R2R3":
R2R3_MYB_counter += 1
elif dom == "3R":
R1R2R3_MYB_counter += 1
elif dom == "pseudo":
pseudo_MYB_counter += 1
with open( myb_domain_doc, "w" ) as out:
sys.stdout.write( "Number of 1R-MYBs: " + str( R1_MYB_counter ) + "\n" )
out.write( "Number of 1R-MYBs: " + str( R1_MYB_counter ) + "\n" )
sys.stdout.write( "Number of R2R3-MYBs: " + str( R2R3_MYB_counter ) + "\n" )
out.write( "Number of R2R3-MYBs: " + str( R2R3_MYB_counter ) + "\n" )
sys.stdout.write( "Number of 3R-MYBs: " + str( R1R2R3_MYB_counter ) + "\n" )
out.write( "Number of 3R-MYBs: " + str( R1R2R3_MYB_counter ) + "\n" )
sys.stdout.write( "Number of pseudo MYBs and unclassified ones: " + str( pseudo_MYB_counter ) + "\n" )
out.write( "Number of pseudo MYBs and unclassified ones: " + str( pseudo_MYB_counter ) )
sys.stdout.flush()
def tree_constructor( X_aln_input_file, X_aln_file, X_cln_aln_file, X_bait_seq_file, X_mybs_file, mode, X_output_folder, Xname, Xnumber, mafft, raxml, fasttree, cpur ):
"""! @brief handles the construction of alignments and phylogenetic tree
@note second FASTA file can be an empty string to run this function just based on one FASTA file
"""
if not os.path.isfile( X_aln_input_file ):
if len( X_mybs_file ) > 0:
p = subprocess.Popen( args= "cat " + X_bait_seq_file + " " + X_mybs_file + " > " + X_aln_input_file, shell=True )
p.communicate()
else:
p = subprocess.Popen( args= "cp " + X_bait_seq_file + " " + X_aln_input_file, shell=True )
p.communicate()
if not os.path.isfile( X_aln_file ):
p = subprocess.Popen( args= mafft + " --quiet " + X_aln_input_file + " > " + X_aln_file, shell=True )
p.communicate()
if not os.path.isfile( X_cln_aln_file ):
alignment_trimming( X_aln_file, X_cln_aln_file, occupancy=0.1 )
if mode == "raxml": #RAxML
prefix = X_output_folder + Xname + Xnumber + "RAxML_tree"
tree_file = prefix + ".raxml.bestTree"
if not os.path.isfile( tree_file ):
p = subprocess.Popen( args= " ".join( [ raxml, "--all --threads " + str( cpur ) + " --model LG+G8+F --msa", X_cln_aln_file, "--prefix", prefix ] ), shell=True )
p.communicate()
else: #FastTree2
tree_file = X_output_folder + Xname + Xnumber + "FastTree_tree.tre"
if not os.path.isfile( tree_file ):
p = subprocess.Popen( args= " ".join( [ fasttree, "-wag -nopr -nosupport <", X_cln_aln_file, ">", tree_file ] ), shell=True )
p.communicate()
return tree_file
def construct_fasta_file_w_repr_and_ath_MYBs( ref_mybs, new2ref_mapping_table, repr_and_ath_mybs_for_tree, repr_and_ath_mybs_fasta_file ):
"""! @brief rename sequences with group for final tree construction """
myb2group = {}
myb_id2name = {}
myb_name2id = {}
for each in list( ref_mybs.values() ):
myb2group.update( { each['id']: each['group'] } )
myb_id2name.update( { each['id']: each['name'] } )
myb_name2id.update( { each['name']: each['id'] } )
with open( repr_and_ath_mybs_fasta_file, "w" ) as out:
for key in list( repr_and_ath_mybs_for_tree.keys() ):
try:
group = myb2group[ key ]
except KeyError:
try:
group = myb2group[ new2ref_mapping_table[ key ] ]
except KeyError:
try:
group = myb2group[ myb_name2id[ new2ref_mapping_table[ key ] ] ]
except KeyError:
group = ""
try:
out.write( '>' + myb_id2name[ key ] + "-" + group + "\n" + repr_and_ath_mybs_for_tree[ key ] + "\n" )
except KeyError:
out.write( '>' + key + "-" + group + "\n" + repr_and_ath_mybs_for_tree[ key ] + "\n" )
def load_candidate_myb_to_myb_mapping_table( new_2_ref_myb_mapping_file ):
"""! @brief load mapping table """
new2ref_mapping_table = {}
with open( new_2_ref_myb_mapping_file, "r" ) as f:
f.readline() #remove header
line = f.readline()
while line:
parts = line.strip().split('\t')
new2ref_mapping_table.update( { parts[0]: parts[1] } )
line = f.readline()
return new2ref_mapping_table
def summarize_domain_counts( Y_summary_file, raw_subject_files, num_prefix, output_folder, name ):
"""! @brief summarize domain numbers of all species """
data = []
for jidx, raw_subject_file in enumerate( raw_subject_files ): #use jidx to generate unique IDs for all jobs
job_ID = raw_subject_file.split('/')[-1].split('.')[0]
job_output_folder = output_folder + str( jidx ).zfill(5) + "_" + job_ID + "/"
doc_file = job_output_folder + "RESULTS/" + name + num_prefix + "_MYB_domain_check.doc.txt"
tmp = {}
with open( doc_file, "r" ) as f:
line = f.readline()
while line:
num = re.findall( "\d+", line )[-1]
if "1R-MYBs" in line:
tmp.update( { "1R": num } )
elif "R2R3-MYBs" in line:
tmp.update( { "2R3R": num } )
elif "3R-MYBs" in line:
tmp.update( { "3R": num } )
elif "unclassified ones" in line:
tmp.update( { "x": num } )
line = f.readline()
data.append( { 'id': job_ID, 'info': tmp } )
data = list( sorted( data, key=itemgetter('id') ) )
with open( Y_summary_file, "w" ) as out:
out.write( "\t".join( [ "SpecID", "1R-MYBs", "R2R3-MYBs", "3R-MYBs", "others" ] ) + "\n" )
for each in data:
out.write( "\t".join( list( map( str, [ each['id'], each['info']['1R'], each['info']['2R3R'], each['info']['3R'], each['info']['x'] ] ) ) ) + "\n" )
def load_hmmsearch_results( seq_search_result_file ):
"""! @brief load all hmmsearch hits into a dictionary """
hmm_search_results = {}
with open( seq_search_result_file, "r" ) as f:
line = f.readline()
while line:
if line[0] != "#":
if "\t" in line:
hmm_search_results.update( { line.split('\t')[0]: None } )
elif " " in line:
hmm_search_results.update( { line.split(' ')[0]: None } )
else:
sys.stdout.write( line )
sys.stdout.flush()
line = f.readline()
return hmm_search_results
def main( arguments ):
"""! @brief run everything """
bait_seq_file = arguments[ arguments.index('--baits')+1 ]
info_file = arguments[ arguments.index('--info')+1 ]
output_folder = arguments[ arguments.index('--out')+1 ]
if '--subject' in arguments:
raw_subject_files = [ arguments[ arguments.index('--subject')+1 ] ]
else:
subject_file_dir = arguments[ arguments.index('--subjectdir')+1 ]
if not subject_file_dir[-1] == "/":
subject_file_dir + "/"
extensions = [ ".fasta", ".fa", ".fas", ".FASTA", ".FA", ".FAS", ".fna", ".FNA", ".cds", ".CDS", ".pep", ".PEP" ]
raw_subject_files = [ ]
for each in extensions:
raw_subject_files += glob.glob( subject_file_dir + "*" + each )
raw_subject_files = list( sorted( raw_subject_files ) )
if '--search' in arguments:
search = arguments[ arguments.index('--search')+1 ]
if search not in [ "blast", "hmmer" ]:
search = "blast"
else:
search = "blast"
if search == "hmmer":
myb_bait_hmm = arguments[ arguments.index('--hmm')+1 ]
if '--mode' in arguments:
mode = arguments[ arguments.index('--mode')+1 ]
if mode not in [ "fasttree", "raxml" ]:
mode = "fasttree"
else:
mode = "fasttree"
if '--collapse' in arguments:
collapse_mode = True
else:
collapse_mode = False
if '--ath' in arguments:
ath_myb_file = arguments[ arguments.index('--ath')+1 ]
else:
ath_myb_file = ""
if "--name" in arguments:
name = arguments[ arguments.index('--name')+1 ]
else:
name = ""
if '--blastp' in arguments:
blastp = arguments[ arguments.index('--blastp')+1 ]
else:
blastp = "blastp"
if '--makeblastdb' in arguments:
makeblastdb = arguments[ arguments.index('--makeblastdb')+1 ]
else:
makeblastdb = "makeblastdb"
if '--mafft' in arguments:
mafft = arguments[ arguments.index('--mafft')+1 ]
else:
mafft = "mafft"
if '--hmmsearch' in arguments:
hmmsearch = arguments[ arguments.index('--hmmsearch')+1 ]
else:
hmmsearch = "hmmsearch"
if '--cpu' in arguments:
cpu = int( arguments[ arguments.index('--cpu')+1 ] )
else:
cpu = 4