-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexport_gene_list.pl.bk
executable file
·807 lines (664 loc) · 23.9 KB
/
export_gene_list.pl.bk
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
#!/usr/bin/perl
use warnings;
my ($data_dir, $module_file, $network_type, $fold_change_threshold, $out_dir, $script_dir) = @ARGV;
require "$script_dir/Construct_network.pl";
#cancer gene census
my $cancer_gene_census_file = "$script_dir/cancer_gene_census.csv";
my %cancer_census = ();
open(FILE, $cancer_gene_census_file);
while(<FILE>){
@line = split(/\s+/, $_);
#chomp($line[0]);
$cancer_census{$line[0]} = 1;
}
close(FILE);
my %gene_to_index;
my @index_to_gene;
my @connections;
if($network_type eq "NETBOX"){
#update not done to save computational time
construct_netbox_network($data_dir, \@index_to_gene, \%gene_to_index, \@connections, $script_dir);
}
if($network_type eq "DRIVER_NET"){
construct_driver_net_network(\@index_to_gene, \%gene_to_index, \@connections, $script_dir);
}
my %sample_gene_mutated = ();
my %sample_gene_dysregulated = ();
my %sample_gene_driver = ();
my %sample_gene_phenotype = ();
my %sample_gene_expression = ();
opendir(DIR, $data_dir);
@the_DATA_DIR = readdir(DIR);
close(DIR);
my %naive_mut_list = ();
my %naive_dys_list = ();
my %infer_mut_list = ();
my %infer_dys_list = ();
my %infer_linker_list = ();
@dys_type_order = ("ALL", "DOWN", "UP");
@mut_type_order = ("ALL", "MUT", "DEL", "AMPL", "BOTH");
$nb_samples = 0;
my %gene_impact;
my %gene_impact_filtered;
my %sample_gene_impact;
my %sample_gene_impact_filtered;
#Produce the naive gene lists
foreach my $dir_sample (@the_DATA_DIR){
$mutation_file_name = "$data_dir/$dir_sample/Genelist_Status.txt";
$mutation_file_name_cell = "$data_dir/$dir_sample/Genelist_Status_cell.txt";
if(-e $mutation_file_name || -e $mutation_file_name_cell){
if(-e $mutation_file_name_cell){
$mutation_file_name = $mutation_file_name_cell;
}
my %map = ();
$sample_gene_dysregulated{$dir_sample} = \%map;
my %map2 = ();
$sample_gene_expression{$dir_sample} = \%map2;
#print STDERR " *** ".$mutation_file_name."\n";#<STDIN>;
$nb_samples++;
open(FILE, "$mutation_file_name");
while(<FILE>){
chop ($_);
@line = split(/\t/, $_);
my @parts = split(/_/,$line[0]);
my $gene_name = $parts[0];
my $status = $parts[1];
#initaillaze gene impact sample
my %map1;
$sample_gene_impact{$dir_sample} = \%map1;
my %map2;
$sample_gene_impact_filtered{$dir_sample} = \%map2;
if(exists $gene_to_index{$gene_name}){#filter out all the gene_name that do not belong to the input network
$gene_ID = get_ID($gene_name, \%gene_to_index );
#gene impact initalization
if(! exists $gene_impact{$gene_ID}){
my %map = ("MUT", 0, "UP", 0, "DOWN", 0);
$gene_impact{$gene_ID} = \%map;
#
my %map1 = ("MUT", 0, "UP", 0, "DOWN", 0);
$gene_impact_filtered{$gene_ID} = \%map1;
my %map2 = ("MUT", 0, "UP", 0, "DOWN", 0);
$sample_gene_impact_filtered{$dir_sample}->{$gene_ID} = \%map2;
#
my %map3 = ("MUT", 0, "UP", 0, "DOWN", 0);
$sample_gene_impact_filtered{$dir_sample}->{$gene_ID} = \%map3;
}
if (($status eq "UP" || $status eq "DOWN")){
if(! exists $infer_linker_list{$gene_ID}){
my %map = ("ALL", 0, "UP", 0,"DOWN", 0);
$infer_linker_list{$gene_ID} = \%map;
}
$g_exp = $line[1];
$sample_gene_expression{$dir_sample}->{$gene_ID} = $g_exp;
if(abs($g_exp) >= $fold_change_threshold){
if(! exists $sample_gene_dysregulated{$dir_sample}->{$gene_ID}){
my %map = ("UP", 0,"DOWN", 0);
$sample_gene_dysregulated{$dir_sample}->{$gene_ID} = \%map;
my %map2 = ("UP", 0,"DOWN", 0);
$sample_gene_phenotype{$dir_sample}->{$gene_ID} = \%map2;
}
if(! exists $naive_dys_list{$gene_ID}){
my %map = ("ALL", 0, "UP", 0,"DOWN", 0);
$naive_dys_list{$gene_ID} = \%map;
my %map2 = ("ALL", 0, "UP", 0,"DOWN", 0);
$infer_dys_list{$gene_ID} = \%map2;
}
$sample_gene_dysregulated{$dir_sample}->{$gene_ID}->{$status} = 1;
$naive_dys_list{$gene_ID}->{$status}++;
$naive_dys_list{$gene_ID}->{"ALL"}++;
}
}
}
}
close(FILE);
#Read for alteration, allows to filter alteration that do not correlate with dyregulated genes
open(FILE, "$mutation_file_name");
while(<FILE>){
chop ($_);
@line = split(/\t/, $_);
my @parts = split(/_/,$line[0]);
my $gene_name = $parts[0];
my $status = $parts[1];
if(exists $gene_to_index{$gene_name}){#filter out all the gene_name that do not belong to the input network
$gene_ID = get_ID($gene_name, \%gene_to_index );
if ($status eq "MUT" ||
($status eq "AMPL" && (1 || $sample_gene_dysregulated{$dir_sample}->{$gene_name}->{"UP"} > 0)) ||
($status eq "DEL" && (1 || $sample_gene_dysregulated{$dir_sample}->{$gene_name}->{"DOWN"} < 0))){
if(! exists $sample_gene_mutated{$dir_sample}->{$gene_ID}){
my %map = ("MUT", 0, "AMPL", 0, "DEL", 0, "BOTH", 0);
$sample_gene_mutated{$dir_sample}->{$gene_ID} =\%map;
my %map2 = ("MUT", 0, "AMPL", 0, "DEL", 0, "BOTH", 0);
$sample_gene_driver{$dir_sample}->{$gene_ID} =\%map2;
}
if(! exists $naive_mut_list{$gene_ID}){
my %map = ("ALL", 0, "MUT", 0, "AMPL", 0, "DEL", 0, "BOTH", 0);
$naive_mut_list{$gene_ID} =\%map;
my %map2 = ("ALL", 0, "MUT", 0, "AMPL", 0, "DEL", 0, "BOTH", 0);
$infer_mut_list{$gene_ID} = \%map2;
}
$naive_mut_list{$gene_ID}->{$status}++;
$sample_gene_mutated{$dir_sample}->{$gene_ID}->{$status} = 1;
#CNV and SNV
if($sample_gene_mutated{$dir_sample}->{$gene_ID}->{"MUT"} &&
($sample_gene_mutated{$dir_sample}->{$gene_ID}->{"AMPL"} || $sample_gene_mutated{$dir_sample}->{$gene_ID}->{"DEL"})){
$sample_gene_mutated{$dir_sample}->{$gene_ID}->{"BOTH"} = 1;
$naive_mut_list{$gene_ID}->{"BOTH"}++;
}
else{
$naive_mut_list{$gene_ID}->{"ALL"}++;
}
}
}
}
close(FILE);
}
}
#Produce the driver/phenotype gene lists
#Read the results file
open(FILE, $module_file);
my @dys_gene;
my @mut_gene;
my @linker_gene;
my @all_mod_gene;
my @all_mod_gene_type;
`rm -r $out_dir/MODULE_LIST`;
`mkdir $out_dir/MODULE_LIST`;
my %sample_driver = ();
my %sample_pheno = ();
foreach $s (keys %sample_gene_mutated){
my %map = ();
$sample_pheno{$s} = \%map;
my %map1 = ();
$sample_driver{$s} = \%map1;
}
#open all the file
#######################
#Read the module file to obtain the sample module list
#######################
my %sample_gene_module = ();
my %sample_gene_driver_pheno = ();
my %all_gene_in_module = ();
my @module_order = ();
while(<FILE>){
chop $_;
@line = split(/\t/, $_);
$module_ID = $line[0];
push(@module_order, $module_ID);
@temp = split(/\./, $module_ID);
$sample_name = $temp[0];
@dys_gene = split(/\;/, $line[2]);
@mut_gene = split(/\;/, $line[1]);
@linker_gene = ();
if($line[3] ne "-;"){
@linker_gene = split(/\;/, $line[3]);
}
#$sample_data_dir = "$out_dir/SAMPLE_SPECIFIC_DATA/$sample_name";
#`mkdir $sample_data_dir
@all_mod_gene = @mut_gene;
@all_mod_gene_type = @mut_gene;
$module_impact_filtered = $line[6];
$module_impact = $line[5];
#print STDERR " --- size = ".(@all_mod_gene+0)."\n";#<STDIN>;
foreach $g (@mut_gene){
#print STDERR " *** MUT"."\t".$sample_name."\t".$g."\n";
$gene_ID = get_ID($g, \%gene_to_index );
$sample_gene_impact{$sample_name}->{$gene_ID}->{"MUT"} += $module_impact;
$sample_gene_impact_filtered{$sample_name}->{$gene_ID}->{"MUT"} += $module_impact_filtered;
#
$gene_impact{$gene_ID}->{"MUT"} += $module_impact;
$gene_impact_filtered{$gene_ID}->{"MUT"} += $module_impact_filtered;
if(exists $sample_gene_mutated{$sample_name}->{$gene_ID}){
foreach $type (keys %{$sample_gene_mutated{$sample_name}->{$gene_ID}}){
if($sample_gene_mutated{$sample_name}->{$gene_ID}->{$type} != 0){
$infer_mut_list{$gene_ID}->{$type}++;
$infer_mut_list{$gene_ID}->{"ALL"}++;
if(!exists $sample_driver{$sample_name}->{$gene_ID}){
$sample_driver{$sample_name}->{$gene_ID} = $type;
}
else{
$sample_driver{$sample_name}->{$gene_ID} .= "_".$type if($type ne "BOTH");
}
}
}
$infer_mut_list{$gene_ID}->{"ALL"} -= 2 if($sample_gene_mutated{$sample_name}->{$gene_ID}->{"BOTH"} != 0);
}
else{
print STDERR " *** $g DRIVER GENE PREDICTED in $sample_name BUT NOT MUTATED -_-'\n";
}
}
foreach $g (@dys_gene){
my @parts = split(/_/,$g);
my $gene_name = $parts[0];
push(@all_mod_gene, $gene_name);
push(@all_mod_gene_type, $g);
$gene_ID = get_ID($gene_name, \%gene_to_index );
#print STDERR " *** DYS"."\t".$sample_name."\t".$gene_name."\n";
if(exists $sample_gene_dysregulated{$sample_name}->{$gene_ID}){
foreach $type (keys %{$sample_gene_dysregulated{$sample_name}->{$gene_ID}}){
if($sample_gene_dysregulated{$sample_name}->{$gene_ID}->{$type} != 0){
$infer_dys_list{$gene_ID}->{$type}++ ;
$infer_dys_list{$gene_ID}->{"ALL"}++ ;
$sample_pheno{$sample_name}->{$gene_ID} = $type;
$sample_gene_impact{$sample_name}->{$gene_ID}->{$type} += $module_impact;
$sample_gene_impact_filtered{$sample_name}->{$gene_ID}->{$type} += $module_impact_filtered;
#
$gene_impact{$gene_ID}->{$type} += $module_impact;
$gene_impact_filtered{$gene_ID}->{$type} += $module_impact_filtered;
}
}
}
else{
print STDERR " *** $gene_name PHENOTYPE GENE PREDICTED in $sample_name BUT NOT DYSREGULATED -_-'\n";
}
}
#print STDERR " --- size = ".(@all_mod_gene+0)."\n";#<STDIN>;
if(@linker_gene != 0){
foreach $g (@linker_gene){
my @parts = split(/_/,$g);
my $gene_name = $parts[0];
push(@all_mod_gene, $gene_name);
push(@all_mod_gene_type, $g);
$gene_ID = get_ID($gene_name, \%gene_to_index);
if(exists $sample_gene_dysregulated{$sample_name}->{$gene_ID}){
$infer_linker_list{$gene_ID}->{"ALL"}++ ;
}
else{
print STDERR " *** $g |$gene_name| LINKER GENE PREDICTED in $sample_name BUT NOT DYSREGULATED -_-'\n";
}
}
}
#print STDERR " --- size = ".(@all_mod_gene+0)."\n";<STDIN>;
#out a gene list for each module to be prossess by DAVID
open(OUT_M, ">$out_dir/MODULE_LIST/$module_ID.mod");
print OUT_M "".(join("\n", @all_mod_gene))."\n";
close(OUT_M);
#Update the data structure to output the sample gene in module matrix
#the expression type of dys regulated gene is taken into account, the type of mutation not
if(! exists $sample_gene_module{$sample_name}){
my %map = ();
$sample_gene_module{$sample_name} = \%map;
my %map2;
$sample_gene_driver_pheno{$sample_name} = \%map2;
}
foreach my $gene (@all_mod_gene_type){
$sample_gene_module{$sample_name}->{$gene} = 1;
if(! exists $all_gene_in_module{$gene}){
my %map = ();
$all_gene_in_module{$gene} = \%map;
}
$all_gene_in_module{$gene}->{$module_ID} = 1;
}
}
close(FILE);
#divide the gene impcat by the number of samples
foreach $gene_ID (%gene_impact){
foreach $type (keys %{$gene_impact{$gene_ID}}){
#print STDERR $gene_impact{$gene_ID}->{$type}."\t".$nb_samples."\n";<STDIN>;
$gene_impact{$gene_ID}->{$type} = $gene_impact{$gene_ID}->{$type}/$nb_samples;
$gene_impact_filtered{$gene_ID}->{$type} = $gene_impact_filtered{$gene_ID}->{$type}/$nb_samples;
}
}
#to write the sample specific driver phenotye gene file
my $sample_out_dir = "$out_dir/SAMPLE_SPECIFIC_DATA";
`rm -r $sample_out_dir`;
`mkdir $sample_out_dir`;
my $SAMPLE_DATA_HEADER = "#GENE\tCOORD\tTYPE\tDRIVER\tRAW\tCS";
#read the annotation file
my %all_annotation = ();
#read_annotation_file();
foreach $s (keys %sample_driver){
#for mutation
open(OUT, ">$sample_out_dir/temp.dat");
foreach $g (keys %{$sample_gene_mutated{$s}}){
$g_name = get_name($g, \@index_to_gene);
$type = "";
foreach $t (keys %{$sample_gene_mutated{$s}->{$g}}){
$type .= $t."_" if($t ne "BOTH" && $sample_gene_mutated{$s}->{$g}->{$t} != 0);
}
chop $type;
print OUT $g_name."\t".
#(write_coord($g_name))."\t".
$type."\t".(sprintf("%.3f", $infer_mut_list{$g}->{"ALL"}/$nb_samples))."\t".(sprintf("%.3f", $naive_mut_list{$g}->{"ALL"}/$nb_samples))."\t".(get_gene_annotation_status($g_name))."\n";
}
close(OUT);
$file = "$sample_out_dir/mut_$s.dat";
`echo -e \"$SAMPLE_DATA_HEADER\" > $file`;
`sort -k5,5 -nr $sample_out_dir/temp.dat >> $file`;
#for driver
open(OUT, ">$sample_out_dir/temp.dat");
foreach $g (keys %{$sample_driver{$s}}){
$g_name = get_name($g, \@index_to_gene);
print OUT
$g_name."\t".
#(write_coord($g_name))."\t".
$sample_driver{$s}->{$g}."\t".(sprintf("%.3f", $infer_mut_list{$g}->{"ALL"}/$nb_samples))."\t".(sprintf("%.3f", $naive_mut_list{$g}->{"ALL"}/$nb_samples)).
"\t".(get_gene_annotation_status($g_name)).
"\t".$sample_gene_impact{$s}->{$g}->{"MUT"}."\t".$sample_gene_impact_filtered{$s}->{$g}->{"MUT"}.
"\t".$gene_impact{$g}->{"MUT"}."\t".$gene_impact_filtered{$g}->{"MUT"}.
"\n";
}
close(OUT);
#sorting based on unfiltered impact
#in case of same value (possible if in same module) order according to average gene impact in the whole samples
$file = "$sample_out_dir/impact_driver_$s.dat";
`echo -e \"$SAMPLE_DATA_HEADER\" > $file`;
`sort -k8,8 -nr -k10,10 -nr $sample_out_dir/temp.dat >> $file`;
$file = "$sample_out_dir/impact_filtered_driver_$s.dat";
`echo -e \"$SAMPLE_DATA_HEADER\" > $file`;
`sort -k9,9 -nr -k11,11 -nr $sample_out_dir/temp.dat >> $file`;
$file = "$sample_out_dir/driver_$s.dat";
`echo -e \"$SAMPLE_DATA_HEADER\" > $file`;
`sort -k4,4 -nr $sample_out_dir/temp.dat >> $file`;
#for dysregulated genes
open(OUT, ">$sample_out_dir/temp.dat");
foreach $g (keys %{$sample_gene_dysregulated{$s}}){
$g_name = get_name($g, \@index_to_gene);
$type = "";
foreach $t (keys %{$sample_gene_dysregulated{$s}->{$g}}){
if($sample_gene_dysregulated{$s}->{$g}->{$t} != 0){
$type = $t;
last;
}
}
print OUT $g_name."\t".
#(write_coord($g_name))."\t".
$type."\t".(sprintf("%.3f", $infer_dys_list{$g}->{$type}/$nb_samples))."\t".(sprintf("%.3f", $naive_dys_list{$g}->{$type}/$nb_samples))."\t".(get_gene_annotation_status($g_name))."\n";
}
close(OUT);
$file = "$sample_out_dir/dys_$s.dat";
`echo -e \"$SAMPLE_DATA_HEADER\" > $file`;
`sort -k5,5 -nr $sample_out_dir/temp.dat >> $file`;
#for phenotype
open(OUT, ">$sample_out_dir/temp.dat");
foreach $g (keys %{$sample_pheno{$s}}){
$g_name = get_name($g, \@index_to_gene);
$type = $sample_pheno{$s}->{$g};
print OUT $g_name."\t".
#(write_coord($g_name))."\t".
$type."\t".(sprintf("%.3f", $infer_dys_list{$g}->{$type}/$nb_samples))."\t".(sprintf("%.3f", $naive_dys_list{$g}->{$type}/$nb_samples))."\t".(get_gene_annotation_status($g_name))."\n";
}
close(OUT);
$file = "$sample_out_dir/phenotype_$s.dat";
`echo -e \"$SAMPLE_DATA_HEADER\" > $file`;
`sort -k4,4 -nr $sample_out_dir/temp.dat >> $file`;
}
`rm $sample_out_dir/temp.dat`;
#Write the output files
my $naive_data;
my $infer_data;
my $out_file_name;
#Frequency gene list
for($i = 0; $i < 4; $i++){
if($i <= 1){
$naive_data = \%naive_mut_list;
$infer_data = \%infer_mut_list;
$type_order = \@mut_type_order;
$out_file_name = "ALTERATION";
$out_file_name = "NUM_".$out_file_name if($i == 1);
}
else{
$naive_data = \%naive_dys_list;
$infer_data = \%infer_dys_list;
$type_order = \@dys_type_order;
$out_file_name = "DYSREGULATION";
$out_file_name = "NUM_".$out_file_name if($i == 3);
}
$out_file_name .= ".dat";
open(OUT, ">$out_dir/$out_file_name");
foreach $g (keys %{$naive_data}){
$g_name = get_name($g, \@index_to_gene);
$res = $g_name;
foreach $type (@{$type_order}){
if($i == 0 || $i == 2){
$res .= "\t".(sprintf("%.3f", $naive_data->{$g}->{$type}/$nb_samples));
}
else{
$res .= "\t".$naive_data->{$g}->{$type};
}
}
foreach $type (@{$type_order}){
if($i == 0 || $i == 2){
$res .= "\t".(sprintf("%.3f", ($infer_data->{$g}->{$type}/$nb_samples)));
}
else{
$res .= "\t".$infer_data->{$g}->{$type};
}
}
$res .= get_gene_annotation_status($g_name);
#the impact
if($i <= 1){
$res .= "\t".$gene_impact{$g}->{"MUT"}."\t".$gene_impact_filtered{$g}->{"MUT"};
}
else{
$res .= "\t".($gene_impact{$g}->{"DOWN"}+$gene_impact{$g}->{"UP"})."\t".$gene_impact{$g}->{"DOWN"}."\t".$gene_impact{$g}->{"UP"};
$res .= "\t".($gene_impact_filtered{$g}->{"DOWN"}+$gene_impact_filtered{$g}->{"UP"})."\t".$gene_impact_filtered{$g}->{"DOWN"}."\t".$gene_impact_filtered{$g}->{"UP"};
}
print OUT $res."\n";
}
close(OUT);
}
#File with header [yes I have to deal with header with the other file]
#Gene list for DAVID
for($i = 0; $i < 2; $i++){
if($i == 0){
$naive_data = \%naive_mut_list;
$infer_data = \%infer_mut_list;
$type_order = \@mut_type_order;
$out_file_name = "GENE_LIST_ALTERATION";
}
else{
$naive_data = \%naive_dys_list;
$infer_data = \%infer_dys_list;
$type_order = \@dys_type_order;
$out_file_name = "GENE_LIST_DYSREGULATION";
}
open(OUT_N, ">$out_dir/NAIVE_$out_file_name.dat");
open(OUT_I, ">$out_dir/$out_file_name.dat");
open(OUT_N_2, ">$out_dir/NAIVE_$out_file_name\_2.dat");
open(OUT_I_2, ">$out_dir/$out_file_name\_2.dat");
open(OUT_N_5, ">$out_dir/NAIVE_$out_file_name\_5.dat");
foreach $g (keys %{$naive_data}){
if($naive_data->{$g}->{"ALL"} != 0){
print OUT_N "".get_name($g, \@index_to_gene)."\n";
}
#At least in 1 sample
if($naive_data->{$g}->{"ALL"} > 1){
print OUT_N_2 "".get_name($g, \@index_to_gene)."\n";
}
#In at least 5% of the samples
if(($naive_data->{$g}->{"ALL"}/$nb_samples) >= 0.05){
print OUT_N_5 "".get_name($g, \@index_to_gene)."\n";
}
if($infer_data->{$g}->{"ALL"} != 0){
print OUT_I "".get_name($g, \@index_to_gene)."\n";
}
#At least in 1 sample
if($infer_data->{$g}->{"ALL"} > 1){
print OUT_I_2 "".get_name($g, \@index_to_gene)."\n";
}
}
close(OUT_N);close(OUT_N_2);
close(OUT_I);close(OUT_I_2);
$exe = "cat $out_dir/NAIVE_$out_file_name.dat $out_dir/$out_file_name.dat | sort | uniq -c | awk '{if(\$1 == 1) print \$2}' > $out_dir/NAIVE_NOT_$out_file_name.dat";
print STDERR $exe."\n";
print `$exe`;
$exe = "cat $out_dir/NAIVE_$out_file_name\_2.dat $out_dir/$out_file_name\_2.dat | sort | uniq -c | awk '{if(\$1 == 1) print \$2}' > $out_dir/NAIVE_NOT_$out_file_name\_2.dat";
print STDERR $exe."\n";
print `$exe`;
$exe = "cat $out_dir/NAIVE_$out_file_name\_5.dat $out_dir/$out_file_name.dat | sort | uniq -c | awk '{if(\$1 == 1) print \$2}' > $out_dir/NAIVE_NOT_$out_file_name\_5.dat";
print STDERR $exe."\n";
print `$exe`;
}
#remove the infer gene from the naive gene list
open(OUT_ALL_EXPR, ">$out_dir/ALL_EXPR_MATRIX");
open(OUT_EXPR, ">$out_dir/EXPR_MATRIX");
open(OUT_PHENO, ">$out_dir/PHENO_MATRIX");
#all alterated genes are reported
open(OUT_ALL_ALT, ">$out_dir/ALL_ALT_MATRIX");
#only driver altered genes are reported
open(OUT_ALT, ">$out_dir/ALT_MATRIX");
#only patient specific drivers are reported
open(OUT_DRIVER, ">$out_dir/DRIVER_MATRIX");
#open(OUT_PHENO_ALT, ">$out_dir/PHENO_ALT_MATRIX");
open(OUT_MODULE, ">$out_dir/MODULE_MATRIX");
open(OUT_SAMPLE_MODULE, ">$out_dir/SAMPLE_MODULE_MATRIX");
#HEADER
my @expr = ();
my @sample_order = ();
foreach $sample (keys %sample_gene_expression){
if(keys %{$sample_pheno{$sample}} != 0){
push(@sample_order, $sample);
}
else{
print STDERR " *** WARNING SAMPLE WITHOUT PHENOTYPE $sample\n";
}
}
print OUT_ALL_EXPR (join("\t", @sample_order))."\n";
print OUT_EXPR (join("\t", @sample_order))."\n";
print OUT_PHENO (join("\t", @sample_order))."\n";
#print OUT_PHENO_ALT (join("\t", @sample_order))."\n";
print OUT_ALL_ALT (join("\t", @sample_order))."\n";
print OUT_ALT (join("\t", @sample_order))."\n";
print OUT_DRIVER (join("\t", @sample_order))."\n";
print OUT_SAMPLE_MODULE (join("\t", @sample_order))."\n";
print OUT_MODULE (join("\t", @module_order))."\n";
my $matrix_abscence_value = 1;
my $matrix_precense_value = 2;
#expression value matrix for heat map
foreach $gene (keys %naive_dys_list){
#For the matrix of all the genes
@expr = ();
foreach $sample (@sample_order){
$e = 0;
if(exists $sample_gene_expression{$sample}->{$gene}){
$e = $sample_gene_expression{$sample}->{$gene};
}
push(@expr, $e);
}
print OUT_ALL_EXPR (join("\t", @expr))."\n";
#FOR THE INFER GENE
if($infer_dys_list{$gene}->{"ALL"} != 0
){
#The expression based list
@expr = ();
foreach $sample (@sample_order){
$e = 0;
if(exists $sample_gene_expression{$sample}->{$gene}){
$e = $sample_gene_expression{$sample}->{$gene};
}
push(@expr, $e);
}
print OUT_EXPR (join("\t", @expr))."\n";
#The PHENO BASED list
for($i = 1; $i < @dys_type_order; $i++){
$type = $dys_type_order[$i];
if($infer_dys_list{$gene}->{$type} != 0){
@expr = ();#$sample;
foreach $sample (@sample_order){
#print STDERR $gene."\t".$type."\n";<STDIN>;
$e = $matrix_abscence_value;
if(exists $sample_pheno{$sample}->{$gene} &&
$sample_pheno{$sample}->{$gene} eq $type){
$e = $matrix_precense_value;
}
push(@expr, $e);
}
print OUT_PHENO (join("\t", @expr))."\n";
#print OUT_PHENO_ALT (join("\t", @expr))."\n";
}
}
}
}
close(OUT_ALL_EXPR);
close(OUT_EXPR);
foreach $gene (sort {$gene_impact_filtered{$b}->{"MUT"} <=> $gene_impact_filtered{$a}->{"MUT"}} keys %naive_mut_list){
#print STDERR " *** $gene ".$gene_impact_filtered{$gene}->{"MUT"}."\n";<STDIN>;
@expr_all = ();
@expr = ();
@expr_driver = ();
foreach $sample (@sample_order){
#Altered
$e = $matrix_abscence_value;
if(exists $sample_gene_mutated{$sample}->{$gene}){
$e = $matrix_precense_value;
}
push(@expr_all, $e);
if($infer_mut_list{$gene}->{"ALL"} != 0){
#Driver
$e = $matrix_abscence_value;
#print STDERR $gene."\t".$type."\n";<STDIN>;
if(exists $sample_driver{$sample}->{$gene}){
$e = $matrix_precense_value;
}
push(@expr_driver, $e);
#Altered but driver in one sample
$e = $matrix_abscence_value;
if(exists $sample_gene_mutated{$sample}->{$gene}){
$e = $matrix_precense_value;
}
push(@expr, $e);
}
}
if($infer_mut_list{$gene}->{"ALL"} != 0){
print OUT_DRIVER (join("\t", @expr_driver))."\n";
print OUT_ALT "".get_name($gene, \@index_to_gene)."\t".(join("\t", @expr))."\n";
}
print OUT_ALL_ALT "".get_name($gene, \@index_to_gene)."\t".(join("\t", @expr_all))."\n";
#print OUT_PHENO_ALT (join("\t", @expr))."\n";
}
close(OUT_ALT);
close(OUT_DRIVER);
#close(OUT_PHENO_ALT);
#module value matrix for heat map
foreach $gene (keys %all_gene_in_module){
@expr = ();
#all module of a sample or on the same line
foreach $sample (@sample_order){
$e = $matrix_abscence_value;
if(exists $sample_gene_module{$sample}->{$gene}){
$e = $matrix_precense_value;
}
push(@expr, $e);
}
print OUT_SAMPLE_MODULE (join("\t", @expr))."\n";
if(@expr+0 != @sample_order){
print STDERR " *** MODULE_MATRIX WRONG SAMPLE NUMBER !!!!\n";<STDIN>;
}
#all module are on separate lines
@expr = ();
foreach $module_ID (@module_order){
$e = $matrix_abscence_value;
if(exists $all_gene_in_module{$gene}->{$module_ID}){
$e = $matrix_precense_value;
}
push(@expr, $e);
}
print OUT_MODULE (join("\t", @expr))."\n";
}
close(OUT_SAMPLE_MODULE);
close(OUT_MODULE);
sub get_gene_annotation_status{
my ($g_name) = @_;
my $res = "";
if(exists $cancer_census{$g_name}){
$res .= "\tCC";
}
else{$res .= "\t-";}
return $res;
}
#S100A10
sub write_coord{
my ($gene_name) = @_;
$str = "-";
if( exists $all_annotation{$gene_name} ){
$str = $all_annotation{$gene_name}->{"CHROM"}.":".$all_annotation{$gene_name}->{"TX_S"}."-".$all_annotation{$gene_name}->{"TX_E"};
}
#print STDERR $gene_name."\t".$str."\n";<STDIN>;
return $str;
}
sub run_exe{
my ($exe) = @_;
$run = 1;
print STDERR $exe."\n";
print STDERR `$exe` if($run);
}