-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConstruct_network.pl
More file actions
executable file
·306 lines (253 loc) · 9 KB
/
Construct_network.pl
File metadata and controls
executable file
·306 lines (253 loc) · 9 KB
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
use warnings;
#use strict;
sub construct_driver_net_network{
my ($index_to_gene, $gene_to_index, $connections, $script_dir) = @_;
#print STDERR " *** BEGIN DRIVER_NET NETWORK CONSTRUCTION\n";
$network = "$script_dir/network_FIsInGene_041709.txt";
construct_network_pair($network, $index_to_gene, $gene_to_index, $connections);
#print STDERR " *** END DRIVER_NET NETWORK CONSTRUCTION\n";
}
sub construct_netbox_network{
my ($data_dir, $index_to_gene, $gene_to_index, $connections, $flag_update, $script_dir) = @_;
$network_gene_list = "$script_dir/Homo_sapiens.gene_info";
$network = "$script_dir/netbox.script";
construct_gene_ID($network_gene_list, $index_to_gene, $gene_to_index);
#to obtain the gene that belong to the network
$updated_index_to_gene = construct_network($network, $index_to_gene, $gene_to_index, $connections);
#print STDERR "size: ".(@{$index_to_gene})."\t".(@{$updated_index_to_gene})."\n";
@{$index_to_gene} = @{$updated_index_to_gene};#simply copy the array
#print STDERR "size: ".(@{$index_to_gene})."\t".(@{$updated_index_to_gene})."\n";
if(defined $flag_update && $flag_update ne "NO_DATA_UPDATE"){
update_gene_index_using_data($data_dir, $index_to_gene, $gene_to_index);
}
}
sub construct_network_pair{
my ($network_file, $index_to_gene, $gene_to_index, $connections) = @_;
#print STDERR " *** construct_network_pair\n";
open(FILE, $network_file);
my $index = @{$index_to_gene}+0;
while(<FILE>){
chop $_;
@line = split(/\t/, $_);
for($i = 0; $i < @line; $i++){
$gene = $line[$i];
#print STDERR " *** $i $gene\n";
#a new gene
if(! exists $gene_to_index->{$gene}){
$gene_to_index->{$gene} = $index;
my @tab = ($gene);
push(@{$index_to_gene}, \@tab);
my @tab2 = ();
push(@{$connections}, \@tab2);
$index++;
}
}
$connection_exist = 0;
$index1 = get_ID($line[0], $gene_to_index);
$index2 = get_ID($line[1], $gene_to_index);
#a new connection
if(!belong($connections->[$index1], $index2)){
push(@{$connections->[$index1]}, $index2);
push(@{$connections->[$index2]}, $index1);
}
#print STDERR " *** $index1 $index2\t".@{$connections->[$index1]}."\t".@{$connections->[$index2]}."\n";<STDIN>;
}
#print STDERR " *** end construction\n";
}
sub construct_gene_ID{
my ($network_gene_list, $index_to_gene, $gene_to_index) = @_;
print STDERR " *** construct gene name table\n";
#Read the gene file 2 times to avoid problem with synonyms !!!
#1) compute the index of the main gene name
my $index = 0;
open(GEN, $network_gene_list);
while (<GEN>){
my $line = $_;
chomp($line);
my @parts = split (/\t/, $line);
my $gene = $parts[2];
$gene_to_index->{$gene} = $index;
my @tab = ();
$index_to_gene->[$index] = \@tab;
push(@{$index_to_gene->[$index]}, $gene);
$index++;
}
close(GEN);
#2) Add the index of the synonys that do not have any index to avoid confusion as in the following example:
#9606 26 ABP1 - ABP|AOC1|DAO|DAO1|KAO
#9606 1610 DAO - DAAO|DAMOX|MGC35381|OXDA
open(GEN, $network_gene_list);
while (<GEN>){
my $line = $_;
chomp($line);
my @parts = split (/\t/, $line);
my $gene = $parts[2];
$index = $gene_to_index->{$gene};
if (index($parts[4], "|") != -1){
my @synonyms = split(/\|/,$parts[4]);
foreach my $syn (@synonyms){
if(! exists $gene_to_index->{$syn}){
$gene_to_index->{$syn} = $index ;
push(@{$index_to_gene->[$index]}, $syn);
}
}
}
}
close(GEN);
}
sub construct_network{
my ($network, $index_to_gene, $gene_to_index, $connections) = @_;
print STDERR " *** construct network \n";#<STDIN>;
# 2. read network info into matrix
#my @connections = ();
read_network($connections, $index_to_gene, $gene_to_index, $network);
#remove all the genes that do not belong to the network
my @updated_index_to_gene = ();
$updated_index = 0;
for(my $i = 0; $i < @{$index_to_gene}; $i++){
if(@{$connections->[$i]}){
$updated_index_to_gene[$updated_index] = $index_to_gene->[$i];
$updated_index++;
}
else{
for($j = 0; $j < @{$index_to_gene->[$i]}; $j++){
delete $gene_to_index->{$index_to_gene->[$i]->[$j]};
}
}
}
#@index_to_gene = ();
$index_to_gene = \@updated_index_to_gene;
for(my $i = 0; $i < @{$index_to_gene}; $i++){
foreach $g (@{$index_to_gene->[$i]}){
if(get_name($i, $index_to_gene) eq "MMP9"){
#print STDERR $g."\t".(get_ID($g))."\t".$i."\n";<STDIN>;
}
#print STDERR $g."\t".$i."\n";<STDIN>;
$gene_to_index->{$g} = $i;
}
}
#read the network a 2nd time to obtain construct the edges with the updated index
#print STDERR "********** construct network 2\n";<STDIN>;
undef @{$connections};
read_network($connections, $index_to_gene, $gene_to_index, $network);
return \@updated_index_to_gene;
}
sub get_ID{
my ($gene, $gene_to_index) = @_;
return $gene_to_index->{$gene};
}
sub get_name{
my ($id, $index_to_gene) = @_;
if(!exists $index_to_gene->[$id]){
print ("Sorry there is NO gene-name for the ID $id \n");
<STDIN>;#exit();
}
return $index_to_gene->[$id]->[0];
}
sub read_network{
my ($connections, $index_to_gene, $gene_to_index, $net) = @_;
for (my $i=0; $i < @{$index_to_gene}; $i++){
my @tab = ();
push(@{$connections}, \@tab);
}
open(NET, $net);
my @line_parts;
my @genes;
while(<NET>){
my $line = $_;
chomp($line);
if (index($line, "INSERT INTO INTERACTION VALUES") != -1){
@line_parts = split(/,/,$line);
@genes = split(/'/,$line_parts[2]);
$gene1 = $genes[1];
@genes = split(/'/,$line_parts[3]);
$gene2 = $genes[1];
$index1 = get_ID($gene1, $gene_to_index);
$index2 = get_ID($gene2, $gene_to_index);
if(!belong($connections->[$index1], $index2)){
push(@{$connections->[$index1]}, $index2);
push(@{$connections->[$index2]}, $index1);
}
else{
#print STDERR "relation present multiple time $gene1 $gene2 ** $_";<STDIN>;
}
}
}
close(NET);
}
#to remove weird conflict due to gene with synonymous name is the input data sets from the ahsh table index_to_gene
#only one of the synonymous gene (look code for tie break) is used during the analysis
sub update_gene_index_using_data{
my ($data_dir, $index_to_gene, $gene_to_index) = @_;
opendir(DIR, $data_dir);
@the_DATA_DIR = readdir(DIR);
close(DIR);
#To track problem in gene naming, espessialy synonymous gene names in
my %gene_ID_name_update = ();
foreach my $dir_sample (@the_DATA_DIR){
$mutation_file_name = "$data_dir/$dir_sample/Genelist_Status.txt";
if(-e $mutation_file_name){
$nb_sample++;
$nb_mutated_gene = 0;
open(FILE, "$mutation_file_name");
#print STDERR " *** read file $mutation_file_name\n";#<STDIN>;
#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 );
#to make sure that the outputed gene name is the one present in the input files
$expected_gene_name = get_name($gene_ID, $index_to_gene);
if($expected_gene_name ne $gene_name){
if(exists $gene_ID_name_update{$gene_ID}){
$used_gene_name = $gene_ID_name_update{$gene_ID};
$index_to_gene->[$gene_ID]->[0] = $used_gene_name;
print STDERR " *** WARNING GENE $gene_ID WITH SYNONYMOUS NAMES DETECTED: $gene_name and ".$expected_gene_name. " -> ONLY DATA FROM GENE ".$used_gene_name." WILL BE USED/REPORTED DURING THE ANALYSIS\n";#<STDIN>;
#remove the gene for which we add a conflict
if($used_gene_name ne $gene_name){
delete $gene_to_index->{$gene_name};
}
else{
delete $gene_to_index->{$expected_gene_name};
}
#if(exists $gene_to_index->{$gene_name}){
#print STDERR " *** WEIRD !!!!!\n";<STDIN>;
#}
}
else{
#save the 1st gene name of the synomym list will be used in case of further conflict
$gene_ID_name_update{$gene_ID} = $expected_gene_name;
#To output the gene name present in the input file
$index_to_gene->[$gene_ID]->[0] = $gene_name;
print STDERR " *** SWAP GENE NAME ACCORDING TO DATA: $gene_ID: $expected_gene_name => $gene_name\n";#.($index_to_gene->[$gene_ID]->[0])."\n";#<STDIN>;
}
}
else{
$gene_ID_name_update{$gene_ID} = $expected_gene_name;
}
}
}
close(FILE);
#last if($nb_sample == $nb_sample_to_test);
}
}
}
sub is_connected{
my ($g1, $g2, $connections) = @_;
return belong($connections->[$g1], $g2);
}
sub belong{
my ($tab, $val) = @_;
my $res = 0;
my $comp = 0;
while($comp < @{$tab} && !$res){
$res = ($tab->[$comp] == $val);
$comp++;
}
return $res;
}
return 1;