lib/SemMed/Interface/DataAccess.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 136 SCAR 47.0
CC
10
Cov
0%
getPredicateConnections
90.0
new
6.0
getCUI
2.0
CRAP 110.0
1 #!/usr/bin/perl
2 #
3 # @File DataAccess.pm
4 # @Author andriy
5 # @Created Jun 27, 2016 3:27:32 PM
6 #
7  
8 package DataAccess;
9 use DBI;
10 use UMLS::Association;
11  
12  
13  
14  
15 my $errorhandling = "";
16 my $association = "";
17 my $dbh = "";
18  
19 # method to create a new SemRep::Interface object
20 # input : $SemMedLoginParams <- reference to hash containing SemMed login parameter
21 # $AssociationLoginParams <- reference to hash containing the UMLS::Association login parameters
22 # output:
23 sub new {
24 $class = shift;
25 my $self = {};
26 my $SemMedLoginParams = shift; #hash containing the SemMed login parameters
27 my $AssociationLoginParams = shift; #hash containing the UMLS::Association login parameters
28  
29 my $database = $SemMedLoginParams->{'database'};
30 my $hostname = $SemMedLoginParams->{'hostname'};
31 my $port = $SemMedLoginParams->{'port'};
32 my $userid = $SemMedLoginParams->{'username'};
33 my $password = $SemMedLoginParams->{'password'};
34  
35 my $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
36  
37 if($association){
38 $association = new UMLS::Association($AssociationLoginParams);
39 $errorhandling = UMLS::Association::ErrorHandler->new();
40 }
41  
42  
43  
44 $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;
45 $dbh->{InactiveDestroy} = 1; #allows forking of threads containing this DB connect
46  
47 bless $self, $class;
48 return $self;
49 }
50  
51  
52 #given a CUI, this method will return all Predicate-CUI connections leading from the given CUI
53  
54 #INPUT: SOURCE_CUI_OBJECT, WEIGHT_STATISTICAL_MEASURE(string) or 1
55 #OPTIONAL INPUT: , List of predicates to only include, List of Predicates to Ignore
56  
57 #OUTPUT: ARRAY OF PREDICATE objects whos source is SOURCE_CUI_OBJECT and destination is what was matched in DB.
58 sub getPredicateConnections {
59  
60 my $self = shift;
61  
62 my $cui = shift;
63 my $measure = shift;
64 my $includedPredicates = shift;
65 my $excludedPredicates = shift;
66 unless($measure){ #default to tscore
67 $measure = "tscore";
68 }
69  
70 my $cuiid = $cui->getId();
71  
72 my $queryString =
73 "SELECT s_cui, s_name, o_cui, o_name, predicate, $measure FROM SemMedDB.DISTINCT_PREDICATION_AGGREGATE
74 WHERE s_cui = '$cuiid'";
75  
76  
77 ### Will add in query parameters for removing certain predicate types
78 if($includedPredicates){
79 my $perm = " AND (";
80 foreach $pred ( @$includedPredicates){
81 $perm .= "predicate = '$pred' OR ";
82 }
83 substr($perm, -4) = ""; #remove extra OR
84 $perm .= ")";
85 $queryString .= $perm;
86 }
87  
88 if($excludedPredicates){
89 my $perm = " AND NOT (";
90 foreach $pred ( @$excludedPredicates){
91 $perm .= "predicate = '$pred' OR ";
92 }
93 substr($perm, -4) = "";
94 $perm .= ")";
95 $queryString .= $perm;
96 }
97  
98 #print $queryString."\n";
99  
100  
101  
102 my $sth = $dbh->prepare($queryString);
103 $sth->execute() or die $DBI::errstr;
104 my @edges = ();
105  
106  
107 while (my @row = $sth->fetchrow_array()) {
108 my $source = new CUI($row[0], $row[1]); #create source vertex
109 my $destination = new CUI($row[2], $row[3]); #create dest vertext
110  
111 my $weight;
112  
113 my $validConcepts = $errorhandling->_validCui($row[0]) && $errorhandling->_validCui($row[2]);
114  
115 if($measure == 1){ #no stats needed, we are just running a BFS
116 $weight = 1;
117 }else{
118  
119 #if value is cached in db, use it
120 if($row[5]){
121 $weight = $row[5];
122  
123 }else{
124 #obtain value from UMLS, then cache it
125 if($validConcepts){
126 # print "calculating stats $row[1] $row[3] \n";
127 $weight = $association->calculateStatistic($source->getId(), $destination->getId(), $measure);
128 }else{
129 $weight = -1;
130 }
131  
132  
133 my $updateString = "UPDATE SemMedDB.DISTINCT_PREDICATION_AGGREGATE SET $measure = $weight WHERE s_cui = '$row[0]' AND predicate = '$row[4]' AND o_cui = '$row[2]' LIMIT 1;";
134 my $update = $dbh->prepare($updateString);
135 $update->execute() or die $DBI::errstr;
136 }
137 #we need to calculate an edge weight, use the measure given to do so from UMLS::Association package
138  
139  
140  
141 }
142  
143 if($weight == -1){next;}#if no information was found, ignore this edge
144  
145 #The weight is now a statistic, a value further from 0 will indicate that the two words are highly associated
146 #Take the multiplicative reciprical of this so that highly associated words will correspond to lower edge weights
147 if($weight){ #make sure its not zero
148 $weight = abs(1/$weight);
149 }
150  
151  
152 my $predicate = new Predicate($source, $row[4], $destination, $weight); #create edge
153 push @edges, $predicate; #push to main array holding all edges
154 }
155 $sth->finish();
156  
157 return @edges;
158 }
159  
160 #
161 #OBTAIN CUI FROM CUI ID
162 #INPUT: CUI | PREFERRED NAME
163 #OUTPUT: CUI_OBJECT with fields complete.
164 sub getCUI{
165 my($self, $cui) = @_;
166 my $sth = $dbh->prepare("SELECT CUI, PREFERRED_NAME FROM CONCEPT
167 WHERE CUI = '$cui' OR PREFERRED_NAME = '$cui'
168 LIMIT 1");
169 $sth->execute() or die $DBI::errstr;
170 my @row = $sth->fetchrow_array();
171 return new CUI($row[0], $row[1]);
172 }
173  
174  
175  
176  
177 sub getConnections {
178 my $self = shift;
179 my $concept = shift;
180 my $sth = $dbh->prepare("SELECT predicate, o_cui FROM DISTINCT_PREDICATION_AGGREGATE WHERE s_cui = '$concept'");
181 $sth->execute() or die $DBI::errstr;
182 @rows = $sth->fetchrow_array();
183 return \@rows;
184 }
185  
186  
187 #
188 # OBTAIN SEMTYPE FROM CUI ID
189 #INPUT: CUI | PREFERRED NAME
190 #OUTPUT: Semantic Type Associated with that CUI or Term.
191 sub getSemtype {
192 my($self, $cui) = @_;
193 my $sth = $dbh->prepare(
194 "SELECT CS.SEMTYPE FROM CONCEPT C
195 JOIN CONCEPT_SEMTYPE CS ON (C.CONCEPT_ID = CS.CONCEPT_ID)
196 WHERE C.CUI = '$cui' OR C.PREFERRED_NAME LIKE '$cui'
197 LIMIT 1"
198 );
199 $sth->execute() or die $DBI::errstr;
200 return ($sth->fetchrow_array())[0];
201  
202 }
203 1;