forked from WGLab/phenolyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disease_annotation.pl
executable file
·1922 lines (1614 loc) · 60.5 KB
/
disease_annotation.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
######################################################################
## LIBRARIES
use strict;
use warnings;
use Carp;
use Pod::Usage;
use Getopt::Long;
use Cwd;
use File::Basename;
use Bio::OntologyIO;
use Graph::Directed;
use File::Copy;
use JSON;
## LIBRARIES CONDITIONALLY LOADED
## Shown here for documentation purposes.
# Parallel::ForkManager;
##
######################################################################
######################################################################
## INITIALISATION
our (@out_gene_scores_file);
# Declare help options
our (
$verbose,
$help,
$man,
$buildver,
$bedfile
);
# Declare query options
our (
$query_diseases,
$if_file,
$if_exact_match,
$prediction,
$is_phenotype,
$if_wordcloud,
$if_use_precalc
);
# Declare test options
our (
$if_hi,
$if_rvis
);
# Declare
our (
$out,
$database_directory,
$precalc_db_directory,
$if_logistic_regression,
$user_nproc
);
# Declare weight options
our (
$HPRD_WEIGHT,
$BIOSYSTEM_WEIGHT,
$GENE_FAMILY_WEIGHT,
%GENE_WEIGHT,
$HTRI_WEIGHT,
$GENE_DISEASE_WEIGHT,
$INTERCEPT
);
# Declare files options
our (
$ctd_disease_file,
$hprd_file,
$biosystem_file,
$disease_count_file,
$gene_disease_score_file,
$hpo_annotation_file,
$gene_annotation_file,
$omim_disease_id_file,
$biosystem_to_info_file,
$gene_family_file,
$htri_file,
$omim_description_file,
$hi_gene_score_file,
$rvis_gene_score_file,
$addon_gene_disease_score_file,
$addon_gene_gene_score_file,
$addon_gene_disease_weight,
$addon_gene_gene_weight,
$genelist,
@genes,
%gene_hash,
%gene_id,
%hi_score,
%rvis_score,
$path,
$work_path,
$phenotype_to_gene_file
);
# %gene_hash ( $gene => "Not Annotated" or "Chr:Pos1 - Pos2")
# %omim_disease (lc $first_disease => join(";",$each_disease) )
# Declare options
GetOptions(
'verbose|v' =>\$verbose,
'help|h' =>\$help,
'man|m' =>\$man,
'file|f' =>\$if_file,
'directory|d=s' =>\$database_directory,
'use_precalc' =>\$if_use_precalc,
'work_directory|w=s' =>\$work_path,
'out=s' =>\$out,
'prediction|p' =>\$prediction,
'buildver=s' =>\$buildver,
'bedfile=s' =>\$bedfile,
'gene=s' =>\$genelist,
'phenotype|ph' =>\$is_phenotype,
'exact' =>\$if_exact_match,
'logistic' =>\$if_logistic_regression,
'haploinsufficiency|hi' =>\$if_hi,
'intolerance|it' =>\$if_rvis,
'addon=s' =>\$addon_gene_disease_score_file,
'addon_gg=s' =>\$addon_gene_gene_score_file,
'addon_weight=s' =>\$addon_gene_disease_weight,
'addon_gg_weight=s' =>\$addon_gene_gene_weight,
'hprd_weight=s' =>\$HPRD_WEIGHT,
'biosystem_weight=s' =>\$BIOSYSTEM_WEIGHT,
'gene_family_weight=s' =>\$GENE_FAMILY_WEIGHT,
'htri_weight=s' =>\$HTRI_WEIGHT,
'gwas_weight=s' =>\$GENE_WEIGHT{"GWAS"},
'gene_reviews_weight=s' =>\$GENE_WEIGHT{"GENE_REVIEWS"},
'clinvar_weight=s' =>\$GENE_WEIGHT{"CLINVAR"},
'omim_weight=s' =>\$GENE_WEIGHT{"OMIM"},
'orphanet_weight=s' =>\$GENE_WEIGHT{"ORPHANET"},
'wordcloud' =>\$if_wordcloud,
'nproc=i' =>\$user_nproc
) or pod2usage ();
# Launch help or man
$help and pod2usage (-verbose=>1, -exitval=>1, -output=>\*STDOUT);
$man and pod2usage (-verbose=>2, -exitval=>1, -output=>\*STDOUT);
@ARGV or pod2usage (" ERROR: Please enter disease names!!");
@ARGV == 1 or pod2usage (" ERROR: too many input arguments");
# Initialise logistics regression
if($if_logistic_regression) {
print STDOUT "NOTICE: The logistic regression model was used!!!\n";
$GENE_DISEASE_WEIGHT = 9.5331966;
$HPRD_WEIGHT = 0.8335866;
$BIOSYSTEM_WEIGHT = 0.1755904 ;
$GENE_FAMILY_WEIGHT = 0.3561601 ;
$HTRI_WEIGHT = 4.1003533 ;
}
# Initialise variables
$GENE_DISEASE_WEIGHT = 1.0 unless (defined $GENE_DISEASE_WEIGHT);
$HPRD_WEIGHT = 0.1 unless (defined $HPRD_WEIGHT);
$BIOSYSTEM_WEIGHT = 0.05 unless (defined $BIOSYSTEM_WEIGHT);
$GENE_FAMILY_WEIGHT = 0.05 unless (defined $GENE_FAMILY_WEIGHT);
$HTRI_WEIGHT = 0.05 unless (defined $HTRI_WEIGHT );
$GENE_WEIGHT{"GWAS"} = 1.0 unless (defined $GENE_WEIGHT{"GWAS"});
$GENE_WEIGHT{"GENE_REVIEWS"} = 1.0 unless (defined $GENE_WEIGHT{"GENE_REVIEWS"});
$GENE_WEIGHT{"CLINVAR"} = 1.0 unless (defined $GENE_WEIGHT{"CLINVAR"} );
$GENE_WEIGHT{"OMIM"} = 1.0 unless (defined $GENE_WEIGHT{"OMIM"} );
$GENE_WEIGHT{"ORPHANET"} = 1.0 unless (defined $GENE_WEIGHT{"ORPHANET"} );
$GENE_WEIGHT{"HPO_PHENOTYPE_GENE"} = 1.0 unless (defined $GENE_WEIGHT{"HPO_PHENOTYPE_GENE"} );
$addon_gene_disease_weight = 1.0 unless (defined $addon_gene_disease_weight);
$addon_gene_gene_weight = 1.0 unless (defined $addon_gene_gene_weight);
$user_nproc = 1 unless (defined $user_nproc);
# Test input values
$user_nproc >= 1 or pod2usage (" ERROR: number of requested processes is zero or negative");
##
######################################################################
######################################################################
## Main program
setup_variables();
annotate_bedfile() if (defined $bedfile);
output_gene_prioritization();
##
######################################################################
######################################################################
## Subroutines
sub print_array
{
foreach (@_){
print "$_\n";
}
}
sub process_individual_term
{
my $individual_term = $_[0];
my $raw_individual_term = $individual_term;
if ($individual_term =~ /^\W*$/){ return; } # next if there is no word
$individual_term=TextStandardize($individual_term);
# For a normal term, disease name extension is needed
if ($individual_term !~/^all([\s_\-]diseases?)?$/i) {
# Expand the disease term and save them into files
# @diseases are lower case disease array, %disease_hash keeps the original disease name
my $diseases_reference = disease_extension($individual_term);
my %disease_hash = %$diseases_reference;
my @diseases;
for my $disease_key (keys %disease_hash) {
my @records =split("\n", $disease_hash{$disease_key});
for my $record (@records) {
return if(not $record);
my ($disease_line, $source) = split("\t", $record);
my @disease_terms = split(";", $disease_line);
for (@disease_terms){
my $each = $_;
$each=TextStandardize($each);
my $change2= $each =~ s/\btype //ig;
if($change2){
push @diseases,$each;
$disease_hash{$disease_key} = $each.';'.$disease_hash{$disease_key};
}
}
push @diseases,@disease_terms;
}
}
my ($hash, %disease_score_hash, @hpo_ids);
if ($is_phenotype) {
($hash, @hpo_ids) = phenotype_extension($individual_term);
%disease_score_hash = %$hash;
for (@diseases) {
my $disease_key = lc $_;
delete $disease_score_hash{$disease_key} if($disease_score_hash{$disease_key});
$disease_key = TextStandardize($disease_key);
delete $disease_score_hash{$disease_key} if($disease_score_hash{$disease_key});
}
for (keys %disease_score_hash) {
my $disease_score = join ("\t", ($disease_score_hash{$_}[1], $disease_score_hash{$_}[0]) );
push (@diseases, lc $disease_score);
}
}
#The non-word characters are changed into '_'
$individual_term=~s/\W+/_/g;
if(@hpo_ids) {
open(OUT_PHENOTYPE, ">$out"."_$individual_term"."_hpo") or die;
print OUT_PHENOTYPE $_."\n" for @hpo_ids;
close(OUT_PHENOTYPE);
}
open (OUT_DISEASE,">$out"."_$individual_term"."_diseases") or die;
for (keys %disease_hash) {
my @lines=split("\n", $disease_hash{$_});
for my $line (@lines) {
return if(not $line);
my @words = split("\t", $line);
my @diseases = split(";", $words[0]);
@diseases = Unique(@diseases);
my $disease_line = join(";",@diseases);
my $out_line = join("\t",($disease_line,$words[1]));
print OUT_DISEASE $out_line."\n";
}
}
if ($is_phenotype) {
print OUT_DISEASE join ("\t", ($disease_score_hash{$_}[1], $disease_score_hash{$_}[0]) )."\n"
for (keys %disease_score_hash);
}
close (OUT_DISEASE);
generate_wordcloud($individual_term, \%disease_hash, \%disease_score_hash) if($if_wordcloud);
if(@diseases==0){
print STDERR "ERROR: The input term -----$individual_term------ has no corresponding names in the disease database, please check your spelling!!!\n";
return;
}
my $i=0;
#Output the gene_score files
# $item = { $gene => [$score, $information_string] }
# $information_string = "ID (SOURCE) DISEASE_NAME RAW_SCORE
@diseases = map {
my @words = split("\t");
$words[0] = TextStandardize($words[0]);
$words[0] = lc $words[0];
join("\t", @words);
} @diseases;
@diseases = Unique(@diseases);
my ($item,$count) = score_genes (\@diseases, \@hpo_ids, $raw_individual_term);
my %output=();
@{$output{$_}} = @{$item->{$_}} for keys %$item;
if($count==0){
print STDERR "ERROR: The input term -----$individual_term------ has no results!!!\n";
return;
}
open( OUT_GENE_SCORE,">$out"."_$individual_term"."_gene_scores") or die "can't write to "."$out"."_$individual_term"."_gene_scores";
print OUT_GENE_SCORE "Tuple number in the gene_disease database for the term $individual_term: $count\n";
for my $gene (sort{ $output{$b}[0] <=> $output{$a}[0] }keys %output){
#The probability of the gene when the disease is given
my $p=$output{$gene}[0]/$count;
print OUT_GENE_SCORE $gene."\t"."Normalized score: $p\tRaw Score: $output{$gene}[0]\n".$output{$gene}[1]."\n";
}
print STDOUT "$raw_individual_term: -------------------------------------------------- \n";
close (OUT_GENE_SCORE);
}
# For the term 'all disease(s)'(case insensitive)
else {
$individual_term=~s/\W+/_/g;
open(OUT_GENE_SCORE,">$out"."_$individual_term"."_gene_scores") or die "can't write to "."$out"."_$individual_term"."_gene_scores";
my ($item,$count)=score_all_genes();
my %output=();
@{$output{$_}} = @{$item->{$_}} for keys %$item;
print OUT_GENE_SCORE "Tuple number in the gene_disease database for the term $individual_term: $count\n";
for my $gene(sort{ $output{$b}[0] <=> $output{$a}[0] }keys %output){
#The probability of the gene when the disease is given
my $p=$output{$gene}[0]/$count;
print OUT_GENE_SCORE $gene."\t"."Normalized score: $p\tRaw Score: $output{$gene}[0]\n".$output{$gene}[1]."\n";
}
print STDOUT "------------------------------------------------------------------------ \n";
close (OUT_GENE_SCORE);
}
}
sub get_terms_precalc_db
{
open(DB_MASTER, "$precalc_db_directory/hpos.txt") or die "Can't open $precalc_db_directory/hpos.txt !";
chomp(my @lines = <DB_MASTER>);
close(DB_MASTER);
return @lines
}
sub process_terms
{
my @disease_input = sort @_;
## Process each individual term
# Find pre-processed terms from the input
my @hpo_list = grep /hp:[0-9]*/, @disease_input;
# Search HPO DB only if there are HPO inputs
if ($if_use_precalc && @hpo_list > 0) {
my @found_list;
# Read master table
my @db_terms = get_terms_precalc_db();
my $i = 0;
for my $hpo(@hpo_list) {
(my $num = $hpo) =~ s/hp:*//;
# This assumes the HPO inputs are ordered and so are the DB terms
for(; $i < @db_terms; $i++) {
if( $num == $db_terms[$i] ){
push (@found_list, $hpo);
$i++;
last;
} elsif($num < $db_terms[$i]) {
last;
}
}
}
my $exe_ext = "";
unless ($^O =~ /Unix|Linux|unix|linux/) {
$exe_ext = ".exe";
}
# Copy preprocessed terms to directory
for my $hpo(@found_list) {
print("$hpo: Using HPO expansion found in pre-expanded database ...\n");
(my $mod_hpo = $hpo) =~ s/:/_/;
my $basename = "precalc_db_$mod_hpo";
my $zipfile = "$precalc_db_directory/db/$basename";
my $tempdir = "$precalc_db_directory/temp";
my $dst = "$out"."_$mod_hpo";
foreach (qw(_hpo _diseases _gene_scores)) {
# Extract from DB
if (system("unzip$exe_ext -q $zipfile $basename$_ -d $tempdir")) {
if (system("7z$exe_ext -e -y $zipfile $basename$_ -o $tempdir")) {
die "Failed unzipping file from DB. Install unzip or 7z command line program. File: $zipfile to $basename$_";
}
}
if (system("mv $tempdir/$basename$_ $dst$_")) {
die "Failed moving file from temp DB: $tempdir/$basename$_ -> $dst$_";
}
}
# Push to list of gene_scores files
push @out_gene_scores_file, "$dst"."_gene_scores";
}
# Continue with remaining terms
# @disease_input = @remaining_list;
my %temp;
@temp{ @disease_input } = ();
delete @temp{ @found_list };
@disease_input = keys %temp;
if (@disease_input == 0) {
return;
}
}
# Determine number of parallel subprocesses
my $n_procs = $user_nproc <= @disease_input ? $user_nproc : @disease_input;
print STDOUT "NOTICE: Processing $n_procs phenotypes at a time!\n";
if ($n_procs > 1) {
# Run in parallel after loading ForkManager. Test if the module is available.
eval {require Parallel::ForkManager};
if ($@) {
pod2usage ("Error in argument: you need to install Parallel::ForkManager module before parallelizing with the -nproc argument");
}
my $parallel_mngr = Parallel::ForkManager->new($n_procs);
for my $individual_term(@disease_input)
{
$parallel_mngr->start and next;
process_individual_term($individual_term);
$parallel_mngr->finish;
}
$parallel_mngr->wait_all_children();
} else {
# Process one input at a time.
for my $individual_term(@disease_input)
{
process_individual_term($individual_term);
}
}
## Fill @out_gene_scores_file array
for my $individual_term(@disease_input)
{
(my $individual_term = $individual_term) =~ s/:/_/;
my $dst = "$out"."_$individual_term";
if (-f "$dst"."_gene_scores") {
push @out_gene_scores_file, "$dst"."_gene_scores";
}
}
}
# The main sub to output prioritized genelist
sub output_gene_prioritization
{
my @disease_input = split (qr/[^ _,\w\.\-'\(\)\[\]\{\}:]+/,lc $query_diseases); #'
s/^\s+|\s+// for @disease_input;
# Process individual terms
process_terms(@disease_input);
# Finish processing individual terms
# Merge the gene_score files
# %output ( gene =>[score, content])
my ($item, $count)=merge_result();
my %output=();
my ($max_score, $min_score) = (0, 1);
@{$output{$_}} = @{$item->{$_}} for keys %$item;
open (MERGE, ">$out.merge_gene_scores") or die;
# open (MERGE_LEGACY, ">$out.merge_gene_scores_legacy") or die;
open (ANNOTATED,">$out.annotated_gene_scores") if (%gene_hash and not $prediction);
open (my $seed_fh, ">$out.seed_gene_list");
my $annotated_seed_fh;
#### header for seed gene list ####
printHeader($seed_fh, 0);
if (%gene_hash and not $prediction) {
open ($annotated_seed_fh, ">$out.annotated_gene_list");
printHeader($annotated_seed_fh,0);
}
# print MERGE_LEGACY "{\n\"header\": \"Tuple number in the gene_disease database for all the terms: $count\" \n";
print MERGE "[";
print ANNOTATED "Tuple number in the gene_disease databse for all the terms: $count \n" if (%gene_hash and not $prediction);
#Find the max and min score
for my $gene (keys %output) {
$max_score = $output{$gene}[0] if ($max_score < $output{$gene}[0]);
$min_score = $output{$gene}[0] if ($min_score > $output{$gene}[0]);
}
my $rank = 0;
my $annotated_rank = 0;
my (%hi_out, %rvis_out);
for my $gene(sort{ $output{$b}[0] <=> $output{$a}[0] }keys %output) {
$rank++;
my ($score, $content) = ($output{$gene}[0], $output{$gene}[1]);
#$score = ($score - $min_score)/$diff;
my $normalized_score = $score/$max_score;
#normalize scores of each detail
chomp($content);
my @content_lines = split("\n", $content);
my @content_lines_output;
my @content_lines_next;
my %content_json_dict;
for(@content_lines) {
my @words = split("\t");
my $detail_score = $words[3];
$detail_score = $detail_score/$max_score;
my $new_score = $detail_score * $GENE_DISEASE_WEIGHT;
my $line = join("\t", (@words[0,1,2],$detail_score));
my $new_line = join("\t", (@words[0,1,2],$new_score));
push (@content_lines_output,$line);
push (@content_lines_next, $new_line);
# Build disease hash, organized by origin (OMIM, ORPHANET...: words[0])
my ($db, $temp) = split(':', $words[0]);
my ($db_ids, $db_note) = $words[0] =~ /(.*) \((.*)\)/; # first group captures all
$db_note =~ s/(\r)//g; # remove "\r" if present
$words[2] =~ s/(\D+)//g; # remove "hp "
if (!exists($content_json_dict{$db})) {
$content_json_dict{"$db"}=[];
}
my %temp = (Id => $db_ids, IdNote => $db_note, Condition => $words[1], Hp => $words[2], Score => $detail_score);
push(@{ $content_json_dict{$db} }, \%temp);
}
my $content_json = encode_json \%content_json_dict;
$content = join("\n", @content_lines_output)."\n";
my $new_content = join("\n", @content_lines_next)."\n";
#Normalized the input for prediction
$item->{$gene}[0] = $normalized_score * $GENE_DISEASE_WEIGHT;
$item->{$gene}[1] = $new_content;
$item->{$gene}[2] = $normalized_score;
#print out results
if($rank != 1) {
print MERGE ","
}
print MERGE "{\"Name\":\"$gene\",\"Id\":\"ID:$gene_id{$gene}\",\"Score\":$normalized_score,\"Diseases\":[$content_json]}";
# print MERGE_LEGACY $gene."\t"."ID:$gene_id{$gene} -\t$normalized_score\n".$content."\n";
print ANNOTATED $gene."\tID:$gene_id{$gene} ".$gene_hash{$gene}."\t$normalized_score\n".$content."\n"
if ($gene_hash{$gene} and not $prediction);
#Normalized score for the genelist
$normalized_score = sprintf('%.4g', $normalized_score);
print $seed_fh $rank."\t".$gene."\t".$gene_id{$gene}."\t".$normalized_score;
if($if_hi) {
$hi_score{$gene} = 0 if not defined $hi_score{$gene};
print $seed_fh "\t$hi_score{$gene}";
}
if($if_rvis) {
$rvis_score{$gene} = 0 if not defined $rvis_score{$gene};
print $seed_fh "\t$rvis_score{$gene}";
}
print $seed_fh "\n";
#Normalized score for the annotation list
if ($gene_hash{$gene} and not $prediction) {
print $annotated_seed_fh join("\t",(++$annotated_rank, $gene, $gene_id{$gene}, $normalized_score));
if($if_hi) {
$hi_score{$gene} = 0 if not defined $hi_score{$gene};
print $annotated_seed_fh "\t$hi_score{$gene}";
}
if($if_rvis){
$rvis_score{$gene} = 0 if not defined $rvis_score{$gene};
print $annotated_seed_fh "\t$rvis_score{$gene}";
}
print $annotated_seed_fh "\n";
}
}
print MERGE "]";
close (MERGE);
# close (MERGE_LEGACY);
close ($seed_fh);
close (ANNOTATED) if (%gene_hash and not $prediction);
close (ANNOTATED_GENE_LIST) if (%gene_hash and not $prediction);
# Integrate scores from relation databases
if($prediction) {
($max_score, $min_score) = (0, 1);
my $predicted_item = predict_genes($item);
my %predicted_output = ();
my $annotated_fh;
@{$predicted_output{$_}} = @{$predicted_item->{$_}} for keys %$predicted_item;
if(%gene_hash) {
open (ANNOTATED,">$out.annotated_gene_scores") ;
open ($annotated_fh, ">$out.annotated_gene_list");
printHeader($annotated_fh,1);
}
open (PREDICTED, ">$out.predicted_gene_scores");
open (my $final_fh,">$out.final_gene_list");
printHeader($final_fh, 1);
print PREDICTED "Tuple number in the gene_disease database for all the terms: $count \n";
print ANNOTATED "Tuple number in the gene_disease database for all the terms: $count \n"
if %gene_hash;
#Find the max and min score
for my $gene (keys %predicted_output) {
$max_score = $predicted_output{$gene}[0] if ($max_score < $predicted_output{$gene}[0]);
$min_score = $predicted_output{$gene}[0] if ($min_score > $predicted_output{$gene}[0]);
}
# my $diff = $max_score - $min_score;
my $rank = 0;
my $annotated_rank = 0;
for my $gene (sort{ $predicted_output{$b}[0] <=> $predicted_output{$a}[0] } keys %predicted_output) {
$rank++;
$predicted_output{$gene}[1] =~ /^.*?\( (.+?) \).*?\t/x;
my $source = $1;
my $status;
if(
($source eq "HPRD") or
($source eq "BIOSYSTEM") or
($source eq "GENE_FAMILY") or
($source eq "HTRI") or
($source eq "ADDON_GENE_GENE")
) {
$status = "Predicted";
} else {
$status = "SeedGene";
}
my ($score, $content) = ($predicted_output{$gene}[0], $predicted_output{$gene}[1]);
my $normalized_score = $predicted_output{$gene}[0]/$max_score;
print PREDICTED $gene."\t"."ID:$gene_id{$gene} - $status\t".$score."\tNormalized score: $normalized_score\n".$content."\n";
print ANNOTATED $gene."\tID:$gene_id{$gene} $gene_hash{$gene} $status"."\t".$score."\tNormalized score: $normalized_score\n".$content."\n"
if ($gene_hash{$gene});
#Normalize score for the genelist
$normalized_score = sprintf('%.4g', $normalized_score);
print $final_fh $rank."\t".$gene."\t".$gene_id{$gene}."\t".$normalized_score."\t".$status;
if($if_hi) {
$hi_score{$gene} = 0 if not defined $hi_score{$gene};
print $final_fh "\t$hi_score{$gene}";
}
if($if_rvis) {
$rvis_score{$gene} = 0 if not defined $rvis_score{$gene};
print $final_fh "\t$rvis_score{$gene}";
}
print $final_fh "\n";
if ($gene_hash{$gene}) {
print $annotated_fh join("\t",(++$annotated_rank,$gene,$gene_id{$gene},$normalized_score,$status)) ;
if($if_hi) {
$hi_score{$gene} = 0 if not defined $hi_score{$gene};
print $annotated_fh "\t$hi_score{$gene}";
}
if($if_rvis) {
$rvis_score{$gene} = 0 if not defined $rvis_score{$gene};
print $annotated_fh "\t$rvis_score{$gene}";
}
print $annotated_fh "\n";
}
}
close (PREDICTED);
close ($final_fh);
close (ANNOTATED) if %gene_hash;
close (ANNOTATED_GENE_LIST) if (%gene_hash);
}
}
# Input some disease terms and return all its extended diseases
sub disease_extension{
@_==1 or die "The input should be only one string!";
my $input_term=$_[0];
print STDOUT "$input_term: The journey to find all related disease names of your query starts!\n";
$input_term =~ s/[\W_]+/ /g;
-f "${path}/$disease_count_file" or die "Could not open ${path}/$disease_count_file";
-f "${path}/${ctd_disease_file}" or die "Could not open ${path}/${ctd_disease_file}";
open(DISEASE,"${path}/$disease_count_file") or die "can't open ${path}/$disease_count_file";
open(CTD_DISEASE,"${path}/${ctd_disease_file}") or die "can't open ${path}/${ctd_disease_file}";
open(OMIM_DISEASE_ID, "${path}/$omim_disease_id_file") or die "can't open ${path}/$omim_disease_id_file";
my %disease_extend=();
my @disease_occur=<DISEASE>;
my @disease_ctd=<CTD_DISEASE>;
print STDOUT "$input_term: The item was queried in the databases!! \n";
print STDOUT "$input_term: The exact match (case non-sensitive) was used for disease/phenotype name match!! \n"
if($if_exact_match);
for (<OMIM_DISEASE_ID>) {
chomp();
my ($id, $disease_line) = split("\t");
next if ($id eq "OMIM_ID");
# When compare, get rid of '-' if it is not after a number
my $disease_line_key = $disease_line;
$disease_line_key =~ s/\bs\b//g;
$disease_line_key =~ s/\W+/ /g;
my $query_term = $input_term;
$query_term =~ s/\bs\b//g;
$query_term =~ s/\W+/ /g;
if ($disease_line_key =~/\b$query_term\b/i or $query_term eq $id) {
# If exact match
next if($if_exact_match and $disease_line_key !~ /(^|;)$query_term($|;)/i);
my @diseases = split(";",$disease_line);
my $disease_key =lc $diseases[0];
$disease_extend{$disease_key} = $disease_line;
}
}
print STDOUT "$input_term: The word matching in OMIM disease synonyms file has been done!! \n";
$input_term = lc $input_term;
#Query disease in the compiled list from gene_disease relations
for my $term (@disease_occur) {
chomp($term);
my @words=split('\t',$term);
my $disease=$words[0];
my $id = $words[1];
my ($id_source,$id_num) = split (":", $id);
next if(not $id_source);
my $disease_key = lc $disease;
$disease_key =~ s/\bs\b//g;
$disease_key =~ s/\W+/ /g;
my $query_term = $input_term;
$query_term =~ s/\bs\b//g;
$query_term =~ s/\W+/ /g;
# If the term matches
if(
$disease_key =~ /\b$query_term\b/i or
(
$id_num and
$query_term eq $id_num and
$id_source eq "OMIM"
)
) {
# If exact match
next if($if_exact_match and $disease_key !~ /(^|;)$query_term($|;)/i);
if ($disease_extend{$disease_key}) {
$disease_extend{$disease_key}.=";".$disease;
} else {
#Save the disease name into hash, key is the disease name in lower case form withougt "-"
$disease_extend{$disease_key}=$disease;
}
}
}
$disease_extend{$_} .= "\tGENE_DISEASE\n" for keys %disease_extend;
my @tree_number=();
print STDOUT "$input_term: The word matching search in the compiled disease databases for gene_disease relations has been done!\n";
for my $term(@disease_ctd) {
chomp($term);
my @words = split('\t',$term);
my $disease = $words[0];
my $disease_key = lc $disease;
$disease_key =~ s/\bs\b//g;
$disease_key =~ s/\W+/ /g;
my $synonym_key = $words[1];
$synonym_key =~ s/\bs\b//g;
$synonym_key =~ s/\W+/ /g;
my $query_term = $input_term;
$query_term =~ s/\bs\b//g;
$query_term =~ s/\W+/ /g;
# First push all the matched disease names or synonyms in
if($disease_key=~/\b$query_term\b/i or $synonym_key=~/\b$query_term\b/i) {
#If exact match
next if($if_exact_match and $disease_key.$words[1] !~ /(^|\|)$input_term($|\|)/i);
# Retrieve all the synonyms
my @synonyms = split('\|',$words[1]);
#Record the tree_number of each term and trace all their children later
push @tree_number,split('\|',$words[2]);
$disease_extend{$disease_key} .= join(';', ($disease,@synonyms)) and
$disease_extend{$disease_key} .= "\tCTD_DISEASE\n";
}
}
print STDOUT "$input_term: The word matching search in the CTD (Medic) databases has been done! \n";
#Second find all children of the terms found in the first round
for my $term(@disease_ctd) {
chomp($term);
my @words=split('\t',$term);
my @synonyms=split('\|',$words[1]);
my $disease = $words[0];
my $disease_key = lc $disease;
for my $each_tree_num(@tree_number) {
if($words[2] =~ qr/(^|\|) $each_tree_num [^\|]+ /x) {
$disease_extend{$disease_key} .=join (';', ($disease, @synonyms) )
and $disease_extend{$disease_key} .= "\tCTD_DISEASE\n"
if(not defined $disease_extend{$disease_key} or $disease_extend{$disease_key}!~/\bCTD_DISEASE\b/);
}
}
}
print STDOUT "$input_term: The descendants search in the CTD (Medic) databases has been done! \n";
if (-f "$work_path/ontology_search.pl") {
print STDERR "ERROR: The doio.obo file couldn't be found!!! The disease_ontology search wouldn't be conducted properly!! \n"
if ( not -f "$path/doid.obo");
$input_term =~ s/[^ \w-]s?//g;
my $system_command = "perl $work_path/ontology_search.pl -o $path/doid.obo '$input_term' -f name,id";
$system_command .= " -exact" if($if_exact_match);
$system_command .= " 2>/dev/null";
my $line = `$system_command`;
my @ontology_diseases = split('DOID:\d*\n',$line);
for (@ontology_diseases) {
next if(not $_);
my ($disease, @synonyms) = split('\n');
my $disease_key = lc $disease;
$disease_extend{$disease_key} .= join(';', ($disease, @synonyms));
$disease_extend{$disease_key} .= "\tDISEASE_ONTOLOGY\n";
}
print STDOUT "$input_term: The descendants search in disease_ontology (DO) database has been done! \n";
} else {
print STDERR "ERROR: $input_term: The $work_path/ontology_search.pl file couldn't be found, so the disease_ontology (DO) database won't be used! \n";
}
return \%disease_extend;
}
sub phenotype_extension{
@_==1 or die "ERROR: Only one phenotype term is accepted!!! ";
my %hpo_score_system = (
"very rare" => 0.01,
"rare" => 0.05,
"occasional"=> 0.075,
"frequent" => 0.33,
"typical" => 0.5,
"variable" => 0.5,
"common" => 0.75,
"hallmark" => 0.90,
"obligate" => 1.00
);
my $input_term = $_[0];
my $raw_input_term = $input_term;
print STDOUT "$raw_input_term: The phenotype search and annotation process starts!! \n";
$input_term =~s/[\W_]+/ /g;
my %disease_hash;
my @hpo_ids;
# %disease_hash( "disease_name_key" => [score, original_disease_name] )
if( -f "$work_path/ontology_search.pl" ) {
print STDERR "ERROR: The hpo.obo file couldn't be found!!! The phenotype_ontology search wouldn't be conducted properly!! \n"
if ( not -f "$path/hpo.obo");
print STDERR "ERROR: The $hpo_annotation_file couldn't be found!!! The phenotype_annotation couldn't be conducted properly!! \n"
if ( not -f "$path/$hpo_annotation_file" );
open (HPO_ANNOTATION, "$path/$hpo_annotation_file") or die "ERROR: Can't open $hpo_annotation_file!!! \n";
open (OMIM_DESCRIPTION, "$path/$omim_description_file") or die "ERROR: Can't open $omim_description_file!!! \n";
my $line = `perl $work_path/ontology_search.pl -o $path/hpo.obo -format id -p '$input_term' `;
print STDOUT "$raw_input_term: executing ontology_search.pl to expand phenotype terms\n";
@hpo_ids = split("\n", $line);
print STDOUT "$raw_input_term: Found ", scalar (@hpo_ids), " additional phenotype terms\n";
my @hpo_annotation = <HPO_ANNOTATION>;
shift @hpo_annotation;
@hpo_ids = sort @hpo_ids;
my ($i,$j) = (0,0);
while($i<@hpo_ids and $j<@hpo_annotation) {
$hpo_annotation[$j] =~s/[\r\n]+//g;
#[0]HPO_ID [1]SOURCE [2]HPO_DISEASE_NAME [3]FREQUENCY
my @words=split("\t",$hpo_annotation[$j]);
if($hpo_ids[$i] eq "HP:".$words[0]) {
my @diseases = split(";",$words[2]);
for my $individual_disease(@diseases) {
next if(not $individual_disease);
$individual_disease = TextStandardize($individual_disease);
my $individual_disease_key = lc $individual_disease;
$disease_hash{$individual_disease_key}[0] = 0;
$disease_hash{$individual_disease_key}[1] =lc $individual_disease if not $disease_hash{$individual_disease_key}[1];
my $score;
if (defined $words[3] and $hpo_score_system{$words[3]}) {
$score = $hpo_score_system{$words[3]};
} else {
if($words[3] and $words[3] =~ /^(\d*\.?\d+)\%$/) {
$score = $1 * 0.01;
} else {
$score = $hpo_score_system{"frequent"};
}
}
$disease_hash{$individual_disease_key}[0] = $score if($disease_hash{$individual_disease_key}[0] < $score);
}
$j++;
}
if($hpo_ids[$i] lt "HP:".$words[0]) { $i++; next; }
if($hpo_ids[$i] gt "HP:".$words[0]) { $j++; next; }
}
} else {
print STDERR "NOTICE: The $work_path/ontology_search.pl file couldn't be found, so the HPO database won't be used! \n";
}
my %omim_description;
for my $line (<OMIM_DESCRIPTION>) {
next if($line =~ /^OMIM_ID/);
chomp($line);
my ($id, $disease, $description) = split("\t", $line);
$disease = TextStandardize($disease);
if($description =~ /\b$input_term\b/i) {
$omim_description{$disease} = 1 if(not $omim_description{$disease});
$omim_description{$disease}++ if($omim_description{$disease});
}
}
my $total=0;
for my $disease (keys %omim_description) {
$total += $omim_description{$disease};
}
for my $disease (keys %omim_description) {
my $score = ($omim_description{$disease}+0.0)/$total;
my @diseases = split(";",$disease);
for my $individual_disease(@diseases) {
next if(not $individual_disease);
my $individual_disease_key = lc $individual_disease;
if(not $disease_hash{$individual_disease_key}) {
$disease_hash{$individual_disease_key}[0] = $score;
$disease_hash{$individual_disease_key}[1] = $individual_disease;
} else {
$disease_hash{$individual_disease_key}[0] = $score if($disease_hash{$individual_disease_key}[0] < $score);
}
}
}
return (\%disease_hash,@hpo_ids);
}
# Input the disease list and return all the genes and item count
sub get_phenotype_to_gene_hash
{
open(PHENOTYPE_TO_GENE, "${path}/$phenotype_to_gene_file") or die "can't open file: ${path}/$phenotype_to_gene_file";
my @lines = <PHENOTYPE_TO_GENE>;
shift @lines;
my $line;
my %hpo_id_to_gene_hash;
my $hpo_id;
my $gene_names;
foreach $line (@lines)
{
chomp $line;
my @split_line = split('\t', $line);
$hpo_id = $split_line[0];
$gene_names = $split_line[2];
$hpo_id_to_gene_hash{$hpo_id} = $gene_names;
}
close (PHENOTYPE_TO_GENE);
return \%hpo_id_to_gene_hash;
}
sub get_hpo_id_to_name_hash
{
open(PHENOTYPE_TO_GENE, "${path}/$phenotype_to_gene_file") or die "can't open file: ${path}/$phenotype_to_gene_file";
my @lines = <PHENOTYPE_TO_GENE>;
shift @lines;
my $line;
my %hpo_id_to_name_hash;
my $hpo_id;
my $hpo_name;
foreach $line (@lines)
{
chomp $line;
my @split_line = split('\t', $line);
$hpo_id = $split_line[0];
$hpo_name = $split_line[1];
if (not exists($hpo_id_to_name_hash{$hpo_id} ) ){
$hpo_id_to_name_hash{$hpo_id} = "$hpo_name";
}
}
close (PHENOTYPE_TO_GENE);
return \%hpo_id_to_name_hash;
}