utils/bff_pxf_simulator/bff-pxf-simulator

filter: Statementstmt 96.2% 207 / 215 Branchbran 84.3% 54 / 64 Conditioncond 40.5% 15 / 37 MC/DCmcdc 13.6% 3 / 22 Subroutinesub 95.7% 45 / 47 Podpod n/a totaltotal 84.1% 324 / 385 CC 50 SCAR 13.6
CC
50
Cov
87%
bff_generator
10.5
validate_args
10.2
say_errors
6.0
CRAP 3.9
« lib/Pheno/Ranker.pm ↑ Summary lib/Pheno/Ranker/Config.pm »
1 #!/usr/bin/env perl
2 #
3 # A script that creates a JSON array of simulated BFF/PXF
4 #
5 # Note: Check also Monarch Initiative:
6 # https://github.com/monarch-initiative/PhenoImp
7 #
8 # Last Modified: Jun/04/2025
9 #
10 # $VERSION taken from Pheno::Ranker
11 #
12 # Copyright (C) 2023-2025 Manuel Rueda - CNAG (manuel.rueda@cnag.eu)
13 #
14 # License: Artistic License 2.0
15 #
16 # If this program helps you in your research, please cite.
17  
18 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶use Getopt::Long qw(:config no_ignore_case);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 ##### Main #####
25 11 randomize_ga4gh();
26 ################
27 5 exit;
28  
29 11 ▶sub randomize_ga4gh {
Subroutine randomize_ga4gh: called CC=4 SCAR=14.6
30 11 my $VERSION = '1.08';
31 11 my $format = 'bff';
32 11 my $number = 100;
33 11 my $output = 'individuals.json';
34 11 my ( $phenotypicFeatures, $diseases, $treatments, $procedures, $exposures,
35 $ethnicity )
36 = (1) x 6;
37  
38 # Reading arguments
39 GetOptions(
40 'format|f=s' => \$format, # string
41 'number|n=i' => \$number, # string
42 'output|o=s' => \$output, # string
43  
44 #
45 'diseases=i' => \$diseases, # integer
46 'exposures=i' => \$exposures, # integer
47 'phenotypicFeatures=i' => \$phenotypicFeatures, # integer
48 'procedures=i' => \$procedures, # integer
49 'treatments=i' => \$treatments, # integer
50  
51 #
52 'max-diseases-pool=i' => \my $max_diseases_pool, # integer
53 'max-ethnicity-pool=i' => \my $max_ethnicity_pool, # integer
54 'max-exposures-pool=i' => \my $max_exposures_pool, # integer
55 'max-phenotypicFeatures-pool=i' => \my $max_phenotypicFeatures_pool, # integer
56 'max-treatments-pool=i' => \my $max_treatments_pool, # integer
57 'max-procedures-pool=i' => \my $max_procedures_pool, # integer
58  
59 #
60 'random-seed=i' => \my $random_seed, # integer
61 'external-ontologies=s' => \my $ext_ontologies, # string
62  
63 #
64 'help|?' => \my $help, # flag
65 'man' => \my $man, # flag
66 'debug=i' => \my $debug, # integer
67 'verbose|' => \my $verbose, # flag
68 0 ▶ 'version|V' => sub { print "$0 Version $VERSION\n"; exit; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 11 ▶ ) or pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("format|f=s", \$format, "number|n=i", \$number, "output|o=s", \$output, "diseases=i", \$diseases, "exposures=i", \$exposures, "phenotypicFeatures=i", \$phenotypicFeatures, "procedures=i", \$procedures, "treatments=i", \$treatments, "max-diseases-pool=i", \my $max_diseases_pool, "max-ethnicity-pool=i", \my $max_ethnicity_pool, "max-exposures-pool=i", \my $max_exposures_pool, "max-phenotypicFeatures-pool=i", \my $max_phenotypicFeatures_pool, "max-treatments-pool=i", \my $max_treatments_pool, "max-procedures-pool=i", \my $max_procedures_pool, "random-seed=i", \my $random_seed, "external-ontologies=s", \my $ext_ontologies, "help|?", \my $help, "man", \my $man, "debug=i", \my $debug, "verbose|", \my $verbose, "version|V", sub { print "$0 Version $VERSION\n"; exit; } ) or pod2usage(2) 11 0 11
70 11 ▶ pod2usage(1) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 11 11
71 11 ▶ pod2usage( -verbose => 2, -exitval => 0 ) if $man;
Branch: 1 branch
BranchTrueFalseTotal
if $man 0 11 11
72 11 validate_args(
73 {
74 format => $format,
75 number => $number,
76 output => $output,
77 diseases => $diseases,
78 exposures => $exposures,
79 phenotypicFeatures => $phenotypicFeatures,
80 procedures => $procedures,
81 treatments => $treatments,
82 max_diseases_pool => $max_diseases_pool,
83 max_ethnicity_pool => $max_ethnicity_pool,
84 max_exposures_pool => $max_exposures_pool,
85 max_phenotypicFeatures_pool => $max_phenotypicFeatures_pool,
86 max_procedures_pool => $max_procedures_pool,
87 max_treatments_pool => $max_treatments_pool,
88 ext_ontologies => $ext_ontologies,
89 }
90 );
91  
92 # Create object
93 6 my $randomize = Randomizer->new(
94 {
95 format => $format,
96 number => $number,
97 output => $output,
98 diseases => $diseases,
99 ethnicity => $ethnicity,
100 exposures => $exposures,
101 phenotypicFeatures => $phenotypicFeatures,
102 procedures => $procedures,
103 treatments => $treatments,
104 max_diseases_pool => $max_diseases_pool,
105 max_ethnicity_pool => $max_ethnicity_pool,
106 max_exposures_pool => $max_exposures_pool,
107 max_phenotypicFeatures_pool => $max_phenotypicFeatures_pool,
108 max_procedures_pool => $max_procedures_pool,
109 max_treatments_pool => $max_treatments_pool,
110 random_seed => $random_seed,
111 ext_ontologies => $ext_ontologies,
112 debug => $debug,
113 verbose => $verbose
114 }
115 );
116  
117 # Run method
118 6 $randomize->run;
119 }
120  
121 11 ▶sub validate_args {
Subroutine validate_args: called CC=10 SCAR=23.3
122 11 my $arg = shift;
123  
124 usage_error("--format must be one of: bff, pxf")
125 11 ▶ unless $arg->{format} =~ /\A(?:bff|pxf)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->{"format"} =~ /\A(?:bff|pxf)\z/ 1 10 11
126  
127 10 validate_positive_int( '--number', $arg->{number} );
128  
129 9 for my $name (qw(diseases exposures phenotypicFeatures procedures treatments)) {
130 41 validate_non_negative_int( "--$name", $arg->{$name} );
131 }
132  
133 8 for my $name (
134 qw(max_diseases_pool max_ethnicity_pool max_exposures_pool max_phenotypicFeatures_pool max_procedures_pool max_treatments_pool)
135 )
136 {
137 43 ▶ next unless defined $arg->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg->{$name} 25 18 43
138 18 ( my $option = $name ) =~ s/_/-/g;
139 18 validate_positive_int( "--$option", $arg->{$name} );
140 }
141  
142 7 ▶ if ( defined $arg->{ext_ontologies} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg->{"ext_ontologies"}) 2 5 7
143 usage_error("--external-ontologies must be a readable file")
144 2 ▶ unless -f $arg->{ext_ontologies} && -r _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $arg->{"ext_ontologies"} and -r _ 1 1 2
Condition66%2/3
-f $arg->{"ext_ontologies"} and -r _
!ll&&!rl&&r
101
MC/DC50%1/2
-f $arg->{"ext_ontologies"} and -r _
-f $arg->{"ext_ontologies"} -r _
Truth table66%2/3
A and B
A= -f $arg->{"ext_ontologies"}B= -r _
ABresult
0X0
100
111
145 }
146  
147 6 my $out_dir = dirname( $arg->{output} );
148 6 ▶ usage_error("--output directory <$out_dir> is not writable")
Branch: 1 branch
BranchTrueFalseTotal
unless -d $out_dir and -w _ 0 6 6
Condition33%1/3
-d $out_dir and -w _
!ll&&!rl&&r
006
MC/DC0%0/2
-d $out_dir and -w _
-d $out_dir -w _
Truth table33%1/3
A and B
A= -d $out_dirB= -w _
ABresult
0X0
100
111
149 unless -d $out_dir && -w _;
150  
151 6 return 1;
152 }
153  
154 28 ▶sub validate_positive_int {
Subroutine validate_positive_int: called CC=4 SCAR=14.6
155 28 my ( $name, $value ) = @_;
156 28 ▶ usage_error("$name must be a positive integer")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value =~ /\A[0-9]+\z/ and $value > 0 2 26 28
Condition50%3/6
defined $value and $value =~ /\A[0-9]+\z/ and $value > 0
!ll&&!rl&&r
0226
defined $value and $value =~ /\A[0-9]+\z/
!ll&&!rl&&r
0028
MC/DC33%1/3
defined $value and $value =~ /\A[0-9]+\z/ and $value > 0
defined $value $value =~ /\A[0-9]+\z/ $value > 0
Truth table50%2/4
A and B and C
A= defined $valueB= $value =~ /\A[0-9]+\z/C= $value > 0
ABCresult
0XX0
10X0
1100
1111
157 unless defined $value && $value =~ /\A[0-9]+\z/ && $value > 0;
158 26 return 1;
159 }
160  
161 41 ▶sub validate_non_negative_int {
Subroutine validate_non_negative_int: called CC=3 SCAR=11.0
162 41 my ( $name, $value ) = @_;
163 41 ▶ usage_error("$name must be a non-negative integer")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value =~ /\A[0-9]+\z/ 1 40 41
Condition66%2/3
defined $value and $value =~ /\A[0-9]+\z/
!ll&&!rl&&r
0140
MC/DC50%1/2
defined $value and $value =~ /\A[0-9]+\z/
defined $value $value =~ /\A[0-9]+\z/
Truth table66%2/3
A and B
A= defined $valueB= $value =~ /\A[0-9]+\z/
ABresult
0X0
100
111
164 unless defined $value && $value =~ /\A[0-9]+\z/;
165 40 return 1;
166 }
167  
168 5 ▶sub usage_error {
Subroutine usage_error: called CC=1 SCAR=0.0
169 5 my $message = shift;
170 5 pod2usage( -message => "$message\n", -exitval => 1 );
171 }
172  
173 package Randomizer;
174  
175 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
177 11 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
178 11 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
179  
180 #use Data::Printer;
181 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
182 11 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 11 ▶use List::Util 1.50 qw(head shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
184 11 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
185 11 ▶use Data::Fake qw(Core Company Dates Names);
Subroutine BEGIN: called CC=1 SCAR=0.0
186 11 ▶use FindBin qw($Bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
187 11 ▶use lib $Bin;
Subroutine BEGIN: called CC=1 SCAR=0.0
188 use Ontologies
189 11 ▶ qw($hpo_array $omim_array $rxnorm_array $ncit_procedures_array $ncit_exposures_array $ethnicity_array);
Subroutine BEGIN: called CC=1 SCAR=0.0
190  
191 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
192 6 my ( $class, $self ) = @_;
193 6 bless $self, $class;
194 6 return $self;
195 }
196  
197 6 ▶sub run {
Subroutine run: called CC=4 SCAR=13.9
198 6 my $self = shift;
199 6 my $number = $self->{number};
200 6 my $format = $self->{format};
201 6 my $output = $self->{output};
202 6 my $random_seed = $self->{random_seed};
203 6 my %func = (
204 bff => \&bff_generator,
205 pxf => \&pxf_generator
206 );
207  
208 # Set seed if defined
209 6 ▶ srand($random_seed) if defined $random_seed; # user can set it to 0
Branch: 1 branch
BranchTrueFalseTotal
if defined $random_seed 5 1 6
210  
211 # Load external ontologies file if present
212 $self->{ontologies_data} =
213 $self->{ext_ontologies}
214 ? validate_json( $self->{ext_ontologies} )
215 6 ▶ : undef; # setter
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ext_ontologies'} ? : 1 5 6
216  
217 #########
218 # START #
219 #########
220  
221 6 my $json_data;
222 6 ▶ for ( my $i = 1 ; $i <= $number ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $number) 306 5 311
223 306 push @$json_data, $func{$format}->( $i, $self );
224 }
225  
226 #######
227 # END #
228 #######
229 #p $json_data;
230  
231 # Serialize the data and write
232 5 write_json( { filepath => $output, data => $json_data } );
233 }
234  
235 5 ▶sub write_json {
Subroutine write_json: called CC=1 SCAR=0.0
236 5 my $arg = shift;
237 5 my $file = $arg->{filepath};
238 5 my $json_data = $arg->{data};
239  
240 # Note that canonical DOES not match the order of nsort from Sort::Naturally
241 5 my $json = JSON::XS->new->utf8->canonical->pretty->encode($json_data);
242 5 path($file)->spew_utf8($json);
243 5 return 1;
244 }
245  
246 102 ▶sub pxf_generator {
Subroutine pxf_generator: called CC=1 SCAR=0.0
247 102 my ( $id, $self ) = @_;
248 102 my $result_hash = run_functions($self);
249 102 my $subject_id = "IndividualId_$id";
250 my $pxf = fake_hash(
251 {
252 id => "Phenopacket_" . $id,
253 subject => {
254 id => $subject_id,
255 age => {
256 iso8601duration =>
257 fake_template( "P%dY", fake_int_mod( 1, 99 ) )
258 },
259 sex => fake_pick_mod( [ 'MALE', 'FEMALE' ] )
260 },
261 diseases => $result_hash->{diseases},
262 phenotypicFeatures => $result_hash->{phenotypicFeatures},
263 interpretations =>
264 102 pxf_interpretations( $result_hash->{diseases}, $subject_id ),
265 medicalActions => merge_medical_actions($result_hash)
266 }
267 );
268 102 return $pxf->();
269 }
270  
271 102 ▶sub pxf_interpretations {
Subroutine pxf_interpretations: called CC=3 SCAR=11.0
272 102 my ( $diseases, $subject_id ) = @_;
273 102 ▶ $diseases ||= [];
Condition50%1/2
$diseases ||= []
l!l
1020
MC/DC0%0/1
$diseases ||= []
$diseases
Truth table50%1/2
A
A= $diseases
Aresult
11
00
274 102 my @status = qw(SOLVED UNSOLVED);
275 102 my @interpretations;
276  
277 102 for my $i ( 0 .. $#{$diseases} ) {
278 1002 my $disease = $diseases->[$i]{term};
279 1002 push @interpretations,
280 {
281 id => 'Interpretation_' . ( $i + 1 ),
282 progressStatus => $status[ $i % @status ],
283 diagnosis => {
284 disease => $disease,
285 genomicInterpretations => [
286 {
287 subjectOrBiosampleId => $subject_id,
288 interpretationStatus => 'CAUSATIVE',
289 geneDescriptor => {
290 valueId => 'HGNC:' . ( 1000 + $i + 1 ),
291 symbol => 'GENE' . ( $i + 1 )
292 }
293 }
294 ]
295 }
296 };
297 }
298  
299 102 return \@interpretations;
300 }
301  
302 102 ▶sub merge_medical_actions {
Subroutine merge_medical_actions: called CC=3 SCAR=11.1
303 102 my $hash = shift;
304  
305 # Initialize empty arrays for treatments and procedures
306 102 my @processed_treatments;
307 my @processed_procedures;
308  
309 # Process treatments if defined
310 102 ▶ if ( defined $hash->{treatments} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash->{"treatments"}) 102 0 102
311 @processed_treatments =
312 102 map { { treatment => $_ } } @{ $hash->{treatments} };
313 }
314  
315 # Process procedures if defined
316 102 ▶ if ( defined $hash->{procedures} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash->{"procedures"}) 102 0 102
317 @processed_procedures =
318 102 map { { procedure => $_ } } @{ $hash->{procedures} };
319 }
320  
321 # Merge the processed arrays and return a reference
322 # NB: If undef no elements will be added
323 102 return [ @processed_treatments, @processed_procedures ];
324 }
325  
326 204 ▶sub bff_generator {
Subroutine bff_generator: called CC=6 SCAR=23.5
327 204 my ( $id, $self ) = @_;
328 204 my $default_array = [];
329 204 my $result_hash = run_functions($self);
330 my $bff = fake_hash(
331 {
332 id => "Beacon_" . $id,
333 ethnicity => $result_hash->{ethnicity},
334 sex => fake_pick_mod(
335 [
336 { id => "NCIT:C20197", label => "Male" },
337 { id => "NCIT:C16576", label => "Female" }
338 ]
339 ),
340 diseases => $result_hash->{diseases} // $default_array,
341 phenotypicFeatures => $result_hash->{phenotypicFeatures}
342 // $default_array,
343 treatments => $result_hash->{treatments} // $default_array,
344 interventionsOrProcedures => $result_hash->{procedures}
345 // $default_array,
346 203 ▶ exposures => $result_hash->{exposures} // $default_array
Condition33%5/15
$result_hash->{"diseases"} // $default_array
l!l&&r!l&&!r
20300
$result_hash->{"phenotypicFeatures"} // $default_array
l!l&&r!l&&!r
20300
$result_hash->{"treatments"} // $default_array
l!l&&r!l&&!r
20300
$result_hash->{"procedures"} // $default_array
l!l&&r!l&&!r
20300
$result_hash->{"exposures"} // $default_array
l!l&&r!l&&!r
20300
MC/DC0%0/2
$result_hash->{"diseases"} // $default_array
$result_hash->{"diseases"} $default_array
MC/DC0%0/2
$result_hash->{"phenotypicFeatures"} // $default_array
$result_hash->{"phenotypicFeatures"} $default_array
MC/DC0%0/2
$result_hash->{"treatments"} // $default_array
$result_hash->{"treatments"} $default_array
MC/DC0%0/2
$result_hash->{"procedures"} // $default_array
$result_hash->{"procedures"} $default_array
MC/DC0%0/2
$result_hash->{"exposures"} // $default_array
$result_hash->{"exposures"} $default_array
Truth table33%1/3
A // B
A= $result_hash->{"diseases"}B= $default_array
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $result_hash->{"phenotypicFeatures"}B= $default_array
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $result_hash->{"treatments"}B= $default_array
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $result_hash->{"procedures"}B= $default_array
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $result_hash->{"exposures"}B= $default_array
ABresult
1X1
011
000
347 }
348 );
349 203 return $bff->();
350 }
351  
352 ######################
353 # START ARRAY TERMS #
354 ######################
355  
356 1526 ▶sub create_entries {
Subroutine create_entries: called CC=4 SCAR=13.9
357 1526 my ( $params, $ontologies_array, $n, $max ) = @_;
358 1526 ▶ return [] if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 208 1318 1526
359 1318 my $shuffled_slice = shuffle_slice( $max, $ontologies_array );
360 die "Requested $n terms, but only "
361 1 . scalar( @{$shuffled_slice} )
362 . " ontology terms are available\n"
363 1318 ▶ if $n > @{$shuffled_slice};
Branch: 1 branch
BranchTrueFalseTotal
if $n > @{$shuffled_slice;} 1 1317 1318
364 1317 my $array;
365 1317 ▶ for ( my $i = 0 ; $i < $n ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 9428 1317 10745
366 push @$array,
367 9428 $params->{entry_creator}->( $shuffled_slice->[$i], $params );
368 }
369 1317 return $array;
370 }
371  
372 7323 ▶sub common_entry_creator {
Subroutine common_entry_creator: called CC=1 SCAR=0.0
373 7323 my ( $element, $params ) = @_;
374 return {
375 $params->{type} => $element,
376 $params->{onset} => {
377 7323 age => {
378 iso8601duration =>
379 fake_template( "P%dY", fake_int_mod( 1, 99 ) )
380 }
381 }
382 };
383 }
384  
385 305 ▶sub phenotypicFeatures {
Subroutine phenotypicFeatures: called CC=3 SCAR=11.0
386 305 my ( $format, $ontologies_array, $n, $max ) = @_;
387 305 ▶ my $params = {
Branch: 2 branches
BranchTrueFalseTotal
$format eq 'bff' ? : 203 102 305
$format eq 'bff' ? : 203 102 305
388 type => $format eq 'bff' ? 'featureType' : 'type',
389 onset => $format eq 'bff' ? 'ageOfOnset' : 'onset',
390 entry_creator => \&common_entry_creator
391 };
392 305 return create_entries( $params, $ontologies_array, $n, $max );
393 }
394  
395 306 ▶sub diseases {
Subroutine diseases: called CC=3 SCAR=11.0
396 306 my ( $format, $ontologies_array, $n, $max ) = @_;
397 306 ▶ my $params = {
Branch: 2 branches
BranchTrueFalseTotal
$format eq 'bff' ? : 204 102 306
$format eq 'bff' ? : 204 102 306
398 type => $format eq 'bff' ? 'diseaseCode' : 'term',
399 onset => $format eq 'bff' ? 'ageOfOnset' : 'onset',
400 entry_creator => \&common_entry_creator
401 };
402 306 return create_entries( $params, $ontologies_array, $n, $max );
403 }
404  
405 305 ▶sub treatments {
Subroutine treatments: called CC=1 SCAR=0.0
406 305 my ( $format, $ontologies_array, $n, $max ) = @_;
407 my $params = {
408 2105 ▶ entry_creator => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
409 2105 my ( $element, $p ) = @_;
410 2105 ▶ return $format eq 'bff'
Branch: 1 branch
BranchTrueFalseTotal
$format eq 'bff' ? : 1103 1002 2105
411 ? { treatmentCode => $element }
412 : { agent => $element };
413 }
414 305 };
415 305 return create_entries( $params, $ontologies_array, $n, $max );
416 }
417  
418 305 ▶sub procedures {
Subroutine procedures: called CC=3 SCAR=11.0
419 305 my ( $format, $ontologies_array, $n, $max ) = @_;
420 305 ▶ my $params = {
Branch: 2 branches
BranchTrueFalseTotal
$format eq 'bff' ? : 203 102 305
$format eq 'bff' ? : 203 102 305
421 type => $format eq 'bff' ? 'procedureCode' : 'code',
422 onset => $format eq 'bff' ? 'ageAtProcedure' : 'onset',
423 entry_creator => \&common_entry_creator
424 };
425 305 return create_entries( $params, $ontologies_array, $n, $max );
426 }
427  
428 305 ▶sub exposures {
Subroutine exposures: called CC=4 SCAR=13.9
429 305 my ( $format, $ontologies_array, $n, $max ) = @_;
430 305 my $default_duration = 'P999Y';
431 305 my $default_ontology_term =
432 { id => 'NCIT:C126101', label => 'Not Available' };
433 305 ▶ my $params = {
Branch: 2 branches
BranchTrueFalseTotal
$format eq 'bff' ? : 203 102 305
$format eq 'bff' ? : 203 102 305
434 type => $format eq 'bff' ? 'exposureCode' : 'term',
435 onset => $format eq 'bff' ? 'ageAtExposure' : 'onset',
436 entry_creator => \&common_entry_creator
437 };
438 305 my $entries = create_entries( $params, $ontologies_array, $n, $max );
439  
440 # Add 'duration' 'and 'unit' to pass bff-validator
441 305 foreach my $entry (@$entries) {
442 1100 $entry->{duration} = $default_duration;
443 1100 $entry->{unit} = $default_ontology_term;
444 }
445 305 return $entries;
446 }
447  
448 305 ▶sub ethnicity {
Subroutine ethnicity: called CC=1 SCAR=0.0
449 305 my ( undef, $ontologies_array, undef, $max ) = @_;
450 305 my $shuffled_slice = shuffle_slice( $max, $ontologies_array );
451 305 return $shuffled_slice->[0];
452 }
453  
454 ####################
455 # END ARRAY TERMS #
456 ####################
457  
458 306 ▶sub load_ontology_hash {
Subroutine load_ontology_hash: called CC=1 SCAR=0.0
459 306 my $self = shift;
460 306 my %ont = (
461 diseases => $omim_array,
462 ethnicity => $ethnicity_array,
463 exposures => $ncit_exposures_array,
464 phenotypicFeatures => $hpo_array,
465 procedures => $ncit_procedures_array,
466 treatments => $rxnorm_array
467 );
468 306 return \%ont;
469 }
470  
471 306 ▶sub run_functions {
Subroutine run_functions: called CC=3 SCAR=11.0
472 306 my $self = shift;
473 306 my $ontologies = load_ontology_hash($self);
474  
475 306 my %func = (
476 diseases => \&diseases,
477 ethnicity => \&ethnicity,
478 exposures => \&exposures,
479 phenotypicFeatures => \&phenotypicFeatures,
480 procedures => \&procedures,
481 treatments => \&treatments
482 );
483  
484 306 my %hash;
485  
486 # *** IMPORTANT ***
487 # sort keys (below) is mandatory for reproducibility
488 306 for my $key ( sort keys %func ) {
489 my $ontologies_array =
490 exists $self->{ontologies_data}{$key}
491 ? $self->{ontologies_data}{$key}
492 1831 ▶ : $ontologies->{$key};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'ontologies_data'}{$key} ? : 600 1231 1831
493 $hash{$key} = $func{$key}->(
494 $self->{format}, $ontologies_array, $self->{$key},
495 1831 $self->{ 'max_' . $key . '_pool' }
496 );
497 }
498  
499 305 return \%hash;
500 }
501  
502 1623 ▶sub shuffle_slice {
Subroutine shuffle_slice: called CC=3 SCAR=11.0
503 1623 my ( $max, $array ) = @_;
504  
505 # head -> 1.50 List::Util (5.26 has 1.4602)
506 #my @items = sample $count, @values; # 1.54 List::Util
507 # *** IMPORTANT ***
508 # If $max was defined by the user then use it, otherwise @$array;
509 1623 ▶ my @slice = defined $max ? head $max, @$array : @$array; # slice of refs
Branch: 1 branch
BranchTrueFalseTotal
defined $max ? : 1212 411 1623
510 1623 my @shuffled_slice = shuffle @slice;
511 1623 ▶ return wantarray ? @shuffled_slice : \@shuffled_slice;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1623 1623
512 }
513  
514 7425 ▶sub fake_int_mod {
Subroutine fake_int_mod: called CC=1 SCAR=0.0
515  
516 # This subroutine was built because fake_int did not respond to srand
517 7425 my ( $low, $high ) = @_;
518 7425 my $range = $high - $low;
519 7425 return int( rand($range) ) + 1;
520 }
521  
522 305 ▶sub fake_pick_mod {
Subroutine fake_pick_mod: called CC=1 SCAR=0.0
523  
524 # This subroutine was built because fake_pick did not respond to srand
525 # NB: The original from Data::Fake worked with array (not with arrayref)
526 305 my $array = shift;
527 305 return $array->[ int( rand(@$array) ) ];
528 }
529  
530 1 ▶sub validate_json {
Subroutine validate_json: called CC=3 SCAR=11.9
531 1 my $file = shift;
532 1 my $data = read_yaml($file);
533 1 my $schema = {
534 '$schema' => 'http://json-schema.org/draft-07/schema#',
535 type => "object",
536 properties => {
537 diseases => { '$ref' => '#/$defs/array' },
538 phenotypicFeatures => { '$ref' => '#/$defs/array' },
539 treatments => { '$ref' => '#/$defs/array' },
540 procedures => { '$ref' => '#/$defs/array' },
541 exposures => { '$ref' => '#/$defs/array' },
542 ethnicity => { '$ref' => '#/$defs/array' }
543 },
544 '$defs' => {
545 array => {
546 type => "array",
547 items => { '$ref' => '#/$defs/item' }
548 },
549 item => {
550 type => "object",
551 required => [ "id", "label" ],
552 properties => {
553 id => { type => "string", pattern => qq/^\\w[^:]+:.+\$/ },
554 label => { type => "string" }
555 }
556 }
557 }
558 };
559  
560 # Load at runtime
561 1 require JSON::Validator;
562  
563 # Create object and load schema
564 1 my $jv = JSON::Validator->new;
565  
566 # Load schema in object
567 1 $jv->schema($schema);
568  
569 # Validate data
570 1 my @errors = $jv->validate($data);
571  
572 # Show error if any
573 1 ▶ say_errors( \@errors ) and die if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 1 1
Condition20%1/5
@errors && (say_errors(\@errors) && die())
!ll&&!rl&&r
100
say_errors(\@errors) and die
!ll
00
MC/DC0%0/2
@errors && (say_errors(\@errors) && die())
@errors say_errors(\@errors)
Truth table33%1/3
A && B
A= @errorsB= say_errors(\@errors)
ABresult
0X0
100
111
574  
575 # return data if ok
576 1 return $data;
577  
578 }
579  
580 0 ▶sub say_errors {
Subroutine say_errors: not called CC=2 SCAR=17.9
581 0 my $errors = shift;
582 0 ▶ if ( @{$errors} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$errors;}) 0 0 0
583 0 say join "\n", @{$errors};
584 }
585 0 return 1;
586 }
587  
588 1 ▶sub read_yaml {
Subroutine read_yaml: called CC=1 SCAR=0.0
589  
590 # Load at runtime
591 1 require YAML::XS;
592 1 YAML::XS->import('LoadFile');
593 1 return LoadFile(shift); # Decode to Perl data structure
594 }
595  
596 1;
597  
598 =head1 NAME
599  
600 bff-pxf-simulator: A script that creates a JSON array of simulated BFF/PXF
601  
602 =head1 SYNOPSIS
603  
604 bff-pxf-simulator [-options]
605  
606 Options:
607 -f, --format <format> Format [bff|pxf]
608 -n, --number <number> Set the number of individuals to generate [100]
609 -o, --output <file> Output file [individuals.json]
610 --external-ontologies <file> Path to a YAML file containing ontology terms
611 --random-seed <seed> Initializes pseudorandom number sequences (seed must be an integer)
612  
613 --diseases <number> Set the number of diseases per individual [1]
614 --exposures <number> Set the number of exposures per individual [1]
615 --phenotypicFeatures <number> Set the number of phenotypic features per individual [1]
616 --procedures <number> Set the number of procedures per individual [1]
617 --treatments <number> Set the number of treatments per individual [1]
618 --max-[term]-pool <size> Limit the selection to the first N elements of the term array
619 --max-ethnicity-pool <size> Restrict the ethnicity pool size; each individual will have only one ethnicity
620  
621 Generic Options:
622 -debug <level> Print debugging (from 1 to 5, being 5 max)
623 -h, --help Brief help message
624 -man Full documentation
625 -v, --verbose Verbosity on
626 -V, --version Print version
627  
628 =head1 DESCRIPTION
629  
630 This script generates a JSON array of simulated BFF/PXF data. The files can be created based on pre-loaded ontologies or by utilizing an external YAML file.
631  
632 =head1 SUMMARY
633  
634 A script that creates a JSON array of simulated BFF/PXF.
635  
636 Implemented array terms:
637  
638 B<BFF:> C<diseases, exposures, interventionsOrProcedures, phenotypicFeatures, treatments>.
639  
640 procedures = interventionsOrProcedures
641  
642 B<PXF:> C<diseases, interpretations, interventionsOrProcedures, medicalActions.procedure, medicalActions.treatment, phenotypicFeatures>.
643  
644 procedures = medicalActions.procedure
645  
646 treatments = medicalActions.treatment
647  
648 =head1 INSTALLATION
649  
650 (only needed if you did not install C<Pheno-Ranker>)
651  
652 $ cpanm --installdeps .
653  
654 =head3 System requirements
655  
656 * Ideally a Debian-based distribution (Ubuntu or Mint), but any other (e.g., CentOs, OpenSuse) should do as well.
657 * Perl 5 (>= 5.10 core; installed by default in most Linux distributions). Check the version with "perl -v"
658 * 1GB of RAM.
659 * 1 core (it only uses one core per job).
660 * At least 1GB HDD.
661  
662 =head1 HOW TO RUN BFF-PXF-SIMULATOR
663  
664 When run without any arguments, the software will use default settings. To modify any parameters, please refer to the synopsis for guidance.
665  
666 If you prefer not to include a specific term in the analysis, set its value to zero. For example:
667  
668 C<--treatments 0>
669  
670 B<Examples:>
671  
672 $ ./bff-pxf-simulator -f pxf # BFF with 100 samples
673  
674 $ ./bff-pxf-simulator -f pxf -n 1000 -o pxf.json # PXF with 1K samples and saved to pxf.json
675  
676 $ ./bff-pxf-simulator -phenotypicFeatures 10 # BFF with 100 samples and 10 pF each
677  
678 $ ./bff-pxf-simulator -diseases 0 -exposures 0 -procedures 0 -phenotypicFeatures 0 -treatments 0 # Only sex and ethnicity
679  
680 =head2 COMMON ERRORS AND SOLUTIONS
681  
682 * Error message: Foo
683 Solution: Bar
684  
685 * Error message: Foo
686 Solution: Bar
687  
688 =head1 AUTHOR
689  
690 Written by Manuel Rueda, PhD. Info about CNAG can be found at L<https://www.cnag.eu>.
691  
692 =head1 COPYRIGHT AND LICENSE
693  
694 This Perl file is copyrighted. See the LICENSE file included in this distribution.
695  
696 =cut
« lib/Pheno/Ranker.pm ↑ Summary lib/Pheno/Ranker/Config.pm »