-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrandom_sample.pl
executable file
·279 lines (229 loc) · 7.82 KB
/
random_sample.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
#!/usr/bin/perl
use warnings;
my ( $data_dir, $network_type, $nb_real_sample_used, $nb_random_sample,
$flag_mutation_fixed, $out_dir, $script_dir, $seed )
= @ARGV;
require "$script_dir/Construct_network.pl";
my %gene_to_index;
my @index_to_gene;
my @connections;
print STDERR " *** construct network\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 );
}
if($seed != -1){
srand($seed);
}
`rm -r $out_dir`;
`mkdir $out_dir`;
opendir( DIR, $data_dir );
@the_DATA_DIR = readdir(DIR);
close(DIR);
#Compute the set of network genes that have an expression value
#gene without expression will not be swapped during the random process => the random sample will have the same graph as the true sample
my @index_to_gene_dys;
my %gene_dys_to_index;
my $ID_cmp = 0;
print STDERR " *** read real samples\n";
foreach my $dir_s (@the_DATA_DIR) {
$mutation_file_name = "$data_dir/$dir_s/Genelist_Status.txt";
if ( -e $mutation_file_name ) {
open( FILE, "$mutation_file_name" );
#print STDERR "********** read file $mutation_file_name\n";#<STDIN>;
while (<FILE>) {
chop($_);
@line = split( /\t/, $_ );
@parts = split( /_/, $line[0] );
$gene_name = $parts[0];
$status = $parts[1];
#count the dysregulated genes that belong to the network
if ( ( $status eq "UP" || $status eq "DOWN" )
&& exists $gene_to_index{$gene_name} )
{
if ( !exists $gene_dys_to_index{$gene_name} ) {
$gene_dys_to_index{$gene_name} = $ID_cmp;
$index_to_gene_dys[$ID_cmp] = $gene_name;
$ID_cmp++;
}
}
}
close(FILE);
}
}
#random data set that maintain the frequencies
my @all_random_mutation_ID = ();
my @all_random_expression_ID = ();
print STDERR " *** construct random ID\n";
for ( $i = 0 ; $i < $nb_random_sample ; $i++ ) {
$all_random_mutation_ID[$i] = compute_random_ID( \@index_to_gene );
$all_random_expression_ID[$i] = compute_random_ID( \@index_to_gene_dys );
}
my @all_real_dir = ();
foreach my $dir_s (@the_DATA_DIR) {
$mutation_file_name = "$data_dir/$dir_s/Genelist_Status.txt";
if ( -e $mutation_file_name ) {
push( @all_real_dir, $dir_s );
}
}
#Compute the number of sample used
#$nb_real_sample_used = @all_real_dir;
#if ( $nb_real_sample_usedfraction_real_sample_used ne "ALL" ) {
# $nb_real_sample_used =
# sprintf( "%.0f", $nb_real_sample_used * $fraction_real_sample_used );
#}
my @sample_mutated_gene_name = ();
my @sample_dys_gene_name = ();
my @real_data_set_used = ();
print STDERR " *** write random samples\n";
#Construct the REAL_ALL data set
`mkdir $out_dir/REAL_ALL`;
foreach my $dir_s (@all_real_dir) {
$mutation_file = "Genelist_Status.txt";
`mkdir $out_dir/REAL_ALL/$dir_s/`;
`ln -s $data_dir/$dir_s/$mutation_file $out_dir/REAL_ALL/$dir_s/Genelist_Status.txt`;
}
for ( $i = 0 ; $i < $nb_random_sample ; $i++ ) {
$dir_name_rand = "$out_dir/RANDOM_$i";
`mkdir $dir_name_rand` unless ( -d $dir_name_rand );
#The real data dirctory are only constructed once if all the sample are analyzed
#fake directory linking to REAL_0 will be created during the optimal parameter inference
$dir_name_real = "$out_dir/REAL_$i";
if ( !-d $dir_name_real
&& ( $nb_real_sample_used != @all_real_dir ) )
{
`mkdir $dir_name_real`;
}
#randomly choose X element of the real data
@real_data_set_used = ();
if ( $nb_real_sample_used == @all_real_dir ) {
@real_data_set_used = @all_real_dir;
}
else {
random_data_sampling( \@all_real_dir, \@real_data_set_used,
$nb_real_sample_used );
}
foreach my $dir_s (@real_data_set_used) {
$mutation_file = "Genelist_Status.txt";
$mutation_file_name = "$data_dir/$dir_s/Genelist_Status.txt";
#directory to store the random data
`mkdir $dir_name_rand/$dir_s`;
#for the corresponding "truncated" real data set
if ( $nb_real_sample_used != @all_real_dir ) {
`mkdir $dir_name_real/$dir_s`;
`ln -s $data_dir/$dir_s/$mutation_file $dir_name_real/$dir_s/Genelist_Status.txt`;
}
open( FILE, "$mutation_file_name" );
#print STDERR "********** read file $mutation_file_name\n";#<STDIN>;
@sample_mutated_gene_name = ();
@sample_dys_gene_name = ();
while (<FILE>) {
chop($_);
@line = split( /\t/, $_ );
@parts = split( /_/, $line[0] );
$gene_name = $parts[0];
$status = $parts[1];
#count the mutated gene that belong to the network
if ( exists $gene_to_index{$gene_name} ) { #
if ( $status eq "MUT" || $status eq "AMPL" || $status eq "DEL" )
{
push( @sample_mutated_gene_name, $gene_name );
#$rand_gene_ID = get_name;
#print OUT "".get_name($rand_gene_ID, \@index_to_gene)."_MUT\n";
}
if ( exists $gene_dys_to_index{$gene_name}
&& ( $status eq "UP" || $status eq "DOWN" ) )
{
push( @sample_dys_gene_name, $_ );
}
}
}
close(FILE);
#$nb_dys_gene_sample = @sample_dys_gene_name + 0;
#construct random mutation and expression gene without changing their frenquencies
#Mutation
$out_file = "$dir_name_rand/$dir_s/Genelist_Status.txt";
open( OUT, ">$out_file" );
for ( $j = 0 ; $j < @sample_mutated_gene_name ; $j++ ) {
$name = $sample_mutated_gene_name[$j];
$ID = get_ID( $name, \%gene_to_index ); #NO RANDOM ID
$new_ID = $all_random_mutation_ID[$i]->[$ID];
$new_name = get_name( $new_ID, \@index_to_gene );
if ( $flag_mutation_fixed eq "MUT_FIXED" ) {
print OUT "" . $name . "_MUT\n";
}
else {
print OUT "" . $new_name . "_MUT\n";
}
#print STDERR "".$name."\t".$new_name."_MUT\n";<STDIN>;
}
#Dys
for ( $j = 0 ; $j < @sample_dys_gene_name ; $j++ ) {
#print STDERR "".$sample_dys_gene_name[$j]."\n";<STDIN>;
@line = split( /\t/, $sample_dys_gene_name[$j] );
@parts = split( /_/, $line[0] );
$name = $parts[0];
$status = $parts[1];
#to change to obtain 2 different random sampling
#$ID = get_ID($gene_name, \%gene_to_index);
#Only gene with an expression value in at least one real sample could have an expression value in the random samples
if ( exists $gene_dys_to_index{$name} ) {
$new_ID = $all_random_expression_ID[$i]->[ $gene_dys_to_index{$name} ];
$new_name = $index_to_gene_dys[$new_ID];
print OUT ""
. $new_name . "_"
. $status . "\t"
. $line[1] . "\n";
#print OUT "".$name."_".$status."\t".$line[1]."\n";
if ( !exists $gene_to_index{$name} ) {
print STDERR
" *** WEIRD random dysregulated gene that do not belong to the network "
. $name . "\t"
. $dir_s . "\n";
}
if ( !exists $gene_dys_to_index{$name} ) {
print STDERR
" *** WEIRD random dysregulated gene that is never dysregulated in the real samples "
. $name . "\t"
. $dir_s . "\n";
}
}
}
close(OUT);
}
}
sub random_data_sampling {
my ( $all_real_dir, $real_data_set_used, $nb_real_sample_used ) = @_;
my %take = ();
my $nb_real_dir = @{$all_real_dir};
while ( @{$real_data_set_used} < $nb_real_sample_used ) {
$r = int( rand($nb_real_dir) );
if ( !exists $take{$r} ) {
$take{$r} = 1;
push( @{$real_data_set_used}, $all_real_dir->[$r] );
}
}
}
sub compute_random_ID {
my ($true_ID) = @_;
my @random_ID = ();
for ( $j = 0 ; $j < @{$true_ID} ; $j++ ) {
$random_ID[$j] = $j;
}
$max = @random_ID - 1;
while ( $max != 0 ) {
$r = int( rand($max) );
$rand_gene_ID = $random_ID[$r];
$random_ID[$r] = $random_ID[$max];
$random_ID[$max] = $rand_gene_ID;
$max--;
}
return \@random_ID;
}
#./PIPELINE/10_Cluster_Algo_fast.pl TEST_RAND/RANDOM_0/ Homo_sapiens.gene_info netbox.script 1000000 25 0 3 TEST_RAND/