lib/Convert/Pheno/Source/CBioPortal.pm

filter: Statementstmt 97.2% 358 / 368 Branchbran 64.7% 114 / 176 Conditioncond 47.3% 35 / 74 MC/DCmcdc 13.0% 6 / 46 Subroutinesub 100% 34 / 34 Podpod 0.0% 0 / 3 totaltotal 78.0% 547 / 701 CC 133 SCAR 30.4
CC
133
Cov
82%
_normalize_package
49.2
_parse_clinical_table
23.7
__ANON__
18.2
CRAP 20.9
« bin/convert-pheno ↑ Summary lib/Convert/Pheno/OMOP/Vocabulary.pm »
1 package Convert::Pheno::Source::CBioPortal;
2  
3 136 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 136 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 136 ▶use Encode qw(decode FB_CROAK);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 136 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 136 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 136 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 136 ▶use IO::Uncompress::Unzip qw($UnzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 136 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 136 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 136 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 136 ▶use Convert::Pheno::BFF::DerivedEntities qw(mapping_entity_overrides);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 136 ▶use Convert::Pheno::IO::CSVHandler qw(read_mapping_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 136 ▶use Convert::Pheno::Mapping::Compiler qw(compile_mapping);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 136 ▶use Convert::Pheno::Mapping::Shared qw(get_info get_metaData);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 136 ▶use Convert::Pheno::Source::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 14 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
22 14 my ( $class, $converter ) = @_;
23 14 return bless { converter => $converter }, $class;
24 }
25  
26 14 ▶sub load {
Subroutine load: called CC=4 SCAR=13.9
Pod undocumented
27 14 my ($self) = @_;
28 14 my $converter = $self->{converter};
29  
30 my $package = exists $converter->{data}
31 ? _memory_package( $converter->{data} )
32 14 ▶ : _path_package( $converter->{in_file} );
Branch: 1 branch
BranchTrueFalseTotal
exists $converter->{'data'} ? : 2 12 14
33 14 my $normalized = _normalize_package($package);
34  
35 13 my %artifacts = ( study => $normalized->{study} );
36 13 ▶ if ( defined $converter->{mapping_file}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $converter->{"mapping_file"} and length $converter->{"mapping_file"}) 1 12 13
Condition66%2/3
defined $converter->{"mapping_file"} and length $converter->{"mapping_file"}
!ll&&!rl&&r
1201
MC/DC50%1/2
defined $converter->{"mapping_file"} and length $converter->{"mapping_file"}
defined $converter->{"mapping_file"} length $converter->{"mapping_file"}
Truth table66%2/3
A and B
A= defined $converter->{"mapping_file"}B= length $converter->{"mapping_file"}
ABresult
0X0
100
111
37 && length $converter->{mapping_file} )
38 {
39 my $mapping = read_mapping_file(
40 {
41 mapping_file => $converter->{mapping_file},
42 self_validate_schema => $converter->{self_validate_schema},
43 schema_file => $converter->{schema_file},
44 }
45 1 );
46 my $compiled = compile_mapping(
47 $mapping,
48 source_profile => 'cbioportal',
49 headers => $normalized->{headers},
50 1 );
51 1 $artifacts{mapping} = $mapping;
52 1 $artifacts{entity_mapping} = $compiled;
53 }
54  
55 return Convert::Pheno::Source::Result->new(
56 {
57 data => $normalized->{records},
58 13 owned => 1,
59 artifacts => \%artifacts,
60 }
61 );
62 }
63  
64 10 ▶sub prepare {
Subroutine prepare: called CC=8 SCAR=22.2
Pod undocumented
65 10 my ($self) = @_;
66 10 my $converter = $self->{converter};
67 return 1 if $converter->{cbioportal_input_prepared}
68 10 ▶ && exists $converter->{data};
Branch: 1 branch
BranchTrueFalseTotal
if $converter->{"cbioportal_input_prepared"} and exists $converter->{"data"} 0 10 10
Condition33%1/3
$converter->{"cbioportal_input_prepared"} and exists $converter->{"data"}
!ll&&!rl&&r
1000
MC/DC0%0/2
$converter->{"cbioportal_input_prepared"} and exists $converter->{"data"}
$converter->{"cbioportal_input_prepared"} exists $converter->{"data"}
Truth table33%1/3
A and B
A= $converter->{"cbioportal_input_prepared"}B= exists $converter->{"data"}
ABresult
0X0
100
111
69  
70 $converter->{_cbioportal_source_data} = $converter->{data}
71 if exists $converter->{data}
72 10 ▶ && !exists $converter->{_cbioportal_source_data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $converter->{"data"} and not exists $converter->{"_cbioportal_source_data"} 0 10 10
Condition33%1/3
exists $converter->{"data"} and not exists $converter->{"_cbioportal_source_data"}
!ll&&!rl&&r
1000
MC/DC0%0/2
exists $converter->{"data"} and not exists $converter->{"_cbioportal_source_data"}
exists $converter->{"data"} not exists $converter->{"_cbioportal_source_data"}
Truth table33%1/3
A and not(B)
A= exists $converter->{"data"}B= not exists $converter->{"_cbioportal_source_data"}
ABresult
0X0
100
111
73 $converter->{data} = $converter->{_cbioportal_source_data}
74 if !exists $converter->{data}
75 10 ▶ && exists $converter->{_cbioportal_source_data};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $converter->{"data"} and exists $converter->{"_cbioportal_source_data"} 0 10 10
Condition33%1/3
not exists $converter->{"data"} and exists $converter->{"_cbioportal_source_data"}
!ll&&!rl&&r
0100
MC/DC0%0/2
not exists $converter->{"data"} and exists $converter->{"_cbioportal_source_data"}
not exists $converter->{"data"} exists $converter->{"_cbioportal_source_data"}
Truth table33%1/3
not(A) and B
A= not exists $converter->{"data"}B= exists $converter->{"_cbioportal_source_data"}
ABresult
0X0
100
111
76  
77 10 my $source = $self->load;
78 10 $source->apply_to($converter);
79 10 $converter->{cbioportal_study} = $source->artifact('study');
80  
81 10 my $compiled = $source->artifact('entity_mapping');
82 10 ▶ if ( defined $compiled ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $compiled) { } 1 9 10
83 1 $converter->{data_mapping_file} = $compiled;
84 }
85 else {
86 9 delete $converter->{data_mapping_file};
87 }
88  
89 $converter->{mapping_file_derived_entity_overrides} =
90 10 mapping_entity_overrides( $source->artifact('mapping') );
91 10 $converter->{metaData} = get_metaData($converter);
92 10 $converter->{convertPheno} = get_info($converter);
93 10 $converter->{cbioportal_input_prepared} = 1;
94 10 return 1;
95 }
96  
97 12 ▶sub _path_package {
Subroutine _path_package: called CC=6 SCAR=21.4
98 12 my ($input) = @_;
99 12 ▶ die "cBioPortal input requires a study directory or ZIP file\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input and length $input 0 12 12
Condition33%1/3
defined $input and length $input
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $input and length $input
defined $input length $input
Truth table33%1/3
A and B
A= defined $inputB= length $input
ABresult
0X0
100
111
100 unless defined $input && length $input;
101  
102 12 ▶ return _directory_package($input) if -d $input;
Branch: 1 branch
BranchTrueFalseTotal
if -d $input 10 2 12
103 2 ▶ return _zip_package($input) if -f $input && $input =~ /\.zip\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if -f $input and $input =~ /\.zip\z/i 2 0 2
Condition33%1/3
-f $input and $input =~ /\.zip\z/i
!ll&&!rl&&r
002
MC/DC0%0/2
-f $input and $input =~ /\.zip\z/i
-f $input $input =~ /\.zip\z/i
Truth table33%1/3
A and B
A= -f $inputB= $input =~ /\.zip\z/i
ABresult
0X0
100
111
104  
105 0 die "cBioPortal input <$input> must be a study directory or .zip file\n";
106 }
107  
108 10 ▶sub _directory_package {
Subroutine _directory_package: called CC=1 SCAR=0.0
109 10 my ($directory) = @_;
110 10 my $root = File::Spec->rel2abs($directory);
111 10 my %entries;
112  
113 find(
114 {
115 no_chdir => 1,
116 100 ▶ wanted => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
117 100 ▶ return unless -f $File::Find::name;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $File::Find::name 20 80 100
118 80 my $name = File::Spec->abs2rel( $File::Find::name, $root );
119 80 $name =~ tr{\\}{/};
120 80 $name = _normalize_entry_name($name);
121 80 $entries{$name} = { size => -s $File::Find::name };
122 },
123 },
124 10 $root,
125 );
126  
127 10 my %cache;
128 return {
129 label => $directory,
130 entries => \%entries,
131 20 ▶ read_many => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
132 20 my ($names) = @_;
133 20 for my $name ( @{$names} ) {
134 60 ▶ next if exists $cache{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cache{$name} 0 60 60
135 die "cBioPortal package <$directory> does not contain <$name>\n"
136 60 ▶ unless exists $entries{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $entries{$name} 0 60 60
137 60 my $file = File::Spec->catfile( $root, split m{/}, $name );
138 60 $cache{$name} = path($file)->slurp_utf8;
139 }
140 20 return { map { $_ => $cache{$_} } @{$names} };
141 },
142 10 };
143 }
144  
145 2 ▶sub _zip_package {
Subroutine _zip_package: called CC=7 SCAR=20.3
146 2 my ($zip_file) = @_;
147 2 my %entries;
148 2 ▶ my $zip = IO::Uncompress::Unzip->new($zip_file)
Branch: 1 branch
BranchTrueFalseTotal
my $zip = "IO::Uncompress::Unzip"->new($zip_file) or die "Cannot open cBioPortal ZIP <$zip_file>: $Convert::Pheno::Source::CBioPortal::UnzipError\n" 2 0 2
149 or die "Cannot open cBioPortal ZIP <$zip_file>: $UnzipError\n";
150  
151 2 while (1) {
152 14 my $header = $zip->getHeaderInfo;
153 14 my $raw_name = $header->{Name};
154 14 ▶ if ( defined $raw_name && $raw_name !~ m{/\z} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw_name and not $raw_name =~ m[/\z]) 14 0 14
Condition33%1/3
defined $raw_name and not $raw_name =~ m[/\z]
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $raw_name and not $raw_name =~ m[/\z]
defined $raw_name not $raw_name =~ m[/\z]
Truth table33%1/3
A and not(B)
A= defined $raw_nameB= not $raw_name =~ m[/\z]
ABresult
0X0
100
111
155 14 my $name = _normalize_entry_name($raw_name);
156 die "cBioPortal ZIP <$zip_file> contains duplicate entry <$name>\n"
157 14 ▶ if exists $entries{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $entries{$name} 0 14 14
158 $entries{$name} = {
159 14 ▶ size => $header->{UncompressedLength} // 0,
Condition50%1/2
$header->{"UncompressedLength"} // 0
l!l
140
MC/DC0%0/1
$header->{"UncompressedLength"} // 0
$header->{"UncompressedLength"}
Truth table50%1/2
A
A= $header->{"UncompressedLength"}
Aresult
11
00
160 };
161 }
162 14 ▶ last unless $zip->nextStream;
Branch: 1 branch
BranchTrueFalseTotal
unless $zip->nextStream 2 12 14
163 }
164 2 close $zip;
165  
166 2 my %cache;
167 return {
168 label => $zip_file,
169 entries => \%entries,
170 4 ▶ read_many => sub {
Subroutine __ANON__: called CC=15 SCAR=29.0
171 4 my ($names) = @_;
172 4 my %wanted = map { $_ => 1 } grep { !exists $cache{$_} } @{$names};
173 0 ▶ return { map { $_ => $cache{$_} } @{$names} } unless %wanted;
Branch: 1 branch
BranchTrueFalseTotal
unless %wanted 0 4 4
174  
175 4 for my $name ( keys %wanted ) {
176 die "cBioPortal ZIP <$zip_file> does not contain <$name>\n"
177 12 ▶ unless exists $entries{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $entries{$name} 0 12 12
178 }
179  
180 4 ▶ my $reader = IO::Uncompress::Unzip->new($zip_file)
Branch: 1 branch
BranchTrueFalseTotal
my $reader = "IO::Uncompress::Unzip"->new($zip_file) or die "Cannot reopen cBioPortal ZIP <$zip_file>: $Convert::Pheno::Source::CBioPortal::UnzipError\n" 4 0 4
181 or die "Cannot reopen cBioPortal ZIP <$zip_file>: $UnzipError\n";
182 4 while (1) {
183 22 my $header = $reader->getHeaderInfo;
184 22 my $raw_name = $header->{Name};
185 22 ▶ if ( defined $raw_name && $raw_name !~ m{/\z} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw_name and not $raw_name =~ m[/\z]) 22 0 22
Condition33%1/3
defined $raw_name and not $raw_name =~ m[/\z]
!ll&&!rl&&r
0022
MC/DC0%0/2
defined $raw_name and not $raw_name =~ m[/\z]
defined $raw_name not $raw_name =~ m[/\z]
Truth table33%1/3
A and not(B)
A= defined $raw_nameB= not $raw_name =~ m[/\z]
ABresult
0X0
100
111
186 22 my $name = _normalize_entry_name($raw_name);
187 22 ▶ if ( $wanted{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted{$name}) 12 10 22
188 12 my $bytes = q{};
189 12 my $buffer;
190 12 while (1) {
191 24 my $read = $reader->read($buffer);
192 24 ▶ die "Cannot read cBioPortal ZIP entry <$name>: $UnzipError\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $read or $read < 0 0 24 24
Condition33%1/3
not defined $read or $read < 0
l!l&&r!l&&!r
0024
MC/DC0%0/2
not defined $read or $read < 0
not defined $read $read < 0
Truth table33%1/3
not(A) or B
A= not defined $readB= $read < 0
ABresult
1X1
011
000
193 if !defined $read || $read < 0;
194 24 ▶ last if $read == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $read == 0 12 12 24
195 12 $bytes .= $buffer;
196 }
197 12 my $text = eval { decode( 'UTF-8', $bytes, FB_CROAK ) };
198 12 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 12 12
199 0 chomp $error;
200 0 die "cBioPortal ZIP entry <$name> is not valid UTF-8: $error\n";
201 }
202 12 $cache{$name} = $text;
203 12 delete $wanted{$name};
204 }
205 }
206 22 ▶ last unless %wanted && $reader->nextStream;
Branch: 1 branch
BranchTrueFalseTotal
unless %wanted and $reader->nextStream 4 18 22
Condition66%2/3
%wanted and $reader->nextStream
!ll&&!rl&&r
4018
MC/DC50%1/2
%wanted and $reader->nextStream
%wanted $reader->nextStream
Truth table66%2/3
A and B
A= %wantedB= $reader->nextStream
ABresult
0X0
100
111
207 }
208 4 close $reader;
209  
210 4 ▶ die "Could not read cBioPortal ZIP entries <"
Branch: 1 branch
BranchTrueFalseTotal
if %wanted 0 4 4
211 . join( '>, <', sort keys %wanted ) . ">\n"
212 if %wanted;
213 4 return { map { $_ => $cache{$_} } @{$names} };
214 },
215 2 };
216 }
217  
218 2 ▶sub _memory_package {
Subroutine _memory_package: called CC=7 SCAR=20.2
219 2 my ($data) = @_;
220 die "In-memory cBioPortal input must contain a <files> object\n"
221 2 ▶ unless ref($data) eq 'HASH' && ref( $data->{files} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" and ref $data->{"files"} eq "HASH" 0 2 2
Condition33%1/3
ref $data eq "HASH" and ref $data->{"files"} eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $data eq "HASH" and ref $data->{"files"} eq "HASH"
ref $data eq "HASH" ref $data->{"files"} eq "HASH"
Truth table33%1/3
A and B
A= ref $data eq "HASH"B= ref $data->{"files"} eq "HASH"
ABresult
0X0
100
111
222  
223 2 my ( %files, %entries );
224 2 for my $raw_name ( keys %{ $data->{files} } ) {
225 10 my $name = _normalize_entry_name($raw_name);
226 10 my $content = $data->{files}{$raw_name};
227 10 ▶ die "In-memory cBioPortal file <$name> must contain text\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $content 0 10 10
228 if ref($content);
229 die "In-memory cBioPortal input contains duplicate file <$name>\n"
230 10 ▶ if exists $files{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $files{$name} 0 10 10
231 10 ▶ $files{$name} = defined $content ? "$content" : q{};
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 10 0 10
232 10 $entries{$name} = { size => length $files{$name} };
233 }
234  
235 return {
236 label => 'in-memory cBioPortal study',
237 entries => \%entries,
238 4 ▶ read_many => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
239 4 my ($names) = @_;
240 4 for my $name ( @{$names} ) {
241 die "In-memory cBioPortal package does not contain <$name>\n"
242 10 ▶ unless exists $files{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $files{$name} 0 10 10
243 }
244 4 return { map { $_ => $files{$_} } @{$names} };
245 },
246 2 };
247 }
248  
249 14 ▶sub _normalize_package {
Subroutine _normalize_package: called CC=45 SCAR=38.9
250 14 my ($package) = @_;
251 14 my $entries = $package->{entries};
252  
253 104 my @study_meta = sort grep { m{(?:\A|/)meta_study\.txt\z}i }
254 14 keys %{$entries};
255 14 ▶ die "cBioPortal package <$package->{label}> must contain one <meta_study.txt>\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @study_meta == 1 0 14 14
256 unless @study_meta == 1;
257  
258 14 my $study_meta_name = $study_meta[0];
259 14 my $root = dirname($study_meta_name);
260 14 ▶ $root = q{} if $root eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $root eq "." 11 3 14
261 14 ▶ my $prefix = length $root ? "$root/" : q{};
Branch: 1 branch
BranchTrueFalseTotal
length $root ? : 3 11 14
262  
263 my @meta_candidates = sort grep {
264 104 my $relative = substr( $_, length $prefix );
265 104 ▶ index( $_, $prefix ) == 0
Condition83%5/6
index($_, $prefix) == 0 && index($relative, '/') == -1 && $relative =~ m[\Ameta_[^/]+\.txt\z]i
!ll&&!rl&&r
144941
index($_, $prefix) == 0 and index($relative, "/") == -1
!ll&&!rl&&r
01490
MC/DC66%2/3
index($_, $prefix) == 0 && index($relative, '/') == -1 && $relative =~ m[\Ameta_[^/]+\.txt\z]i
index($_, $prefix) == 0 index($relative, "/") == -1 $relative =~ m[\Ameta_[^/]+\.txt\z]i
Truth table75%3/4
A and B && C
A= index($_, $prefix) == 0B= index($relative, "/") == -1C= $relative =~ m[\Ameta_[^/]+\.txt\z]i
ABCresult
0XX0
10X0
1100
1111
266 && index( $relative, '/' ) < 0
267 && $relative =~ /\Ameta_[^\/]+\.txt\z/i
268 14 } keys %{$entries};
269 14 my $candidate_text = $package->{read_many}->(\@meta_candidates);
270  
271 my $study = _parse_meta_file(
272 14 $candidate_text->{$study_meta_name},
273 $study_meta_name,
274 1,
275 );
276 14 _require_meta_fields(
277 $study,
278 $study_meta_name,
279 qw(cancer_study_identifier name),
280 );
281  
282 14 my ( @patient_meta, @sample_meta );
283 14 for my $name (@meta_candidates) {
284 41 ▶ next if $name eq $study_meta_name;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $study_meta_name 14 27 41
285 27 my $meta = _parse_meta_file( $candidate_text->{$name}, $name, 0 );
286 27 ▶ next unless $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta 0 27 27
287 27 ▶ next unless uc( $meta->{genetic_alteration_type} // q{} ) eq 'CLINICAL';
Branch: 1 branch
BranchTrueFalseTotal
unless uc($meta->{"genetic_alteration_type"} // "") eq "CLINICAL" 0 27 27
Condition50%1/2
$meta->{"genetic_alteration_type"} // ""
l!l
270
MC/DC0%0/1
$meta->{"genetic_alteration_type"} // ""
$meta->{"genetic_alteration_type"}
Truth table50%1/2
A
A= $meta->{"genetic_alteration_type"}
Aresult
11
00
288  
289 27 ▶ my $datatype = uc( $meta->{datatype} // q{} );
Condition50%1/2
$meta->{"datatype"} // ""
l!l
270
MC/DC0%0/1
$meta->{"datatype"} // ""
$meta->{"datatype"}
Truth table50%1/2
A
A= $meta->{"datatype"}
Aresult
11
00
290 27 ▶ push @patient_meta, [ $name, $meta ] if $datatype eq 'PATIENT_ATTRIBUTES';
Branch: 1 branch
BranchTrueFalseTotal
if $datatype eq "PATIENT_ATTRIBUTES" 13 14 27
291 27 ▶ push @sample_meta, [ $name, $meta ] if $datatype eq 'SAMPLE_ATTRIBUTES';
Branch: 1 branch
BranchTrueFalseTotal
if $datatype eq "SAMPLE_ATTRIBUTES" 14 13 27
292 }
293  
294 14 ▶ die "cBioPortal package <$package->{label}> contains multiple PATIENT_ATTRIBUTES meta files\n"
Branch: 1 branch
BranchTrueFalseTotal
if @patient_meta > 1 0 14 14
295 if @patient_meta > 1;
296 14 ▶ die "cBioPortal package <$package->{label}> must contain one SAMPLE_ATTRIBUTES meta file\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @sample_meta == 1 0 14 14
297 unless @sample_meta == 1;
298  
299 14 my $study_id = _trim( $study->{cancer_study_identifier} );
300 14 for my $entry ( @patient_meta, @sample_meta ) {
301 27 my ( $name, $meta ) = @{$entry};
302 27 _require_meta_fields( $meta, $name, qw(cancer_study_identifier data_filename) );
303 27 my $meta_study_id = _trim( $meta->{cancer_study_identifier} );
304 27 ▶ die "cBioPortal meta file <$name> references study <$meta_study_id>, expected <$study_id>\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $meta_study_id eq $study_id 0 27 27
305 unless $meta_study_id eq $study_id;
306 }
307  
308 my $sample_data_name = _resolve_package_name(
309 $prefix,
310 $sample_meta[0][1]{data_filename},
311 14 $entries,
312 $sample_meta[0][0],
313 );
314 my $patient_data_name = @patient_meta
315 ? _resolve_package_name(
316 $prefix,
317 $patient_meta[0][1]{data_filename},
318 14 ▶ $entries,
Branch: 1 branch
BranchTrueFalseTotal
@patient_meta ? : 13 1 14
319 $patient_meta[0][0],
320 )
321 : undef;
322  
323 14 my $case_prefix = $prefix . 'case_lists/';
324 my @case_names = sort grep {
325 104 ▶ index( $_, $case_prefix ) == 0
Condition66%4/6
index($_, $case_prefix) == 0 && length substr($_, length $case_prefix) && index(substr($_, length $case_prefix), '/') == -1
!ll&&!rl&&r
90014
index($_, $case_prefix) == 0 and length substr($_, length $case_prefix)
!ll&&!rl&&r
90014
MC/DC33%1/3
index($_, $case_prefix) == 0 && length substr($_, length $case_prefix) && index(substr($_, length $case_prefix), '/') == -1
index($_, $case_prefix) == 0 length substr($_, length $case_prefix) index(substr($_, length $case_prefix), '/') == -1
Truth table50%2/4
A and B && C
A= index($_, $case_prefix) == 0B= length substr($_, length $case_prefix)C= index(substr($_, length $case_prefix), '/') == -1
ABCresult
0XX0
10X0
1100
1111
326 && length substr( $_, length $case_prefix )
327 && index( substr( $_, length $case_prefix ), '/' ) < 0
328 14 } keys %{$entries};
329  
330 14 my @selected = ( $sample_data_name, @case_names );
331 14 ▶ push @selected, $patient_data_name if defined $patient_data_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $patient_data_name 13 1 14
332 14 my $selected_text = $package->{read_many}->(\@selected);
333  
334 my $sample_table = _parse_clinical_table(
335 14 $selected_text->{$sample_data_name},
336 $sample_data_name,
337 [qw(PATIENT_ID SAMPLE_ID)],
338 );
339 my $patient_table = defined $patient_data_name
340 ? _parse_clinical_table(
341 14 ▶ $selected_text->{$patient_data_name},
Branch: 1 branch
BranchTrueFalseTotal
defined $patient_data_name ? : 13 1 14
342 $patient_data_name,
343 ['PATIENT_ID'],
344 )
345 : undef;
346  
347 14 my ( %patients, @patient_order );
348 14 ▶ if ($patient_table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($patient_table) 13 1 14
349 13 for my $row ( @{ $patient_table->{rows} } ) {
350 130 my $id = _required_id( $row->{PATIENT_ID}, 'PATIENT_ID', $patient_data_name );
351 die "cBioPortal patient table <$patient_data_name> contains duplicate PATIENT_ID <$id>\n"
352 130 ▶ if exists $patients{$id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $patients{$id} 0 130 130
353 130 $row->{PATIENT_ID} = $id;
354 130 $patients{$id} = $row;
355 130 push @patient_order, $id;
356 }
357 }
358  
359 14 my ( %samples, %samples_by_patient );
360 14 for my $row ( @{ $sample_table->{rows} } ) {
361 my $patient_id = _required_id(
362 140 $row->{PATIENT_ID}, 'PATIENT_ID', $sample_data_name,
363 );
364 my $sample_id = _required_id(
365 140 $row->{SAMPLE_ID}, 'SAMPLE_ID', $sample_data_name,
366 );
367 die "cBioPortal sample table <$sample_data_name> contains duplicate SAMPLE_ID <$sample_id>\n"
368 140 ▶ if exists $samples{$sample_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $samples{$sample_id} 0 140 140
369  
370 140 ▶ if ( $patient_table && !exists $patients{$patient_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($patient_table and not exists $patients{$patient_id}) 0 140 140
Condition66%2/3
$patient_table and not exists $patients{$patient_id}
!ll&&!rl&&r
101300
MC/DC0%0/2
$patient_table and not exists $patients{$patient_id}
$patient_table not exists $patients{$patient_id}
Truth table66%2/3
A and not(B)
A= $patient_tableB= not exists $patients{$patient_id}
ABresult
0X0
100
111
371 0 die "cBioPortal sample <$sample_id> references unknown PATIENT_ID <$patient_id>\n";
372 }
373 140 ▶ if ( !$patient_table && !exists $patients{$patient_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($patient_table or exists $patients{$patient_id}) 10 130 140
Condition66%2/3
$patient_table or exists $patients{$patient_id}
l!l&&r!l&&!r
130010
MC/DC50%1/2
$patient_table or exists $patients{$patient_id}
$patient_table exists $patients{$patient_id}
Truth table66%2/3
A or B
A= $patient_tableB= exists $patients{$patient_id}
ABresult
1X1
011
000
374 10 $patients{$patient_id} = { PATIENT_ID => $patient_id };
375 10 push @patient_order, $patient_id;
376 }
377  
378 140 $row->{PATIENT_ID} = $patient_id;
379 140 $row->{SAMPLE_ID} = $sample_id;
380 140 $samples{$sample_id} = $row;
381 140 push @{ $samples_by_patient{$patient_id} }, $row;
382 }
383  
384 14 ▶ die "cBioPortal study <$study_id> does not contain any patients\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @patient_order 0 14 14
385 unless @patient_order;
386  
387 14 my ( @case_lists, %case_ids );
388 14 for my $name (@case_names) {
389 14 my $case = _parse_meta_file( $selected_text->{$name}, $name, 1 );
390 14 _require_meta_fields(
391 $case,
392 $name,
393 qw(cancer_study_identifier stable_id case_list_name case_list_ids),
394 );
395 14 my $case_study_id = _trim( $case->{cancer_study_identifier} );
396 14 ▶ die "cBioPortal case list <$name> references study <$case_study_id>, expected <$study_id>\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $case_study_id eq $study_id 0 14 14
397 unless $case_study_id eq $study_id;
398  
399 14 my $stable_id = _trim( $case->{stable_id} );
400 die "cBioPortal package contains duplicate case-list stable_id <$stable_id>\n"
401 14 ▶ if $case_ids{$stable_id}++;
Branch: 1 branch
BranchTrueFalseTotal
if $case_ids{$stable_id}++ 0 14 14
402  
403 131 my @sample_ids = grep { length } map { _trim($_) }
404 14 split /\t/, $case->{case_list_ids};
405 14 ▶ die "cBioPortal case list <$name> does not contain sample identifiers\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @sample_ids 0 14 14
406 unless @sample_ids;
407  
408 14 my ( %seen_patient, @individual_ids );
409 14 for my $sample_id (@sample_ids) {
410 die "cBioPortal case list <$name> references unknown SAMPLE_ID <$sample_id>\n"
411 131 ▶ unless exists $samples{$sample_id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $samples{$sample_id} 1 130 131
412 130 my $patient_id = $samples{$sample_id}{PATIENT_ID};
413 130 ▶ push @individual_ids, $patient_id unless $seen_patient{$patient_id}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_patient{$patient_id}++ 130 0 130
414 }
415  
416 13 push @case_lists,
417 {
418 metadata => $case,
419 source => $name,
420 sampleIds => \@sample_ids,
421 individualIds => \@individual_ids,
422 };
423 }
424  
425 13 my %seen_header;
426 181 my @headers = grep { !$seen_header{$_}++ } (
427 13 ▶ @{ $patient_table ? $patient_table->{headers} : ['PATIENT_ID'] },
Branch: 1 branch
BranchTrueFalseTotal
$patient_table ? : 12 1 13
428 13 @{ $sample_table->{headers} },
429 );
430  
431 my $study_context = {
432 id => $study_id,
433 metadata => $study,
434 source => $package->{label},
435 patientAttributeDefinitions => $patient_table
436 ? $patient_table->{definitions}
437 : { PATIENT_ID => {} },
438 sampleAttributeDefinitions => $sample_table->{definitions},
439 13 ▶ caseLists => \@case_lists,
Branch: 1 branch
BranchTrueFalseTotal
$patient_table ? : 12 1 13
440 patientCount => scalar @patient_order,
441 sampleCount => scalar keys %samples,
442 };
443  
444 13 my @records;
445 13 for my $index ( 0 .. $#patient_order ) {
446 130 my $patient_id = $patient_order[$index];
447 push @records,
448 {
449 id => $patient_id,
450 patient => $patients{$patient_id},
451 130 ▶ samples => $samples_by_patient{$patient_id} || [],
Branch: 1 branch
BranchTrueFalseTotal
$index == 0 ? : 13 117 130
Condition50%1/2
$samples_by_patient{$patient_id} || []
l!l
1300
MC/DC0%0/1
$samples_by_patient{$patient_id} || []
$samples_by_patient{$patient_id}
Truth table50%1/2
A
A= $samples_by_patient{$patient_id}
Aresult
11
00
452 study => $study_context,
453 isFirst => $index == 0 ? 1 : 0,
454 };
455 }
456  
457 return {
458 13 records => \@records,
459 headers => \@headers,
460 study => $study_context,
461 };
462 }
463  
464 27 ▶sub _parse_clinical_table {
Subroutine _parse_clinical_table: called CC=21 SCAR=31.7
465 27 my ( $text, $label, $required_headers ) = @_;
466 27 $text =~ s/^\x{FEFF}//;
467 27 $text =~ s/\r\n?/\n/g;
468  
469 27 my ( @metadata_rows, @data_lines );
470 27 for my $line ( split /\n/, $text, -1 ) {
471 432 ▶ if ( $line =~ /^#(.*)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#(.*)\z/) 108 324 432
472 108 ▶ push @metadata_rows, $1 unless @data_lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @data_lines 108 0 108
473 108 next;
474 }
475 324 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 27 297 324
476 297 push @data_lines, $line;
477 }
478 27 ▶ die "cBioPortal clinical file <$label> does not contain a header row\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @data_lines 0 27 27
479 unless @data_lines;
480  
481 27 my $body = join "\n", @data_lines;
482 27 ▶ open my $fh, '<', \$body
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", \$body or die "Cannot parse cBioPortal clinical file <$label>: $!\n" 27 0 27
483 or die "Cannot parse cBioPortal clinical file <$label>: $!\n";
484 27 my $csv = Text::CSV_XS->new(
485 {
486 binary => 1,
487 sep_char => "\t",
488 blank_is_undef => 0,
489 empty_is_undef => 0,
490 }
491 );
492  
493 27 my $headers = $csv->getline($fh);
494 27 ▶ die "Cannot parse cBioPortal clinical header in <$label>: "
Branch: 1 branch
BranchTrueFalseTotal
unless $headers 0 27 27
495 . $csv->error_diag . "\n"
496 unless $headers;
497  
498 27 my %seen;
499 27 for my $header ( @{$headers} ) {
500 194 $header = _trim($header);
501 194 ▶ die "cBioPortal clinical file <$label> contains an empty column name\n"
Branch: 1 branch
BranchTrueFalseTotal
unless length $header 0 194 194
502 unless length $header;
503 die "cBioPortal clinical file <$label> contains duplicate column <$header>\n"
504 194 ▶ if $seen{$header}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$header}++ 0 194 194
505 }
506 27 for my $required ( @{$required_headers} ) {
507 die "cBioPortal clinical file <$label> is missing required column <$required>\n"
508 41 ▶ unless $seen{$required};
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$required} 0 41 41
509 }
510  
511 27 my @rows;
512 27 ▶ while ( my $values = $csv->getline($fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $csv->getline($fh)) 270 27 297
513 0 die "cBioPortal clinical row in <$label> contains " . scalar( @{$values} )
514 0 . ' values but ' . scalar( @{$headers} ) . " columns are defined\n"
515 270 ▶ unless @{$values} == @{$headers};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$values;} == @{$headers;} 0 270 270
516 270 my %row;
517 270 @row{ @{$headers} } = @{$values};
518 270 push @rows, \%row;
519 }
520 27 ▶ die "Cannot parse cBioPortal clinical data in <$label>: "
Branch: 1 branch
BranchTrueFalseTotal
unless $csv->eof 0 27 27
521 . $csv->error_diag . "\n"
522 unless $csv->eof;
523 27 close $fh;
524  
525 27 my @metadata_names = qw(displayName description datatype priority);
526 27 my %definitions;
527 27 for my $column_index ( 0 .. $#{$headers} ) {
528 194 my %definition;
529 194 for my $row_index ( 0 .. $#metadata_names ) {
530 776 ▶ next unless defined $metadata_rows[$row_index];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $metadata_rows[$row_index] 0 776 776
531 776 my @values = split /\t/, $metadata_rows[$row_index], -1;
532 776 ▶ next unless defined $values[$column_index] && length $values[$column_index];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values[$column_index] and length $values[$column_index] 0 776 776
Condition33%1/3
defined $values[$column_index] and length $values[$column_index]
!ll&&!rl&&r
00776
MC/DC0%0/2
defined $values[$column_index] and length $values[$column_index]
defined $values[$column_index] length $values[$column_index]
Truth table33%1/3
A and B
A= defined $values[$column_index]B= length $values[$column_index]
ABresult
0X0
100
111
533 776 $definition{ $metadata_names[$row_index] } = $values[$column_index];
534 }
535 194 $definitions{ $headers->[$column_index] } = \%definition;
536 }
537  
538 return {
539 27 headers => $headers,
540 rows => \@rows,
541 definitions => \%definitions,
542 };
543 }
544  
545 55 ▶sub _parse_meta_file {
Subroutine _parse_meta_file: called CC=10 SCAR=25.3
546 55 my ( $text, $label, $strict ) = @_;
547 55 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 55 55
548 55 $text =~ s/^\x{FEFF}//;
549 55 $text =~ s/\r\n?/\n/g;
550  
551 55 my %meta;
552 55 my $found = 0;
553 55 for my $line ( split /\n/, $text ) {
554 304 ▶ next if $line =~ /^\s*(?:#|\z)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*(?:#|\z)/ 0 304 304
555 304 ▶ if ( $line !~ /^\s*([^:]+?)\s*:\s*(.*)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\s*([^:]+?)\s*:\s*(.*)\z/) 0 304 304
556 0 ▶ return unless $strict;
Branch: 1 branch
BranchTrueFalseTotal
unless $strict 0 0 0
557 0 die "Invalid cBioPortal meta line in <$label>: <$line>\n";
558 }
559 304 my ( $key, $value ) = ( lc _trim($1), _trim($2) );
560 die "cBioPortal meta file <$label> contains duplicate key <$key>\n"
561 304 ▶ if exists $meta{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $meta{$key} 0 304 304
562 304 $meta{$key} = $value;
563 304 $found = 1;
564 }
565  
566 55 ▶ return $found ? \%meta : undef unless $strict;
Branch: 2 branches
BranchTrueFalseTotal
unless $strict 27 28 55
$found ? : 27 0 27
567 28 ▶ die "cBioPortal meta file <$label> is empty\n" unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 28 28
568 28 return \%meta;
569 }
570  
571 55 ▶sub _require_meta_fields {
Subroutine _require_meta_fields: called CC=4 SCAR=15.2
572 55 my ( $meta, $label, @fields ) = @_;
573 55 for my $field (@fields) {
574 die "cBioPortal meta file <$label> is missing <$field>\n"
575 138 ▶ unless defined $meta->{$field} && length _trim( $meta->{$field} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meta->{$field} and length _trim($meta->{$field}) 0 138 138
Condition33%1/3
defined $meta->{$field} and length _trim($meta->{$field})
!ll&&!rl&&r
00138
MC/DC0%0/2
defined $meta->{$field} and length _trim($meta->{$field})
defined $meta->{$field} length _trim($meta->{$field})
Truth table33%1/3
A and B
A= defined $meta->{$field}B= length _trim($meta->{$field})
ABresult
0X0
100
111
576 }
577 55 return 1;
578 }
579  
580 27 ▶sub _resolve_package_name {
Subroutine _resolve_package_name: called CC=2 SCAR=7.0
581 27 my ( $prefix, $relative, $entries, $meta_name ) = @_;
582 27 $relative = _normalize_entry_name( _trim($relative) );
583 27 my $name = $prefix . $relative;
584 die "cBioPortal meta file <$meta_name> references missing data file <$relative>\n"
585 27 ▶ unless exists $entries->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $entries->{$name} 0 27 27
586 27 return $name;
587 }
588  
589 153 ▶sub _normalize_entry_name {
Subroutine _normalize_entry_name: called CC=5 SCAR=17.8
590 153 my ($name) = @_;
591 153 ▶ die "cBioPortal package contains an empty file name\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 153 153
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
00153
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
592 unless defined $name && length $name;
593 153 $name =~ tr{\\}{/};
594 153 $name =~ s{\A\./+}{};
595 die "Unsafe cBioPortal package path <$name>\n"
596 153 ▶ if $name =~ m{\A/} || grep { $_ eq '..' } split m{/+}, $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[\A/] or grep {$_ eq "..";} split(m[/+], $name, 0) 0 153 153
Condition33%1/3
$name =~ m[\A/] or grep {$_ eq "..";} split(m[/+], $name, 0)
l!l&&r!l&&!r
00153
MC/DC0%0/2
$name =~ m[\A/] or grep {$_ eq "..";} split(m[/+], $name, 0)
$name =~ m[\A/] grep {$_ eq "..";} split(m[/+], $name, 0)
Truth table33%1/3
A or B
A= $name =~ m[\A/]B= grep {$_ eq "..";} split(m[/+], $name, 0)
ABresult
1X1
011
000
597 153 $name =~ s{/+}{/}g;
598 153 return $name;
599 }
600  
601 410 ▶sub _required_id {
Subroutine _required_id: called CC=3 SCAR=12.0
602 410 my ( $value, $field, $label ) = @_;
603 410 my $id = _trim($value);
604 410 ▶ die "cBioPortal clinical file <$label> contains a row without <$field>\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and length $id 0 410 410
Condition33%1/3
defined $id and length $id
!ll&&!rl&&r
00410
MC/DC0%0/2
defined $id and length $id
defined $id length $id
Truth table33%1/3
A and B
A= defined $idB= length $id
ABresult
0X0
100
111
605 unless defined $id && length $id;
606 410 return $id;
607 }
608  
609 1577 ▶sub _trim {
Subroutine _trim: called CC=3 SCAR=11.3
610 1577 my ($value) = @_;
611 1577 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 1577 1577
612 1577 ▶ return $value if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 1577 1577
613 1577 $value =~ s/^\s+|\s+$//g;
614 1577 return $value;
615 }
616  
617 136 1;
« bin/convert-pheno ↑ Summary lib/Convert/Pheno/OMOP/Vocabulary.pm »