lib/Bio/SDRS.pm

filter: Statementstmt 16.1% 67 / 414 Branchbran 4.7% 7 / 146 Conditioncond 4.1% 2 / 48 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 42.4% 14 / 33 Podpod n/a totaltotal 13.4% 90 / 669 CC 100 SCAR 53.9
CC
100
Cov
12%
calculate
702.0
AUTOLOAD
338.2
ec50data
90.0
CRAP 218.2
↑ Summary lib/Bio/sdrs.pl »
1 package Bio::SDRS;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Math::NumberCruncher;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Statistics::Distributions;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 require Exporter;
13  
14 @ISA = qw(Exporter);
15  
16 # Items to export into callers namespace by default. Note: do not export
17 # names by default without a very good reason. Use EXPORT_OK instead.
18 # Do not simply export all your public functions/methods/constants.
19  
20 # This allows declaration
21 # use Bio::SDRS ':all';
22 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
23 # will save memory.
24 our %EXPORT_TAGS = ( 'all' => [ qw() ] );
25  
26 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
27  
28 our @EXPORT = qw();
29  
30 our $AUTOLOAD;
31  
32 my %component = map { ($_ => 1) } ('MULTIPLE', 'LDOSE', 'HDOSE', 'STEP',
33 'MAXPROC', 'TRIM', 'SIGNIFICANCE',
34 'TMPDIR', 'DEBUG');
35  
36 our $VERSION = '0.08';
37  
38 1;
39
40 =head1 NAME
41  
42 Bio::SDRS - Perl extension for Sigmoidal Dose Response Search, a tool
43 for characterizing biological responses to compounds.
44  
45 =head1 SYNOPSIS
46  
47 use Bio::SDRS;
48 my $sdrs = new Bio::SDRS;
49  
50 $sdrs->doses(0.423377, 1.270132, 3.810395, 11.431184, 34.293553,
51 102.880658, 308.641975, 925.925926, 2777.777778, 8333.333333,
52 25000);
53 $sdrs->set_assay('C8-BMS-208882',
54 1.885, 1.82, 2.2, 2.205, 2.78,
55 4.965, 9.21, 31.275, 74.445, 99.03,
56 100);
57 $sdrs->calculate;
58 foreach my $assay ($sdrs->assays) {
59 print "$assay\n";
60 foreach my $prop (('MAX', 'MIN', 'LOW', 'HIGH', 'EC50',
61 'PVALUE', 'EC50RANGE', 'PEAK', 'A', 'B',
62 'D', 'FOLD')) {
63 printf " %s = %s\n", $prop, $sdrs->ec50data($assay, $prop);
64 }
65 print "\n";
66 }
67  
68 =head2 Constructor
69  
70 $obj = new Bio::SDRS;
71  
72 # You can also use $obj = Bio::SDRS->new();
73  
74 =head2 Object Methods
75  
76 =head3 Input Methods
77  
78 $sdrs->multiple([$new_multiple]);
79 $sdrs->ldose([$new_ldose]);
80 $sdrs->hdose([$new_hdose]);
81 $sdrs->step([$new_step]);
82 $sdrs->maxproc([$new_maxproc]);
83 $sdrs->trim([$new_trim]);
84 $sdrs->significance([$new_significance]);
85 $sdrs->tmpdir([$new_tmpdir]);
86 $sdrs->debug([$new_debug]);
87 $sdrs->doses(doses...);
88 $sdrs->set_assay(assay, {response}...)
89  
90 =head3 Output Methods
91  
92 $sdrs->assays;
93 $sdrs->scandata;
94 $sdrs->score_doses;
95 $sdrs->sorted_assays_by_dose([$dose]);
96 $sdrs->pvalues_by_dose([$dose])
97 $sdrs->ec50data([$assay[, $property[, $precision]]]);
98  
99 =head3 Other Methods
100  
101 $sdrs->calculate;
102  
103 =head1 DESCRIPTION
104  
105 Bio::SDRS implements the Sigmoidal Dose Response Search of assay responses
106 described in the paper by
107  
108 Rui-Ru Ji, Nathan O. Siemers, Lei Ming, Liang Schweizer, and Robert E
109 Bruccoleri.
110  
111 The module is implemented using a simple object oriented paradigm
112 where the object stores all the information needed for a calculation
113 along with a state variable, C<STATE>. The state variable has three
114 possible values, C<'setup'>, C<'calculating'> and C<'calculated'>. The
115 value of C<'setup'> indicates that the object is being setup with
116 data, and any results in the object are inconsistent with the data.
117 The value of C<'calculating'> indicates the object's computations are
118 in progress and tells the code not to delete intermediate files. This
119 object runs in parallel, and the object destruction code gets called
120 when each thread exits. Intermediate files must be protected at that
121 time. The value of C<'calculated'> indicates that the object's
122 computational results are consistent with the data, and may be
123 returned to a calling program.
124  
125 The C<'calculate'> method is used to update all the calculated values
126 from the input data. It checks the state variable first, and only does
127 the calculation if the state is C<'setup'>. Once the calculations are
128 complete, then the state variable is set to C<'calculated'>. Thus, the
129 C<calculate> method can be called whenever a calculated value is
130 needed, and there is no performance penalty.
131  
132 The module initializes the C<Bio::SDRS> object with a state of
133 C<'setup'>. Any data input sets the state to C<'setup'>. Any requests
134 for calculated data, calls C<'calculate'>, which updates the state
135 variable so futures requests for calculated data return quickly.
136  
137 B<N.B.> This module uses parallel programming via a fork call to get
138 high performance. I<You must close all database connections prior to
139 calling the C<calculate> method, and reopen them afterwards. In
140 addition, you must ensure that any automated DESTROY methods take in
141 account their execution when the child processes terminated.>
142  
143 =head1 METHODS
144  
145 The following methods are provided:
146  
147 =over 4
148  
149 =cut
150
151  
152 =item C<new()>
153  
154 Creates a new Bio::SDRS object.
155  
156 =cut
157  
158 sub new {
159 1 ▶ my $pkg;
Subroutine new: called CC=2 SCAR=6.9
160 1 my $class = shift;
161 1 eval {($pkg) = caller(0);};
162 1 ▶ if ($class ne $pkg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class ne $pkg) 0 1 1
163 0 unshift @_, $class;
164 }
165 1 my $self = {};
166 1 bless $self;
167 1 $self->_init_data;
168 1 $self->{MULTIPLE} = 1.18;
169 1 $self->{LDOSE} = 0.17;
170 1 $self->{HDOSE} = 30000;
171 1 $self->{STEP} = 60;
172 1 $self->{MAXPROC} = 2;
173 1 $self->{TRIM} = 0.0;
174 1 $self->{SIGNIFICANCE} = 0.05;
175 1 $self->{DEBUG} = 0;
176 1 $self->_init_tmp;
177
178 1 return $self;
179 }
180  
181 sub _init_data {
182 1 ▶ my $self = shift;
Subroutine _init_data: called CC=1 SCAR=0.0
183 1 $self->{STATE} = "setup";
184 1 $self->{DOSES} = [];
185 1 $self->{RESPONSES} = {};
186 }
187  
188 sub _init_tmp {
189 1 ▶ my $self = shift;
Subroutine _init_tmp: called CC=3 SCAR=11.4
190 1 ▶ my $tmp = exists($ENV{TMPDIR}) ? $ENV{TMPDIR} : "/tmp";
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'TMPDIR'} ? : 0 1 1
191 1 $tmp .= "/sdrs";
192 1 ▶ if (exists($ENV{USER})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"USER"}) 0 1 1
193 0 $tmp .= "." . $ENV{USER};
194 }
195 1 $tmp .= "." . $$;
196 1 $self->{TMPDIR} = $tmp;
197 1 $self->{TMP_CREATED} = 0;
198 }
199  
200 sub DESTROY {
201
202 # There's an important multiprocessing issue to keep in mind here.
203 # When this process forks, this method will be invoked for all the subprocesses.
204 # Thus, we need to prevent the deletion of the temporary array until the
205 # calculation is completed. That is why there is a check on the STATE.
206
207 1 ▶ my $self = shift;
Subroutine DESTROY: called CC=4 SCAR=18.9
208 1 ▶ if (not $self->{DEBUG} and
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"DEBUG"} and $self->{"TMP_CREATED"} and $self->{"STATE"} eq "calculated") 0 1 1
Condition33%2/6
not $self->{"DEBUG"} and $self->{"TMP_CREATED"} and $self->{"STATE"} eq "calculated"
!ll&&!rl&&r
100
not $self->{"DEBUG"} and $self->{"TMP_CREATED"}
!ll&&!rl&&r
010
MC/DC0%0/3
not $self->{"DEBUG"} and $self->{"TMP_CREATED"} and $self->{"STATE"} eq "calculated"
not $self->{"DEBUG"} $self->{"TMP_CREATED"} $self->{"STATE"} eq "calculated"
Truth table25%1/4
not(A) and B and C
A= not $self->{"DEBUG"}B= $self->{"TMP_CREATED"}C= $self->{"STATE"} eq "calculated"
ABCresult
0XX0
10X0
1100
1111
209 $self->{TMP_CREATED} and
210 $self->{STATE} eq 'calculated') {
211 &_system_with_check("rm -rf " . $self->{TMPDIR},
212 0 $self->{DEBUG});
213 }
214 }
215  
216 # documentation for autoloaded methods goes here.
217  
218 =item C<multiple([$multiple_value])>
219  
220 Retrieves the current setting for the C<multiple> value,
221 and optionally sets it. This value specifies the multiplicity factor
222 for increasing the dose in the search. It must be greater than one.
223  
224 =item C<ldose([$ldose_value])>
225  
226 Retrieves the current setting for the C<ldose> value,
227 and optionally sets it. This value specifies the lowest dose in the search.
228 It must be greater than zero.
229  
230 =item C<hdose([$hdose_value])>
231  
232 Retrieves the current setting for the C<hdose> value,
233 and optionally sets it. This value specifies the highest
234 dose in the search. It must be greater than the ldose value.
235  
236 =item C<step([$step_value])>
237  
238 Retrieves the current setting for the C<step> value, and optionally
239 sets it. This value specifies the maximum change in doses in the
240 search. In the search process, this module starts at the ldose
241 value. It tries multiplying the current dose by the C<multiple> value,
242 but it will only increase the dose by no more than the C<step> value
243 specified here. It must be positive.
244  
245 =item C<maxproc([$maxproc_value])>
246  
247 Retrieves the current setting for the C<maxproc> value,
248 and optionally sets it. This value specifies the maximum number of processes that
249 can be used for the search. The upper limit is 64 and the lower limit is 1.
250  
251 =item C<trim([$trim_value])>
252  
253 Retrieves the current setting for the C<trim> value, and optionally
254 sets it. This value specifies the trimming factor for the number of
255 assays. If the C<trim> is 0, then all assays will be used, and if 1,
256 no assays will be used. It must be between 0 and 1.
257  
258 =item C<significance([$significance_value])>
259  
260 Retrieves the current setting for the C<significance> value, and
261 optionally sets it. This value specifies the minimum permitted
262 significance value for the F score cutoff. It must be between zero and
263 1.
264  
265 =item C<tmpdir([$tmpdir_value])>
266  
267 Retrieves the current setting for the C<tmpdir> value, and optionally
268 sets it. This value specifies the temporary directory where scans of
269 the dose calculation are written. The default is
270 /tmp/sdrs.C<user>.C<process_id>.
271  
272 =item C<debug([$debug_value])>
273  
274 Retrieves the current setting for the C<debug> variable, and
275 optionally sets it. This value specifies whether debugging information
276 is printed and if the temporary directory (see above) is deleted after
277 execution of this module.
278  
279 =cut
280  
281 sub AUTOLOAD {
282 1 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=23 SCAR=58.2
283 1 my $type = ref($self);
284 1 my $pkg = __PACKAGE__;
285 1 my ($pos, $new_val);
286  
287 1 ▶ croak "$self is not an object\n" if not $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 1 1
288 1 ▶ croak "$pkg AUTOLOAD function fails on $type\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^$pkg$/ 0 1 1
289 if $type !~ m/^$pkg$/;
290 1 my $name = uc($AUTOLOAD);
291 1 $name =~ s/^.*:://;
292 1 ▶ if (not exists($component{$name})) {
Branch: 3 branches
BranchTrueFalseTotal
if (not exists $component{$name}) { } 1 0 1
elsif (not exists $self->{$name}) { } 0 0 0
elsif (defined $_[0]) { } 0 0 0
293 1 croak "$name is not a valid method for $type.\n";
294 }
295 elsif (not exists($self->{$name})) {
296 0 croak "$name is not a valid method for $type\n";
297 }
298 elsif (defined $_[0]) {
299 0 $self->{$name} = shift;
300 0 $self->{STATE} = "setup";
301 }
302 0 ▶ if ($name eq "MULTIPLE" and
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "MULTIPLE" and $self->{"MULTIPLE"} <= 1) 0 0 0
Condition0%0/3
$name eq "MULTIPLE" and $self->{"MULTIPLE"} <= 1
!ll&&!rl&&r
000
MC/DC0%0/2
$name eq "MULTIPLE" and $self->{"MULTIPLE"} <= 1
$name eq "MULTIPLE" $self->{"MULTIPLE"} <= 1
Truth table0%0/3
A and B
A= $name eq "MULTIPLE"B= $self->{"MULTIPLE"} <= 1
ABresult
0X0
100
111
303 $self->{MULTIPLE} <= 1.0) {
304 0 croak "multiple parameter must be greater than 1.\n";
305 }
306 0 ▶ if ($name eq "LDOSE" and
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "LDOSE" and $self->{"LDOSE"} <= 0) 0 0 0
Condition0%0/3
$name eq "LDOSE" and $self->{"LDOSE"} <= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$name eq "LDOSE" and $self->{"LDOSE"} <= 0
$name eq "LDOSE" $self->{"LDOSE"} <= 0
Truth table0%0/3
A and B
A= $name eq "LDOSE"B= $self->{"LDOSE"} <= 0
ABresult
0X0
100
111
307 $self->{LDOSE} <= 0.0) {
308 0 croak "ldose parameter must be positive.\n";
309 }
310 0 ▶ if ($name eq "HDOSE" and
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "HDOSE" and $self->{"HDOSE"} <= $self->{"LDOSE"}) 0 0 0
Condition0%0/3
$name eq "HDOSE" and $self->{"HDOSE"} <= $self->{"LDOSE"}
!ll&&!rl&&r
000
MC/DC0%0/2
$name eq "HDOSE" and $self->{"HDOSE"} <= $self->{"LDOSE"}
$name eq "HDOSE" $self->{"HDOSE"} <= $self->{"LDOSE"}
Truth table0%0/3
A and B
A= $name eq "HDOSE"B= $self->{"HDOSE"} <= $self->{"LDOSE"}
ABresult
0X0
100
111
311 $self->{HDOSE} <= $self->{LDOSE}) {
312 0 croak "hdose parameter must be greater than the ldose parameter.\n";
313 }
314 0 ▶ if ($name eq "STEP" and
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "STEP" and $self->{"STEP"} <= 0) 0 0 0
Condition0%0/3
$name eq "STEP" and $self->{"STEP"} <= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$name eq "STEP" and $self->{"STEP"} <= 0
$name eq "STEP" $self->{"STEP"} <= 0
Truth table0%0/3
A and B
A= $name eq "STEP"B= $self->{"STEP"} <= 0
ABresult
0X0
100
111
315 $self->{STEP} <= 0.0) {
316 0 croak "step parameter must be positive.\n";
317 }
318 0 ▶ if ($name eq "MAXPROC" and
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "MAXPROC" and $self->{"MAXPROC"} > 64 || $self->{"MAXPROC"} < 1) 0 0 0
Condition0%0/6
$name eq "MAXPROC" and $self->{"MAXPROC"} > 64 || $self->{"MAXPROC"} < 1
!ll&&!rl&&r
000
$self->{"MAXPROC"} > 64 or $self->{"MAXPROC"} < 1
l!l&&r!l&&!r
000
MC/DC0%0/3
$name eq "MAXPROC" and $self->{"MAXPROC"} > 64 || $self->{"MAXPROC"} < 1
$name eq "MAXPROC" $self->{"MAXPROC"} > 64 $self->{"MAXPROC"} < 1
Truth table0%0/4
A and B or C
A= $name eq "MAXPROC"B= $self->{"MAXPROC"} > 64C= $self->{"MAXPROC"} < 1
ABCresult
0XX0
1000
11X1
1011
319 ($self->{MAXPROC} > 64 or $self->{MAXPROC} < 1)) {
320 0 croak "maxproc parameter must be between 1 and 64.\n";
321 }
322 0 ▶ if ($name eq "TRIM" and
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "TRIM" and $self->{"TRIM"} < 0 || $self->{"TRIM"} > 1) 0 0 0
Condition0%0/6
$name eq "TRIM" and $self->{"TRIM"} < 0 || $self->{"TRIM"} > 1
!ll&&!rl&&r
000
$self->{"TRIM"} < 0 or $self->{"TRIM"} > 1
l!l&&r!l&&!r
000
MC/DC0%0/3
$name eq "TRIM" and $self->{"TRIM"} < 0 || $self->{"TRIM"} > 1
$name eq "TRIM" $self->{"TRIM"} < 0 $self->{"TRIM"} > 1
Truth table0%0/4
A and B or C
A= $name eq "TRIM"B= $self->{"TRIM"} < 0C= $self->{"TRIM"} > 1
ABCresult
0XX0
1000
11X1
1011
323 ($self->{TRIM} < 0.0 or
324 $self->{TRIM} > 1.0)) {
325 0 croak "trim parameter must be between 0 and 1, inclusive\n";
326 }
327 0 ▶ if ($name eq "SIGNIFICANCE" and
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "SIGNIFICANCE" and $self->{"SIGNIFICANCE"} <= 0 || $self->{"SIGNIFICANCE"} >= 1) 0 0 0
Condition0%0/6
$name eq "SIGNIFICANCE" and $self->{"SIGNIFICANCE"} <= 0 || $self->{"SIGNIFICANCE"} >= 1
!ll&&!rl&&r
000
$self->{"SIGNIFICANCE"} <= 0 or $self->{"SIGNIFICANCE"} >= 1
l!l&&r!l&&!r
000
MC/DC0%0/3
$name eq "SIGNIFICANCE" and $self->{"SIGNIFICANCE"} <= 0 || $self->{"SIGNIFICANCE"} >= 1
$name eq "SIGNIFICANCE" $self->{"SIGNIFICANCE"} <= 0 $self->{"SIGNIFICANCE"} >= 1
Truth table0%0/4
A and B or C
A= $name eq "SIGNIFICANCE"B= $self->{"SIGNIFICANCE"} <= 0C= $self->{"SIGNIFICANCE"} >= 1
ABCresult
0XX0
1000
11X1
1011
328 ($self->{SIGNIFICANCE} <= 0.0 or
329 $self->{SIGNIFICANCE} >= 1.0)) {
330 0 croak "trim parameter must be between 0 and 1, exclusive\n";
331 }
332 0 return $self->{$name};
333 }
334  
335 =item C<doses($dose1, $dose2, ...)>
336  
337 Specify the list of compound doses used in the all the assays in the
338 experiment. The doses must be in numerical order, from smallest to
339 largest.
340  
341 =cut
342  
343 sub doses {
344 0 ▶ my $self = shift;
Subroutine doses: not called CC=4 SCAR=30.0
345 0 ▶ if (scalar(@_) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 0) { } 0 0 0
346 0 return @{$self->{DOSES}};
347 }
348 else {
349 0 $self->_init_data;
350 0 my $dose1 = shift;
351 0 push(@{$self->{DOSES}}, $dose1);
352 0 foreach my $dose (@_) {
353 0 ▶ if ($dose < $dose1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dose < $dose1) 0 0 0
354 0 croak "Doses not in order. Current dose is $dose, and previous does is $dose1\n";
355 }
356 0 push(@{$self->{DOSES}}, $dose);
357 0 $dose1 = $dose;
358 }
359 0 $self->{STATE} = "setup";
360 }
361 }
362  
363 =item C<set_assay($assay_name, $response1, ...)>
364  
365 Adds an assay to the list to be searched over. Each response in the
366 list corresponds to the doses specified in the C<doses> method. The
367 number must match, and they must be in numerical order, from smallest
368 to largest.
369  
370 =cut
371  
372 sub set_assay {
373 0 ▶ my $self = shift;
Subroutine set_assay: not called CC=4 SCAR=30.0
374 0 my $assay = shift;
375  
376 0 $self->{STATE} = "setup";
377 0 ▶ if (exists($self->{RESPONSES}->{$assay})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"RESPONSES"}{$assay}) 0 0 0
378 0 croak "Duplicate assay ($assay) response specified.\n";
379 }
380 0 $self->{RESPONSES}->{$assay} = [ @_ ];
381 0 ▶ if (scalar(@{$self->{DOSES}}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"DOSES"};} == 0) 0 0 0
382 0 croak "You must call the doses method before calling the set_assay method.\n";
383 }
384 0 my $nresp = scalar(@{$self->{RESPONSES}->{$assay}});
385 0 my $ndoses = scalar(@{$self->{DOSES}});
386 0 ▶ if ($nresp != $ndoses) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nresp != $ndoses) 0 0 0
387 0 croak "The number of assay responses ($nresp) does not equal the number of doses ($ndoses)\n";
388 }
389 }
390  
391 =item C<assays()>
392  
393 Return the list of assay names;
394  
395 =cut
396
397 sub assays {
398 0 ▶ my $self = shift;
Subroutine assays: not called CC=1 SCAR=6.9
399 0 return keys %{$self->{RESPONSES}};
400 }
401  
402 =item C<calculate()>
403  
404 Perform the SDRS calculation.
405  
406 =cut
407  
408 sub calculate {
409 0 ▶ my $self = shift;
Subroutine calculate: not called CC=26 SCAR=65.5
410 0 ▶ return if $self->{STATE} eq 'calculated';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"STATE"} eq "calculated" 0 0 0
411 0 $self->_make_tmp;
412 0 $self->{STATE} = "calculating";
413 #
414 # Calculate F Test degrees of freedom.
415 #
416 0 my $ndoses = scalar(@{$self->{DOSES}});
417 0 my $p = 4; #number of parameters
418 0 $self->{FDISTR_N} = $p - 1;
419 0 $self->{FDISTR_M} = $ndoses - $p;
420 $self->{CUTOFF} = Statistics::Distributions::fdistr($self->{FDISTR_N},
421 $self->{FDISTR_M},
422 0 $self->{SIGNIFICANCE});
423  
424 0 my $count = scalar(keys %{$self->{RESPONSES}});
425 0 foreach my $assay (keys %{$self->{RESPONSES}}) {
426 0 my @data = @{$self->{RESPONSES}->{$assay}};
427 0 my ($max, $min) = Math::NumberCruncher::Range(\@data);
428 0 $self->{MAX}->{$assay} = $max;
429 0 $self->{MIN}->{$assay} = $min;
430 0 my $value = _compute_sst(\@data); #total sum of squares
431 0 $self->_define_ab_boundary($assay, \@data);
432 0 $self->{SST}->{$assay} = $value;
433 0 $self->{VALUES}->{$assay} = \@data;
434 }
435 0 my $assayNum = int($count * (1 - $self->{TRIM}));
436 0 my %expressedAssays;
437 0 $count = 0;
438 0 foreach my $g (sort {$self->{MAX}->{$b} <=> $self->{MAX}->{$a}} (keys %{$self->{MAX}})) {
439 0 ▶ last if ($count >= $assayNum);
Branch: 1 branch
BranchTrueFalseTotal
if $count >= $assayNum 0 0 0
440 0 $expressedAssays{$g} = 1;
441 0 $count++;
442 }
443  
444 0 my (%pvalues, %sortedprobes, $pid, @pids, $file, $iproc, $err);
445 0 my $tmpdir = $self->{TMPDIR};
446 0 ▶ for ($iproc = 0; $iproc < $self->{MAXPROC}; $iproc++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iproc < $self->{"MAXPROC"}) 0 0 0
447 0 ▶ if ($pid = fork) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pid = fork) { } 0 0 0
elsif (defined $pid) { } 0 0 0
448 0 ▶ print STDERR "Process $pid forked.\n" if $self->{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 0 0
449 0 push @pids, $pid;
450 }
451 elsif (defined $pid) {
452 0 sleep $iproc;
453 0 $file = "$tmpdir/sdrs.out.$iproc";
454 0 ▶ open (ECOUT, ">$file") || die "can not open EC50 output file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open ECOUT, ">$file" or die "can not open EC50 output file: $!\n" 0 0 0
455 #
456 # The following statement causes the system to write each output line
457 # to disk immediately, rather than waiting for a buffer to fill.
458 #
459 0 select((select(ECOUT), $| = 1)[$[]); # Change buffering to line by line.
460 0 my $last_dose = $self->{LDOSE};
461 0 my $icount = 0;
462 0 ▶ for (my $dose = $self->{LDOSE};
Branch: 1 branch
BranchTrueFalseTotal
if ($dose < $self->{"HDOSE"} * $self->{"MULTIPLE"}) 0 0 0
463 $dose < $self->{HDOSE} * $self->{MULTIPLE};
464 $dose *= $self->{MULTIPLE}) {
465 0 ▶ if ($dose - $last_dose > $self->{STEP}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dose - $last_dose > $self->{"STEP"}) 0 0 0
466 0 $dose = $last_dose + $self->{STEP};
467 }
468 0 ▶ if (($icount++ % $self->{MAXPROC}) == $iproc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($icount++ % $self->{"MAXPROC"} == $iproc) 0 0 0
469 0 print ECOUT $self->_scan_dose_point($dose);
470 }
471 0 $last_dose = $dose;
472 }
473 0 close ECOUT;
474 0 exit 0;
475 }
476 else {
477 0 die "Can't fork: $!\n";
478 }
479 }
480 0 $err = 0;
481 0 foreach $pid (@pids) {
482 0 waitpid($pid, 0);
483 0 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
484 0 print STDERR "Process $pid return status was $?\n";
485 0 $err = 1;
486 }
487 }
488
489 # Now collect all the outputs together.
490 0 %{$self->{GSCORE}} = ();
491 0 %{$self->{GPARAM}} = ();
492 0 %{$self->{FSCORES}} = ();
493 0 my $multiple = $self->{MULTIPLE};
494 0 my $step = $self->{STEP};
495 0 @{$self->{SCANDATA}} = ();
496
497 0 ▶ for ($iproc = 0; $iproc < $self->{MAXPROC}; $iproc++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iproc < $self->{"MAXPROC"}) 0 0 0
498 0 my $infile = "$tmpdir/sdrs.out.$iproc";
499 0 ▶ open (IN, "<$infile") || die "can not open input file $infile: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<$infile" or die "can not open input file ${infile}: $!\n" 0 0 0
500 0 ▶ while (<IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 0 0 0
501 0 push (@{$self->{SCANDATA}}, $_);
502 0 chomp;
503 0 my ($assay, $dose, $fscore, $a, $b, $d) = split (/\t/, $_);
504 0 $self->{GSCORE}->{$assay}->{$dose} = $fscore;
505 0 $self->{GPARAM}->{$assay}->{$dose} = "$a:$b:$d";
506 0 ▶ if (defined $expressedAssays{$assay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expressedAssays{$assay}) 0 0 0
507 0 $self->{FSCORES}->{$dose}->{$assay} = $fscore;
508 }
509 }
510 0 close IN;
511 }
512
513 #sort probesets based on F scores for every selected doses, output P values for FDR calculation
514 0 %{$self->{SORTED_DATA}} = ();
515 0 %{$self->{PVAL_DATA}} = ();
516 0 foreach my $dose (keys %{$self->{FSCORES}}) {
517 0 my %fs = %{$self->{FSCORES}->{$dose}};
518 0 foreach my $assay (sort {$fs{$b} <=> $fs{$a}} (keys %fs)) {
519 0 push (@{$self->{SORTED_DATA}->{$dose}}, $assay);
520 my $pvalue = Statistics::Distributions::fprob($self->{FDISTR_N},
521 $self->{FDISTR_M},
522 0 $fs{$assay});
523 0 push (@{$self->{PVAL_DATA}->{$dose}}, $pvalue);
524 }
525 }
526
527 #calculates EC50 for every probeset
528 0 %{$self->{EC50DATA}} = ();
529 0 foreach my $assay (keys %{$self->{GPARAM}}) {
530 0 my (%fscore, %param);
531 0 %fscore = %{$self->{GSCORE}->{$assay}};
532 0 %param = %{$self->{GPARAM}->{$assay}};
533  
534 0 my @sorted_list = sort {$a<=>$b} (values %fscore);
535 0 my $max = pop @sorted_list;
536 0 my $min = shift @sorted_list;
537 0 @sorted_list = sort {$fscore{$a}<=>$fscore{$b}} (keys %fscore);
538 0 my $ec50 = pop @sorted_list; #highest f score
539 0 my ($range, $high, $low, $peak, $ec50range, $fold);
540 0 ▶ if ($max >= $self->{CUTOFF}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max >= $self->{'CUTOFF'}) { } 0 0 0
541 0 $range = $self->_find_ec50_range(\%fscore, $ec50);
542 0 ($high, $low, $peak) = split (/\-/, $range);
543 0 $ec50range = $high - $low;
544 } else {
545 0 $high = $low = $peak = $ec50range = '';
546 }
547 my $pvalue = Statistics::Distributions::fprob($self->{FDISTR_N},
548 $self->{FDISTR_M},
549 0 $max);
550 0 my ($a, $b, $d) = split (/\:/, $param{$ec50});
551 0 ▶ if ($d >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d >= 0) { } 0 0 0
552 0 ▶ if ($a != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a != 0) { } 0 0 0
553 0 $fold = -($b/$a);
554 } else {
555 0 $fold = -99999;
556 }
557 } else {
558 0 ▶ if ($a != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a != 0) { } 0 0 0
559 0 $fold = $b/$a;
560 } else {
561 0 $fold = 99999;
562 }
563 }
564 0 $self->{EC50DATA}->{$assay} = { MAX => $max,
565 MIN => $min,
566 LOW => $low,
567 HIGH => $high,
568 EC50 => $ec50,
569 PVALUE => $pvalue,
570 EC50RANGE => $ec50range,
571 PEAK => $peak,
572 A => $a,
573 B => $b,
574 D => $d,
575 FOLD => $fold };
576 }
577 0 $self->{STATE} = "calculated";
578 }
579  
580 =item C<scandata()>
581  
582 Return the complete list of scan data used in the SDRS calculation. This is just an array of lines containing the values of the EC50 calculations.
583  
584 =cut
585  
586 sub scandata {
587 0 ▶ my $self = shift;
Subroutine scandata: not called CC=1 SCAR=6.9
588 0 return @{$self->{SCANDATA}};
589 }
590  
591 =item C<score_doses()>
592  
593 Return the list of doses used in the SDRS calculation that can be
594 used as arguments for assays and pvalues.
595  
596 =cut
597  
598 sub score_doses {
599 0 ▶ my $self = shift;
Subroutine score_doses: not called CC=1 SCAR=6.9
600 0 return keys %{$self->{FSCORES}};
601 }
602  
603 =item C<sorted_assays_by_dose([$dose])>
604  
605 Return a list of assays for each dose in $dose sorted by F-score.
606  
607 =cut
608
609 sub sorted_assays_by_dose {
610 0 ▶ my $self = shift;
Subroutine sorted_assays_by_dose: not called CC=4 SCAR=30.0
611 0 my $dose = shift;
612 0 ▶ if ($self->{STATE} ne 'calculated') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"STATE"} ne "calculated") 0 0 0
613 0 $self->calculate;
614 }
615  
616 0 ▶ if (not defined $dose) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $dose) { } 0 0 0
617 0 return %{$self->{SORTED_DATA}};
618 }
619 else {
620 0 ▶ if (not exists($self->{SORTED_DATA}->{$dose})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'SORTED_DATA'}{$dose}) { } 0 0 0
621 0 carp "Sorted assays by dose = $dose does not exist.\n";
622 0 return undef;
623 }
624 else {
625 0 return @{$self->{SORTED_DATA}->{$dose}};
626 }
627 }
628 }
629
630  
631 =item C<pvalues_by_dose([$dose])>
632  
633 Return a list of P values for the assays returned by sorted_assays_by_dose
634 for each dose in $dose sorted by F-score.
635  
636 =cut
637
638 sub pvalues_by_dose {
639 0 ▶ my $self = shift;
Subroutine pvalues_by_dose: not called CC=4 SCAR=30.0
640 0 my $dose = shift;
641 0 ▶ if ($self->{STATE} ne 'calculated') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"STATE"} ne "calculated") 0 0 0
642 0 $self->calculate;
643 }
644  
645 0 ▶ if (not defined $dose) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $dose) { } 0 0 0
646 0 return %{$self->{PVAL_DATA}};
647 }
648 else {
649 0 ▶ if (not exists($self->{PVAL_DATA}->{$dose})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'PVAL_DATA'}{$dose}) { } 0 0 0
650 0 carp "P values by dose = $dose does not exist.\n";
651 0 return undef;
652 }
653 else {
654 0 return @{$self->{PVAL_DATA}->{$dose}};
655 }
656 }
657 }
658  
659 =item C<ec50data([$assay[, $property[, $precision]]])>
660  
661 Returns EC50 data for the calculation. If no arguments are specified,
662 then the internal hash for the EC50 calculation are returned. If just
663 an C<$assay> is specified, then the internal hash for all the EC50
664 values associated with that C<$assay> is returned. If an C<$assay> and
665 C<$property> are specified, then the property for that assay is
666 returned. If the C<$precision> operand is specified, then it controls
667 how many digits of precision are returned for the value.
668  
669 Here is the list of possible properties.
670  
671 MAX Maximum F score
672 MIN Minimum F score
673 LOW Lower bound of 95% confidence interval for the estimated EC50.
674 HIGH Upper bound of 95% confidence interval for the estimated EC50.
675 EC50 Estimated EC50.
676 PVALUE P value of the best fitting model
677 EC50RANGE range of 95% confidence interval for the estimated EC50.
678 PEAK Number of peaks in the F scores at search doses across experimental dose range.
679 A Estimated value for A in the best model.
680 B Estimated value for B in the best model.
681 D Estimated value for D in the best model.
682 FOLD Ratio of B/A or 99999.0. If A == 0. Positive if D < 0, negative otherwise.
683  
684 =cut
685  
686 sub ec50data {
687 0 ▶ my $self = shift;
Subroutine ec50data: not called CC=9 SCAR=45.0
688 0 my $assay = shift;
689 0 my $property = shift;
690 0 my $precision = shift;
691  
692 0 ▶ $precision = 6 if not defined $precision;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $precision 0 0 0
693 0 ▶ if ($self->{STATE} ne 'calculated') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"STATE"} ne "calculated") 0 0 0
694 0 $self->calculate;
695 }
696 0 ▶ if (not defined $assay) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $assay) { } 0 0 0
697 0 return %{$self->{EC50DATA}};
698 }
699 else {
700 0 ▶ if (not exists($self->{EC50DATA}->{$assay})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"EC50DATA"}{$assay}) 0 0 0
701 0 carp "EC50 data for $assay does not exist.\n";
702 0 return undef;
703 }
704 0 my $assaydata = $self->{EC50DATA}->{$assay};
705 0 ▶ if (not defined $property) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $property) { } 0 0 0
706 0 return %{$self->{EC50DATA}->{$assay}};
707 }
708 else {
709 0 ▶ if (not exists($assaydata->{$property})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $assaydata->{$property}) { } 0 0 0
710 0 carp "EC50 data for property $property for assay $assay does not exist.\n";
711 0 return undef;
712 }
713 else {
714 0 my $ret = $assaydata->{$property};
715 0 ▶ if ($ret ne "" and $ret != int($ret)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret ne "" and $ret != int $ret) 0 0 0
Condition0%0/3
$ret ne "" and $ret != int $ret
!ll&&!rl&&r
000
MC/DC0%0/2
$ret ne "" and $ret != int $ret
$ret ne "" $ret != int $ret
Truth table0%0/3
A and B
A= $ret ne ""B= $ret != int $ret
ABresult
0X0
100
111
716 0 $ret = sprintf("%.${precision}g", $ret) + 0.0;
717 }
718 0 return $ret;
719 }
720 }
721 }
722 }
723  
724 sub _make_tmp {
725 0 ▶ my $self = shift;
Subroutine _make_tmp: not called CC=6 SCAR=37.4
726 0 ▶ if (-d $self->{TMPDIR} and -w $self->{TMPDIR}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->{"TMPDIR"} and -w $self->{"TMPDIR"}) 0 0 0
Condition0%0/3
-d $self->{"TMPDIR"} and -w $self->{"TMPDIR"}
!ll&&!rl&&r
000
MC/DC0%0/2
-d $self->{"TMPDIR"} and -w $self->{"TMPDIR"}
-d $self->{"TMPDIR"} -w $self->{"TMPDIR"}
Truth table0%0/3
A and B
A= -d $self->{"TMPDIR"}B= -w $self->{"TMPDIR"}
ABresult
0X0
100
111
727 0 return;
728 }
729 0 ▶ if (-d $self->{TMPDIR}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->{"TMPDIR"}) 0 0 0
730 0 croak "Unable to write " . $self->{TMPDIR} . "\n";
731 }
732 0 ▶ if (-w $self->{TMPDIR}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-w $self->{"TMPDIR"}) 0 0 0
733 unlink($self->{TMPDIR}) ||
734 0 ▶ croak "Unable to delete " . $self->{TMPDIR} . "\n";
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->{"TMPDIR"} or croak('Unable to delete ' . $self->{"TMPDIR"} . "\n") 0 0 0
735 }
736 &_system_with_check("mkdir -p " . $self->{TMPDIR},
737 0 $self->{DEBUG});
738 0 $self->{TMP_CREATED} = 1;
739 }
740  
741 sub _find_ec50_range {
742 0 ▶ my $self = shift;
Subroutine _find_ec50_range: not called CC=9 SCAR=45.0
743 0 my ($scores, $ec50) = @_;
744 0 my @range = ();
745 0 my $off_flag = 1;
746 0 my $peak = 0;
747 0 my $current;
748 0 my $seen = 0;
749 0 my $boundary;
750 0 foreach my $dose (sort {$a<=>$b} keys %$scores) {
751 0 $current = $$scores{$dose};
752 0 ▶ $seen = 1 if ($dose == $ec50);
Branch: 1 branch
BranchTrueFalseTotal
if $dose == $ec50 0 0 0
753 0 ▶ if ($current >= $self->{CUTOFF}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current >= $self->{'CUTOFF'}) { } 0 0 0
754 0 push @range, $dose;
755 0 ▶ $off_flag = 0 if ($off_flag == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $off_flag == 1 0 0 0
756 } else {
757 0 ▶ if ($seen == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen == 1) 0 0 0
758 0 $boundary = _find_boundary(\@range);
759 0 $seen = 0;
760 }
761 0 @range = ();
762 0 ▶ if ($off_flag == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off_flag == 0) 0 0 0
763 0 $off_flag = 1;
764 0 $peak++;
765 }
766 }
767 }
768 0 ▶ $boundary = _find_boundary(\@range) if ($seen == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $seen == 1 0 0 0
769 0 ▶ $peak++ if ($current >= $self->{CUTOFF});
Branch: 1 branch
BranchTrueFalseTotal
if $current >= $self->{"CUTOFF"} 0 0 0
770
771 0 return "$boundary-$peak";
772 }
773
774 sub _find_boundary {
775 0 ▶ my $range = shift;
Subroutine _find_boundary: not called CC=2 SCAR=17.9
776 0 my ($high, $low);
777  
778 0 ▶ if (@$range >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$range >= 2) { } 0 0 0
779 0 $low = shift @$range;
780 0 $high = pop @$range;
781 } else {
782 0 $low = shift @$range;
783 0 $high = $low;
784 }
785 0 return "$high-$low";
786 }
787  
788 sub _scan_dose_point {
789 0 ▶ my $self = shift;
Subroutine _scan_dose_point: not called CC=2 SCAR=17.9
790 0 my $dose = shift;
791 0 my $ecstring = '';
792 0 my $m = $self->{FDISTR_M};
793 0 my $n = $self->{FDISTR_N};
794 0 foreach my $assay (keys %{$self->{VALUES}}) {
795 0 my ($al, $ah, $astep) = split (/\:/, $self->{ARANGE}->{$assay});
796 0 my ($bl, $bh, $bstep) = split (/\:/, $self->{BRANGE}->{$assay});
797 my ($a, $b, $d, $best_sse) = _compute_best_sse($al, $ah, $astep,
798 $bl, $bh, $bstep,
799 $dose,
800 $self->{VALUES}->{$assay},
801 0 $self->{DOSES});
802 0 my $f_score = ($self->{SST}->{$assay} - $best_sse) * $m / ($best_sse * $n);
803 0 $f_score = sprintf("%.3f", $f_score);
804 0 $a = sprintf("%.3f", $a);
805 0 $b = sprintf("%.3f", $b);
806 0 $d = sprintf("%.3f", $d);
807 0 $ecstring .= "$assay\t" .
808 sprintf("%.5f", $dose) .
809 "\t$f_score\t$a\t$b\t$d\n";
810 }
811 0 return $ecstring;
812 }
813  
814 sub _define_ab_boundary {
815 0 ▶ my $self = shift;
Subroutine _define_ab_boundary: not called CC=1 SCAR=6.9
816 0 my ($assay, $data) = @_;
817 0 my @sorted = sort {$a<=>$b} @$data;
818 0 my @sorted_copy = @sorted;
819 0 my @brange = splice (@sorted, $#sorted-5, 6);
820 0 $self->_find_step($assay, 'b', \@brange);
821 0 my @arange = splice (@sorted_copy, 0, 6);
822 0 $self->_find_step($assay, 'a', \@arange);
823 }
824  
825 sub _find_step {
826  
827 0 ▶ my $self = shift;
Subroutine _find_step: not called CC=7 SCAR=40.3
828 0 my ($assay, $pam, $data) = @_;
829 0 my $stdev = Math::NumberCruncher::StandardDeviation($data);
830 0 my $mean = Math::NumberCruncher::Mean($data);
831 0 my $cutoff = $mean / 10;
832 0 ▶ $stdev = $cutoff if ($stdev eq 'NaN' || $stdev < $cutoff);
Branch: 1 branch
BranchTrueFalseTotal
if $stdev eq "NaN" or $stdev < $cutoff 0 0 0
Condition0%0/3
$stdev eq "NaN" or $stdev < $cutoff
l!l&&r!l&&!r
000
MC/DC0%0/2
$stdev eq "NaN" or $stdev < $cutoff
$stdev eq "NaN" $stdev < $cutoff
Truth table0%0/3
A or B
A= $stdev eq "NaN"B= $stdev < $cutoff
ABresult
1X1
011
000
833 0 my ($l, $h, $step);
834 0 $step = $stdev / 2.5;
835 0 $step = sprintf("%.3f", $step);
836 0 ▶ if ($pam eq 'a') {
Branch: 2 branches
BranchTrueFalseTotal
if ($pam eq 'a') { } 0 0 0
elsif ($pam eq 'b') { } 0 0 0
837 0 $h = $mean + 2.3*$stdev;
838 0 $l = $mean - 2*$stdev;
839 0 ▶ $l = $self->{MIN}->{$assay} if ($l <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $l <= 0 0 0 0
840 0 $h = sprintf("%.3f", $h);
841 0 $l = sprintf("%.3f", $l);
842 0 $self->{ARANGE}->{$assay} = "$l:$h:$step";
843 } elsif ($pam eq 'b') {
844 0 $h = $mean + 2*$stdev;
845 0 $l = $mean - 2.3*$stdev;
846 0 ▶ $l = $self->{MIN}->{$assay} if ($l <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $l <= 0 0 0 0
847 0 $h = sprintf("%.3f", $h);
848 0 $l = sprintf("%.3f", $l);
849 0 $self->{BRANGE}->{$assay} = "$l:$h:$step";
850 }
851 }
852  
853 sub _compute_sst {
854 0 ▶ my $data = shift;
Subroutine _compute_sst: not called CC=2 SCAR=17.9
855 0 my $sst = 0;
856 0 my $mean = Math::NumberCruncher::Mean($data);
857
858 0 foreach my $data (@$data) {
859 0 $sst += ($data - $mean)**2;
860 }
861 0 return $sst;
862 }
863  
864 sub _system_with_check {
865  
866 0 ▶ my $command = shift;
Subroutine _system_with_check: not called CC=4 SCAR=30.0
867 0 my $echo = shift;
868  
869 0 ▶ if (defined $echo and $echo) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $echo and $echo) 0 0 0
Condition0%0/3
defined $echo and $echo
!ll&&!rl&&r
000
MC/DC0%0/2
defined $echo and $echo
defined $echo $echo
Truth table0%0/3
A and B
A= defined $echoB= $echo
ABresult
0X0
100
111
870 0 &_dated_mesg($command);
871 }
872 0 my $status = system("$command");
873 0 ▶ if ($status != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 0) 0 0 0
874 0 croak "Shell command: $command returned $status error code.\n";
875 }
876 }
877  
878 sub _dated_mesg {
879  
880 # Print a dated message to STDERR
881 0 ▶ my $mesg = shift;
Subroutine _dated_mesg: not called CC=2 SCAR=17.9
882  
883 0 my $date = &_date;
884 0 ▶ if (substr($mesg, -1, 1) ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($mesg, -1, 1) ne "\n") 0 0 0
885 0 $mesg .= "\n";
886 }
887 0 print STDERR "At $date: $mesg";
888 }
889  
890 sub _date {
891 0 ▶ return strftime("%a %b %d %T %Z %Y", localtime);
Subroutine _date: not called CC=1 SCAR=6.9
892 }
893  
894 =back
895  
896 =head1 SEE ALSO
897  
898 sdrs.pl
899  
900 =head1 AUTHORS
901  
902 Ruiru Ji <ruiruji@gmail.com>
903 Nathan O. Siemers <nathan.siemers@bms.com>
904 Lei Ming <lei.ming@bms.com>
905 Liang Schweizer <liang.schweizer@bms.com>
906 Robert Bruccoleri <bruc@acm.org>
907  
908 =cut
909  
910
911 1 ▶use Inline C => <<'END_OF_C_CODE', NAME => 'Bio::SDRS::FastSSE';
Subroutine BEGIN: called CC=1 SCAR=0.0
912  
913 void extract_double_array(SV *arrayp,
914 double a[],
915 I32 asize,
916 I32 *limitp);
917  
918 void _compute_best_sse(double al,
919 double ah,
920 double astep,
921 double bl,
922 double bh,
923 double bstep,
924 double dose,
925 SV* valuesp,
926 SV* dosesp)
927 {
928 double a, b, d, diff, sse, tmp, y_hat, d2;
929 double a_best, b_best, d_best, sse_best;
930 SV* param_ret;
931 SV* best_sse_ret;
932 I32 count = 0;
933 I32 i;
934 I32 vlimit, dlimit;
935 #define DOSE_SIZE 100
936 double doses[DOSE_SIZE];
937 double values[DOSE_SIZE];
938 Inline_Stack_Vars;
939  
940 extract_double_array(valuesp, values, DOSE_SIZE, &vlimit);
941 extract_double_array(dosesp, doses, DOSE_SIZE, &dlimit);
942 if (vlimit != dlimit) {
943 fprintf(stderr, "Error in compute_best_sse: limit mismatch. vlimit = %d dlimit = %s\n",
944 vlimit, dlimit);
945 exit(1);
946 }
947
948 for (a = al; a < ah; a += astep) {
949 for (b = bh; b > bl && a <= b; b -= bstep) {
950 diff = b - a;
951 for (d = -6; d < 6.3; d += 0.3) {
952 sse = 0;
953 for (i = 0; i <= dlimit; i++) {
954 tmp = doses[i] / dose;
955 tmp = pow(tmp, d);
956 y_hat = a + diff / (1 + tmp);
957 d2 = (y_hat - values[i]);
958 sse += d2 * d2;
959 }
960 if (++count == 1 || sse < sse_best) {
961 a_best = a;
962 b_best = b;
963 d_best = d;
964 sse_best = sse;
965 }
966 }
967 }
968 }
969 Inline_Stack_Reset;
970 Inline_Stack_Push(sv_2mortal(newSVnv(a_best)));
971 Inline_Stack_Push(sv_2mortal(newSVnv(b_best)));
972 Inline_Stack_Push(sv_2mortal(newSVnv(d_best)));
973 Inline_Stack_Push(sv_2mortal(newSVnv(sse_best)));
974 Inline_Stack_Done;
975 }
976  
977 void extract_double_array(SV *arrayp,
978 double a[],
979 I32 asize,
980 I32 *limitp)
981 /*
982 * Extract the elements of the Perl array, pointed to by arrayp,
983 * into the C array a. The size of a is asize, and the code will check
984 * for overflow. The limiting subscript for arrayp will be returned in
985 * *limitp. */
986  
987 {
988 AV* array;
989 I32 limit, i;
990  
991 array = SvRV(arrayp);
992 limit = av_len(array);
993 if (limit+1 > asize) {
994 fprintf(stderr,
995 "extract_double_array: Perl array is too big. limit = %d asize = %d\n",
996 limit, asize);
997 exit(1);
998 }
999 for (i = 0; i <= limit; i++) {
1000 SV *element, **elementp;
1001 elementp = av_fetch(array, i, 0);
1002 element = *elementp;
1003 a[i] = SvNV(element);
1004 }
1005 *limitp = limit;
1006 }
1007  
1008 END_OF_C_CODE
↑ Summary lib/Bio/sdrs.pl »