lib/WebService/UMLSKS/GetNeighbors.pm untested

filter: Statementstmt 0.0% 0 / 172 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 296 CC 39 SCAR 56.8
CC
39
Cov
0%
format_homogeneous_hash
600.0
read_object
72.0
format_object
42.0
CRAP 294.1
1  
2 =head1 NAME
3  
4 WebService::UMLSKS::GetNeighbors - Fetches all the neighbor concepts for the input concept.
5  
6 =head1 SYNOPSIS
7  
8 =head2 Basic Usage
9  
10 use WebService::UMLSKS::GetNeighbors;
11
12 my $Neighbors_ref = call_getconceptproperties($cui);# Neighbors_ref is a hash reference
13 my $read_Neighbors = new GetNeighbors;
14 # $ref is a reference of an array of all Neighbors' CUI for the input cui.
15 my $ref = $read_Neighbors->read_object( $Neighbors_ref );
16
17  
18  
19 =head1 DESCRIPTION
20  
21 This module has package GetParents which has subroutines 'new', 'read_object','extract_object_class',
22 'format_object', 'format_homogeneous_hash', 'format_scalar', format_homogeneous_array.
23  
24  
25 =head1 SUBROUTINES
26  
27 The subroutines are as follows:
28  
29  
30 =cut
31  
32 ###############################################################################
33 ########## CODE STARTS HERE #################################################
34  
35  
36  
37 use SOAP::Lite;
38 use strict;
39 use warnings;
40  
41 no warnings qw/redefine/;
42  
43  
44 package WebService::UMLSKS::GetNeighbors;
45 our $ConceptInfo_ref;
46 use Log::Message::Simple qw[msg error debug];
47 my %ConceptInfo;
48 my %directions;
49 my @parents;
50 my @children;
51 my @siblings;
52 my $indentation;
53 my $verbose = 0;
54 my @attribute = ();
55 my @relationattr = ();
56  
57  
58 #print "\n in format hash";
59 #my %directions = %$WebService::UMLSKS::FormGraph::Directions_ref;
60
61 #$directions{"PAR"} = "U";
62 #$directions{"CHD"} = "D";
63 #$directions{"RB"} = "H";
64 #$directions{"RN"} = "H";
65  
66 =head2 new
67  
68 This sub creates a new object of GetNeighbors.
69  
70 =cut
71  
72 sub new {
73 my $class = shift;
74 my $self = {};
75  
76 #print "in new in display_info";
77 bless( $self, $class );
78 return $self;
79 }
80  
81 =head2 read_object
82  
83 This sub reads hash reference object passed to this
84 sub and fetches the required Neighbors' information.
85  
86 =cut
87  
88 sub read_object {
89  
90 my $self = shift;
91 my $object_refr = shift;
92 my $qterm = shift;
93 my $ver = shift;
94 my $directions_ref = shift;
95 my $attribute_ref = shift;
96
97 $verbose = $ver;
98 %directions = %$directions_ref;
99 # printHash(\%directions);
100  
101  
102 # If the attributes are specified, then set the relations and relation attributes.
103  
104 if(@$attribute_ref){
105
106 foreach my $attr (@$attribute_ref){
107 $attr =~ /(.*?)\s*-\s*(.*)$/;
108 my $rel = $1;
109 my $att = $2;
110 $att =~ s/\s*//g;
111 $rel =~ s/\s*//g;
112
113
114
115 # If this relation is in directions
116 if($rel ~~ %directions){
117 unless($rel ~~ @relationattr){
118 push(@relationattr,$rel);
119 }
120 unless($att ~~ @attribute){
121 push(@attribute,$att);
122 }
123
124 }
125 }
126
127
128 }
129  
130 undef @parents;
131 undef @children;
132 undef @siblings;
133
134 @parents = ();
135 @children = ();
136 @siblings = ();
137
138
139 #msg ("\t relation for which atttributes are specified : @relationattr",$verbose);
140 #msg ("\t attribues are : @attribute",$verbose);
141 my @neighbors = ();
142
143 #my $return_ref =
144 format_object($object_refr);
145 chomp(@parents);
146 chomp(@children);
147 chomp(@siblings);
148  
149 $ConceptInfo_ref = \%ConceptInfo;
150  
151 my $parents_ref = findUnique(\@parents,$qterm);
152 my $children_ref = findUnique(\@children,$qterm);
153 my $siblings_ref = findUnique(\@siblings,$qterm);
154
155 if(defined $parents_ref){
156 #print "\n parents are @unique";
157 push(@neighbors,$parents_ref);
158 }
159 else{
160 #msg( "\n No parents found for $qterm in current Source/s", $verbose);
161 push(@neighbors,"empty");
162 }
163 if(defined $children_ref){
164 #print "\n parents are @unique";
165 push(@neighbors,$children_ref);
166 }
167 else{
168 #msg( "\n No children found for $qterm in current Source/s", $verbose);
169 push(@neighbors,"empty");
170 }
171 if(defined $siblings_ref){
172 #print "\n siblings are @$siblings_ref";
173 push(@neighbors,$siblings_ref);
174 }
175 else{
176 #msg( "\n No siblings found for $qterm in current Source/s", $verbose);
177 push(@neighbors,"empty");
178 }
179
180 undef $object_refr;
181 return \@neighbors;
182
183 }
184  
185  
186 =head2 findUnique
187  
188 This sub finds unique elements in an array.
189  
190 =cut
191  
192 sub findUnique
193 {
194  
195 my $array_ref = shift;
196 my $qterm = shift;
197 my @array = @$array_ref;
198
199 # The following code snippet to delete duplicate elements from an array is referred from
200 # perfaq4 and is modified according to need. For details refer :
201 # http://perldoc.perl.org/perlfaq4.html#How-can-I-remove-duplicate-elements-from-a-list-or-array%3f
202 # The first time the loop sees an element, that element has no key in %Seen .
203 # The next time the loop sees that same element, its key exists in the hash and the value for that key
204 # is true (since it's not 0 or undef), so the skip that iteration and the loop goes
205 # to the next element.
206  
207 my @unique = ();
208 my %seen = ();
209 foreach my $elem (@array) {
210 if ( $seen{$elem}++) {
211  
212 }
213 else {
214 unless($elem eq '1' | $elem eq '0' | $elem eq $qterm){
215
216 push( @unique, $elem );
217
218 }
219
220 }
221 }
222  
223 # Code snippet from perlfaq4 ends here.
224
225 return \@unique;
226
227 }
228  
229 # This sub formats the structures returned by the web service. It calls
230 # the appropriate subroutines depending on the type of structure
231 # it is called with. If the input reference is a hash reference it calls
232 # format_homogenous_hash method. If input is array reference,
233 # it calls format homogenous array and simillarly for scalar input
234 # reference it calls format_scalar.
235  
236 =head2 format_object
237  
238 This sub calls appropriate functions like format_homogenous_hash,
239 format_scalar, format_homogenous_array depending on the object reference it is called with.
240  
241 =cut
242  
243 sub format_object {
244  
245 my $object_ref = shift;
246  
247 #print "in format object";
248  
249 unless ( defined $object_ref ) {
250 return 'undefined';
251 }
252 else {
253 if ( $object_ref =~ /HASH/o ) {
254 return format_homogeneous_hash($object_ref);
255 }
256 elsif ( $object_ref =~ /ARRAY/o ) {
257 return format_homogeneous_array($object_ref);
258 }
259 elsif ( $object_ref =~ /SCALAR/o ) {
260 return format_scalar($object_ref);
261 }
262 elsif ( defined $object_ref ) {
263 return $object_ref;
264 }
265 else {
266 return 'term is not present';
267 }
268 }
269
270 undef $object_ref;
271 }
272  
273  
274 =head2 format_scalar
275  
276 This sub formats scalar object.
277  
278 =cut
279  
280  
281 sub format_scalar {
282 my $scalar_ref = shift;
283 #my $q = shift;
284
285 #print "\n In format scalar";
286 #print "\n scalar_ref is $$scalar_ref";
287 format_object($$scalar_ref);
288
289 }
290  
291  
292  
293 =head2 format_homogeneous_hash
294  
295 This sub formats hash.
296  
297 =cut
298  
299 sub format_homogeneous_hash {
300
301 my $hash_ref = shift;
302 my $flag = 0;
303 my $flag2 = 0;
304 my $t_flag = 0;
305 my $c_flag = 0;
306 my $current_term;
307 my $current_cui;
308 my $q_cui;
309 my $q_term;
310 my $relation;
311 my $roflag = 0;
312 my $relflag = 0;
313 my $accept_rela = 1;
314
315
316 foreach my $att (keys %$hash_ref) {
317
318 if ( $att =~ /\brel\b/) {
319 if(defined $hash_ref->{$att})
320 {
321 if($hash_ref->{$att} ~~ %directions){
322 if($hash_ref->{$att} ~~ @relationattr)
323 {
324 #msg("relation is :$hash_ref->{$att}", $verbose );
325 $roflag = 1;
326 #print "\n roflag : $roflag";
327 $flag = 0;
328
329 }
330 else{
331 #msg( "\n got relation $att : $hash_ref->{$att}", $verbose);
332 $flag = 1;
333
334
335 }
336 $relation = $hash_ref->{$att};
337
338 }
339 else{
340 $flag = 0;
341 }
342
343
344 }
345 }
346
347 if ( $flag == 1 || $roflag == 1 ) {
348 if($att =~ /CN/){
349 #print " \n got term , $att : $hash_ref->{$att}";
350 $current_term = $hash_ref->{$att};
351 $t_flag = 1;
352
353 }
354 if($roflag == 1 && $att =~ /\brelA\b/){
355 if($hash_ref->{$att} ~~ @attribute){
356
357 $relflag = 1;
358 #msg(" \n RELA : for term $current_term , $att : $hash_ref->{$att}",$verbose);
359 }
360 }
361
362 if($roflag == 1 && $relflag == 0)
363 {
364 #print "\n not the rela I want";
365 $accept_rela = 0;
366 }
367 if($roflag == 1 && $relflag == 1){
368 $accept_rela = 1;
369 }
370 if($t_flag == 1 && $accept_rela == 1){
371 #print "\n yehhhhhh got the rela I want";
372
373 if(defined $hash_ref->{$att}){
374 if($hash_ref->{$att} =~ /^C[0-9]/){
375
376 $current_cui = $hash_ref->{$att};
377 if(defined $current_cui){ #c 1
378 unless ($current_cui ~~ %ConceptInfo){
379 #msg(" \n got rela , $att : $hash_ref->{$att}",$verbose);
380 #print " \n inserting in hash $current_cui : $current_term";
381 $ConceptInfo{$current_cui} = $current_term;
382 }
383
384 # Push all the respective neighbors in the the lists
385 if($directions{$relation} eq "U")
386 {
387 push(@parents, $current_cui);
388 # msg( " \n inserting in parent , relation is $relation $current_cui : $current_term",$verbose);
389 #print " \n inserting in parents $current_cui : $current_term";
390 }
391 elsif($directions{$relation} eq "D")
392 {
393 push(@children, $current_cui);
394 #msg( " \n inserting in children , relation is $relation $current_cui : $current_term",$verbose);
395
396 }
397 elsif($directions{$relation} eq "H")
398 {
399 push(@siblings, $current_cui);
400 #print " \n inserting in siblings $current_cui : $current_term";
401
402 }
403
404 }
405 }
406 }
407 }
408
409 }
410
411 if ( $att =~ /CN/ ) {
412
413 $q_term = $hash_ref->{$att};
414 #print "\n got xtra term : $q_term";
415 $c_flag = 1;
416 }
417 elsif( $att =~ /CUI|cui|Cui/){
418 if($c_flag == 1){
419 $q_cui = $hash_ref ->{$att};
420 #print "\n got xtra cui : $q_cui";
421 unless($q_cui ~~ %ConceptInfo){
422 $ConceptInfo{$q_cui} = $q_term;
423 }
424
425 }
426 }
427
428 #Follwing regular expression is used to get just the required information.
429 if ( $att =~ /contents|CUI|Concept|rels|Relation|relSources/ ) {
430  
431 format_object($hash_ref->{$att});
432  
433 }
434 #format_object($hash_ref->{$att});
435 }
436  
437 }
438  
439  
440 =head2 format_homogeneous_array
441  
442 This sub formats array.
443  
444 =cut
445  
446 sub format_homogeneous_array {
447 my $array_ref = shift;
448 #print "\n in format array";
449 foreach my $val (@$array_ref) {
450 format_object($val);
451 }
452 }
453  
454  
455  
456  
457 =head2 extract_object_class
458  
459 This sub is used to remove exact reference to object.
460  
461 =cut
462  
463 sub extract_object_class {
464 my $object_ref = shift;
465  
466 # remove exact reference
467 $object_ref =~ s/\(0x[\d\w]+\)$//o;
468  
469 my ($class, $type) = split /=/, $object_ref;
470  
471 my $res = undef;
472 if ($type) {
473 $res = $class;
474 } else {
475 $res = $object_ref;
476 }
477
478
479 return $res;
480 }
481  
482 =head2 printHash
483  
484 This sub prints argument hash.
485  
486 =cut
487  
488 sub printHash
489 {
490 msg("\nIn print hash", $verbose);
491 my $ref = shift;
492 my %hash = %$ref;
493 foreach my $key(keys %hash)
494 {
495 msg( "\n $key => $hash{$key}",$verbose);
496 }
497 }
498  
499  
500 undef %ConceptInfo;
501 undef %directions;
502 undef @children;
503 undef @parents;
504 undef @siblings;
505  
506  
507 #-------------------------------PERLDOC STARTS HERE-------------------------------------------------------------
508  
509  
510  
511 =head1 SEE ALSO
512  
513 ValidateTerm.pm GetUserData.pm Query.pm ws-getAllowablePath.pl
514  
515 =cut
516  
517  
518 =head1 AUTHORS
519  
520 Mugdha Choudhari, University of Minnesota Duluth
521 E<lt>chou0130 at d.umn.eduE<gt>
522  
523 Ted Pedersen, University of Minnesota Duluth
524 E<lt>tpederse at d.umn.eduE<gt>
525  
526  
527  
528  
529 =head1 COPYRIGHT
530  
531 Copyright (C) 2011, Mugdha Choudhari, Ted Pedersen
532  
533 This program is free software; you can redistribute it and/or modify
534 it under the terms of the GNU General Public License as published by
535 the Free Software Foundation; either version 2 of the License, or (at
536 your option) any later version.
537  
538 This program is distributed in the hope that it will be useful, but
539 WITHOUT ANY WARRANTY; without even the implied warranty of
540 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
541 General Public License for more details.
542  
543 You should have received a copy of the GNU General Public License
544 along with this program; if not, write to
545 The Free Software Foundation, Inc.,
546 59 Temple Place - Suite 330,
547 Boston, MA 02111-1307, USA.
548  
549 =cut
550  
551 #---------------------------------PERLDOC ENDS HERE---------------------------------------------------------------
552  
553 1;