-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedge2neo4jcsv.pl
486 lines (391 loc) · 13.4 KB
/
edge2neo4jcsv.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
=head1 NAME
edge2neo4jcsv.pl
graphcompare - A command-line tool to compare graph files in DOT or tabular format.
=head1 SYNOPSIS
edge2neo4jcsv.pl -wholegraph FILE \
-biogrid FILE \
-string FILE \
-ppaxe FILE \
-maxlvl INT \
-prefix PREFIX
=head1 OPTIONS
=over 8
=item B<-h>, B<-help>
Shows this help.
=item B<-w>, B<-wholegraph> <FILE>
REQUIRED. Wholegraph file is a required graph file (DOT or graphviz)
created by filter_interactions_to_graph2.pl.
=item B<-b>, B<-biogrid> <FILE>
REQUIRED. Biogrid file (tabular) contains index (col1), interactions (col2 and
col3), type of interactions (col4) and pubmed ID (col 5)from the BioGrid database.\n
=item B<-s>, B<-string> <FILE>
REQUIRED. String file (tabular) contains protein interaction (col1 and col2),
type of interaction (col3), and othe data (col4, col5, col6) from the string protein
database. Only the interactions are necessary and must be human only.
=item B<-pp>, B<-ppaxe> <FILE>
REQUIRED. Ppaxe file (tabular) is created from the ppaxe program that sifts through
pubmed articles looking for possible interactions. The file contains the Pubmed ID
(col1), the interaction (col2, col3) and the score(col4).
=item B<-m>, B<-maxlvl> <INT>
REQUIRED. Is an integer that represents the max level graph created, that is not
the wholegraph, from the filter_interactions_to_graph2.pl program. This value can
be acquiered by looking at the folder where the graphs are stored and finding the
highest number after "graph_lvl+".
=item B<-pr>, B<-prefix> <PREFIX>
This contains the prefix of the graph level files. Example: "all_graph_lvl+".
=item B<-o>, B<-output> <FILE>
REQUIRED. Output CSV file.
=back
=head1 FUNCTIONS
=cut
use warnings;
use strict;
use Getopt::Long qw(:config no_ignore_case);
use Data::Dumper;
use Pod::Usage;
# -----------------------------------------------------------------------------
=over 2
=item get_cmdline_arguments()
Reads command line arguments
=back
=cut
sub get_cmdline_arguments {
# INPUTS:
# whole_graph_file
# interaction_prefix
# max_lvl
# interaction_type_file
# interaction_source_file
# OPTIONS:
# \%options
my $options = shift;
# Print synopsis if no arguments
pod2usage(
-verbose => 0,
-output => \*STDOUT
) if (!@ARGV);
GetOptions(
$options,
'help|?',
'verbose|?',
'wholegraph=s',
'maxlvl=s',
'prefix=s',
'biogrid=s',
'string=s',
'ppaxe=s',
'output=s'
);
# Print help if -help option
if (defined $options->{'help'}){
pod2usage(
-verbose => 1,
-output => \*STDOUT
);
}
my @required = qw( wholegraph maxlvl prefix biogrid string ppaxe output );
foreach my $req (@required) {
defined $options->{$req}
or die "\n# Command line option $req is missing!\n";
}
return;
}
# -----------------------------------------------------------------------------
=over 2
=item init_graph()
Initializes whole graph
=back
=cut
sub init_graph {
my $wholegraph = shift;
my $maxlvl = shift;
my $verbose = shift;
print STDERR "\tReading graph file: ", $wholegraph, " ..." if $verbose;
my %data = ();
open (my $fh, "<", $wholegraph)
or die "Can't read $wholegraph : $!\n";
while (my $line = <$fh>) {
chomp($line);
next unless $line =~ m/->/;
my ($gene1, $gene2) = split /->/, $line;
$gene1 = gene_cleaner($gene1);
$gene2 = gene_cleaner($gene2);
my $interaction1 = $gene1 . "->" . $gene2;
my $interaction2 = $gene2 . "->" . $gene1;
my @inter = ($interaction1, $interaction2);
foreach my $interaction (@inter){
$data{$interaction} = () unless exists $data{$interaction};
$data{$interaction}->{"level"} = $maxlvl + 1;
$data{$interaction}->{"genetic_interaction"} = 0;
$data{$interaction}->{"physical_interaction"} = 0;
$data{$interaction}->{"unknown_interaction"} = 0;
$data{$interaction}->{"biogrid"} = 0;
$data{$interaction}->{"string"} = 0;
$data{$interaction}->{"ppaxe"} = 0;
$data{$interaction}->{"ppaxe_score"} = "NA";
$data{$interaction}->{"biogrid_pubmedid"} = "NA";
$data{$interaction}->{"ppaxe_pubmedid"} = "NA";
}
}
print STDERR "done.\n" if $verbose;
return \%data;
}
# -----------------------------------------------------------------------------
=over 2
=item gene_cleaner()
Removes non-valid characters from genes by taking only the symbol between quotes.
=back
=cut
sub gene_cleaner {
my $dirty_gene = shift;
if ($dirty_gene =~ m/"(.*?)"/) {
return $1;
} else {
die "Cannot clean $dirty_gene\n";
}
}
# -----------------------------------------------------------------------------
=over 2
=item add_orphan_interactions()
This function initializes interactions that are misteriously
not present in the whole graph.
=back
=cut
sub add_orphan_interactions {
my $interaction = shift;
my $data = shift;
my $maxlvl = shift;
my $filename = shift;
my $verbose = shift;
print STDERR "\n\t\t[WARNING] Orphan found in lvl file: $filename => $interaction ";
my ($gene1, $gene2) = split /->/, $interaction;
my $rev_interaction = $gene2 . "->" . $gene1;
foreach my $int ($interaction, $rev_interaction) {
$data->{$int} = () unless exists $data->{$int};
$data->{$int}->{"level"} = $maxlvl + 1;
$data->{$int}->{"genetic_interaction"} = 0;
$data->{$int}->{"physical_interaction"} = 0;
$data->{$int}->{"unknown_interaction"} = 0;
$data->{$int}->{"biogrid"} = 0;
$data->{$int}->{"string"} = 0;
$data->{$int}->{"ppaxe"} = 0;
$data->{$int}->{"ppaxe_score"} = "NA";
$data->{$int}->{"biogrid_pubmedid"} = "NA";
$data->{$int}->{"ppaxe_pubmedid"} = "NA";
}
return;
}
# -----------------------------------------------------------------------------
sub get_level {
# INPUTS: filename, data structure
my $prefix = shift;
my $maxlvl = shift;
my $data = shift;
my $verbose = shift;
print STDERR "\tAdding interaction level... " if $verbose;
for my $currlvl (0..$maxlvl) {
my $filename = $prefix . $currlvl . ".dot";
open (my $fh, "<", $filename)
or die "Cannot open $filename!\n";
while (my $line = <$fh>) {
chomp($line);
next unless $line =~ m/->/;
my ($gene1, $gene2) = split /->/, $line;
$gene1 = gene_cleaner($gene1);
$gene2 = gene_cleaner($gene2);
my $interaction = $gene1 . "->" . $gene2;
if (not exists $data->{$interaction}) {
add_orphan_interactions($interaction, $data, $maxlvl, $filename, $verbose);
}
$data->{$interaction}{"level"} = $currlvl if ($data->{$interaction}{"level"} == ($maxlvl + 1));
}
}
print STDERR "done.\n" if $verbose;
return;
}
# -----------------------------------------------------------------------------
sub biogrid_type_converter {
my $biogrid_type = shift;
my %interaction_dict = (
"Affinity Capture-Luminescence" => 1,
"Affinity Capture-MS" => 1,
"Affinity Capture-RNA" => 1,
"Affinity Capture-Western" => 1,
"Biochemical Activity" => 1,
"Co-crystal Structure" => 1,
"Co-fractionation" => 1,
"Co-localization" => 1,
"Co-purification" => 1,
"Far Western" => 1,
"FRET" => 1,
"PCA" => 1,
"Protein-peptide" => 1,
"Protein-RNA" => 1,
"Proximity Label-MS" => 1,
"Reconstituted Complex" => 1,
"Two-hybrid" => 1,
"Dosage Growth Defect" => 0,
"Dosage Lethality" => 0,
"Dosage Rescue" => 0,
"Negative" => 0,
"Phenotypic Enhancement" => 0,
"Phenotypic Suppression" => 0,
"Positive" => 0,
"Synthetic Growth Defect" => 0,
"Synthetic Haploinsufficiency" => 0,
"Synthetic Lethality" => 0,
"Synthetic Rescue" => 0,
"## UNKOWN ##" => 4
);
my $tp = exists ($interaction_dict{$biogrid_type})
? $interaction_dict{$biogrid_type}
: $interaction_dict{"## UNKOWN ##"};
#print "$biogrid_type -> $tp\n";
return $tp;
}
###############################################################################
## Source and Type ##
###############################################################################
# Note: Check file type before finishing this!
# -----------------------------------------------------------------------------
sub biogrid_filter {
# INPUTS: filename, datastructure, data for biogrid type and source
my $biogrid_source = shift;
my $data = shift;
my $verbose = shift;
print STDERR "\tFiltering biogrid... " if $verbose;
my $filename = $biogrid_source;
open (my $fh, "<", $filename)
or die "Cannot open $filename!\n";
my $header = <$fh>;
while (my $line = <$fh>) {
chomp($line);
my @columns = split /\t/, $line;
my $gene1 = $columns[7];
my $gene2 = $columns[8];
my $type = $columns[12];
my $PMID = $columns[14];
$type = biogrid_type_converter($type);
my $interaction = $gene1 . "->" . $gene2;
if (exists ($data->{$interaction})) {
$data->{$interaction}{"biogrid"} = 1;
$data->{$interaction}{"biogrid_pubmedid"} = $PMID;
}
if (exists ($data->{$interaction}) and $type = 0) {
$data->{$interaction}{"genetic_interaction"}++;
}
if (exists ($data->{$interaction}) and $type = 1) {
$data->{$interaction}{"physical_interaction"}++;
}
if (exists ($data->{$interaction}) and $type = 4) {
$data->{$interaction}{"unkown_interaction"}++;
}
}
print STDERR "done.\n" if $verbose;
return;
}
# -----------------------------------------------------------------------------
sub string_filter {
# INPUTS: filename, datastructure, data for string type and source
my $string_source = shift;
my $data = shift;
my $verbose = shift;
print STDERR "\tFiltering string... " if $verbose;
my $filename = $string_source;
open (my $fh, "<", $filename)
or die "Cannot open $filename!\n";
while (my $line = <$fh>) {
chomp($line);
my ($gene1, $gene2, $type, undef, undef, undef) = split /\t/, $line;
my $interaction = $gene1 . "->" . $gene2;
if (exists ($data->{$interaction})) {
$data->{$interaction}{"physical_interaction"}++;
$data->{$interaction}{"string"} = 1;
}
}
print STDERR "done.\n" if $verbose;
return;
}
# -----------------------------------------------------------------------------
sub ppaxe_filter {
# INPUTS: filename, datastructure, data for ppaxe source and PMID
my $ppaxe_source = shift;
my $data = shift;
my $verbose = shift;
print STDERR "\tFiltering ppaxe... " if $verbose;
my $filename = $ppaxe_source;
open (my $fh, "<", $filename)
or die "Cannot open $filename!\n";
while (my $line = <$fh>) {
chomp($line);
my ($gene1, $gene2, $PMID, $score, undef) = split /\t/, $line;
my $interaction = $gene1 . "->" . $gene2;
if (exists ($data->{$interaction})) {
$data->{$interaction}{"unknown_interaction"}++;
$data->{$interaction}{"ppaxe_pubmedid"} = $PMID;
$data->{$interaction}{"ppaxe"} = 1;
$data->{$interaction}{"ppaxe_score"} = $score;
}
} # $line from $fh
close($fh);
print STDERR "done.\n" if $verbose;
return;
}
# -----------------------------------------------------------------------------
=over 2
=item print_csv()
Explain format for CSV/NEO4j
=back
=cut
sub print_csv {
my $data = shift;
my $output = shift;
my $verbose = shift;
print STDERR "\tPrinting csv to $output ... " if $verbose;
open (my $fh, ">", $output)
or die "Cannot open $output!\n";
foreach my $interaction (keys %$data) {
my ($gene1, $gene2) = split /->/, $interaction;
# Skip interactions not present in biogrid OR string OR ppaxe
if (not $data->{$interaction}{biogrid} and not $data->{$interaction}{string} and not not $data->{$interaction}{ppaxe}) {
next;
}
print $fh "$gene1", ",", "$gene2";
foreach my $attribute (qw(level genetic_interaction physical_interaction unknown_interaction biogrid biogrid_pubmedid string ppaxe ppaxe_score ppaxe_pubmedid)) {
print $fh ",$data->{$interaction}{$attribute}";
}
print $fh "\n";
}
close($fh);
print STDERR "done.\n" if $verbose;
return;
}
# -----------------------------------------------------------------------------
# MAIN PROGRAM
my %options;
get_cmdline_arguments(\%options);
# START REPORT
my $start_time = time();
my $current_time = localtime();
print STDERR "\nPROGRAM STARTED\n",
"\tProgram edge2neo4jcsv.pl
"\tVersion v0.1.0\n",
"\tStart time $current_time\n\n" if $options{'verbose'};
my $data = init_graph($options{'wholegraph'}, $options{'maxlvl'}, $options{'verbose'});
get_level($options{'prefix'}, $options{'maxlvl'}, $data, $options{'verbose'});
biogrid_filter($options{'biogrid'}, $data, $options{'verbose'});
string_filter($options{'string'}, $data, $options{'verbose'});
ppaxe_filter($options{'ppaxe'}, $data, $options{'verbose'});
print_csv($data, $options{'output'}, $options{'verbose'});
# END REPORT
if ($options{'verbose'}) {
my $end_time = time();
$current_time = localtime();
my $sec = $end_time - $start_time;
my $hours = ($sec/3600) % 24;
my $minutes = ($sec/60) % 60;
my $seconds = $sec % 60;
print STDERR "\nPROGRAM FINISHED\n",
"\tEnd time \t$current_time\n\n",
"\tJob took ~ $hours hours, $minutes minutes and $seconds seconds\n\n";
}