lib/Pheno/Ranker/Compare/Ontology.pm

filter: Statementstmt 100% 47 / 47 Branchbran n/a Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 93.1% 54 / 58 CC 4 SCAR 6.4
CC
4
Cov
98%
add_hpo_ascendants
3.0
parse_hpo_json
2.0
BEGIN
1.0
CRAP 1.9
« lib/Pheno/Ranker/Options.pm ↑ Summary lib/Pheno/Ranker/Compare.pm »
1 package Pheno::Ranker::Compare::Ontology;
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  
6 8 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 our @EXPORT_OK = qw(add_hpo_ascendants parse_hpo_json);
8  
9 8 ▶use constant DEVEL_MODE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶sub add_hpo_ascendants {
Subroutine add_hpo_ascendants: called CC=3 SCAR=11.0
Pod undocumented
12 2 my ( $key, $nodes, $edges, $nomenclature ) = @_;
13 2 ▶ $nomenclature ||= {};
Condition50%1/2
$nomenclature ||= {}
l!l
20
MC/DC0%0/1
$nomenclature ||= {}
$nomenclature
Truth table50%1/2
A
A= $nomenclature
Aresult
11
00
14  
15 # First we obtain the ontology (0000539) from HP:0000539
16 2 $key =~ m/HP:(\w+)$/;
17 2 my $ontology = $1;
18  
19 # We'll use it to build a string equivalent to a key from $edges
20 2 my $hpo_url = 'http://purl.obolibrary.org/obo/HP_';
21 2 my $hpo_key = $hpo_url . $ontology;
22  
23 # We will include all ascendants in an array
24 2 my @ascendants;
25 2 for my $parent_id ( @{ $edges->{$hpo_key} } ) {
26  
27 # We have to create a copy to not modify the original $parent_id
28 # as it can appear in multiple individuals
29 2 my $copy_parent_id = $parent_id;
30 2 $copy_parent_id =~ m/\/(\w+)$/;
31 2 $copy_parent_id = $1;
32 2 $copy_parent_id =~ tr/_/:/;
33  
34 # *** IMPORTANT ***
35 # We cannot add any label to the ascendants, otherwise they will
36 # not be matched by an indv down the tree
37 # Myopia
38 # Mild Myopia
39 # We want that 'Mild Myopia' matches 'Myopia', thus we can not add a label from 'Mild Myopia'
40 # Use the labels only for debug
41 2 my $asc_key = DEVEL_MODE ? $key . '.HPO_asc_DEBUG_ONLY' : $key;
42 2 $asc_key =~ s/HP:$ontology/$copy_parent_id/g;
43 2 push @ascendants, $asc_key;
44  
45 # We finally add the label to %nomenclature
46 2 my $hpo_asc_str = $hpo_url
47 . $copy_parent_id; # 'http://purl.obolibrary.org/obo/HP_HP:0000539
48 2 $hpo_asc_str =~ s/HP://; # 0000539
49 2 $nomenclature->{$asc_key} = $nodes->{$hpo_asc_str}{lbl};
50 }
51 2 return \@ascendants;
52 }
53  
54 1 ▶sub parse_hpo_json {
Subroutine parse_hpo_json: called CC=2 SCAR=6.9
Pod undocumented
55 1 my $data = shift;
56  
57 # The <hp.json> file is a structured representation of the Human Phenotype Ontology (HPO) in JSON format.
58 # The HPO is structured into a directed acyclic graph (DAG)
59 # Here's a brief overview of the structure of the hpo.json file:
60 # - graphs: This key contains an array of ontology graphs. In the case of HPO, there is only one graph. The graph has two main keys:
61 # - nodes: An array of objects, each representing an HPO term. Each term object has the following keys:
62 # - id: The identifier of the term (e.g., "HP:0000118").
63 # - lbl: The label (name) of the term (e.g., "Phenotypic abnormality").
64 # - meta: Metadata associated with the term, including definition, synonyms, and other information.
65 # - type: The type of the term, usually "CLASS".
66 # - edges: An array of objects, each representing a relationship between two HPO terms. Each edge object has the following keys:
67 # - sub: The subject (child) term ID (e.g., "HP:0000924").
68 # - obj: The object (parent) term ID (e.g., "HP:0000118").
69 # - pred: The predicate that describes the relationship between the subject and object terms, typically "is_a" in HPO.
70 # - meta: This key contains metadata about the HPO ontology as a whole, such as version information, description, and other details.
71  
72 1 my $graph = $data->{graphs}->[0];
73 1 my %nodes = map { $_->{id} => $_ } @{ $graph->{nodes} };
74 1 my %edges = ();
75  
76 1 for my $edge ( @{ $graph->{edges} } ) {
77 1 my $child_id = $edge->{sub};
78 1 my $parent_id = $edge->{obj};
79 1 push @{ $edges{$child_id} }, $parent_id;
80 }
81 1 return \%nodes, \%edges;
82 }
83  
84 8 1;
« lib/Pheno/Ranker/Options.pm ↑ Summary lib/Pheno/Ranker/Compare.pm »