lib/UMLS/Association/CuiFinder.pm

filter: Statementstmt 31.5% 64 / 203 Branchbran 12.5% 11 / 88 Conditioncond 9.0% 3 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 55.5% 10 / 18 Podpod 0.0% 0 / 1 totaltotal 24.1% 88 / 365 SCAR 72.6
CC
56
Cov
24%
_checkTablesExist
90.0
_checkTableExists
56.0
_setOptions
42.0
CRAP 1428.6
« lib/UMLS/Association/StatFinder.pm ↑ Summary script/CUICollector.pl »
1 # UMLS::Association::CuiFinder
2 #
3 # Perl module that provides a perl interface to the
4 # semantic network extracted from the MetaMapped Medline Baseline
5 #
6 # This program borrows heavily from the UMLS::Interface package.x
7 #
8 # Copyright (c) 2015,
9 #
10 # Bridget T. McInnes, Virginia Commonwealth University
11 # btmcinnes at vcu.edu
12 #
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to
25 #
26 # The Free Software Foundation, Inc.,
27 # 59 Temple Place - Suite 330,
28 # Boston, MA 02111-1307, USA.
29  
30 package UMLS::Association::CuiFinder;
31  
32 1 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # error handling variables
40 1 my $errorhandler = "";
41 1 my $pkg = "UMLS::Association::CuiFinder";
42  
43 # debug variables
44 1 local(*DEBUG_FILE);
45  
46 # global variables
47 1 my $debug = 0;
48 1 my $option_t;
49  
50  
51 ######################################################################
52 # functions to initialize the package
53 ######################################################################
54  
55 # method to create a new UMLS::Association::CuiFinder object
56 # input : $parameters <- reference to a hash of parameters
57 # output: $self
58 sub new {
59  
60 1 ▶ my $self = {};
Subroutine new: called CC=2 SCAR=7.5
Pod undocumented
61 1 my $className = shift;
62 1 my $params = shift;
63  
64 # bless the object.
65 1 bless($self, $className);
66  
67 # initialize error handler
68 1 $errorhandler = UMLS::Association::ErrorHandler->new();
69 1 ▶ if(! defined $errorhandler) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $errorhandler) 0 1 1
70 0 print STDERR "The error handler did not get passed properly.\n";
71 0 exit;
72 }
73  
74 # initialize global variables
75 1 $debug = 0;
76  
77 # initialize the object.
78 1 $self->_initialize($params);
79  
80 0 return $self;
81 }
82  
83 # method to initialize the UMLS::Association::CuiFinder object.
84 # input : $parameters <- reference to a hash
85 # output:
86 sub _initialize {
87  
88 1 ▶ my $self = shift;
Subroutine _initialize: called CC=4 SCAR=16.3
89 1 my $params = shift;
90  
91 1 my $function = "_initialize";
92 1 &_debug($function);
93  
94 # check self
95 1 ▶ if(!defined $self || !ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and ref $self) 0 1 1
Condition33%1/3
defined $self and ref $self
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table33%1/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
96 0 $errorhandler->_error($pkg, $function, "", 2);
97 }
98  
99 1 ▶ $params = {} if(!defined $params);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params 0 1 1
100  
101 # to set and store the database object
102 1 $self->_setDatabase($params);
103  
104 # set up the options
105 0 $self->_setOptions($params);
106  
107 # check that all of the tables required exist in the db
108 0 $self->_checkTablesExist();
109  
110 }
111
112 # method to set the association database
113 # input : $params <- reference to a hash
114 # output:
115 sub _setDatabase {
116  
117 1 ▶ my $self = shift;
Subroutine _setDatabase: called CC=12 SCAR=34.0
118 1 my $params = shift;
119  
120 1 my $function = "_setDatabase";
121 1 &_debug($function);
122  
123 # check self
124 1 ▶ if(!defined $self || !ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and ref $self) 0 1 1
Condition33%1/3
defined $self and ref $self
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table33%1/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
125 0 $errorhandler->_error($pkg, $function, "", 2);
126 }
127  
128 # check the params
129 1 ▶ $params = {} if(!defined $params);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params 0 1 1
130  
131 # get the database connection parameters
132 1 my $database = $params->{'database'};
133 1 my $hostname = $params->{'hostname'};
134 1 my $socket = $params->{'socket'};
135 1 my $port = $params->{'port'};
136 1 my $username = $params->{'username'};
137 1 my $password = $params->{'password'};
138  
139 # set up defaults if the options were not passed
140 1 ▶ if(! defined $database) { $database = "cuicounts"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $database) 1 0 1
141 1 ▶ if(! defined $socket) { $socket = "/var/run/mysqld/mysqld.sock"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket) 1 0 1
142 1 ▶ if(! defined $hostname) { $hostname = "localhost"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hostname) 1 0 1
143  
144 # initialize the database handler
145 1 my $db = "";
146  
147 # create the database object...
148 1 ▶ if(defined $username and defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $username and defined $password) { } 0 1 1
Condition33%1/3
defined $username and defined $password
!ll&&!rl&&r
100
MC/DC0%0/2
defined $username and defined $password
defined $username defined $password
Truth table33%1/3
A and B
A= defined $usernameB= defined $password
ABresult
0X0
100
111
149 0 ▶ if($debug) { print STDERR "Connecting with username and password\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
150 0 $db = DBI->connect("DBI:mysql:database=$database;mysql_socket=$socket;host=$hostname",$username, $password, {RaiseError => 0});
151 }
152 else {
153 1 ▶ if($debug) { print STDERR "Connecting using the my.cnf file\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 1 1
154 1 my $dsn = "DBI:mysql:umls;mysql_read_default_group=client;database=$database";
155 1 $db = DBI->connect($dsn);
156 }
157  
158 # check if there is an error
159 0 $errorhandler->_checkDbError($pkg, $function, $db);
160  
161 # check that the db exists
162 0 ▶ if(!$db) { $errorhandler->_error($pkg, $function, "Error with db.", 3); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($db) 0 0 0
163  
164 # set database parameters
165 0 $db->{'mysql_enable_utf8'} = 1;
166 0 $db->do('SET NAMES utf8');
167 0 $db->{mysql_auto_reconnect} = 1;
168  
169 # set the self parameters
170 0 $self->{'db'} = $db;
171 0 $self->{'username'} = $username;
172 0 $self->{'password'} = $password;
173 0 $self->{'hostname'} = $hostname;
174 0 $self->{'socket'} = $socket;
175 0 $self->{'database'} = $database;
176  
177 }
178  
179 # function checks to see if a given table exists
180 # input : $table <- string
181 # output: 0 | 1 <- integers
182 sub _checkTableExists {
183  
184 0 ▶ my $self = shift;
Subroutine _checkTableExists: not called CC=7 SCAR=40.3
185 0 my $table = shift;
186  
187 0 my $function = "_checkTableExists";
188 0 &_debug($function);
189  
190 # check self
191 0 ▶ if(!defined $self || !ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and ref $self) 0 0 0
Condition0%0/3
defined $self and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table0%0/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
192 0 $errorhandler->_error($pkg, $function, "", 2);
193 }
194  
195 0 ▶ if(!defined $table) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $table) 0 0 0
196 0 $errorhandler->_error($pkg, $function, "Error with input variable \$table.", 4);
197 }
198  
199 # check that the database exists
200 0 my $sdb = $self->{'sdb'};
201 0 ▶ if(!$sdb) { $errorhandler->_error($pkg, $function, "Error with sdb.", 3); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($sdb) 0 0 0
202  
203 # set an execute the query to show all of the tables
204 0 my $sth = $sdb->prepare("show tables");
205 0 $sth->execute();
206 0 $errorhandler->_checkDbError($pkg, $function, $sth);
207  
208 0 my $t = "";
209 0 my %tables = ();
210 0 ▶ while(($t) = $sth->fetchrow()) {
Branch: 1 branch
BranchTrueFalseTotal
if (($t) = $sth->fetchrow) 0 0 0
211 0 $tables{lc($t)} = 1;
212 }
213 0 $sth->finish();
214  
215 0 ▶ if(! (exists$tables{lc($table)})) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $tables{lc $table}) { } 0 0 0
216 0 else { return 1; }
217  
218 }
219  
220 # return the database connection to the bigram database
221 # input :
222 # output: $db <- database handler
223 sub _getDB {
224 0 ▶ my $self = shift;
Subroutine _getDB: not called CC=4 SCAR=30.0
225  
226 0 my $function = "_getDB";
227 0 &_debug($function);
228  
229 # check self
230 0 ▶ if(!defined $self || !ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and ref $self) 0 0 0
Condition0%0/3
defined $self and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table0%0/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
231 0 $errorhandler->_error($pkg, $function, "", 2);
232 }
233  
234 # get the databawse
235 0 my $db = $self->{'db'};
236 0 ▶ if(!$db) { $errorhandler->_error($pkg, $function, "Error with db.", 3); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($db) 0 0 0
237  
238 # return the database
239 0 return $db;
240 }
241  
242 # check if the bigram score tables required all exist
243 # input :
244 # output:
245 sub _checkTablesExist {
246  
247 0 ▶ my $self = shift;
Subroutine _checkTablesExist: not called CC=9 SCAR=45.0
248  
249 0 my $function = "_checkTablesExist";
250 0 &_debug($function);
251  
252 # check self
253 0 ▶ if(!defined $self || !ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and ref $self) 0 0 0
Condition0%0/3
defined $self and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table0%0/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
254 0 $errorhandler->_error($pkg, $function, "", 2);
255 }
256  
257 # set up the database
258 0 my $db = $self->{'db'};
259 0 ▶ if(!$db) { $errorhandler->_error($pkg, $function, "Error with db.", 3); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($db) 0 0 0
260  
261 # check if the tables exist...
262 0 my $sth = $db->prepare("show tables");
263 0 $sth->execute();
264 0 $errorhandler->_checkDbError($pkg, $function, $sth);
265  
266 0 my $table = "";
267 0 my %tables = ();
268 0 ▶ while(($table) = $sth->fetchrow()) {
Branch: 1 branch
BranchTrueFalseTotal
if (($table) = $sth->fetchrow) 0 0 0
269 0 $tables{$table} = 1;
270 }
271 0 $sth->finish();
272  
273 0 ▶ if(!defined $tables{"N_11"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tables{"N_11"}) 0 0 0
274 0 $errorhandler->_error($pkg, $function, "Table N_11 not found in database", 7);
275 }
276 0 ▶ if(!defined $tables{"N_P1"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tables{"N_P1"}) 0 0 0
277 0 $errorhandler->_error($pkg, $function, "Table N_P1 not found in database", 7);
278 }
279 0 ▶ if(!defined $tables{"N_1P"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tables{"N_1P"}) 0 0 0
280 0 $errorhandler->_error($pkg, $function, "Table N_1P not found in database", 7);
281 }
282 0 ▶ if(!defined $tables{"N_PP"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $tables{"N_PP"}) 0 0 0
283 0 $errorhandler->_error($pkg, $function, "Table N_PP not found in database", 7);
284 }
285 }
286  
287 # method to set the global parameter options
288 # input : $params <- reference to a hash
289 # output:
290 sub _setOptions {
291 0 ▶ my $self = shift;
Subroutine _setOptions: not called CC=6 SCAR=37.4
292 0 my $params = shift;
293  
294 0 my $function = "_setOptions";
295 0 &_debug($function);
296  
297 # check self
298 0 ▶ if(!defined $self || !ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and ref $self) 0 0 0
Condition0%0/3
defined $self and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table0%0/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
299 0 $errorhandler->_error($pkg, $function, "", 2);
300 }
301  
302 # check the params
303 0 ▶ $params = {} if(!defined $params);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params 0 0 0
304  
305 # get all the parameters
306 0 my $t = $params->{'t'};
307 0 my $debugoption = $params->{'debug'};
308
309 0 ▶ if(defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t) 0 0 0
310 0 $option_t = 1;
311 }
312
313 0 ▶ if(defined $debugoption) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $debugoption) 0 0 0
314 0 $debug = $debugoption;
315 }
316 }
317  
318  
319  
320 # method to destroy the created object.
321 # input :
322 # output:
323 sub _disconnect {
324 0 ▶ my $self = shift;
Subroutine _disconnect: not called CC=5 SCAR=34.0
325  
326 0 my $function = "_disconnect";
327  
328 # check self
329 0 ▶ if(!defined $self || !ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and ref $self) 0 0 0
Condition0%0/3
defined $self and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table0%0/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
330 0 $errorhandler->_error($pkg, $function, "", 2);
331 }
332  
333 0 ▶ if($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 0 0 0
334 0 my $db = $self->{'db'};
335 0 ▶ $db->disconnect() if($db);
Branch: 1 branch
BranchTrueFalseTotal
if $db 0 0 0
336 }
337 }
338  
339 sub _debug {
340 2 ▶ my $function = shift;
Subroutine _debug: called CC=2 SCAR=8.1
341 2 ▶ if($debug) { print STDERR "In UMLS::Association::CuiFinder::$function\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 2 2
342 }
343  
344 # Method to check if a CUI exists in the database.
345 # input : $concept <- string containing a cui
346 # output: $bool <- string indicating if the cui exists
347 sub _exists {
348  
349 0 ▶ my $self = shift;
Subroutine _exists: not called CC=6 SCAR=37.4
350 0 my $concept = shift;
351  
352 0 my $function = "_exists";
353  
354 # check self
355 0 ▶ if(!defined $self || !ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and ref $self) 0 0 0
Condition0%0/3
defined $self and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table0%0/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
356 0 $errorhandler->_error($pkg, $function, "", 2);
357 }
358  
359 # check parameter exists
360 0 ▶ if(!defined $concept) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $concept) 0 0 0
361 0 $errorhandler->_error($pkg, $function, "Error with input variable \$concept.", 4);
362 }
363  
364 # check if valid concept
365 0 ▶ if(! ($errorhandler->_validCui($concept)) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($errorhandler->_validCui($concept)) 0 0 0
366 0 $errorhandler->_error($pkg, $function, "Concept ($concept) is not valid.", 6);
367 }
368
369 # set up database
370 0 my $db = $self->_getDB();
371  
372 0 my $arrRef = $db->selectcol_arrayref("select * from N_11 where cui_1='$concept' or cui_2='$concept' LIMIT 1");
373  
374 # check the database for errors
375 0 $errorhandler->_checkDbError($pkg, $function, $db);
376  
377 # get the count
378 0 my $count = scalar(@{$arrRef});
379  
380 0 ▶ return 1 if($count); return 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count 0 0 0
381 }
382
383 # Method to return 'parents' of a CUI
384 # input: $concept <- string containing cui
385 # output: $array <- reference to an array containing a list of cuis
386 sub _getParents {
387  
388 0 ▶ my $self = shift;
Subroutine _getParents: not called CC=5 SCAR=34.0
389 0 my $concept = shift;
390
391 0 my $function = "_getParents";
392
393 # check self
394 0 ▶ if(!defined $self || !ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and ref $self) 0 0 0
Condition0%0/3
defined $self and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table0%0/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
395 0 $errorhandler->_error($pkg, $function, "", 2);
396 }
397
398 # check parameter exists
399 0 ▶ if(!defined $concept) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $concept) 0 0 0
400 0 $errorhandler->_error($pkg, $function, "Error with input variable \$concept.", 4);
401 }
402
403 # check if valid concept
404 0 ▶ if(! ($errorhandler->_validCui($concept)) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($errorhandler->_validCui($concept)) 0 0 0
405 0 $errorhandler->_error($pkg, $function, "Concept ($concept) is not valid.", 6);
406 }
407
408 # connect to the database
409 0 my $db = $self->_getDB();
410
411 0 my $arrRef = $db->selectcol_arrayref("select distinct cui_1 from N_11 where cui_2='$concept'");
412
413 0 return $arrRef;
414 }
415  
416 # Method to return 'children' of a CUI
417 # input: $concept <- string containing cui
418 # output: $array <- reference to an array containing a list of cuis
419 sub _getChildren {
420  
421 0 ▶ my $self = shift;
Subroutine _getChildren: not called CC=5 SCAR=34.0
422 0 my $concept = shift;
423
424 0 my $function = "_getChildren";
425
426 # check self
427 0 ▶ if(!defined $self || !ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and ref $self) 0 0 0
Condition0%0/3
defined $self and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table0%0/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
428 0 $errorhandler->_error($pkg, $function, "", 2);
429 }
430
431 # check parameter exists
432 0 ▶ if(!defined $concept) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $concept) 0 0 0
433 0 $errorhandler->_error($pkg, $function, "Error with input variable \$concept.", 4);
434 }
435
436 # check if valid concept
437 0 ▶ if(! ($errorhandler->_validCui($concept)) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($errorhandler->_validCui($concept)) 0 0 0
438 0 $errorhandler->_error($pkg, $function, "Concept ($concept) is not valid.", 6);
439 }
440
441 # connect to the database
442 0 my $db = $self->_getDB();
443
444 0 my $arrRef = $db->selectcol_arrayref("select distinct cui_2 from N_11 where cui_1='$concept'");
445
446 0 return $arrRef;
447 }
448  
449 1 1;
450  
451 __END__
« lib/UMLS/Association/StatFinder.pm ↑ Summary script/CUICollector.pl »