-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path10_Cluster_Algo_fast.pl
executable file
·399 lines (337 loc) · 11.8 KB
/
10_Cluster_Algo_fast.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
#!/usr/bin/perl
use warnings;
my (
$data_dir, $network_type, $depth_th,
$hub_th, $nb_joker, $fold_change_threshold,
$out_dir, $flag_real, $script_dir
) = @ARGV;
#$flag_real = 0;
#$dir_TCGA_sample -> directory where the data are organize
#$network_gene_list -> ~/Software/netbox/data/Homo_sapiens.gene_info
#$network -> ~/Software/netbox/db/netbox.script
#$depth_th -> Threshold on maximum depth for defining module expansion
#$hub_th -> No Gene that is connected to more that $hub_th other gene will be included.
#flag_real in this case out_dir is the root of all the experiments, the run must be performed on the full data set and all the real directory will be upadeted at the same time at the end
require "$script_dir/Construct_network.pl";
my @length_th = ();
for ( my $i = 2 ; $i <= 20 ; $i += 2 ) {
push( @length_th, $i );
}
push( @length_th, 1000000 );
my @dys_status_corress = ( "DOWN", "UP" );
# 1. Indexing
#not used due to synonyms problems/confusions
my %gene_to_index;
my @index_to_gene;
my @connections;
#print STDERR " *** --- $network_type\n";
if ( $network_type eq "NETBOX" ) {
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 );
}
# 4. Construct the set of explained genes for each samples
my $nb_sample = 0;
#Expression stats
my %explained_gene_frequency_all_depth = ();
foreach $depth (@length_th) {
my @explained_gene_frequency = ();
for ( $i = 0 ; $i < @index_to_gene ; $i++ ) {
my @tab_status = ();
for ( $j = 0 ; $j < 2 ; $j++ ) {
my %hash = ();
$tab_status[$j] = \%hash;
}
$explained_gene_frequency[$i] = \@tab_status;
}
$explained_gene_frequency_all_depth{$depth} = \@explained_gene_frequency;
}
my @dysregulated_gene_frequency = ();
for ( $i = 0 ; $i < @index_to_gene ; $i++ ) {
my @tab_status = ();
for ( $j = 0 ; $j < 2 ; $j++ ) { my %hash = (); $tab_status[$j] = \%hash; }
$dysregulated_gene_frequency[$i] = \@tab_status;
}
my %samples_explained_gene_set;
my @explored; #array use during the breath first search
my %all_sample_gene_dysregulated
; #to keep trak of the dysregulation status of each gene for each samples (use at step 3 and 4)
my %sample_gene_mutated;
my @sample_gene_mutated_list = ();
opendir( DIR, $data_dir );
@the_DATA_DIR = readdir(DIR);
close(DIR);
my $nb_sample_to_test = -10;
my %BUG_not_present_in_network = ();
foreach my $dir_sample (@the_DATA_DIR) {
$mutation_file_name = "$data_dir/$dir_sample/Genelist_Status.txt";
if ( -e $mutation_file_name) {
@sample_gene_mutated_list = ();
#initilazed the mutational/expression gene status
%sample_gene_mutated = ();
my @sample_gene_dysregulated;
for ( my $i = 0 ; $i < @index_to_gene ; $i++ ) {
my @status_tab = ( 0, 0 ); #DOWN, UP
$sample_gene_dysregulated[$i] = \@status_tab;
$sample_gene_mutated_list[$i] = 0;
}
my %all_explained_gene_set = ();
$nb_sample++;
$nb_mutated_gene = 0;
open( FILE, "$mutation_file_name" );
print STDERR " *** read file $mutation_file_name\n";
#read the file to obtain the dysregulated and mutated genes
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
my $gene_ID = get_ID( $gene_name, \%gene_to_index );
if ( $status eq "MUT" || $status eq "AMPL" || $status eq "DEL" )
{
$sample_gene_mutated{$gene_ID} = 1;
$sample_gene_mutated_list[$gene_ID] = 1;
}
else {
$fold_change = $line[1];
if ( ( $status eq "UP" || $status eq "DOWN" )
&& abs($fold_change) >= $fold_change_threshold )
{
$status_ID = 0;
$status_ID = 1 if ( $status eq "UP" );
$sample_gene_dysregulated[$gene_ID]->[$status_ID] =
$fold_change;
$dysregulated_gene_frequency[$gene_ID]->[$status_ID]
->{$dir_sample} = $gene_name;
#print STDERR "|".$_."|\t".$sample_gene_dysregulated[$gene_ID]->[$status_ID]."\t".$status_ID."\n";
}
}
}
else {
$BUG_not_present_in_network{$gene_name} = 1;
}
}
close(FILE);
if ( ( keys %sample_gene_mutated ) == 0 ) {
print STDERR "SAMPLE WITHOUT MUTATED GENES WEIRD !!!\n";
<STDIN>;
}
foreach my $gene ( keys %sample_gene_mutated ) {
$nb_mutated_gene++;
#print "****MUT TTT $gene ".(get_name($gene))."\n";
#print STDERR "- construct explainend gene set for ".(get_name($gene))."\n";
for ( $i = 0 ; $i < @index_to_gene ; $i++ ) { $explored[$i] = -1; }
#my @false = ();
$explained_gene_set =
construct_explained_gene_set_dijkstra( $gene,
\@sample_gene_dysregulated, \@sample_gene_mutated_list,
\@explored, $depth_th, $hub_th );
if ( @{$explained_gene_set} > 1 ) {
#compute the frequency
foreach $eg ( @{$explained_gene_set} ) {
for ( my $dys_status = 0 ; $dys_status < 2 ; $dys_status++ )
{
if ( $sample_gene_dysregulated[$eg]->[$dys_status] )
{ #to remove the mutated genes and the jocker!
#the gene is explain at least by gene at that depth
foreach $depth (@length_th) {
if ( $explored[$eg] <= $depth ) {
$explained_gene_frequency_all_depth{$depth}
->[$eg]->[$dys_status]->{$dir_sample} =
$gene;
}
}
}
}
}
}
}
last if ( $nb_sample == $nb_sample_to_test );
}
}
$BUG_nb_not_present_in_network = ( keys %BUG_nb_not_present_in_network );
#show the explained gene frequency
#add the expression frequency and look at the spearman correlation !!!!
#Output the mutated gene set that explained each gene
my $res_dys;
my $res_exp;
if ( !$flag_real ) {
foreach $depth_th (@length_th) {
$f = "$out_dir/exp_gene_freq_$depth_th\_$hub_th.dat.gz";
open( OUT, " | gzip -c >$f" );
#$OUT = $all_out{$depth_th};
for ( $i = 0 ; $i < @index_to_gene ; $i++ ) {
for ( my $dys_status = 0 ; $dys_status < 2 ; $dys_status++ ) {
$res_dys = keys %{ $dysregulated_gene_frequency[$i]->[$dys_status] };
next if($res_dys == 0);
$res_exp = keys %{ $explained_gene_frequency_all_depth{$depth_th}->[$i]->[$dys_status] };
#print STDERR "".(sprintf("%.3f",$res_dys/$nb_sample))."\n";<STDIN>;
$g = get_name( $i, \@index_to_gene );
#print STDERR " *** gene_name: $g $i\n";<STDIN>;
print OUT ""
#. $g . "_" . $dys_status_corress[$dys_status] . "\t"
#. ( @{ $connections[$i] } ) . "\t"
. $res_dys . "\t"
#. ( sprintf( "%.3f", $res_dys / $nb_sample ) ) . "\t"
. $res_exp
#. "\t"
#. ( sprintf( "%.3f", $res_exp / $nb_sample ) )
. "\n";
}
}
close(OUT);
}
}
else {
opendir( DIR, $out_dir );
@the_PARAM_DIR = readdir(DIR);
close(DIR);
print STDERR " *** SECOND STEP @the_PARAM_DIR\n";
foreach my $real_sub_sample_dir (@the_PARAM_DIR) {
print STDERR " *** file name $real_sub_sample_dir\n";
if ( index( $real_sub_sample_dir, "REAL" ) != -1 ) {
#Get the sample of the real subsample data
opendir( DIR, "$out_dir/$real_sub_sample_dir" );
@the_real_sample_set = readdir(DIR);
close(DIR);
%the_real_sample_map = ();
foreach my $s (@the_real_sample_set) {
$the_real_sample_map{$s} = 1;
}
$dir_res =
"$out_dir/$real_sub_sample_dir/EXPLAINED_FREQ_DIFF_$fold_change_threshold/";
`mkdir $dir_res` if ( !-d $dir_res );
foreach $depth_th (@length_th) {
$f = "$dir_res/exp_gene_freq_$depth_th\_$hub_th.dat.gz";
print STDERR " *** res_file $f\n";
open( OUT, " | gzip -c >$f" );
for ( $i = 0 ; $i < @index_to_gene ; $i++ ) {
for ( my $dys_status = 0 ; $dys_status < 2 ; $dys_status++ )
{
$res_exp = 0;
$res_dys = 0;
foreach my $s ( keys %the_real_sample_map ) {
#print STDERR " *** sss $s\n";<STDIN>;
$res_exp++
if (
$explained_gene_frequency_all_depth{$depth_th}
->[$i]->[$dys_status]->{$s} );
$res_dys++
if (
$dysregulated_gene_frequency[$i]->[$dys_status]
->{$s} );
}
next if($res_dys == 0);
#print STDERR "".(sprintf("%.3f",$res_dys/$nb_sample))."\n";<STDIN>;
$g = get_name( $i, \@index_to_gene );
#print STDERR " *** gene_name: $g $i\n";<STDIN>;
print OUT ""
#. $g . "_" . $dys_status_corress[$dys_status] . "\t"
#. ( @{ $connections[$i] } ) . "\t"
. $res_dys . "\t"
#. ( sprintf( "%.3f", $res_dys / $nb_sample ) ) . "\t"
. $res_exp
#. "\t"
#. ( sprintf( "%.3f", $res_exp / $nb_sample ) )
. "\n";
}
}
close(OUT);
}
}
}
}
#perform a breath first search to search from path of dysregulated gene of length <= $depth_th that do not contain hub_gene
sub construct_explained_gene_set_dijkstra {
my ( $mutated_gene, $sample_gene_dysregulated, $sample_gene_mutated,
$dist_to_mutated_gene, $depth_th, $hub_th )
= @_;
#print STDERR "\nconstruct_explained_gene_set_dijkstra $mutated_gene\n";
$dist_to_mutated_gene->[$mutated_gene] = 0;
my @queue = ($mutated_gene);
my @res = ();
$max_gene_dist = 0;
#my $nb_joker = 1;
while ( @queue + 0 != 0 ) {
#search for the smallest element
#print STDERR "***************** |@queue|"."\n";
$min_index = 0;
for ( my $i = 0 ; $i < @queue ; $i++ ) {
if ( $dist_to_mutated_gene->[ $queue[$i] ] <
$dist_to_mutated_gene->[ $queue[$min_index] ] )
{
$min_index = $i;
}
}
$gene = $queue[$min_index];
$gene_dist = $dist_to_mutated_gene->[$gene];
#remove the gene for the queue
splice( @queue, $min_index, 1 );
if ( $gene_dist > 100
&& $gene_dist < 100 * ( $nb_joker + 1 )
&& $gene_dist % 100 >= 2 )
{
print STDERR "remove $gene "
. get_name( $gene, \@index_to_gene )
. " $gene_dist\n"; # =========== |@queue|\n";<STDIN>;
}
#print STDERR "**** EXPLORED gene: $gene $gene_dist\n";
if ( int( $gene_dist / 100 ) <= $nb_joker
&& int( $gene_dist / 100 ) + $gene_dist % 100 <= $depth_th )
{
#($gene_dist - ($nb_joker * 100) <= 0 || $gene_dist - ($nb_joker * 100) <= $depth_th){
#this gene is explained by the mutation
push( @res, $gene );
$max_gene_dist = $gene_dist if ( $gene_dist > $max_gene_dist );
if ( $mutated_gene == $gene || @{ $connections[$gene] } <= $hub_th )
{ #not a hub
foreach $neigh ( @{ $connections[$gene] } ) {
#print STDERR "\t".$neigh."\t".@{$sample_gene_dysregulated}."\t".$sample_gene_dysregulated->[$neigh]->[0]."\t".$sample_gene_dysregulated->[$neigh]->[1]."\t".$sample_gene_mutated->[$neigh]."\t".@{$connections[$neigh]}."\n";
#to have an additional control on the neighborhood
if
( #$sample_gene_mutated->[$neigh] == 0 &&#the dysregulation is more likely to be explained by a mutation
#@{$connections[$neigh]} <= $hub_th && #not a hub
(
$nb_joker != 0
|| $sample_gene_dysregulated->[$neigh]->[0] != 0
|| $sample_gene_dysregulated->[$neigh]->[1] != 0
) #at least 1 jocker OR the neighbour is dysregulated
)
{
#print STDERR "\t".$neigh." --------------> ADD\n";
#update the queue if it the first time we explore the gene
push( @queue, $neigh )
if ( $dist_to_mutated_gene->[$neigh] == -1 )
; #not already explored;
#update the distance
$edge_weight = 100000;
if ( @{$sample_gene_dysregulated} == 0
|| $sample_gene_dysregulated->[$neigh]->[0] != 0
|| $sample_gene_dysregulated->[$neigh]->[1] != 0 )
{ #dysregulated
$edge_weight = 1;
}
else {
$edge_weight = 100;
}
$dist_to_mutated_gene->[$neigh] =
$gene_dist + $edge_weight
if ( $dist_to_mutated_gene->[$neigh] == -1
|| $dist_to_mutated_gene->[$neigh] >
$gene_dist + $edge_weight );
}
#print STDERR "\n";
}
}
}
}
return \@res;
}