-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_edges_with_scores_file.pl
72 lines (53 loc) · 2.06 KB
/
create_edges_with_scores_file.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
# Reads HOC CSV Formatted Data And Retains Specific Columns, Saved Into New File
#
# ~Statler
use strict;
use warnings;
my $edges_csv_file_path = "data/MESHD008881_MESHD008274/edges.csv";
my $edges_with_scores_path = "data/MESHD008881_MESHD008274/edges_with_scores.csv";
my @header_columns_to_save = ( ":START_ID", ":END_ID", "year:int", "metric_jaccard:float[]" );
my @header_indices_to_save = ();
# Do Not Modify
my $header_parsed_flag = 0;
my $header_length = 0;
open( FILE, "<:", "$edges_csv_file_path" ) or die "Error: Unable To Open File - $!";
open( wFILE, ">:", "$edges_with_scores_path" ) or die "Error: Unable To Create File - $!";
while( <FILE> )
{
chomp;
my @tokens = split( /\,/, $_ );
# Parse Header To Get Desired Header Column Indices
if( $header_parsed_flag == 0 )
{
print( "Parsing Header Column Info\n" );
for( 0..$#tokens )
{
my $current_header_index = $_;
my $current_header_info = $tokens[$current_header_index];
for my $requested_header_info ( @header_columns_to_save )
{
push( @header_indices_to_save, $current_header_index ) if( $requested_header_info eq $current_header_info );
}
}
$header_length = scalar @tokens;
print( "Obtained Header Columns From File: \"$edges_csv_file_path\"\n" );
print( "Obtained " . scalar @header_indices_to_save . " Column Elements\n" );
my $header_info = join( ",", @header_columns_to_save );
print( wFILE "$header_info\n" );
$header_parsed_flag = 1;
}
# Parse Actual Data And Write To New File
else
{
my $new_data = "";
for my $token_index ( @header_indices_to_save )
{
$new_data .= "$tokens[$token_index]," if $token_index < scalar @tokens;
}
$new_data =~ s/\,$//;
print( wFILE "$new_data\n" ) if scalar @tokens == $header_length;
}
}
close( FILE );
close( wFILE );
print( "~Fin\n" );