Source of file Tolerance.php
Size: 4,919 Bytes - Last Modified: 2019-05-10T12:24:09+01:00
src/Core/Tolerance.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
Covered by 12 test(s):
51
Covered by 1 test(s):
52
Covered by 1 test(s):
535455
Covered by 11 test(s):
5657
Covered by 11 test(s):
58
Covered by 11 test(s):
59
Covered by 11 test(s):
60
Covered by 6 test(s):
61
Covered by 6 test(s):
62
Covered by 5 test(s):
63
Covered by 5 test(s):
64
Covered by 4 test(s):
65
Covered by 4 test(s):
66
Covered by 1 test(s):
67
Covered by 1 test(s):
68
Covered by 1 test(s):
69
Covered by 11 test(s):
70
Covered by 10 test(s):
717273747576777879
Covered by 1 test(s):
80818283848586878889
Covered by 1 test(s):
90919293949596979899100101
Covered by 2 test(s):
102
Covered by 1 test(s):
103104105
Covered by 1 test(s):
106107
Covered by 1 test(s):
108109110111112113114115116117118119
Covered by 2 test(s):
120
Covered by 1 test(s):
121122123
Covered by 1 test(s):
124125
Covered by 1 test(s):
126127128129130131132133134135136137138139
Covered by 3 test(s):
140141
Covered by 3 test(s):
142
Covered by 1 test(s):
143144145
Covered by 2 test(s):
146147148149150151152153154155156157158159
Covered by 3 test(s):
160
Covered by 1 test(s):
161162163
Covered by 2 test(s):
164165166167168169170171172173174175176177
Covered by 2 test(s):
178179180
| <?php /** * Copyright 2016 University of Liverpool * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace pgb_liv\php_ms\Core; /** * Class to encapsulate tolerance values with their unit type. * * @author Andrew Collins */ class Tolerance { const DA = 'Da'; const PPM = 'ppm'; const PSI_PPM_ACCESSION = 'UO:0000169'; const PSI_DA_ACCESSION = 'UO:0000221'; private $tolerance; private $unit; /** * Creates a new instance of this object with a specified tolerance value and unit type * * @param float $tolerance * Tolerance numeric value * @param string $unit * Tolerance unit type (See Tolerance::DA and Tolerance::PPM) * @throws \InvalidArgumentException If Argument 1 is not a float or argument 2 is not an acceptable unit type */ public function __construct($tolerance, $unit) { if (! is_float($tolerance) && ! is_int($tolerance)) { throw new \InvalidArgumentException( 'Argument 1 must be a float or int value. Valued passed is of type ' . gettype($tolerance)); } $this->tolerance = $tolerance; switch (strtolower($unit)) { case strtolower(Tolerance::PPM): case strtolower(Tolerance::PSI_PPM_ACCESSION): $this->unit = Tolerance::PPM; break; case strtolower(Tolerance::DA): case strtolower(Tolerance::PSI_DA_ACCESSION): $this->unit = Tolerance::DA; break; default: throw new \InvalidArgumentException( 'Argument 2 must equal "Da" or "ppm". Valued passed is "' . $unit . '"'); } } /** * Gets the numeric tolerance value. * * @return float */ public function getTolerance() { return $this->tolerance; } /** * Gets the tolerance unit type, Tolerance::PPM or Tolerance::DA * * @return string */ public function getUnit() { return $this->unit; } /** * Calculates the Dalton tolerance value of this mass using the set tolerance value * * @param float $mass * Mass to calculate tolerance for * @return float Tolerance value */ public function getDaltonDelta($mass) { if ($this->unit == Tolerance::DA) { return $this->tolerance; } $toleranceRatio = $this->tolerance / 1000000; return $mass * $toleranceRatio; } /** * Calculates the ppm tolerance value of this mass using the set tolerance value * * @param float $mass * Mass to calculate tolerance for * @return float Tolerance value */ public function getPpmDelta($mass) { if ($this->unit == Tolerance::PPM) { return $this->tolerance; } $toleranceRatio = $this->tolerance / $mass; return $toleranceRatio * 1000000; } /** * Test if the observed and expected values are within the accepted tolerance * * @param float $observed * The observed value * @param float $expected * The expected value * @return boolean */ public function isTolerable($observed, $expected) { $diff = $this->getDifference($observed, $expected); if ($diff > $this->tolerance) { return false; } return true; } /** * Gets the difference between the observed and expected, and returns it depending on the instance unit * * @param float $observed * The observed value * @param float $expected * The expected value * @return float */ public function getDifference($observed, $expected) { if ($this->unit == Tolerance::DA) { return abs($observed - $expected); } return abs(self::getDifferencePpm($observed, $expected)); } /** * Gets the ppm difference for the observed and expected value * * @param float $observed * The observed value * @param float $expected * The expected value * @return float */ public static function getDifferencePpm($observed, $expected) { return (($observed - $expected) / $expected) * 1000000; } } |