blib/lib/auto/share/dist/Pheno-Ranker/diseases/hpo/scripts/hpo_disease_converter.pl untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 144 CC 7 SCAR 25.4
CC
7
Cov
0%
add_feature
30.0
initialize_entry
12.0
get_random_sex_pxf
2.0
CRAP 12.6
1 #!/usr/bin/perl
2 #
3 # A script that creates a disease-based
4 # BFF/PXF JSON array from HPO CSV data
5 #
6 # Last Modified: Jan/15/2024
7 #
8 # $VERSION taken from Pheno::Ranker
9 #
10 # Copyright (C) 2023-2025 Manuel Rueda - CNAG (manuel.rueda@cnag.eu)
11 #
12 # License: Artistic License 2.0
13 #
14 # If this program helps you in your research, please cite.
15  
16 use strict;
17 use warnings;
18 use Text::CSV_XS;
19 use JSON::XS;
20 use Getopt::Long qw(:config no_ignore_case);
21  
22 # Command-line arguments
23 GetOptions(
24 'i|input=s' => \my $input_file,
25 'f|format=s' => \my $format,
26 ) or die "Usage: $0 -i input.csv -f [pxf|bff]\n";
27  
28 # Handles both: <genes_to_phenotype.txt> <phenotype_to_genes.txt>
29 die "Input file is required.\nUsage: $0 -i input.csv -f [pxf|bff]\n" unless $input_file;
30 die "Format must be 'pxf' or 'bff'.\n" unless $format && ($format eq 'pxf' || $format eq 'bff');
31  
32 # Set a fixed seed for reproducibility
33 srand(12345);
34  
35 # Initialize CSV parser
36 my $csv = Text::CSV_XS->new({
37 binary => 1,
38 sep_char => "\t",
39 auto_diag => 1,
40 });
41  
42 # Open the input CSV file
43 open my $fh, '<', $input_file or die "Could not open '$input_file': $!";
44  
45 # Read the header
46 my $header = $csv->getline($fh);
47 $csv->column_names(@$header);
48  
49 # Data structures to hold diseases categorized by type
50 my %diseases = (
51 OMIM => {},
52 ORPHA => {},
53 );
54  
55 # Process each row in the CSV
56 while (my $row = $csv->getline_hr($fh)) {
57 my $hpo_id = $row->{'hpo_id'};
58 my $hpo_name = $row->{'hpo_name'};
59 my $disease_id = $row->{'disease_id'};
60  
61 my ($disease_type, $disease_identifier) = split(':', $disease_id, 2);
62  
63 unless (exists $diseases{$disease_type}) {
64 warn "Unknown disease type '$disease_type' for ID '$disease_id'. Skipping.\n";
65 next;
66 }
67  
68 unless (exists $diseases{$disease_type}{$disease_identifier}) {
69 $diseases{$disease_type}{$disease_identifier} = initialize_entry($disease_id, $format);
70 }
71  
72 add_feature($diseases{$disease_type}{$disease_identifier}, $hpo_id, $hpo_name, $format);
73 }
74  
75 close $fh;
76  
77 # Convert disease hashes to arrays for JSON encoding
78 my %output_data;
79 foreach my $type (keys %diseases) {
80 $output_data{$type} = [ values %{ $diseases{$type} } ];
81 }
82  
83 # Initialize JSON encoder
84 my $json = JSON::XS->new->utf8->pretty->canonical;
85  
86 # Write JSON files
87 foreach my $type (keys %output_data) {
88 my $filename = lc($type) . "." . lc($format) . ".json";
89 write_json($output_data{$type}, $filename, $json);
90 }
91  
92 print "Successfully wrote:\n" . (exists $output_data{'OMIM'} ? "omim.$format.json\n" : "")
93 . (exists $output_data{'ORPHA'} ? "orpha.$format.json\n" : "");
94  
95 # Subroutine to initialize an entry
96 sub initialize_entry {
97 my ($disease_id, $format) = @_;
98 my $entry = {
99 id => $disease_id,
100 };
101 if ($format eq 'pxf') {
102 $entry->{subject} = {
103 id => $disease_id,
104 sex => get_random_sex_pxf(),
105 vitalStatus => { status => "ALIVE" },
106 };
107 } elsif ($format eq 'bff') {
108 $entry->{sex} = get_random_sex_bff();
109 }
110 $entry->{phenotypicFeatures} = [];
111 return $entry;
112 }
113  
114 # Subroutine to add a feature
115 sub add_feature {
116 my ($entry, $hpo_id, $hpo_name, $format) = @_;
117 foreach my $feature (@{ $entry->{phenotypicFeatures} }) {
118 if ($format eq 'pxf' && $feature->{type}{id} eq $hpo_id) {
119 return;
120 }
121 if ($format eq 'bff' && $feature->{featureType}{id} eq $hpo_id) {
122 return;
123 }
124 }
125 if ($format eq 'pxf') {
126 push @{ $entry->{phenotypicFeatures} }, { type => { id => $hpo_id, label => $hpo_name } };
127 } elsif ($format eq 'bff') {
128 push @{ $entry->{phenotypicFeatures} }, { featureType => { id => $hpo_id, label => $hpo_name } };
129 }
130 }
131  
132 # Subroutine to get random sex for pxf
133 sub get_random_sex_pxf {
134 my @sex_options = ("MALE", "FEMALE");
135 return $sex_options[int(rand(@sex_options))];
136 }
137  
138 # Subroutine to get random sex for bff
139 sub get_random_sex_bff {
140 my @sex_options = (
141 { id => "NCIT:C20197", label => "Male" },
142 { id => "NCIT:C16576", label => "Female" }
143 );
144 return $sex_options[int(rand(@sex_options))];
145 }
146  
147 # Subroutine to write JSON data to a file
148 sub write_json {
149 my ($data_ref, $filename, $json_encoder) = @_;
150 open my $out_fh, '>', $filename or die "Could not open '$filename' for writing: $!";
151 print $out_fh $json_encoder->encode($data_ref);
152 close $out_fh;
153 }