| 1 | package Convert::Pheno::OMOP::ParticipantStream; | ||||||||||||||||||||||||||||
| 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 | 136 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 136 | ▶ | use feature qw(say); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 136 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 136 | ▶ | use List::Util qw(any); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 136 | ▶ | use Convert::Pheno::OMOP::Definitions; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 136 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 14 | omop_require_core_tables | ||||||||||||||||||||||||||||
| 15 | omop_init_caches_and_metadata | ||||||||||||||||||||||||||||
| 16 | omop_prepare_data_shape | ||||||||||||||||||||||||||||
| 17 | omop_stream_dispatcher | ||||||||||||||||||||||||||||
| 18 | process_csv_files_stream | ||||||||||||||||||||||||||||
| 19 | process_sqldump_stream | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 43 | ▶ | sub omop_require_core_tables { | ||||||||||||||||||||||||||
|
Subroutine
omop_require_core_tables: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 43 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||
| 24 | 43 | for my $table (qw(CONCEPT PERSON)) { | |||||||||||||||||||||||||||
| 25 | die "The table <$table> is missing from the input data\n" | ||||||||||||||||||||||||||||
| 26 | 85 | ▶ | unless exists $data->{$table}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 41 | return 1; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 39 | ▶ | sub omop_init_caches_and_metadata { | ||||||||||||||||||||||||||
|
Subroutine
omop_init_caches_and_metadata: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 39 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | $self->{data_ohdsi_dict} = | ||||||||||||||||||||||||||||
| 35 | 39 | Convert::Pheno::convert_table_aoh_to_hoh( $data, 'CONCEPT', $self ); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 39 | ▶ | if ( $self->{stream} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 8 | $self->{person} = Convert::Pheno::convert_table_aoh_to_hoh( $data, 'PERSON', $self ); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 39 | ▶ | if ( exists $data->{VISIT_OCCURRENCE} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | $self->{visit_occurrence} = | ||||||||||||||||||||||||||||
| 43 | 10 | Convert::Pheno::convert_table_aoh_to_hoh( $data, 'VISIT_OCCURRENCE', $self ); | |||||||||||||||||||||||||||
| 44 | 10 | delete $data->{VISIT_OCCURRENCE}; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 39 | $self->{exposures} = Convert::Pheno::load_exposures( $self->{exposures_file} ); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 39 | $self->{metaData} = Convert::Pheno::get_metaData($self); | |||||||||||||||||||||||||||
| 50 | 39 | $self->{convertPheno} = Convert::Pheno::get_info($self); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 39 | return 1; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 41 | ▶ | sub omop_prepare_data_shape { | ||||||||||||||||||||||||||
|
Subroutine
omop_prepare_data_shape: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 41 | my ( $self, $data ) = @_; | |||||||||||||||||||||||||||
| 57 | $self->{data} = | ||||||||||||||||||||||||||||
| 58 | 41 | ▶ | $self->{stream} ? $data : Convert::Pheno::transpose_omop_data_structure( $self, $data ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 41 | return 1; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 9 | ▶ | sub omop_stream_dispatcher { | ||||||||||||||||||||||||||
|
Subroutine
omop_stream_dispatcher: called CC=13 SCAR=26.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 9 | my $arg = shift; | |||||||||||||||||||||||||||
| 64 | 9 | my $self = $arg->{self}; | |||||||||||||||||||||||||||
| 65 | 9 | my $filepath = $arg->{filepath}; | |||||||||||||||||||||||||||
| 66 | 9 | my $filepaths = $arg->{filepaths}; | |||||||||||||||||||||||||||
| 67 | 9 | my $omop_tables = $self->{prev_omop_tables}; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 9 | my $multi_entity_stream = | |||||||||||||||||||||||||||
| 70 | Convert::Pheno::omop_streams_multiple_entities_wrapper($self); | ||||||||||||||||||||||||||||
| 71 | 9 | my $connections_open = 0; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 9 | my $ok = eval { | |||||||||||||||||||||||||||
| 74 | 9 | ▶ | if ( $self->{method} ne 'bff2pxf' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 9 | Convert::Pheno::open_connections_SQLite($self); | |||||||||||||||||||||||||||
| 76 | 9 | $connections_open = 1; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 9 | ▶ | Convert::Pheno::omop_stream_targets_open_wrapper($self) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | if $multi_entity_stream; | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 9 | ▶ | @$filepaths | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | ? process_csv_files_stream( $self, $filepaths ) | ||||||||||||||||||||||||||||
| 83 | : process_sqldump_stream( $self, $filepath, $omop_tables ); | ||||||||||||||||||||||||||||
| 84 | 8 | 1; | |||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||
| 86 | 9 | my $err = $@; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 9 | my @cleanup_errors; | |||||||||||||||||||||||||||
| 89 | _run_cleanup( | ||||||||||||||||||||||||||||
| 90 | \@cleanup_errors, | ||||||||||||||||||||||||||||
| 91 | 'finalizing streamed entity output', | ||||||||||||||||||||||||||||
| 92 | 4 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 93 | 4 | ▶ | Convert::Pheno::omop_stream_targets_finalize_wrapper( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | $self, | ||||||||||||||||||||||||||||
| 95 | $ok ? 1 : 0, | ||||||||||||||||||||||||||||
| 96 | ); | ||||||||||||||||||||||||||||
| 97 | }, | ||||||||||||||||||||||||||||
| 98 | ) | ||||||||||||||||||||||||||||
| 99 | 9 | ▶ | if $multi_entity_stream; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | _run_cleanup( | ||||||||||||||||||||||||||||
| 101 | \@cleanup_errors, | ||||||||||||||||||||||||||||
| 102 | 'closing SQLite connections', | ||||||||||||||||||||||||||||
| 103 | 9 | ▶ | sub { Convert::Pheno::close_connections_SQLite($self) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 104 | ) | ||||||||||||||||||||||||||||
| 105 | 9 | ▶ | if $connections_open; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | _run_cleanup( | ||||||||||||||||||||||||||||
| 107 | \@cleanup_errors, | ||||||||||||||||||||||||||||
| 108 | 'closing the terminology audit', | ||||||||||||||||||||||||||||
| 109 | 9 | ▶ | sub { Convert::Pheno::finalize_term_audit($self) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 110 | 9 | ); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 9 | ▶ | if ( !$ok || @cleanup_errors ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $ok or @cleanup_errors
MC/DC50%1/2
not $ok or @cleanup_errors
not $ok
@cleanup_errors
Truth table66%2/3
not(A) or B
A= not $okB= @cleanup_errors
| |||||||||||||||||||||||||||||
| 113 | 1 | ▶ | my $message = $err || q{}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$err || ''
MC/DC0%0/1
$err || ''
$err
Truth table50%1/2
A
A= $err
| |||||||||||||||||||||||||||||
| 114 | 1 | ▶ | $message .= "\n" if length($message) && $message !~ /\n\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $message and not $message =~ /\n\z/
MC/DC0%0/2
length $message and not $message =~ /\n\z/
length $message
not $message =~ /\n\z/
Truth table33%1/3
A and not(B)
A= length $messageB= not $message =~ /\n\z/
| |||||||||||||||||||||||||||||
| 115 | 1 | ▶ | $message .= "Stream cleanup failed:\n" if !length($message) && @cleanup_errors; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not length $message and @cleanup_errors
MC/DC0%0/2
not length $message and @cleanup_errors
not length $message
@cleanup_errors
Truth table33%1/3
not(A) and B
A= not length $messageB= @cleanup_errors
| |||||||||||||||||||||||||||||
| 116 | 0 | $message .= join q{}, map { " $_\n" } @cleanup_errors; | |||||||||||||||||||||||||||
| 117 | 1 | die $message; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 8 | return 1; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 22 | ▶ | sub _run_cleanup { | ||||||||||||||||||||||||||
|
Subroutine
_run_cleanup: called CC=3 SCAR=15.4
| |||||||||||||||||||||||||||||
| 124 | 22 | my ( $errors, $label, $code ) = @_; | |||||||||||||||||||||||||||
| 125 | 22 | my $ok = eval { | |||||||||||||||||||||||||||
| 126 | 22 | $code->(); | |||||||||||||||||||||||||||
| 127 | 22 | 1; | |||||||||||||||||||||||||||
| 128 | }; | ||||||||||||||||||||||||||||
| 129 | 22 | ▶ | return 1 if $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | my $error = $@ || 'unknown cleanup error'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$@ || 'unknown cleanup error'
MC/DC0%0/1
$@ || 'unknown cleanup error'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||
| 132 | 0 | chomp $error; | |||||||||||||||||||||||||||
| 133 | 0 | push @{$errors}, "$label: $error"; | |||||||||||||||||||||||||||
| 134 | 0 | return; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 7 | ▶ | sub process_csv_files_stream { | ||||||||||||||||||||||||||
|
Subroutine
process_csv_files_stream: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 138 | 7 | my ( $self, $filepaths ) = @_; | |||||||||||||||||||||||||||
| 139 | 7 | my $person = $self->{person}; | |||||||||||||||||||||||||||
| 140 | 7 | for my $file (@$filepaths) { | |||||||||||||||||||||||||||
| 141 | 8 | ▶ | say "Processing file ... <$file>" if $self->{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | Convert::Pheno::read_csv_stream( | ||||||||||||||||||||||||||||
| 143 | { | ||||||||||||||||||||||||||||
| 144 | in => $file, | ||||||||||||||||||||||||||||
| 145 | sep => $self->{sep}, | ||||||||||||||||||||||||||||
| 146 | 8 | self => $self, | |||||||||||||||||||||||||||
| 147 | person => $person | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 7 | return 1; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 3 | ▶ | sub process_sqldump_stream { | ||||||||||||||||||||||||||
|
Subroutine
process_sqldump_stream: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 155 | 3 | my ( $self, $filepath, $omop_tables ) = @_; | |||||||||||||||||||||||||||
| 156 | 3 | my $person = $self->{person}; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 3 | for my $table (@$omop_tables) { | |||||||||||||||||||||||||||
| 159 | 9 | ▶ | next if any { $_ eq $table } @stream_ram_memory_tables; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 3 | ▶ | say "Processing table <$table> line-by-line..." if $self->{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | _with_temp_field( | ||||||||||||||||||||||||||||
| 163 | $self, | ||||||||||||||||||||||||||||
| 164 | 'omop_tables', | ||||||||||||||||||||||||||||
| 165 | [$table], | ||||||||||||||||||||||||||||
| 166 | 3 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 167 | 3 | Convert::Pheno::read_sqldump_stream( | |||||||||||||||||||||||||||
| 168 | { in => $filepath, self => $self, person => $person } | ||||||||||||||||||||||||||||
| 169 | ); | ||||||||||||||||||||||||||||
| 170 | 3 | return 1; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 3 | ); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 3 | return 1; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 3 | ▶ | sub _with_temp_field { | ||||||||||||||||||||||||||
|
Subroutine
_with_temp_field: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 178 | 3 | my ( $converter, $field, $value, $code ) = @_; | |||||||||||||||||||||||||||
| 179 | 3 | my $had = exists $converter->{$field}; | |||||||||||||||||||||||||||
| 180 | 3 | ▶ | my $old = $had ? $converter->{$field} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 3 | $converter->{$field} = $value; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 3 | my ( $ok, $result ); | |||||||||||||||||||||||||||
| 184 | 3 | $ok = eval { | |||||||||||||||||||||||||||
| 185 | 3 | $result = $code->(); | |||||||||||||||||||||||||||
| 186 | 3 | 1; | |||||||||||||||||||||||||||
| 187 | }; | ||||||||||||||||||||||||||||
| 188 | 3 | my $error = $@; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 3 | ▶ | if ($had) { $converter->{$field} = $old } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 0 | else { delete $converter->{$field} } | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 3 | ▶ | die $error unless $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 3 | return $result; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 136 | 1; | |||||||||||||||||||||||||||