File Coverage

blib/lib/Statistics/Candidates.pm
Criterion Covered Total %
statement 68 69 98.5
branch 10 16 62.5
condition 3 9 33.3
subroutine 10 11 90.9
pod 4 5 80.0
total 95 110 86.3


line stmt bran cond sub pod time code
1             package Statistics::Candidates;
2              
3             ##---------------------------------------------------------------------------##
4             ## Author:
5             ## Hugo WL ter Doest terdoest@cs.utwente.nl
6             ## Description:
7             ## Object/methods for candidate features
8             ##
9             ##---------------------------------------------------------------------------##
10             ## Copyright (C) 1998, 1999 Hugo WL ter Doest terdoest@cs.utwente.nl
11             ##
12             ## This library is free software; you can redistribute it and/or modify
13             ## it under the terms of the GNU General Public License as published by
14             ## the Free Software Foundation; either version 2 of the License, or
15             ## (at your option) any later version.
16             ##
17             ## This library is distributed in the hope that it will be useful,
18             ## but WITHOUT ANY WARRANTY; without even the implied warranty of
19             ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20             ## GNU General Public License for more details.
21             ##
22             ## You should have received a copy of the GNU Library General Public
23             ## License along with this program; if not, write to the Free Software
24             ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25             ##---------------------------------------------------------------------------##
26              
27              
28             ##---------------------------------------------------------------------------##
29             ## Globals
30             ##---------------------------------------------------------------------------##
31 2         141 use vars qw($VERSION
32             @ISA
33             @EXPORT
34 2     2   1638 $VECTOR_PACKAGE);
  2         5  
35              
36              
37             ##---------------------------------------------------------------------------##
38             ## Require libraries
39             ##---------------------------------------------------------------------------##
40 2     2   10 use strict;
  2         5  
  2         49  
41 2     2   10 use diagnostics -verbose;
  2         3  
  2         18  
42 2     2   52 use Carp;
  2         3  
  2         102  
43 2     2   10 use Statistics::SparseVector;
  2         3  
  2         1706  
44             $VECTOR_PACKAGE = "Statistics::SparseVector";
45             require Exporter;
46             require AutoLoader;
47              
48             @ISA = qw(Exporter AutoLoader);
49             # Items to export into callers namespace by default. Note: do not export
50             # names by default without a very good reason. Use EXPORT_OK instead.
51             # Do not simply export all your public functions/methods/constants.
52             @EXPORT = qw(
53             );
54              
55              
56             sub new {
57 1     1 1 11 my($this, $arg) = @_;
58              
59             # for calling $self->new($someth):
60 1   33     7 my $class = ref($this) || $this;
61 1         2 my $self = {};
62 1         3 bless $self, $class;
63 1 50       3 if ($arg) {
64 1         4 $self->read($arg);
65             }
66 1         3 return($self);
67             }
68              
69              
70       0     sub DESTROY {
71              
72             }
73              
74              
75             # reads a candidates file
76             # dies if insufficient events or inconsistent lines
77             # syntax first line: ...
78             # syntax other lines:
79             sub read {
80 1     1 0 2 my($self, $file) = @_;
81              
82 1         2 my($features,
83             $sum,
84             $event,
85             $candidate_names);
86              
87             # prologue
88 1 50       28 open(CANDS, $file) ||
89             die "Could not open $file\n";
90 1         4 print "Opened $file\n";
91              
92             # read candidate names, skip comments
93 1         3 $candidate_names = "";
94 1         2 do {
95 1         29 $candidate_names = ;
96             } until ($candidate_names !~ /\#.*/);
97 1         3 chomp $candidate_names;
98 1         10 $self->{CANDIDATE_NAMES} = [split(/\t/,$candidate_names)];
99 1         2 $self->{NR_CANDIDATES} = $#{$self->{CANDIDATE_NAMES}} + 1;
  1         5  
100             # read the candidate bitvectors
101 1         3 $self->{NR_CLASSES} = 0;
102 1         9 while () {
103 100 50       214 if (!/\#.*/) {
104 100         119 chomp;
105 100         126 $features = $_;
106             $self->{CANDIDATES}[$self->{NR_CLASSES}++] =
107 100         285 $VECTOR_PACKAGE->new_vec($self->{NR_CANDIDATES}, $features, "binary");
108             }
109             }
110              
111             # epilogue
112 1         13 close(CANDS);
113             # check the candidates for constant functions
114 1         5 $self->check();
115 1         5 print "Read $self->{NR_CANDIDATES} candidates for $self->{NR_CLASSES} events; ";
116 1         4 print "closed $file\n";
117             }
118              
119              
120             # check whether for all features f, \sum_x f(x) > 0, and
121             # \sum_x f(x) != nr_classes
122             sub check {
123 1     1 1 3 my($self) = @_;
124              
125 1         2 my($x,
126             $f,
127             $sum);
128              
129 1         5 for ($f = 0; $f < $self->{NR_CANDIDATES}; $f++) {
130 3         6 $sum = 0;
131 3         8 for ($x = 0; $x < $self->{NR_CLASSES}; $x++) {
132 300         668 $sum += $self->{CANDIDATES}[$x]->bit_test($f);
133             }
134 3 50 33     22 if (!$sum || ($sum == $self->{NR_CLASSES})) {
135 0         0 croak "Candidate ",$f+1, " is constant, remove it\n";
136             }
137             }
138             }
139              
140              
141             # writes remaining candidates to a file
142             # syntax: same as input candidates file
143             sub write {
144 1     1 1 14 my($self, $file) = @_;
145              
146 1         2 my($x,
147             $f);
148              
149 1 50 33     9 if (($self->{NR_CANDIDATES} > 0) && ($self->{NR_CLASSES})) {
150 1 50       94 open(CANDIDATES,">$file") ||
151             die "Could not open $file\n";
152 1         4 print "Opened $file\n";
153              
154             # write the list of candidate names that were not added
155 1         6 for ($f = 0; $f < $self->{NR_CANDIDATES}; $f++) {
156 3 100       13 if (!$self->{ADDED}{$f}) {
157 1         8 print CANDIDATES "$self->{CANDIDATE_NAMES}[$f]\t";
158             }
159             }
160 1         3 print CANDIDATES "\n";
161              
162             # write candidates that were not added
163 1         5 for ($x = 0; $x < $self->{NR_CLASSES}; $x++) {
164 100         206 for ($f = 0; $f < $self->{NR_CANDIDATES}; $f++) {
165 300 100       820 if (!$self->{ADDED}{$f}) {
166 100         290 print CANDIDATES $self->{CANDIDATES}[$x]->bit_test($f);
167             }
168             }
169 100         234 print CANDIDATES "\n";
170             }
171              
172 1         35 close CANDIDATES;
173 1         5 print "Closed $file\n";
174             }
175             }
176              
177              
178             sub clear {
179 4     4 1 58 my($self) = @_;
180              
181 4         17 undef $self->{ADDED};
182             }
183              
184             1;
185              
186             __END__