-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathMETABOLIC-C.2nd_run.pl
1939 lines (1747 loc) · 65.9 KB
/
METABOLIC-C.2nd_run.pl
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 perl
###########################
# METABOLIC-C.pl
# METABOLIC => METabolic And BiogeOchemistry anaLyses In miCrobes
# This software gives a metabolic and biogeochemical function trait profile to given genome datasets
# [either metagenome-assembled genomes (MAGs), single-cell amplified genomes (SAGs) or pure culture sequenced genomes].
# It also integrates the genome coverage to make element cycling pathways.
# METABOLIC-C.pl is specifically for users who have metagenomic reads and want to include them in the community analysis.
# Written by Zhichao Zhou, [email protected]
# July, 2019
# This program 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###########################
use 5.010;
use strict;
use warnings;
##modules
use Data::Dumper;
use POSIX qw(strftime);
use Getopt::Long;
use Statistics::Descriptive;
use Parallel::ForkManager;
use File::Spec;
use File::Basename;
=head1 DESCRIPTION
Takes a folder containing genome files to generate a profile of their metablic and biogeochemical functions
=head1 USAGE
perl METABOLIC-C.pl -t 40 -m-cutoff 0.75 -in Genome_proteins -kofam-db full -r omic_reads_parameters.txt -o METABOLIC_out
(When you also want to calculate genome coverages, you would have to add genome files with the same basename and the extention as ".fasta" in this folder)
perl METABOLIC-C.pl -t 40 -m-cutoff 0.75 -in-gn Genome_files -kofam-db full -r omic_reads_parameters.txt -o METABOLIC_out
perl METABOLIC-G.pl -test true
(use the 5 genomes to test the METABOLIC-C script)
perl METABOLIC-G.pl -test true -tax order
(to calculate MW-score contribution of microbial groups at the resolution of the "order" level)
perl METABOLIC-C.pl -t 40 -m-cutoff 0.75 -in-gn Genome_files -kofam-db full -r omic_reads_parameters.txt -st nanopore -o METABOLIC_out
(to use long reads generated by Oxford Nanopore sequencing)
=head1 OPTIONS
-t or -cpu [integer] The cpu numbers to run the hmmsearch (default: 20)
-m-cutoff or -module-cutoff [float] The cutoff value to assign the presence of a specific KEGG module (KEGG module step present numbers / KEGG module step total number) (default: 0.75)
-in [string] The folder pf given genome faa files [should also give the genome fasta files and genone gene files if the (meta)genome/(meta)transciptome datasets are included]
-in-gn [string] The folder of given genome fasta files (Prodigal will be used to annotate your genomes)
-kofam-db [string] To use the "small" size or "full" size of KOfam database in METABOLIC (default: 'full')
-p or -prodigal-method [string] "meta" or "single" for prodigal to annotate the orf
-r or -omic-reads [string] The file which indicates the address of omic reads
-rt or -reads-type [string] To use "metaG" or "metaT" to indicate whether you use the metagenomic reads or metatranscriptomic reads (default: 'metaG')
-st or -sequencing-type [string] To use "illumina" (for Illumina short reads), or "pacbio" (for PacBio CLR reads), or "pacbio_hifi" (for PacBio HiFi/CCS genomic reads (v2.19 or later)), or "pacbio_asm20" (for PacBio HiFi/CCS genomic reads (v2.18 or earlier)), or "nanopore" (for Oxford Nanopore reads) to indicate the sequencing type of metagenomes or metatranscriptomes (default: 'illumina'; Note that all "illumina", "pacbio", "pacbio_hifi", "pacbio_asm20", and "nanopore" should be provided as lowercase letters and the underscore "_" should not be typed as "-" or any other marks)
-tax or -taxonomy [string] To calculate MW-score contribution of microbial groups at the resolution of which taxonomical level (default: "phylum"; other options: "class", "order", "family", "genus", "species", and "bin" (MAG itself))
-o or -output [string] The METABOLIC output folder (default: current address)
-2nd-run [string] The option to make a 2nd run using the same set of genomes of a previous run. This option can use the previous genome annotation intermediate folders or metagenomic/metatranscriptomic mapping result, thus it can save lots of time. "true" or "false" to run the 2nd-run option (default: 'false'). [Note] If this option was set to be "true", option "-o" should be set to the previous output folder of a successful run, METABOLIC-C will use the intermediate files within. You will also need to set the option "-2nd-run-suffix", the suffix will be appended to the new folders and files that are generated by the 2nd run. The Prodigal Method (-p or -prodigal-method), KOfam DB (-kofam-db), Module Cutoff Value (-m-cutoff or -module-cutoff), and Input Genome directory (nucleotides) (-in-gn) should be the same as the previous run.
-2nd-run-suffix [string] The suffix that will be appended to the folders and files that are generated by the 2nd run (default: '2nd_run')
-depth-file [string] The option to use the depth file provided by a previous run (You only to provide the depth file name to this option without adding the path in the front, e.g., "All_gene_collections_mapped.depth.txt"). This option should be used only when you use the option "-2nd-run".
-test [string] The option to test the performance of METABOLIC-G by 5 genomes; "true" or "false" to run the test option. The test option will use 5 CPUs to run the command.
=head1 INSTRUCTIONS
GitHub: https://github.com/AnantharamanLab/METABOLIC
=head1 OUTPUT
Prodigal-annotated protein and gene files will be given in the input folder.
The METABOLIC result table will be generated.
Biogeochemical pathway diagrams will be generated.
=head1 COPYRIGHT
Zhichao Zhou, [email protected]
Patricia Tran, [email protected]
Karthik Anantharaman, [email protected]
Anantharaman Microbiome Laboratory
Department of Bacteriology, University of Wisconsin, Madison
=cut
# Intake the address of METABOLIC directory:
my $METABOLIC_dir = dirname(File::Spec->rel2abs(__FILE__));
# The options
# Option variables with default value
my $cpu_numbers = 20; # Parallel running cpu numbers
my $module_cutoff = 0.75; # The cutoff value to assign the existence of a module
my $input_protein_folder; # Input microbial genome protein files
my $input_genome_folder; # Input microbial genome fasta files
my $omic_reads_parameters; # The address of omic reads
my $prodigal_method = "meta"; # The prodigal method to annotate orfs
my $kofam_db_size = "full"; # The full kofam size
my $omic_reads_type = "metaG"; # Metagenomic reads
my $sequencing_type = "illumina"; # The sequencing type of input omics reads
my $output = `pwd`; # The output folder
my $second_run = "false"; # true or false for a 2nd run
my $second_run_suffix = "2nd_run"; # The suffix to append on the folders and files generated by the 2nd run
my $depth_file; # The depth file that is generated by a previous run
my $taxonomy = "phylum"; # The taxonomy level to calculate MW-score table
my $version="METABOLIC-C.pl v4.0";
my $test = "false";
GetOptions(
'cpu|t=i' => \$cpu_numbers,
'module-cutoff|m-cutoff=f' => \$module_cutoff,
'in=s' => \$input_protein_folder,
'in-gn=s' => \$input_genome_folder,
'prodigal-method|p=s' => \$prodigal_method,
'omic-reads|r=s' => \$omic_reads_parameters,
'reads-type|rt=s' => \$omic_reads_type,
'sequencing-type|st=s' => \$sequencing_type,
'kofam-db=s' => \$kofam_db_size,
'taxonomy|tax=s' => \$taxonomy,
'2nd-run=s' => \$second_run,
'2nd-run-suffix=s' => \$second_run_suffix,
'depth-file=s' => \$depth_file,
'output|o=s' => \$output,
'help|h' => sub{system('perldoc', $0); exit;},
'v|version'=>sub{print $version."\n"; exit;},
'test=s' => \$test
) or die("Getting options from the command line failed, please check your options");
## Pre-required files and documents
# METABOLIC hmm database files
my $METABOLIC_hmm_db_address = "$METABOLIC_dir/METABOLIC_hmm_db";
# KofamKOALA hmm database files
# Link: ftp://ftp.genome.jp/pub/db/kofam/
my $kofam_db_address = "$METABOLIC_dir/kofam_database/profiles";
my $kofam_db_KO_list = "$METABOLIC_dir/kofam_database/ko_list";
# Input hmm information table as a template
my $hmm_table_temp = "$METABOLIC_dir/METABOLIC_template_and_database/hmm_table_template.txt";
my $hmm_table_temp_2 = "$METABOLIC_dir/METABOLIC_template_and_database/hmm_table_template_2.txt";
# The KEGG module information
my $ko_module_table = "$METABOLIC_dir/METABOLIC_template_and_database/ko00002.keg";
# The KEGG module step db
my $ko_module_step_db = "$METABOLIC_dir/METABOLIC_template_and_database/kegg_module_step_db.txt";
# The pathway information to draw element cycling diagrams and metabolic handoff
my $R_pathways = "$METABOLIC_dir/METABOLIC_template_and_database/R_pathways.txt";
my $R_mh_01 = "$METABOLIC_dir/METABOLIC_template_and_database/Sequential_transformations_01.txt";
my $R_mh_02 = "$METABOLIC_dir/METABOLIC_template_and_database/Sequential_transformations_02.txt";
my $R_mh_tsv = "$METABOLIC_dir/METABOLIC_template_and_database/Sequential-transformations.tsv";
my $R_order_of_input_01 = "$METABOLIC_dir/METABOLIC_template_and_database/order_of_input_01.txt";
my $R_order_of_input_02 = "$METABOLIC_dir/METABOLIC_template_and_database/order_of_input_02.txt";
my $CAZy_map_address = "$METABOLIC_dir/METABOLIC_template_and_database/CAZy_map.txt";
# The MW-score reaction table template
my $MW_score_reaction_table = "$METABOLIC_dir/METABOLIC_template_and_database/MW-score_reaction_table.txt";
# The motif files to validate specific protein hits
my $motif_file = "$METABOLIC_dir/METABOLIC_template_and_database/motif.txt";
my $motif_pair_file = "$METABOLIC_dir/METABOLIC_template_and_database/motif.pair.txt";
# The test option:
if ($test eq "true"){
$input_genome_folder = "$METABOLIC_dir/METABOLIC_test_files/Guaymas_Basin_genome_files";
$output = "METABOLIC_out";
$cpu_numbers = "5";
$omic_reads_parameters = "$METABOLIC_dir/METABOLIC_test_files/Reads_address.txt";
}
# To make sure the input taxonomy is right
my %Tax2code = (); # Store the corresponding map of user-defined taxonomy to taxonomy level code
%Tax2code = ('phylum'=> '0', 'class'=> '1', 'order'=> '2', 'family'=> '3', 'genus'=> '4', 'species'=> '5', 'bin' => '6');
if (!exists $Tax2code{$taxonomy}){
die "Your input taxonomy is wrong, please check your spelling. It should be one of these taxonomies: phylum, class, order, family, genus, species or bin (MAG itself)\n";
}
## Main Body
if (!$second_run){
die "Please add option -2nd-run true\n";
}
if (!(-s "$output")){
die "This script is for a 2nd run using the previous genome annotation results. Please provide the output folder of the previous run\n";
}
if (!(-s "$input_genome_folder")){
die "This script is for a 2nd run using the previous genome annotation results. Please provide the input genome folder of the previous run\n";
}
# To make sure the input sequencing type should one of the five following values
if ($sequencing_type ne "illumina" and $sequencing_type ne "pacbio" and $sequencing_type ne "pacbio_asm20" and $sequencing_type ne "pacbio_hifi" and $sequencing_type ne "nanopore"){
die "Your input sequencing type is wrong, please check your spelling. It should be one of these: illumina, pacbio, pacbio_asm20, pacbio_hifi, nanopore\n";
}
# The present time
my $datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
my $starttime = $datestring; my $starttime_raw = time;
# Store the stdout and stderr into log
open STDOUT, "| tee -ai $output/METABOLIC_log.$second_run_suffix.log";
open STDERR, "| tee -ai $output/METABOLIC_log.$second_run_suffix.log";
# Store the hmm table template
my %Hmm_table_temp = (); # line no. => each line
my @Hmm_table_head = (); # The head of the hmm table template
my %METABOLIC_hmm2threshold = (); # hmm file id => threshold and score_type
open IN, "$hmm_table_temp";
while (<IN>){
chomp;
if (!/^#/){
my @tmp = split (/\t/);
$Hmm_table_temp{$tmp[0]} = $_;
if ($tmp[5] and $tmp[5] !~ /K\d\d\d\d\d/){
$METABOLIC_hmm2threshold{$tmp[5]} = $tmp[10];
}
}else{
my $line = $_; @Hmm_table_head = split (/\t/);
}
}
close IN;
# Store the hmm table template 2
my %Hmm_table_temp_2 = (); # line no. => each line;
open IN, "$hmm_table_temp_2";
while (<IN>){
chomp;
if (!/^#/){
my @tmp = split (/\t/);
$Hmm_table_temp_2{$tmp[0]}= $_;
}
}
close IN;
# The hash of hmm file and corresponding threshold and score_type
my %Total_hmm2threshold = (%METABOLIC_hmm2threshold, _get_kofam_db_KO_threshold($kofam_db_KO_list,$kofam_db_address));
if ($input_genome_folder){
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Skip Prodigal running for a 2nd run\n";
$input_protein_folder = $input_genome_folder;
}
my %Genome_id = (); # genome id => 1
my %Seqid2Genomeid = (); # seq id => genome id
my %Total_faa_seq = (); # Store the total faa file into a hash{$line_no}
open IN,"ls $input_protein_folder/*.faa |";
while (<IN>){
chomp;
my $file = $_;
# Store faa file into a hash
%Total_faa_seq = (%Total_faa_seq, _get_faa_seq($file));
my ($gn_id) = $file =~ /^$input_protein_folder\/(.+?)\.faa/;
$Genome_id{$gn_id} = 1;
open IN_, "$file";
while (<IN_>){
if (/>/){
my ($seq) = $_ =~ /^>(.+?)\s/;
$Seqid2Genomeid{$seq} = $gn_id;
}
}
close IN_;
}
if (!(-s "$output/intermediate_files")){
die "$output/intermediate_files folder is not found\n";
}
if (!(-s "$output/intermediate_files/Hmmsearch_Outputs")){
die "$output/intermediate_files/Hmmsearch_Outputs is nor found\n";
}
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Skip hmmsearch running for a 2nd run\n";
`cat $input_protein_folder/*.faa > $input_protein_folder/faa.total; mv $input_protein_folder/faa.total $input_protein_folder/total.faa`;
# Store motif validation files
my %Motif = _get_motif($motif_file); # protein id => motif sequences (DsrC => GPXKXXCXXXGXPXPXXCX)
my %Motif_pair = _get_motif_pair($motif_pair_file); # dsrC => tusE
# Summarize hmmsearch result and print table
my %Hmmscan_result = (); # genome_name => hmm => numbers
my %Hmmscan_hits = (); # genome_name => hmm => hits
my %Hmm_id = (); # hmm => 1
open IN, "find $output/intermediate_files/Hmmsearch_Outputs -type f -name '*.hmmsearch_result.txt' | ";
while (<IN>){
chomp;
my $file_name = $_;
my ($hmm) = $file_name =~ /^$output\/intermediate_files\/Hmmsearch_Outputs\/(.+?\.hmm)\./;
$Hmm_id{$hmm} = 1;
my $gn_id = "";
open INN, "$file_name";
while (<INN>){
chomp;
if (!/^#/){
my $line = $_; $line =~ s/\s+/\t/g;
my @tmp = split (/\t/,$line); $gn_id = $Seqid2Genomeid{$tmp[0]};
my ($threshold,$score_type) = $Total_hmm2threshold{$hmm} =~ /^(.+?)\|(.+?)$/;
if ($score_type eq "domain"){
if ($tmp[8] >= $threshold){
my ($hmm_basename) = $hmm =~ /^(.+?)\.hmm/;
if (exists $Motif{$hmm_basename}){
my $seq;
my $motif = $Motif{$hmm_basename}; $motif =~ s/X/\[ARNDCQEGHILKMFPSTWYV\]/g;
my %Seq_gn = _store_seq("$input_protein_folder/total.faa"); # Get the total genome sequences
$seq = $Seq_gn{">$tmp[0]"};
if ($seq =~ /$motif/){
if (! exists $Hmmscan_hits{$gn_id}{$hmm}){
$Hmmscan_hits{$gn_id}{$hmm} = $tmp[0];
}else{
$Hmmscan_hits{$gn_id}{$hmm} .= "\,".$tmp[0];
}
$Hmmscan_result{$gn_id}{$hmm}++;
}
}elsif(exists $Motif_pair{$hmm_basename}){
my $motif_hmm = "$METABOLIC_hmm_db_address/$hmm_basename.check.hmm";
my $motif_anti_hmm = "$METABOLIC_hmm_db_address/$Motif_pair{$hmm_basename}.check.hmm";
_get_1_from_input_faa("$input_protein_folder/total.faa",">$tmp[0]","$output/tmp.$hmm_basename.check.faa");
`hmmsearch --cpu 1 --tblout $output/tmp.$hmm_basename.check.hmmsearch_result.txt $motif_hmm $output/tmp.$hmm_basename.check.faa`;
`hmmsearch --cpu 1 --tblout $output/tmp.$Motif_pair{$hmm_basename}.check.hmmsearch_result.txt $motif_anti_hmm $output/tmp.$hmm_basename.check.faa`;
my $motif_check_score = _get_check_score("$output/tmp.$hmm_basename.check.hmmsearch_result.txt");
my $motif_anti_check_score = _get_check_score("$output/tmp.$Motif_pair{$hmm_basename}.check.hmmsearch_result.txt");
if ($motif_check_score >= $motif_anti_check_score and $motif_check_score != 0){
if (! exists $Hmmscan_hits{$gn_id}{$hmm}){
$Hmmscan_hits{$gn_id}{$hmm} = $tmp[0];
}else{
$Hmmscan_hits{$gn_id}{$hmm} .= "\,".$tmp[0];
}
$Hmmscan_result{$gn_id}{$hmm}++;
}
`rm $output/tmp.$hmm_basename.check.faa $output/tmp.$hmm_basename.check.hmmsearch_result.txt $output/tmp.$Motif_pair{$hmm_basename}.check.hmmsearch_result.txt`;
}else{ # Do not have motif check step
if (! exists $Hmmscan_hits{$gn_id}{$hmm}){
$Hmmscan_hits{$gn_id}{$hmm} = $tmp[0];
}else{
$Hmmscan_hits{$gn_id}{$hmm} .= "\,".$tmp[0];
}
$Hmmscan_result{$gn_id}{$hmm}++;
}
}
}else{
my ($hmm_basename) = $hmm =~ /^(.+?)\.hmm/;
if (exists $Motif{$hmm_basename}){
my $seq; # The protein seq
my $motif = $Motif{$hmm_basename}; $motif =~ s/X/\[ARNDCQEGHILKMFPSTWYV\]/g;
my %Seq_gn = _store_seq("$input_protein_folder/total.faa"); # get the total genome sequences
$seq = $Seq_gn{">$tmp[0]"};
if ($seq =~ /$motif/){
if (! exists $Hmmscan_hits{$gn_id}{$hmm}){
$Hmmscan_hits{$gn_id}{$hmm} = $tmp[0];
}else{
$Hmmscan_hits{$gn_id}{$hmm} .= "\,".$tmp[0];
}
$Hmmscan_result{$gn_id}{$hmm}++;
}
}elsif(exists $Motif_pair{$hmm_basename}){
my $motif_hmm = "$METABOLIC_hmm_db_address/$hmm_basename.check.hmm";
my $motif_anti_hmm = "$METABOLIC_hmm_db_address/$Motif_pair{$hmm_basename}.check.hmm";
_get_1_from_input_faa("$input_protein_folder/total.faa",">$tmp[0]","$output/tmp.$hmm_basename.check.faa");
`hmmsearch --cpu 1 --tblout $output/tmp.$hmm_basename.check.hmmsearch_result.txt $motif_hmm $output/tmp.$hmm_basename.check.faa`;
`hmmsearch --cpu 1 --tblout $output/tmp.$Motif_pair{$hmm_basename}.check.hmmsearch_result.txt $motif_anti_hmm $output/tmp.$hmm_basename.check.faa`;
my $motif_check_score = _get_check_score("$output/tmp.$hmm_basename.check.hmmsearch_result.txt");
my $motif_anti_check_score = _get_check_score("$output/tmp.$Motif_pair{$hmm_basename}.check.hmmsearch_result.txt");
if ($motif_check_score >= $motif_anti_check_score and $motif_check_score != 0){
if (! exists $Hmmscan_hits{$gn_id}{$hmm}){
$Hmmscan_hits{$gn_id}{$hmm} = $tmp[0];
}else{
$Hmmscan_hits{$gn_id}{$hmm} .= "\,".$tmp[0];
}
$Hmmscan_result{$gn_id}{$hmm}++;
}
`rm $output/tmp.$hmm_basename.check.faa $output/tmp.$hmm_basename.check.hmmsearch_result.txt $output/tmp.$Motif_pair{$hmm_basename}.check.hmmsearch_result.txt`;
}else{
if (! exists $Hmmscan_hits{$gn_id}{$hmm}){
$Hmmscan_hits{$gn_id}{$hmm} = $tmp[0];
}else{
$Hmmscan_hits{$gn_id}{$hmm} .= "\,".$tmp[0];
}
$Hmmscan_result{$gn_id}{$hmm}++;
}
}
}
}
close INN;
}
close IN;
`rm $input_protein_folder/total.faa`;
# Skip printing out each hmm faa collection
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Skip printing out each hmm faa collection\n";
# Skip doing the KEGG module calculating
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Skipe doing the KEGG module result calculating\n";
# Skip doing the KEGG identifier result calculating
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Skip doing the KEGG identifier \(KO id\) result calculating\n";
# Skip searching CAZymes by dbCAN2
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Skip searching CAZymes by dbCAN2\n";
# Skip searching MEROPS peptidase
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Skip searching MEROPS peptidase\n";
# Skip generating METABOLIC table
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Skip generating METABOLIC table\n";
# Draw element cycling diagrams
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Drawing element cycling diagrams...\n";
# Store R pathways
my %R_pathways = (); #step => hmms
my %R_hmm_ids = ();
open IN, "$R_pathways";
while (<IN>){
chomp;
my @tmp = split (/\t/);
$R_pathways{$tmp[0]} = $tmp[1];
if ($tmp[1] !~ /\;/){
my @tmp2 = split (/\,/,$tmp[1]);
foreach my $key (@tmp2){
$R_hmm_ids{$key} = 1;
}
}elsif ($tmp[1] =~ /\;/){
my @tmp2 = split (/\;/,$tmp[1]);
foreach my $key (@tmp2){
my @tmp3 = split (/\,/,$key);
foreach my $key2 (@tmp3){
if ($key2 !~ /NO/){
$R_hmm_ids{$key2} = 1;
}
}
}
}
}
close IN;
`mkdir $output/METABOLIC_Figures_Input.$second_run_suffix`;
`mkdir $output/METABOLIC_Figures_Input.$second_run_suffix/Nutrient_Cycling_Diagram_Input`;
`mkdir $output/METABOLIC_Figures.$second_run_suffix`;
# Get each R pathway input files
my %Total_R_input = (); #pathway => gn => 1 or 0
foreach my $gn (sort keys %Hmmscan_result){
my %R_input = (); #for each input file
foreach my $key (sort keys %R_pathways){
$R_input{$key} = 0; $Total_R_input{$key}{$gn} = 0;
my $hmms = $R_pathways{$key};
if ($hmms !~ /\;/){
foreach my $hmm_id (sort keys %R_hmm_ids){
if ($hmms =~ /$hmm_id/ and $Hmmscan_result{$gn}{$hmm_id}){
$R_input{$key} = 1; $Total_R_input{$key}{$gn} = 1;
}
}
}elsif ($hmms =~ /\;/){
my ($hmms_1,$hmms_2) = $hmms =~ /^(.+?)\;(.+?)$/;
if ($hmms_2 !~ /NO/){
my $logic1 = 0; my $logic2 = 0;
foreach my $hmm_id (sort keys %R_hmm_ids){
if ($hmms_1 =~ /$hmm_id/ and $Hmmscan_result{$gn}{$hmm_id}){
$logic1 = 1;
}
if ($hmms_2 =~ /$hmm_id/ and $Hmmscan_result{$gn}{$hmm_id}){
$logic2 = 1;
}
}
if ($logic1 and $logic2){
$R_input{$key} = 1; $Total_R_input{$key}{$gn} = 1;
}
}elsif ($hmms_2 =~ /NO/){
my $logic1 = 0; my $logic2 = 1;
foreach my $hmm_id (sort keys %R_hmm_ids){
if ($hmms_1 =~ /$hmm_id/ and $Hmmscan_result{$gn}{$hmm_id}){
$logic1 = 1;
}
if ($hmms_2 =~ /$hmm_id/ and $Hmmscan_result{$gn}{$hmm_id}){ # if $hmms_2 contains $hmm_id , and the genome has $hmm_id hit(s), then it will be false (0)
$logic2 = 0;
}
}
if ($logic1 and $logic2){
$R_input{$key} = 1; $Total_R_input{$key}{$gn} = 1;
}
}
}
}
open OUT, ">$output/METABOLIC_Figures_Input.$second_run_suffix/Nutrient_Cycling_Diagram_Input/$gn.R_input.txt";
foreach my $key (sort keys %R_input){
print OUT "$key\t$R_input{$key}\n";
}
close OUT;
}
my %Genome_cov_constant = ();
# The genome coverage: genome id => coverage value
if ($omic_reads_parameters){
my %Genome_cov = ();
if ($depth_file){
%Genome_cov = _get_Genome_coverge_by_depth_file($omic_reads_parameters,$depth_file);
}else{
if ($sequencing_type eq 'illumina'){
%Genome_cov = _get_Genome_coverge($omic_reads_parameters,$input_genome_folder);
}else{
%Genome_cov = _get_Genome_coverge_for_long_reads($omic_reads_parameters,$input_genome_folder);
}
}
%Genome_cov_constant = %Genome_cov;
my %Total_R_input_2 = (); # pathway => genome numbers \t genome coverage percentage
foreach my $pth (sort keys %Total_R_input){
my $gn_no = 0; my $gn_cov_percentage = 0;
foreach my $gn (sort keys %Hmmscan_result){
if ($Total_R_input{$pth}{$gn}){
$gn_no += $Total_R_input{$pth}{$gn};
if ($Genome_cov{$gn}){
$gn_cov_percentage += $Genome_cov{$gn};
}
}
}
$Total_R_input_2{$pth} = "$gn_no\t$gn_cov_percentage";
}
open OUT, ">$output/METABOLIC_Figures_Input.$second_run_suffix/Nutrient_Cycling_Diagram_Input/Total.R_input.txt";
foreach my $key (sort keys %Total_R_input_2){
print OUT "$key\t$Total_R_input_2{$key}\n";
}
close OUT;
}
`Rscript $METABOLIC_dir/draw_biogeochemical_cycles.R $output/METABOLIC_Figures_Input.$second_run_suffix/Nutrient_Cycling_Diagram_Input $output/Output TRUE 2> /dev/null`;
`mv $output/Output/draw_biogeochem_cycles $output/METABOLIC_Figures.$second_run_suffix/Nutrient_Cycling_Diagrams; rm -r $output/Output`;
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Drawing element cycling diagrams finished\n";
my $endtime = "";
if ($omic_reads_parameters){
# Draw metabolic handoff diagrams
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Drawing metabolic handoff diagrams...\n";
# Store metabolic handoff steps 1
my %R_mh_01 = (); #step => KOs
my %R_mh_01_hmm_ids = ();
my %Letter2reaction = (); # D -> S2-=>S0
open IN, "$R_mh_01";
while (<IN>){
chomp;
if (/\:/ or /\+/){
my @tmp = split (/\t/);
if ($tmp[0] =~ /\:/){
my ($step,$reaction) = $tmp[0] =~ /^(\D+?)\:(.+?)$/;
$R_mh_01{$step} = $tmp[2]; $Letter2reaction{$step} = $reaction;
my @tmp2 = split (/\,/,$tmp[2]);
foreach my $key (@tmp2){
$R_mh_01_hmm_ids{$key} = 1;
}
}else{
$R_mh_01{$tmp[0]} = $tmp[2];
}
}
}
close IN;
my %R_mh_01_summary = (); # step => gn => 1 or 0
foreach my $gn (sort keys %Hmmscan_result){
foreach my $key (sort keys %R_mh_01){
$R_mh_01_summary{$key}{$gn} = 0;
if ($key !~ /\+/){
my $hmms = $R_mh_01{$key};
foreach my $hmm_id (sort keys %R_mh_01_hmm_ids){
if ($hmms =~ /$hmm_id/ and $Hmmscan_result{$gn}{$hmm_id}){
$R_mh_01_summary{$key}{$gn} = 1;
}
}
}else{
my $hmms_total = $R_mh_01{$key};
my @tmp_hmm = split (/\;/,$hmms_total);
my $count = 0; my $tmp_hmm_scalar = scalar @tmp_hmm;
foreach my $hmms (@tmp_hmm){
my $logic = 0;
foreach my $hmm_id (sort keys %R_mh_01_hmm_ids){
if ($hmms =~ /$hmm_id/ and $Hmmscan_result{$gn}{$hmm_id}){
$logic = 1 ;
}
}
if ($logic){
$count++;
}
}
if ($tmp_hmm_scalar == $count){
$R_mh_01_summary{$key}{$gn} = 1;
}
}
}
}
# The genome coverage: genome id => coverage value
if ($omic_reads_parameters){
my %Genome_cov = %Genome_cov_constant;
my %Total_R_hm_input_1 = (); # step => genome numbers \t genome coverage percentage
foreach my $step (sort keys %R_mh_01){
my $gn_no = 0; my $gn_cov_percentage = 0;
foreach my $gn (sort keys %Hmmscan_result){
if ($R_mh_01_summary{$step}{$gn}){
$gn_no += $R_mh_01_summary{$step}{$gn};
if ($Genome_cov{$gn}){
$gn_cov_percentage += $Genome_cov{$gn};
}
}
}
$Total_R_hm_input_1{$step} = "$gn_no\t$gn_cov_percentage";
}
open OUT, ">$output/METABOLIC_Figures_Input.$second_run_suffix/Sequential_Transformation_input_1.txt";
foreach my $key (sort keys %Total_R_hm_input_1){
print OUT "$key\t$Total_R_hm_input_1{$key}\n";
}
close OUT;
}
# Store the CAZy map
my %CAZy_map = (); #GH => enzymes
open IN, "$CAZy_map_address";
while (<IN>){
chomp;
if (!/^Family/){
my @tmp = split (/\t/);
$CAZy_map{$tmp[0]} = $tmp[1];
}
}
close IN;
# Store dbCAN running result
my %dbCANout = (); # genome => hmmid => number
my %dbCANout2 = (); # genome => hmmid => hits
my %Hmm_dbCAN2_id = (); # hmm => 1
open IN, "ls $output/intermediate_files/dbCAN2_Files/*.dbCAN2.out.dm.ps |";
while (<IN>)
{
my $file = $_;
my ($gn_id) = $file =~ /^$output\/intermediate_files\/dbCAN2_Files\/(.+?)\.dbCAN2\.out\.dm\.ps/;
open INN, "$file";
while (<INN>){
if (/^GH|^PL/){
my @tmp = split(/\t/,$_);
my ($hmmid) = $tmp[0] =~ /(\S+?)\.hmm/;
my ($hmmid_p1,$hmmid_p2) = $hmmid =~ /^(\D+?)(\d+)/;
my $num=(sprintf "%03d", $hmmid_p2);
$hmmid = $hmmid_p1.$num;
$Hmm_dbCAN2_id{$hmmid} = 1;
my ($name) = $tmp[2];
$dbCANout{$gn_id}{$hmmid}++;
if (!exists $dbCANout2{$gn_id}{$hmmid}){
$dbCANout2{$gn_id}{$hmmid} = $name;
}else{
$dbCANout2{$gn_id}{$hmmid} .= "\;".$name;
}
}
}
close INN;
}
close IN;
# Store metabolic handoff steps 2
my %R_mh_02 = (); #step => enzymes
my %R_mh_02_enzyme_ids = ();
open IN, "$R_mh_02";
while (<IN>){
chomp;
if (/\:/ or /\+/){
my @tmp = split (/\t/);
if ($tmp[0] =~ /\:/){
my ($step,$reaction) = $tmp[0] =~ /^(\D+?)\:(.+?)$/;
$R_mh_02{$step} = $tmp[2]; $Letter2reaction{$step} = $reaction;
my @tmp2 = split (/\;\s/,$tmp[2]);
foreach my $key (@tmp2){
$R_mh_02_enzyme_ids{$key} = 1;
}
}else{
$R_mh_02{$tmp[0]} = $tmp[2];
}
}
}
close IN;
my %R_mh_02_summary = (); # step => gn => 1 or 0
foreach my $step (sort keys %R_mh_02){
foreach my $gn (sort keys %dbCANout){
$R_mh_02_summary{$step}{$gn} = 0;
if ($step !~ /\+/){
my $enzymes = $R_mh_02{$step};
my @tmp_enzymes = split (/\;/, $enzymes);
foreach my $enzyme_id (@tmp_enzymes){
foreach my $GH_id (sort keys %CAZy_map){
my @tmp_enzymes_2 = split (/\;/,$CAZy_map{$GH_id});
foreach my $enzyme_id_2 (@tmp_enzymes_2){
if ($enzyme_id_2 eq $enzyme_id and $dbCANout{$gn}{$GH_id}){
$R_mh_02_summary{$step}{$gn} = 1;
}
}
}
}
}else{
my $hmms_total = $R_mh_02{$step};
my @tmp_hmm = split (/\|/,$hmms_total);
my $count = 0; my $tmp_hmm_scalar = scalar @tmp_hmm;
foreach my $hmms (@tmp_hmm){
my $logic = 0;
my @tmp_enzymes = split (/\;/, $hmms);
foreach my $enzyme_id (@tmp_enzymes){
foreach my $GH_id (sort keys %CAZy_map){
my @tmp_enzymes_2 = split (/\;/,$CAZy_map{$GH_id});
foreach my $enzyme_id_2 (@tmp_enzymes_2){
if ($enzyme_id_2 eq $enzyme_id and $dbCANout{$gn}{$GH_id}){
$logic = 1;
}
}
}
}
if ($logic){
$count++;
}
}
if ($tmp_hmm_scalar == $count){
$R_mh_02_summary{$step}{$gn} = 1;
}
}
}
}
# The genome coverage: genome id => coverage value
if ($omic_reads_parameters){
my %Genome_cov = %Genome_cov_constant;
my %Total_R_hm_input_2 = (); # step => genome numbers \t genome coverage percentage
foreach my $step (sort keys %R_mh_02){
my $gn_no = 0; my $gn_cov_percentage = 0;
foreach my $gn (sort keys %Hmmscan_result){
if ($R_mh_02_summary{$step}{$gn}){
$gn_no += $R_mh_02_summary{$step}{$gn};
if ($Genome_cov{$gn}){
$gn_cov_percentage += $Genome_cov{$gn};
}
}
}
$Total_R_hm_input_2{$step} = "$gn_no\t$gn_cov_percentage";
}
open OUT, ">$output/METABOLIC_Figures_Input.$second_run_suffix/Sequential_Transformation_input_2.txt";
foreach my $key (sort keys %Total_R_hm_input_2){
print OUT "$key\t$Total_R_hm_input_2{$key}\n";
}
close OUT;
}
`mkdir $output/newdir`;
`Rscript $METABOLIC_dir/draw_sequential_reaction_diagram.R $output/METABOLIC_Figures_Input.$second_run_suffix/Sequential_Transformation_input_1.txt $output/METABOLIC_Figures_Input.$second_run_suffix/Sequential_Transformation_input_2.txt $R_mh_tsv $R_order_of_input_01 $R_order_of_input_02 $output/newdir 2> /dev/null`;
`mv $output/newdir/Bar_plot/bar_plot_input_1.pdf $output/METABOLIC_Figures.$second_run_suffix/Sequential_transformation_01.pdf`;
`mv $output/newdir/Bar_plot/bar_plot_input_2.pdf $output/METABOLIC_Figures.$second_run_suffix/Sequential_transformation_02.pdf`;
`rm -r $output/newdir`;
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Drawing metabolic handoff diagrams finished\n";
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Drawing energy flow chart...\n";
# Store the bin category
#system ("gtdbtk classify_wf --cpus $cpu_numbers -x fasta --genome_dir $input_genome_folder --skip_ani_screen --out_dir $output/intermediate_files/gtdbtk_Genome_files 2> /dev/null");
my %Bin2Cat = (); # bin => category, for instance, Acidimicrobiia_bacterium_UWMA-0264 => [0] Actinobacteriota (phylum) [1] XX (class) [2] XX (order) [3] XX (family) [4] XX (genus) [5] XX (species) [6] XX (bin)
if (-e "$output/intermediate_files/gtdbtk_Genome_files/gtdbtk.bac120.summary.tsv"){
open IN, "$output/intermediate_files/gtdbtk_Genome_files/gtdbtk.bac120.summary.tsv";
while (<IN>){
chomp;
if (!/^user_genome/){
my @tmp = split (/\t/);
if ($tmp[1] =~ /p\_\_Proteobacteria/){
my $cat = ""; my $cat1 = ""; my $cat2 = ""; my $cat3 = ""; my $cat4 = ""; my $cat5 = "";
($cat) = $tmp[1] =~ /\;c\_\_(.*?)\;/;
if(!$cat){
$cat = "NK_phylum";
}
$Bin2Cat{$tmp[0]}[0] = $cat; # Store bin to phylum info
($cat1) = $tmp[1] =~ /\;c\_\_(.*?)\;/;
if(!$cat1){
$cat1 = "NK_class";
}
$Bin2Cat{$tmp[0]}[1] = $cat1; # Store bin to class info
($cat2) = $tmp[1] =~ /\;o\_\_(.*?)\;/;
if(!$cat2){
$cat2 = "NK_order";
}
$Bin2Cat{$tmp[0]}[2] = $cat2; # Store bin to order info
($cat3) = $tmp[1] =~ /\;f\_\_(.*?)\;/;
if(!$cat3){
$cat3 = "NK_family";
}
$Bin2Cat{$tmp[0]}[3] = $cat3; # Store bin to family info
($cat4) = $tmp[1] =~ /\;g\_\_(.*?)\;/;
if(!$cat4){
$cat4 = "NK_genus";
}
$Bin2Cat{$tmp[0]}[4] = $cat4; # Store bin to genus info
($cat5) = $tmp[1] =~ /\;s\_\_(.*?)$/;
if(!$cat5){
$cat5 = "NK_species";
}
$Bin2Cat{$tmp[0]}[5] = $cat5; # Store bin to species info
$Bin2Cat{$tmp[0]}[6] = $tmp[0]; # Store bin to bin info
}else{
my $cat = ""; my $cat1 = ""; my $cat2 = ""; my $cat3 = ""; my $cat4 = ""; my $cat5 = "";
($cat) = $tmp[1] =~ /\;p\_\_(.*?)\;/;
if(!$cat){
$cat = "NK_phylum";
}
$Bin2Cat{$tmp[0]}[0] = $cat; # Store bin to phylum info
($cat1) = $tmp[1] =~ /\;c\_\_(.*?)\;/;
if(!$cat1){
$cat1 = "NK_class";
}
$Bin2Cat{$tmp[0]}[1] = $cat1; # Store bin to class info
($cat2) = $tmp[1] =~ /\;o\_\_(.*?)\;/;
if(!$cat2){
$cat2 = "NK_order";
}
$Bin2Cat{$tmp[0]}[2] = $cat2; # Store bin to order info
($cat3) = $tmp[1] =~ /\;f\_\_(.*?)\;/;
if(!$cat3){
$cat3 = "NK_family";
}
$Bin2Cat{$tmp[0]}[3] = $cat3; # Store bin to family info
($cat4) = $tmp[1] =~ /\;g\_\_(.*?)\;/;
if(!$cat4){
$cat4 = "NK_genus";
}
$Bin2Cat{$tmp[0]}[4] = $cat4; # Store bin to genus info
($cat5) = $tmp[1] =~ /\;s\_\_(.*?)$/;
if(!$cat5){
$cat5 = "NK_species";
}
$Bin2Cat{$tmp[0]}[5] = $cat5; # Store bin to species info
$Bin2Cat{$tmp[0]}[6] = $tmp[0]; # Store bin to bin info
}
}
}
close IN;
}
if (-e "$output/intermediate_files/gtdbtk_Genome_files/gtdbtk.ar53.summary.tsv"){
open IN, "$output/intermediate_files/gtdbtk_Genome_files/gtdbtk.ar53.summary.tsv";
while (<IN>){
chomp;
if (!/^user_genome/){
my @tmp = split (/\t/);
my $cat = ""; my $cat1 = ""; my $cat2 = ""; my $cat3 = ""; my $cat4 = ""; my $cat5 = "";
($cat) = $tmp[1] =~ /\;p\_\_(.*?)\;/;
if(!$cat){
$cat = "NK_phylum";
}
$Bin2Cat{$tmp[0]}[0] = $cat; # Store bin to phylum info
($cat1) = $tmp[1] =~ /\;c\_\_(.*?)\;/;
if(!$cat1){
$cat1 = "NK_class";
}
$Bin2Cat{$tmp[0]}[1] = $cat1; # Store bin to class info
($cat2) = $tmp[1] =~ /\;o\_\_(.*?)\;/;
if(!$cat2){
$cat2 = "NK_order";
}
$Bin2Cat{$tmp[0]}[2] = $cat2; # Store bin to order info
($cat3) = $tmp[1] =~ /\;f\_\_(.*?)\;/;
if(!$cat3){
$cat3 = "NK_family";
}
$Bin2Cat{$tmp[0]}[3] = $cat3; # Store bin to family info
($cat4) = $tmp[1] =~ /\;g\_\_(.*?)\;/;
if(!$cat4){
$cat4 = "NK_genus";
}
$Bin2Cat{$tmp[0]}[4] = $cat4; # Store bin to genus info
($cat5) = $tmp[1] =~ /\;s\_\_(.*?)$/;
if(!$cat5){
$cat5 = "NK_species";
}
$Bin2Cat{$tmp[0]}[5] = $cat5; # Store bin to species info
$Bin2Cat{$tmp[0]}[6] = $tmp[0]; # Store bin to bin info
}
}
close IN;
}
my %Hash_gn_n_pth = ();
my %Total_R_community_coverage = (); # genome\tpathway => category \t pathway \t genome coverage percentage
if ($omic_reads_parameters){
my %Genome_cov = %Genome_cov_constant;
foreach my $pth (sort keys %Total_R_input){
my $gn_cov_percentage = 0;
foreach my $gn (sort keys %Hmmscan_result){
if ($Genome_cov{$gn} and $Total_R_input{$pth}{$gn}){
$gn_cov_percentage = $Genome_cov{$gn};
my $cat = $Bin2Cat{$gn}[0];
my $gn_n_pth = "$gn\t$pth"; $Hash_gn_n_pth{$gn_n_pth} = 1;
$Total_R_community_coverage{$gn_n_pth} = "$cat\t$pth\t$gn_cov_percentage";
}
}
}
}
my %Total_R_community_coverage2 = (); # $genome\tpath pair => cat \t coverage percentage average
foreach my $gn (sort keys %Hmmscan_result){
my %Path = (); # path => 1
foreach my $gn_n_pth (sort keys %Total_R_community_coverage){
if ($gn_n_pth =~ /$gn\t/){
my @tmp = split (/\t/,$gn_n_pth);
$Path{$tmp[1]} = 1;
}
}
my @Path_keys = sort keys %Path;
for(my $i=0; $i<=$#Path_keys; $i++){
for(my $j = $i+1; $j<=$#Path_keys; $j++){
my $pair = "$Path_keys[$i]\t$Path_keys[$j]";
my $coverage = 0;
my @tmp1 = split (/\t/, $Total_R_community_coverage{"$gn\t$Path_keys[$i]"});
my @tmp2 = split (/\t/, $Total_R_community_coverage{"$gn\t$Path_keys[$j]"});
$coverage = ($tmp1[2] + $tmp2[2]) / 2;
$Total_R_community_coverage2{"$gn\t$pair"} = $Bin2Cat{$gn}[0]."\t".$coverage;
}
}
}
open OUT, ">$output/METABOLIC_Figures_Input.$second_run_suffix/Metabolic_Sankey_diagram_input.txt";
foreach my $gn_n_pth (sort keys %Hash_gn_n_pth){
print OUT "$Total_R_community_coverage{$gn_n_pth}\n";
}
close OUT;
open OUT, ">$output/METABOLIC_Figures_Input.$second_run_suffix/Functional_network_input.txt";
print OUT "#Genome\tStep1\tStep2\tTaxonomic Group\tCoverage value\(average\)\n";
foreach my $gn_n_pair (sort keys %Total_R_community_coverage2){
print OUT "$gn_n_pair\t$Total_R_community_coverage2{$gn_n_pair}\n";
}
close OUT;
`Rscript $METABOLIC_dir/draw_metabolic_Sankey_diagram.R $output/METABOLIC_Figures_Input.$second_run_suffix/Metabolic_Sankey_diagram_input.txt $output/Output_energy_flow 2> /dev/null`;
`mv $output/Output_energy_flow/Energy_plot/network.plot.pdf $output/METABOLIC_Figures.$second_run_suffix/Metabolic_Sankey_diagram.pdf; rm -r $output/Output_energy_flow`;
`Rscript $METABOLIC_dir/draw_functional_network_diagram.R $output/METABOLIC_Figures_Input.$second_run_suffix/Functional_network_input.txt $output/OutputFolder_Energy 2> /dev/null`;
`mv $output/OutputFolder_Energy/network_plot $output/METABOLIC_Figures.$second_run_suffix/Functional_network_figures; rm -r $output/OutputFolder_Energy`;
$datestring = strftime "%Y-%m-%d %H:%M:%S", localtime;
print "\[$datestring\] Drawing energy flow chart finished\n";