| 1 | package Convert::Pheno::CDISC::ODM::Detector; | ||||||||||||||||||||||||||||
| 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 Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 136 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 9 | attribute_by_namespace | ||||||||||||||||||||||||||||
| 10 | detect_odm_document | ||||||||||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | my %SUPPORTED = ( | ||||||||||||||||||||||||||||
| 14 | 'http://www.cdisc.org/ns/odm/v1.3' => { | ||||||||||||||||||||||||||||
| 15 | adapter => 'v1', | ||||||||||||||||||||||||||||
| 16 | 136 | versions => { map { $_ => 1 } qw(1.3.1 1.3.2) }, | |||||||||||||||||||||||||||
| 17 | }, | ||||||||||||||||||||||||||||
| 18 | 'http://www.cdisc.org/ns/odm/v2.0' => { | ||||||||||||||||||||||||||||
| 19 | adapter => 'v2', | ||||||||||||||||||||||||||||
| 20 | versions => { '2.0' => 1, '2.0.0' => 1 }, | ||||||||||||||||||||||||||||
| 21 | }, | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 77 | ▶ | sub detect_odm_document { | ||||||||||||||||||||||||||
|
Subroutine
detect_odm_document: called CC=16 SCAR=31.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 77 | my ($document) = @_; | |||||||||||||||||||||||||||
| 26 | 77 | ▶ | die "CDISC-ODM input must contain one ODM root element\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | unless ref($document) eq 'HASH'; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 77 | my ( $root_key, $root ) = _root_element($document); | |||||||||||||||||||||||||||
| 30 | 77 | ▶ | die "CDISC-ODM input must contain one ODM root element\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $root_key and ref $root eq "HASH"
MC/DC0%0/2
defined $root_key and ref $root eq "HASH"
defined $root_key
ref $root eq "HASH"
Truth table33%1/3
A and B
A= defined $root_keyB= ref $root eq "HASH"
| |||||||||||||||||||||||||||||
| 31 | unless defined $root_key && ref($root) eq 'HASH'; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 77 | my $namespace = $root->{'-xmlns'}; | |||||||||||||||||||||||||||
| 34 | 77 | ▶ | if ( !defined $namespace && $root_key =~ /\A([^:]+):ODM\z/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $namespace and $root_key =~ /\A([^:]+):ODM\z/
MC/DC0%0/2
not defined $namespace and $root_key =~ /\A([^:]+):ODM\z/
not defined $namespace
$root_key =~ /\A([^:]+):ODM\z/
Truth table33%1/3
not(A) and B
A= not defined $namespaceB= $root_key =~ /\A([^:]+):ODM\z/
| |||||||||||||||||||||||||||||
| 35 | 0 | $namespace = $root->{ '-xmlns:' . $1 }; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 77 | ▶ | die "CDISC-ODM root is missing its ODM namespace\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $namespace and length $namespace
MC/DC0%0/2
defined $namespace and length $namespace
defined $namespace
length $namespace
Truth table33%1/3
A and B
A= defined $namespaceB= length $namespace
| |||||||||||||||||||||||||||||
| 38 | unless defined $namespace && length $namespace; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 77 | my $supported = $SUPPORTED{$namespace}; | |||||||||||||||||||||||||||
| 41 | 77 | ▶ | die "Unsupported CDISC-ODM namespace <$namespace>\n" unless $supported; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 77 | my $version = $root->{'-ODMVersion'}; | |||||||||||||||||||||||||||
| 44 | 77 | ▶ | die "CDISC-ODM root is missing <ODMVersion>\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $version and length $version
MC/DC0%0/2
defined $version and length $version
defined $version
length $version
Truth table33%1/3
A and B
A= defined $versionB= length $version
| |||||||||||||||||||||||||||||
| 45 | unless defined $version && length $version; | ||||||||||||||||||||||||||||
| 46 | die "CDISC-ODM namespace/version mismatch: namespace <$namespace> does not support <ODMVersion=$version>\n" | ||||||||||||||||||||||||||||
| 47 | 77 | ▶ | unless $supported->{versions}{$version}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 76 | ▶ | my $file_type = $root->{'-FileType'} // q{}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$root->{"-FileType"} // ""
MC/DC0%0/1
$root->{"-FileType"} // ""
$root->{"-FileType"}
Truth table50%1/2
A
A= $root->{"-FileType"}
| |||||||||||||||||||||||||||||
| 50 | 76 | ▶ | die "Unsupported CDISC-ODM FileType <$file_type>; only Snapshot input is supported\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | unless lc($file_type) eq 'snapshot'; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 75 | my $namespaces = _namespace_map($root); | |||||||||||||||||||||||||||
| 54 | 75 | ▶ | my $source_system = $root->{'-SourceSystem'} // q{}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$root->{"-SourceSystem"} // ""
MC/DC100%1/1
$root->{"-SourceSystem"} // ""
$root->{"-SourceSystem"}
Truth table100%2/2
A
A= $root->{"-SourceSystem"}
| |||||||||||||||||||||||||||||
| 55 | 75 | my $vendor = _detect_vendor( $source_system, $namespaces ); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | return { | ||||||||||||||||||||||||||||
| 58 | adapter => $supported->{adapter}, | ||||||||||||||||||||||||||||
| 59 | 75 | ▶ | namespace => $namespace, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | namespaces => $namespaces, | ||||||||||||||||||||||||||||
| 61 | odmVersion => $version, | ||||||||||||||||||||||||||||
| 62 | recordProfile => $vendor eq 'redcap' ? 'redcap' : 'cdisc-odm', | ||||||||||||||||||||||||||||
| 63 | root => $root, | ||||||||||||||||||||||||||||
| 64 | sourceSystem => $source_system, | ||||||||||||||||||||||||||||
| 65 | vendor => $vendor, | ||||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 10353 | ▶ | sub attribute_by_namespace { | ||||||||||||||||||||||||||
|
Subroutine
attribute_by_namespace: called CC=6 SCAR=18.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 10353 | my ( $node, $namespaces, $namespace_match, $local_name ) = @_; | |||||||||||||||||||||||||||
| 71 | 10353 | ▶ | return unless ref($node) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 10353 | for my $key ( keys %{$node} ) { | |||||||||||||||||||||||||||
| 74 | 30988 | ▶ | next unless $key =~ /\A-([^:]+):\Q$local_name\E\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 375 | my $uri = $namespaces->{$1}; | |||||||||||||||||||||||||||
| 76 | 375 | ▶ | next unless defined $uri; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 375 | ▶ | return $node->{$key} if _namespace_matches( $uri, $namespace_match ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 9978 | return; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 77 | ▶ | sub _root_element { | ||||||||||||||||||||||||||
|
Subroutine
_root_element: called CC=3 SCAR=11.6
| |||||||||||||||||||||||||||||
| 83 | 77 | my ($document) = @_; | |||||||||||||||||||||||||||
| 84 | 77 | ▶ | my @keys = grep { $_ eq 'ODM' || /:ODM\z/ } keys %{$document}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$_ eq 'ODM' || /:ODM\z/
MC/DC0%0/2
$_ eq 'ODM' || /:ODM\z/
$_ eq 'ODM'
/:ODM\z/
Truth table33%1/3
A || B
A= $_ eq 'ODM'B= /:ODM\z/
| |||||||||||||||||||||||||||||
| 85 | 77 | ▶ | return if @keys != 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 77 | return ( $keys[0], $document->{ $keys[0] } ); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 75 | ▶ | sub _namespace_map { | ||||||||||||||||||||||||||
|
Subroutine
_namespace_map: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 90 | 75 | my ($root) = @_; | |||||||||||||||||||||||||||
| 91 | 75 | my %namespaces; | |||||||||||||||||||||||||||
| 92 | 75 | for my $key ( keys %{$root} ) { | |||||||||||||||||||||||||||
| 93 | 739 | ▶ | next unless $key =~ /\A-xmlns(?::(.+))?\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 162 | ▶ | $namespaces{ defined $1 ? $1 : q{} } = $root->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 75 | return \%namespaces; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 75 | ▶ | sub _detect_vendor { | ||||||||||||||||||||||||||
|
Subroutine
_detect_vendor: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 100 | 75 | my ( $source_system, $namespaces ) = @_; | |||||||||||||||||||||||||||
| 101 | 75 | ▶ | return 'redcap' if $source_system =~ /redcap/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 68 | ▶ | return 'openclinica' if $source_system =~ /openclinica/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 66 | for my $uri ( values %{$namespaces} ) { | |||||||||||||||||||||||||||
| 105 | 130 | ▶ | return 'redcap' if $uri =~ m{\Ahttps?://(?:www\.)?projectredcap\.org/?\z}i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 130 | ▶ | return 'openclinica' if $uri =~ /openclinica/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 66 | return 'standard'; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 375 | ▶ | sub _namespace_matches { | ||||||||||||||||||||||||||
|
Subroutine
_namespace_matches: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 112 | 375 | my ( $uri, $match ) = @_; | |||||||||||||||||||||||||||
| 113 | 375 | ▶ | return $uri eq $match unless ref($match) eq 'Regexp'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 217 | ▶ | return $uri =~ $match ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 136 | 1; | |||||||||||||||||||||||||||