| 1 | package Pheno::Ranker::Graph; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use feature qw(say); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Sort::Naturally qw(nsort); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Pheno::Ranker::IO; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Pheno::Ranker::Metrics; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 8 | our @EXPORT = qw(binary_hash2graph cytoscape2graph); | |||||||||||||||||||||||||||
| 13 | 8 | ▶ | use constant DEVEL_MODE => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | ############################ | ||||||||||||||||||||||||||||
| 16 | ############################ | ||||||||||||||||||||||||||||
| 17 | # SUBROUTINES FOR GRAPHS # | ||||||||||||||||||||||||||||
| 18 | ############################ | ||||||||||||||||||||||||||||
| 19 | ############################ | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | sub binary_hash2graph { | ||||||||||||||||||||||||||
|
Subroutine
binary_hash2graph: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 4 | my $arg = shift; | |||||||||||||||||||||||||||
| 23 | 4 | my $ref_binary_hash = $arg->{ref_binary_hash}; | |||||||||||||||||||||||||||
| 24 | 4 | my $output = $arg->{json}; | |||||||||||||||||||||||||||
| 25 | 4 | my $metric_name = $arg->{metric}; | |||||||||||||||||||||||||||
| 26 | 4 | my $verbose = $arg->{verbose}; | |||||||||||||||||||||||||||
| 27 | 4 | my $graph_stats = $arg->{graph_stats}; | |||||||||||||||||||||||||||
| 28 | 4 | my $graph_min_weight = $arg->{graph_min_weight}; | |||||||||||||||||||||||||||
| 29 | 4 | my $graph_max_weight = $arg->{graph_max_weight}; | |||||||||||||||||||||||||||
| 30 | 4 | my @ids = nsort( keys %$ref_binary_hash ); | |||||||||||||||||||||||||||
| 31 | 4 | my @strings = map { $ref_binary_hash->{$_}{binary_digit_string_weighted} } @ids; | |||||||||||||||||||||||||||
| 32 | 4 | my %similarity_function = ( | |||||||||||||||||||||||||||
| 33 | hamming => \&hd_fast, | ||||||||||||||||||||||||||||
| 34 | jaccard => \&jaccard_similarity_formatted, | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | 4 | my $metric = $similarity_function{$metric_name}; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 4 | my @nodes = map { { data => { id => $_ } } } @ids; | |||||||||||||||||||||||||||
| 39 | 4 | my @edges; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 4 | for my $i ( 0 .. $#ids ) { | |||||||||||||||||||||||||||
| 42 | 78 | ▶ | say "Creating graph edges for <" . $ids[$i] . ">..." if $verbose; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 78 | my $str1 = $strings[$i]; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 78 | for my $j ( $i + 1 .. $#ids ) { | |||||||||||||||||||||||||||
| 46 | 1266 | my $weight = $metric->( $str1, $strings[$j] ); | |||||||||||||||||||||||||||
| 47 | 1266 | ▶ | next unless _keep_edge( $weight, $graph_min_weight, $graph_max_weight ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 796 | push @edges, | |||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | data => { | ||||||||||||||||||||||||||||
| 52 | source => $ids[$i], | ||||||||||||||||||||||||||||
| 53 | target => $ids[$j], | ||||||||||||||||||||||||||||
| 54 | weight => $weight, | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | }; | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 4 | my %graph = ( | |||||||||||||||||||||||||||
| 61 | elements => { | ||||||||||||||||||||||||||||
| 62 | nodes => \@nodes, | ||||||||||||||||||||||||||||
| 63 | edges => \@edges, | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 4 | ▶ | say "Writting <$output> file " if $verbose; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 4 | write_json( { filepath => $output, data => \%graph } ); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 4 | ▶ | return defined $graph_stats ? \%graph : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 1266 | ▶ | sub _keep_edge { | ||||||||||||||||||||||||||
|
Subroutine
_keep_edge: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||
| 74 | 1266 | my ( $weight, $min_weight, $max_weight ) = @_; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1266 | ▶ | return 0 if defined $min_weight && $weight < $min_weight; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $min_weight and $weight < $min_weight
MC/DC0%0/2
defined $min_weight and $weight < $min_weight
defined $min_weight
$weight < $min_weight
Truth table33%1/3
A and B
A= defined $min_weightB= $weight < $min_weight
| |||||||||||||||||||||||||||||
| 77 | 1266 | ▶ | return 0 if defined $max_weight && $weight > $max_weight; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $max_weight and $weight > $max_weight
MC/DC100%2/2
defined $max_weight and $weight > $max_weight
defined $max_weight
$weight > $max_weight
Truth table100%3/3
A and B
A= defined $max_weightB= $weight > $max_weight
| |||||||||||||||||||||||||||||
| 78 | 796 | return 1; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | sub cytoscape2graph { | ||||||||||||||||||||||||||
|
Subroutine
cytoscape2graph: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # This is a very time consuming function, that's why we only load data in Graph | ||||||||||||||||||||||||||||
| 84 | # if the user asks for it | ||||||||||||||||||||||||||||
| 85 | 2 | require Graph; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Decode JSON to a Perl data structure | ||||||||||||||||||||||||||||
| 88 | 2 | my $arg = shift; | |||||||||||||||||||||||||||
| 89 | 2 | my $json_data = $arg->{graph}; | |||||||||||||||||||||||||||
| 90 | 2 | my $output = $arg->{output}; | |||||||||||||||||||||||||||
| 91 | 2 | my $metric = $arg->{metric}; | |||||||||||||||||||||||||||
| 92 | 2 | my $verbose = $arg->{verbose}; | |||||||||||||||||||||||||||
| 93 | 2 | ▶ | my $jaccard = $metric eq 'jaccard' ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 2 | my @nodes = @{ $json_data->{elements}{nodes} }; | |||||||||||||||||||||||||||
| 96 | 2 | my @edges = @{ $json_data->{elements}{edges} }; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Create a new Graph object | ||||||||||||||||||||||||||||
| 99 | 2 | my $graph = Graph->new( undirected => 1 ); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Add nodes and edges to the graph | ||||||||||||||||||||||||||||
| 102 | 2 | foreach my $node (@nodes) { | |||||||||||||||||||||||||||
| 103 | 39 | $graph->add_vertex( $node->{data}{id} ); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | foreach my $edge (@edges) { | |||||||||||||||||||||||||||
| 107 | $graph->add_weighted_edge( | ||||||||||||||||||||||||||||
| 108 | $edge->{data}{source}, | ||||||||||||||||||||||||||||
| 109 | $edge->{data}{target}, | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Convert to distances if Jaccard | ||||||||||||||||||||||||||||
| 112 | $jaccard ? 1 - $edge->{data}{weight} : $edge->{data}{weight} | ||||||||||||||||||||||||||||
| 113 | 633 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Now $graph contains the Graph object populated with the Cytoscape data | ||||||||||||||||||||||||||||
| 117 | 2 | graph_stats( $graph, $output, $metric, $verbose ); | |||||||||||||||||||||||||||
| 118 | 2 | return 1; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | ▶ | sub graph_stats { | ||||||||||||||||||||||||||
|
Subroutine
graph_stats: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 122 | 2 | my ( $g, $output, $metric, $verbose ) = @_; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Open the output file | ||||||||||||||||||||||||||||
| 125 | 2 | ▶ | say "Writting <$output> file " if $verbose; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 2 | open( my $fh, '>:encoding(UTF-8)', $output ); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Basic stats | ||||||||||||||||||||||||||||
| 129 | 2 | print $fh "Metric: ", ucfirst($metric), "\n"; | |||||||||||||||||||||||||||
| 130 | 2 | print $fh "Number of vertices: ", scalar $g->vertices, "\n"; | |||||||||||||||||||||||||||
| 131 | 2 | print $fh "Number of edges: ", scalar $g->edges, "\n"; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # Checking connectivity and components | ||||||||||||||||||||||||||||
| 134 | 2 | print $fh "Is connected: ", $g->is_connected, "\n"; | |||||||||||||||||||||||||||
| 135 | 2 | print $fh "Connected Components: ", scalar $g->connected_components, "\n"; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # Diameter and average path length, check if the graph is connected first | ||||||||||||||||||||||||||||
| 138 | 2 | ▶ | if ( $g->is_connected ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 2 | print $fh "Graph Diameter: ", ( join "->", $g->diameter ), "\n"; | |||||||||||||||||||||||||||
| 140 | 2 | print $fh "Average Path Length: ", | |||||||||||||||||||||||||||
| 141 | sprintf( "%7.3f", $g->average_path_length ), "\n"; | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Display degrees of all vertices | ||||||||||||||||||||||||||||
| 145 | 2 | foreach my $v ( $g->vertices ) { | |||||||||||||||||||||||||||
| 146 | 39 | print $fh "Degree of vertex $v: ", $g->degree($v), "\n"; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # Minimum Spanning Tree | ||||||||||||||||||||||||||||
| 150 | 2 | my $mst = $g->MST_Kruskal; # Assuming Kruskal's is available and appropriate | |||||||||||||||||||||||||||
| 151 | 2 | print $fh "MST has ", scalar $mst->edges, " edges\n"; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # Calculate and write all pairs shortest paths and their lengths to the file | ||||||||||||||||||||||||||||
| 154 | 2 | foreach my $u ( $g->vertices ) { | |||||||||||||||||||||||||||
| 155 | 39 | foreach my $v ( $g->vertices ) { | |||||||||||||||||||||||||||
| 156 | 1305 | ▶ | if ( $u ne $v ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 1266 | my @path = $g->SP_Dijkstra( $u, $v ) | |||||||||||||||||||||||||||
| 158 | ; # Get shortest path using Dijkstra's algorithm | ||||||||||||||||||||||||||||
| 159 | 1266 | ▶ | if (@path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 1266 | my $distance = $g->path_length( $u, $v ); # Recompute | |||||||||||||||||||||||||||
| 161 | 1266 | print $fh "Shortest path from $u to $v is ", | |||||||||||||||||||||||||||
| 162 | ( join "->", @path ), " [", scalar @path, | ||||||||||||||||||||||||||||
| 163 | "] with length $distance\n"; | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | else { | ||||||||||||||||||||||||||||
| 166 | 0 | print $fh "No path from $u to $v\n"; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # Close the output file | ||||||||||||||||||||||||||||
| 173 | 2 | close $fh; | |||||||||||||||||||||||||||
| 174 | 2 | return 1; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 8 | 1; | |||||||||||||||||||||||||||