Source of file ChargedMassTrait.php

Size: 4,587 Bytes - Last Modified: 2019-05-10T12:24:09+01:00

src/Core/ChargedMassTrait.php

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
Covered by 3 test(s):
  • pgb_liv\php_ms\Test\Unit\IonTraitTest::testCanSetMassChargeValid
  • pgb_liv\php_ms\Test\Unit\IonTraitTest::testCanSetMassChargeInvalid
  • pgb_liv\php_ms\Test\Unit\IonTraitTest::testCanGetMassFromMz
50
Covered by 2 test(s):
  • pgb_liv\php_ms\Test\Unit\IonTraitTest::testCanSetMassChargeValid
  • pgb_liv\php_ms\Test\Unit\IonTraitTest::testCanGetMassFromMz
51
Covered by 2 test(s):
  • pgb_liv\php_ms\Test\Unit\IonTraitTest::testCanSetMassChargeValid
  • pgb_liv\php_ms\Test\Unit\IonTraitTest::testCanGetMassFromMz
525354555657585960616263646566676869707172737475
Covered by 1 test(s):
  • pgb_liv\php_ms\Test\Unit\IonTraitTest::testCanSetMassChargeValid
767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
<?php
/**
 * Copyright 2018 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;

use pgb_liv\php_ms\Constant\PhysicalConstants;

/**
 * Trait for providing access to setter/getter of monoisotopic and average mass-to-charge values.
 * Does not store an mass-to-charge ratio but calculates on demand from neutral mass and charge values.
 *
 * @author Andrew Collins
 */
trait ChargedMassTrait
{
    use MassTrait;

    /**
     * The charge value for this instance
     *
     * @var int
     */
    private $charge;

    /**
     * Sets the monoisotopic mass-to-charge ratio for this instance.
     *
     * @param float $massCharge
     *            The mass-to-charge ratio to set to
     * @param int $charge
     *            The charge associated with the $massCharge
     * @throws \InvalidArgumentException If $mz is not a valid floating point value
     */
    public function setMonoisotopicMassCharge($massCharge, $charge)
    {
        $this->setMonoisotopicMass($this->getNeutralMass($massCharge, $charge));
        $this->setCharge($charge);
    }

    /**
     * Sets the average mass-to-charge ratio for this instance
     *
     * @param float $massCharge
     *            The mass-to-charge ratio to set to
     * @param int $charge
     *            The charge associated with the $massCharge
     * @throws \InvalidArgumentException If $mz is not a valid floating point value
     */
    public function setAverageMassCharge($massCharge, $charge)
    {
        $this->setAverageMass($this->getNeutralMass($massCharge, $charge));
        $this->setCharge($charge);
    }

    /**
     * Gets the monoisotopic mass-to-charge ratio for this instance
     *
     * @return float
     */
    public function getMonoisotopicMassCharge()
    {
        return $this->getMassCharge($this->getMonoisotopicMass(), $this->charge);
    }

    /**
     * Gets the average mass-to-charge ratio for this ion
     *
     * @return float
     */
    public function getAverageMassCharge()
    {
        return $this->getMassCharge($this->getAverageMass(), $this->charge);
    }

    /**
     * Sets the charge of this instance
     *
     * @param int $charge
     *            The positive or negative charge to set
     */
    public function setCharge($charge)
    {
        $this->charge = $charge;
    }

    /**
     * Gets the charge value associated with this instance
     *
     * @return int charge value
     */
    public function getCharge()
    {
        return $this->charge;
    }

    /**
     * Gets the mass-to-charge value for the specified mass and charge
     *
     * @param float $mass
     * @param int $charge
     * @return float
     */
    public static function getMassCharge($mass, $charge)
    {
        if (! is_float($mass)) {
            throw new \InvalidArgumentException('Argument 1 must be of type float. Value is of type ' . gettype($mass));
        }

        if (! is_int($charge)) {
            throw new \InvalidArgumentException('Argument 2 must be of type int. Value is of type ' . gettype($charge));
        }

        if ($charge == 0) {
            return $mass;
        }

        return ($mass + ($charge * PhysicalConstants::PROTON_MASS)) / $charge;
    }

    /**
     * gets the neutral mass for the specified mass-to-charge ratio and charge
     *
     * @param float $massCharge
     * @param int $charge
     * @throws \InvalidArgumentException
     * @return float
     */
    public static function getNeutralMass($massCharge, $charge)
    {
        if (! is_float($massCharge)) {
            throw new \InvalidArgumentException(
                'Argument 1 must be of type float. Value is of type ' . gettype($massCharge));
        }

        if (! is_int($charge)) {
            throw new \InvalidArgumentException('Argument 2 must be of type int. Value is of type ' . gettype($charge));
        }

        if ($charge == 0) {
            return $massCharge;
        }

        return ($massCharge * $charge) - (PhysicalConstants::PROTON_MASS * $charge);
    }
}