File Coverage

blib/lib/UMLS/Association/CuiFinder.pm
Criterion Covered Total %
statement 58 197 29.4
branch 11 84 13.1
condition 3 33 9.0
subroutine 10 18 55.5
pod 0 1 0.0
total 82 333 24.6


line stmt bran cond sub pod time code
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     1   3 use Fcntl;
  1         1  
  1         149  
33 1     1   4 use strict;
  1         1  
  1         13  
34 1     1   3 use warnings;
  1         1  
  1         18  
35 1     1   2 use DBI;
  1         1  
  1         26  
36 1     1   3 use bytes;
  1         0  
  1         4  
37 1     1   13 use File::Spec;
  1         1  
  1         1151  
38              
39             # error handling variables
40             my $errorhandler = "";
41             my $pkg = "UMLS::Association::CuiFinder";
42              
43             # debug variables
44             local(*DEBUG_FILE);
45              
46             # global variables
47             my $debug = 0;
48             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     1 0 3 my $self = {};
61 1         2 my $className = shift;
62 1         1 my $params = shift;
63              
64             # bless the object.
65 1         2 bless($self, $className);
66              
67             # initialize error handler
68 1         4 $errorhandler = UMLS::Association::ErrorHandler->new();
69 1 50       4 if(! defined $errorhandler) {
70 0         0 print STDERR "The error handler did not get passed properly.\n";
71 0         0 exit;
72             }
73              
74             # initialize global variables
75 1         9 $debug = 0;
76              
77             # initialize the object.
78 1         4 $self->_initialize($params);
79              
80 0         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     1   2 my $self = shift;
89 1         2 my $params = shift;
90              
91 1         2 my $function = "_initialize";
92 1         4 &_debug($function);
93              
94             # check self
95 1 50 33     10 if(!defined $self || !ref $self) {
96 0         0 $errorhandler->_error($pkg, $function, "", 2);
97             }
98              
99 1 50       3 $params = {} if(!defined $params);
100              
101             # to set and store the database object
102 1         3 $self->_setDatabase($params);
103              
104             # set up the options
105 0         0 $self->_setOptions($params);
106              
107             # check that all of the tables required exist in the db
108 0         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     1   2 my $self = shift;
118 1         2 my $params = shift;
119              
120 1         1 my $function = "_setDatabase";
121 1         3 &_debug($function);
122              
123             # check self
124 1 50 33     6 if(!defined $self || !ref $self) {
125 0         0 $errorhandler->_error($pkg, $function, "", 2);
126             }
127              
128             # check the params
129 1 50       5 $params = {} if(!defined $params);
130              
131             # get the database connection parameters
132 1         3 my $database = $params->{'database'};
133 1         2 my $hostname = $params->{'hostname'};
134 1         2 my $socket = $params->{'socket'};
135 1         2 my $port = $params->{'port'};
136 1         2 my $username = $params->{'username'};
137 1         2 my $password = $params->{'password'};
138              
139             # set up defaults if the options were not passed
140 1 50       4 if(! defined $database) { $database = "cuicounts"; }
  1         2  
141 1 50       3 if(! defined $socket) { $socket = "/var/run/mysqld/mysqld.sock"; }
  1         2  
142 1 50       3 if(! defined $hostname) { $hostname = "localhost"; }
  1         2  
143              
144             # initialize the database handler
145 1         2 my $db = "";
146              
147             # create the database object...
148 1 50 33     5 if(defined $username and defined $password) {
149 0 0       0 if($debug) { print STDERR "Connecting with username and password\n"; }
  0         0  
150 0         0 $db = DBI->connect("DBI:mysql:database=$database;mysql_socket=$socket;host=$hostname",$username, $password, {RaiseError => 0});
151             }
152             else {
153 1 50       3 if($debug) { print STDERR "Connecting using the my.cnf file\n"; }
  0         0  
154 1         37 my $dsn = "DBI:mysql:umls;mysql_read_default_group=client;database=$database";
155 1         8 $db = DBI->connect($dsn);
156             }
157              
158             # check if there is an error
159 0         0 $errorhandler->_checkDbError($pkg, $function, $db);
160              
161             # check that the db exists
162 0 0       0 if(!$db) { $errorhandler->_error($pkg, $function, "Error with db.", 3); }
  0         0  
163              
164             # set database parameters
165 0         0 $db->{'mysql_enable_utf8'} = 1;
166 0         0 $db->do('SET NAMES utf8');
167 0         0 $db->{mysql_auto_reconnect} = 1;
168              
169             # set the self parameters
170 0         0 $self->{'db'} = $db;
171 0         0 $self->{'username'} = $username;
172 0         0 $self->{'password'} = $password;
173 0         0 $self->{'hostname'} = $hostname;
174 0         0 $self->{'socket'} = $socket;
175 0         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     0   0 my $self = shift;
185 0         0 my $table = shift;
186              
187 0         0 my $function = "_checkTableExists";
188 0         0 &_debug($function);
189              
190             # check self
191 0 0 0     0 if(!defined $self || !ref $self) {
192 0         0 $errorhandler->_error($pkg, $function, "", 2);
193             }
194              
195 0 0       0 if(!defined $table) {
196 0         0 $errorhandler->_error($pkg, $function, "Error with input variable \$table.", 4);
197             }
198              
199             # check that the database exists
200 0         0 my $sdb = $self->{'sdb'};
201 0 0       0 if(!$sdb) { $errorhandler->_error($pkg, $function, "Error with sdb.", 3); }
  0         0  
202              
203             # set an execute the query to show all of the tables
204 0         0 my $sth = $sdb->prepare("show tables");
205 0         0 $sth->execute();
206 0         0 $errorhandler->_checkDbError($pkg, $function, $sth);
207              
208 0         0 my $t = "";
209 0         0 my %tables = ();
210 0         0 while(($t) = $sth->fetchrow()) {
211 0         0 $tables{lc($t)} = 1;
212             }
213 0         0 $sth->finish();
214              
215 0 0       0 if(! (exists$tables{lc($table)})) { return 0; }
  0         0  
216 0         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     0   0 my $self = shift;
225              
226 0         0 my $function = "_getDB";
227 0         0 &_debug($function);
228              
229             # check self
230 0 0 0     0 if(!defined $self || !ref $self) {
231 0         0 $errorhandler->_error($pkg, $function, "", 2);
232             }
233              
234             # get the databawse
235 0         0 my $db = $self->{'db'};
236 0 0       0 if(!$db) { $errorhandler->_error($pkg, $function, "Error with db.", 3); }
  0         0  
237              
238             # return the database
239 0         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     0   0 my $self = shift;
248              
249 0         0 my $function = "_checkTablesExist";
250 0         0 &_debug($function);
251              
252             # check self
253 0 0 0     0 if(!defined $self || !ref $self) {
254 0         0 $errorhandler->_error($pkg, $function, "", 2);
255             }
256              
257             # set up the database
258 0         0 my $db = $self->{'db'};
259 0 0       0 if(!$db) { $errorhandler->_error($pkg, $function, "Error with db.", 3); }
  0         0  
260              
261             # check if the tables exist...
262 0         0 my $sth = $db->prepare("show tables");
263 0         0 $sth->execute();
264 0         0 $errorhandler->_checkDbError($pkg, $function, $sth);
265              
266 0         0 my $table = "";
267 0         0 my %tables = ();
268 0         0 while(($table) = $sth->fetchrow()) {
269 0         0 $tables{$table} = 1;
270             }
271 0         0 $sth->finish();
272              
273 0 0       0 if(!defined $tables{"N_11"}) {
274 0         0 $errorhandler->_error($pkg, $function, "Table N_11 not found in database", 7);
275             }
276 0 0       0 if(!defined $tables{"N_P1"}) {
277 0         0 $errorhandler->_error($pkg, $function, "Table N_P1 not found in database", 7);
278             }
279 0 0       0 if(!defined $tables{"N_1P"}) {
280 0         0 $errorhandler->_error($pkg, $function, "Table N_1P not found in database", 7);
281             }
282 0 0       0 if(!defined $tables{"N_PP"}) {
283 0         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     0   0 my $self = shift;
292 0         0 my $params = shift;
293              
294 0         0 my $function = "_setOptions";
295 0         0 &_debug($function);
296              
297             # check self
298 0 0 0     0 if(!defined $self || !ref $self) {
299 0         0 $errorhandler->_error($pkg, $function, "", 2);
300             }
301              
302             # check the params
303 0 0       0 $params = {} if(!defined $params);
304              
305             # get all the parameters
306 0         0 my $t = $params->{'t'};
307 0         0 my $debugoption = $params->{'debug'};
308            
309 0 0       0 if(defined $t) {
310 0         0 $option_t = 1;
311             }
312            
313 0 0       0 if(defined $debugoption) {
314 0         0 $debug = $debugoption;
315             }
316             }
317              
318              
319              
320             # method to destroy the created object.
321             # input :
322             # output:
323             sub _disconnect {
324 0     0   0 my $self = shift;
325              
326 0         0 my $function = "_disconnect";
327              
328             # check self
329 0 0 0     0 if(!defined $self || !ref $self) {
330 0         0 $errorhandler->_error($pkg, $function, "", 2);
331             }
332              
333 0 0       0 if($self) {
334 0         0 my $db = $self->{'db'};
335 0 0       0 $db->disconnect() if($db);
336             }
337             }
338              
339             sub _debug {
340 2     2   4 my $function = shift;
341 2 50       6 if($debug) { print STDERR "In UMLS::Association::CuiFinder::$function\n"; }
  0            
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     0     my $self = shift;
350 0           my $concept = shift;
351              
352 0           my $function = "_exists";
353              
354             # check self
355 0 0 0       if(!defined $self || !ref $self) {
356 0           $errorhandler->_error($pkg, $function, "", 2);
357             }
358              
359             # check parameter exists
360 0 0         if(!defined $concept) {
361 0           $errorhandler->_error($pkg, $function, "Error with input variable \$concept.", 4);
362             }
363              
364             # check if valid concept
365 0 0         if(! ($errorhandler->_validCui($concept)) ) {
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});
  0            
379              
380 0 0         return 1 if($count); return 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     0     my $self = shift;
389 0           my $concept = shift;
390            
391 0           my $function = "_getParents";
392            
393             # check self
394 0 0 0       if(!defined $self || !ref $self) {
395 0           $errorhandler->_error($pkg, $function, "", 2);
396             }
397            
398             # check parameter exists
399 0 0         if(!defined $concept) {
400 0           $errorhandler->_error($pkg, $function, "Error with input variable \$concept.", 4);
401             }
402            
403             # check if valid concept
404 0 0         if(! ($errorhandler->_validCui($concept)) ) {
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     0     my $self = shift;
422 0           my $concept = shift;
423            
424 0           my $function = "_getChildren";
425            
426             # check self
427 0 0 0       if(!defined $self || !ref $self) {
428 0           $errorhandler->_error($pkg, $function, "", 2);
429             }
430            
431             # check parameter exists
432 0 0         if(!defined $concept) {
433 0           $errorhandler->_error($pkg, $function, "Error with input variable \$concept.", 4);
434             }
435            
436             # check if valid concept
437 0 0         if(! ($errorhandler->_validCui($concept)) ) {
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;
450              
451             __END__