| 1 | package UMLS::Association::Collector; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use DBI; | ||
| 6 | use feature qw(say); | ||
| 7 | |||
| 8 | #require Exporter; | ||
| 9 | use Exporter 'import'; | ||
| 10 | our @EXPORT_OK = qw(build_utterance_graph get_bigrams_of); | ||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | my $VERBOSE = 1; | ||
| 17 | my $DEBUG = 0; | ||
| 18 | |||
| 19 | |||
| 20 | sub process_files { | ||
| 21 | my @files = @_; | ||
| 22 | |||
| 23 | my %bigrams; # cui_1 => cui_2 => sum of cui_1 preceeding cui_2 | ||
| 24 | my %n1p; # cui => sum of cui as cui_1 | ||
| 25 | my %np1; # cui => sum of cui as cui_2 | ||
| 26 | my $npp; # total sum of all bigram observations | ||
| 27 | |||
| 28 | # Anonymous subroutine to update the bigram and marginal counts | ||
| 29 | my $incrementor = sub { | ||
| 30 | my($cui_1, $cui_2, $same_phrase) = @_; | ||
| 31 | |||
| 32 | unless (exists $$same_phrase{$cui_1}{$cui_2}) { | ||
| 33 | $bigrams{$cui_1}{$cui_2}++; | ||
| 34 | $n1p{$cui_1}++; | ||
| 35 | $np1{$cui_2}++; | ||
| 36 | $npp++; | ||
| 37 | } | ||
| 38 | |||
| 39 | $$same_phrase{$cui_1}{$cui_2} = 1; | ||
| 40 | }; | ||
| 41 | |||
| 42 | for my $file (@files) { | ||
| 43 | |||
| 44 | if( File::Type->mime_type($file) =~ /x-gzip/) { | ||
| 45 | process_gz_file($file, $incrementor); | ||
| 46 | } | ||
| 47 | else { | ||
| 48 | process_file($file, $incrementor); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | return (\%bigrams, \%n1p, \%np1, $npp); | ||
| 53 | } | ||
| 54 | ############################################################################### | ||
| 55 | sub process_file { | ||
| 56 | my $file = shift; # text/plain file to process | ||
| 57 | my $incrementor = shift; # ref to anon sub that updates bigram counts | ||
| 58 | |||
| 59 | open(my $fh, '<', $file) or die "Cannot read $file: $!"; #fh = filehandle | ||
| 60 | say "Parsing uncompressed file: $file" if $VERBOSE; | ||
| 61 | |||
| 62 | until (eof $fh ){ | ||
| 63 | count_bigrams( read_utterance( $fh ), $incrementor ); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | sub process_gz_file { | ||
| 68 | |||
| 69 | my $file = shift; # compressed .gz file to process | ||
| 70 | my $incrementor = shift; # ref to anon sub that updates bigram counts | ||
| 71 | |||
| 72 | my $gz = gzopen("$file", "rb") or die "Cannot read gzip compressed $file\n"; | ||
| 73 | say "Parsing gzip compressed file: $file" if $VERBOSE; | ||
| 74 | |||
| 75 | # Count the bigrams in each utterance until the end of the file. | ||
| 76 | until ($gz->gzeof()) { | ||
| 77 | count_bigrams( read_gz_utterance($gz), $incrementor ); | ||
| 78 | } | ||
| 79 | |||
| 80 | $gz->gzclose(); | ||
| 81 | } | ||
| 82 | |||
| 83 | ############################################################################### | ||
| 84 | # take filehandle as lexical variable | ||
| 85 | # returns ref to array of phrases for single utterance | ||
| 86 | sub read_utterance { | ||
| 87 | my $fh = shift; | ||
| 88 | |||
| 89 | my @phrases_in_utterance; | ||
| 90 | |||
| 91 | # The following loop will iterate over all the phrases for this utterance | ||
| 92 | while (<$fh>) { | ||
| 93 | |||
| 94 | # Finish when we reach the End Of Utterance (EOU) marker | ||
| 95 | last if /^'EOU'/; | ||
| 96 | |||
| 97 | # Skip all lines that aren't mappings for a phrase in the utterance | ||
| 98 | next unless /^mappings/; | ||
| 99 | |||
| 100 | my @mappings = @{ get_mappings($_) }; | ||
| 101 | |||
| 102 | push @phrases_in_utterance, \@mappings if @mappings; | ||
| 103 | } | ||
| 104 | |||
| 105 | return \@phrases_in_utterance; | ||
| 106 | } | ||
| 107 | |||
| 108 | ############################################################################### | ||
| 109 | # takes the gzip "filehandle" object | ||
| 110 | # returns ref to array of phrases for single utterance | ||
| 111 | sub read_gz_utterance { | ||
| 112 | my $gz = shift; | ||
| 113 | |||
| 114 | my @phrases; | ||
| 115 | |||
| 116 | # The following loop will iterate over all the phrases for this utterance | ||
| 117 | while ($gz->gzreadline($_)) { | ||
| 118 | |||
| 119 | # Finish when we reach the End Of Utterance (EOU) marker | ||
| 120 | last if /^'EOU'/; | ||
| 121 | |||
| 122 | # Skip all lines that aren't mappings for a phrase in the utterance | ||
| 123 | next unless /^mappings/; | ||
| 124 | |||
| 125 | my @mappings = @{ get_mappings($_) }; | ||
| 126 | |||
| 127 | push @phrases, \@mappings if @mappings; | ||
| 128 | } | ||
| 129 | |||
| 130 | return \@phrases; | ||
| 131 | } | ||
| 132 | |||
| 133 | ############################################################################### | ||
| 134 | sub get_mappings { | ||
| 135 | my $mapping_string = shift; # the line for which /^mappings/ is true | ||
| 136 | |||
| 137 | # Break mappings into each possible mapping of phrase into CUIs | ||
| 138 | my @maps = split /map\(/, $mapping_string; | ||
| 139 | |||
| 140 | # Collect the CUIs in each possible mapping (assumes format 'C1234567') | ||
| 141 | # as a set of strings | ||
| 142 | my @mappings; | ||
| 143 | for my $map (@maps) { | ||
| 144 | |||
| 145 | my $CUI_string = join " ", ( $map =~ m/C\d{7}/g ); | ||
| 146 | |||
| 147 | say $CUI_string if $CUI_string and $DEBUG; | ||
| 148 | |||
| 149 | push @mappings, $CUI_string if $CUI_string; | ||
| 150 | } | ||
| 151 | |||
| 152 | return \@mappings; | ||
| 153 | } | ||
| 154 | |||
| 155 | ############################################################################### | ||
| 156 | sub count_bigrams { | ||
| 157 | my( $phrases_ref, # reference to list of mappings for single utterance | ||
| 158 | $incrementor # anonymous subroutine to update counting hashes | ||
| 159 | ) = @_; | ||
| 160 | |||
| 161 | my @phrases = @$phrases_ref; | ||
| 162 | |||
| 163 | # Iterate through n-1 phrases in utterance | ||
| 164 | for (my $i = 0; $i < $#phrases; $i++) { | ||
| 165 | |||
| 166 | my @phrase_1 = @{ $phrases[$i] }; # Mappings for current phrase | ||
| 167 | my @phrase_2 = @{ $phrases[$i + 1] }; # Mappings for the next phrase | ||
| 168 | |||
| 169 | my %prior; # Tracks bigrams within same phrase to avoid double counting | ||
| 170 | |||
| 171 | # Loop through each of the mappings of the current phrase | ||
| 172 | foreach my $map_str_p1 ( @phrase_1 ) { | ||
| 173 | my @cuis = split ' ', $map_str_p1; | ||
| 174 | |||
| 175 | # Count bigrams up to the k-1th CUI | ||
| 176 | for (my $k = 0; $k < $#cuis; $k++) { | ||
| 177 | my $cui_1 = $cuis[$k]; | ||
| 178 | my $cui_2 = $cuis[$k+1]; | ||
| 179 | |||
| 180 | $incrementor->($cui_1, $cui_2, \%prior); | ||
| 181 | } | ||
| 182 | |||
| 183 | # Count the kth CUI with the first of each of the next phrases maps | ||
| 184 | foreach my $map_str_p2 ( @phrase_2 ) { | ||
| 185 | (my $first_of_next_phrase) = $map_str_p2 =~ /(C\d{7})/; | ||
| 186 | |||
| 187 | $incrementor->($cuis[-1], $first_of_next_phrase, \%prior); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | ############################################################################### | ||
| 194 | sub build_utterance_graph { | ||
| 195 | my $phrases_ref = shift; | ||
| 196 | my @phrases = @$phrases_ref; | ||
| 197 | |||
| 198 | # Create utterance graph, u. Counted vertices enabled so same CUI can exist | ||
| 199 | # 2+ in graph with different descendants | ||
| 200 | my $u = Graph::Directed->new(); | ||
| 201 | |||
| 202 | my $cui_index = 0; # Uniquely identifies each CUI in the graph. | ||
| 203 | # Needed to correctly find descendants for repeated | ||
| 204 | # CUIs. | ||
| 205 | # Suffixed to each CUI before adding to graph. | ||
| 206 | # So, for 42nd CUI in the graph, C0123456 | ||
| 207 | # 'C0123456' -> 'C0123456_42' | ||
| 208 | |||
| 209 | # Create a phrase graph for each phrase, then add it to the utterance graph | ||
| 210 | my @phrase_graphs; | ||
| 211 | for my $phrase_ref (@phrases) { | ||
| 212 | |||
| 213 | # Initialize empty phrase graph | ||
| 214 | my $p = Graph::Directed->new(); | ||
| 215 | |||
| 216 | # Iterate over mappings for this phrase to contruct the phrase graph | ||
| 217 | for my $cui_string (@{ $phrase_ref }) { | ||
| 218 | |||
| 219 | my @cuis = split / /, $cui_string; | ||
| 220 | |||
| 221 | # tag each cui with an index ID | ||
| 222 | @cuis = map { join ':', $_, $cui_index++ } @cuis; | ||
| 223 | |||
| 224 | # Add cuis for each mapping to the phrase graph | ||
| 225 | $p->add_vertex( $_ ) for (@cuis); | ||
| 226 | for (my $i = 0; $i < $#cuis; $i++) { | ||
| 227 | $p->add_edge($cuis[$i], $cuis[$i+1]); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | # Store the phrase graph | ||
| 232 | push @phrase_graphs, $p; | ||
| 233 | } | ||
| 234 | $u->add_edges($_->edges) for @phrase_graphs; | ||
| 235 | |||
| 236 | # "Stitch" phrase graphs together in utterance graph by connecting | ||
| 237 | # all CUIs in phraseA without descendants to all CUIs in phraseB without | ||
| 238 | # parents. $pg_i is ith phrase graph in list. | ||
| 239 | for (my $i = 0; $i < $#phrase_graphs; $i++) { | ||
| 240 | |||
| 241 | # Find leaves (no children) in phraseA and roots(no parents) in phraseB | ||
| 242 | my @leaves = grep { | ||
| 243 | $phrase_graphs[$i]->edges_from($_) == 0; | ||
| 244 | } $phrase_graphs[$i]->vertices; | ||
| 245 | my @roots = grep { | ||
| 246 | $phrase_graphs[$i+1]->edges_to($_) == 0; | ||
| 247 | } $phrase_graphs[$i+1]->vertices; | ||
| 248 | |||
| 249 | # Connect the leaves of phraseA to the roots of phrase B | ||
| 250 | for my $leaf (@leaves) { | ||
| 251 | for my $root (@roots) { | ||
| 252 | $u->add_edge($leaf, $root); | ||
| 253 | } | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | |||
| 258 | return $u; | ||
| 259 | } | ||
| 260 | |||
| 261 | ############################################################################### | ||
| 262 | |||
| 263 | # get_bigrams( $utterance_graph_ref, $window_size ) | ||
| 264 | # returns list of bigram pairs as a reference to an array of tuples | ||
| 265 | sub get_all_bigrams { | ||
| 266 | my($u, $window) = @_; | ||
| 267 | |||
| 268 | my @bigrams; | ||
| 269 | |||
| 270 | |||
| 271 | |||
| 272 | return \@bigrams; | ||
| 273 | } | ||
| 274 | |||
| 275 | ############################################################################### | ||
| 276 | sub get_bigrams_of { | ||
| 277 | my($u, $cui, $window) = @_; | ||
| 278 | |||
| 279 | my %bigrams; # use hash as a set of unique values | ||
| 280 | |||
| 281 | # Closure to recursively traverse the utterance graph for bigrams | ||
| 282 | my $get_tokens_at_level; # need to declare separately for recursion | ||
| 283 | $get_tokens_at_level = sub { | ||
| 284 | my ($token, $level) = @_; | ||
| 285 | |||
| 286 | print $token . "\n"; | ||
| 287 | |||
| 288 | # Remove the index tag from the CUI's vertex label | ||
| 289 | $token =~ s/:\d+$//; | ||
| 290 | |||
| 291 | print $token . "\n"; | ||
| 292 | |||
| 293 | # Add the token to the list of bigrams for the CUI | ||
| 294 | $bigrams{$token} = 1; | ||
| 295 | |||
| 296 | # Stop if at the end of the window | ||
| 297 | return if $level == 0; | ||
| 298 | |||
| 299 | # Otherwise, get the next batch of succesor tokens | ||
| 300 | $get_tokens_at_level->($_, $level-1) for ($u->successors($token)); | ||
| 301 | }; | ||
| 302 | |||
| 303 | # Get all the bigrams within window for the target CUI. | ||
| 304 | $get_tokens_at_level->($cui, $window); | ||
| 305 | |||
| 306 | # Remove the target CUI from its own bigram list. | ||
| 307 | # TODO: tweak the recusive function to remove the need for this line | ||
| 308 | delete $bigrams{$cui}; | ||
| 309 | |||
| 310 | # Return a list of CUIs which form bigram pairs for the target CUI | ||
| 311 | return keys %bigrams; | ||
| 312 | } | ||
| 313 | |||
| 314 | ############################################################################### | ||
| 315 | |||
| 316 | 1; # End of Module |