def test_most_probable_isotopic_composition(): assert (Formula.from_string('F').most_probable_isotopic_composition() == ( Formula({ "F[19]": 1, "F[18]": 0 }), 1.0)) Br2 = Formula.from_string("Br2") assert Br2.most_probable_isotopic_composition()[0] == Formula({ "Br[79]": 1, "Br[81]": 1 }) assert rounders(Br2.most_probable_isotopic_composition()[1], "0.000") == decimal.Decimal("0.5") C6Br6 = Formula.from_string("C6Br6") assert C6Br6.most_probable_isotopic_composition()[0] == Formula({ "C[12]": 6, "C[13]": 0, "Br[79]": 3, "Br[81]": 3 }) assert rounders(C6Br6.most_probable_isotopic_composition()[1], "0.000") == decimal.Decimal("0.293") assert ( Formula.from_string("F10").most_probable_isotopic_composition() == ( Formula({ "F[19]": 10, }), 1.0)) assert Formula.from_string("CF4").most_probable_isotopic_composition( elements_with_isotopes=['F'], ) == (Formula({'C': 1, "F[19]": 4}), 1.0) # yapf: disable
def test_formula_sum(): # Test sum of Formula objects. assert Formula.from_string("C6H12O6") + Formula.from_string("H2O") == { 'C': 6, 'H': 14, 'O': 7 }
def test_formula(self): assert Molecule( name="Dimethyl Phthalate", formula="C10 H10 O4" ).formula == Formula({'C': 10, 'H': 10, 'O': 4}) assert Molecule( name="Dimethyl Phthalate", formula=Formula({'C': 10, 'H': 10, 'O': 4}) ).formula == Formula({'C': 10, 'H': 10, 'O': 4}) assert Molecule(name="Dimethyl Phthalate").formula == Formula()
def test_iter_isotopologues(): iter_isotopologues = Formula.from_string("C6Br6").iter_isotopologues() assert len(list(iter_isotopologues)) == 49 iter_isotopologues = Formula.from_string("C6Br6").iter_isotopologues( elements_with_isotopes="Br") assert len(list(iter_isotopologues)) == 7
def test_isotope_formula(): # from dict f3 = Formula({"C[12]": 10, "C[13]": 2, 'H': 11, 'N': 1}) assert dict(f3) == {"[12C]": 10, "[13C]": 2, 'H': 11, 'N': 1} # from composition f5 = Formula(f3) assert f5 == f3
def test_properties(): f = Formula.from_string("C6H12O6") # Sugar assert f.hill_formula == "C6H12O6" assert f.empirical_formula == "CH2O" assert f.n_atoms == 24 assert f.n_elements == 3 f = Formula.from_string("D2O") # heavy water assert f.hill_formula == "D2O" assert f.empirical_formula == "D2O" assert f.monoisotopic_mass == 20.02311817516 assert f.mass == 20.0276085556 assert f.n_atoms == 3 assert f.n_elements == 2 assert D.nominalmass == 2 assert O.nominalmass == 16
def test_calculate_mz(charge): # Calculate m/z of an ion. assert Formula.from_string("C12H13N+").exact_mass == Formula.from_string( "C12H13N", charge=1).exact_mass # perhaps math.isclose() # assert Formula.from_string("C12H13N+").mass == Formula.from_string("C12H13N", charge=1).exact_mass # assert Formula.from_string("C12H13N+").exact_mass == Formula.from_string("C12H13N", charge=1).mass # assert Formula.from_string("C12H13N+").mass == Formula.from_string("C12H13N", charge=1).mass assert Formula.from_string("C12H13N+").get_mz() == Formula.from_string( "C12H13N", charge=1).get_mz() assert Formula.from_string("C12H13N+").average_mz == Formula.from_string( "C12H13N", charge=1).get_mz() assert Formula.from_string("C12H13N+").mz == Formula.from_string( "C12H13N", charge=1).get_mz(average=False) assert Formula.from_string("C12H13N+").get_mz() == Formula.from_string( "C12H13N", charge=1).average_mz
def test_charged_formula(): # From string f1 = Formula.from_string("(C6H5)2NH+") f2 = Formula.from_string("C12H11N", charge=1) assert f1 == f2 # from dict f3 = Formula({'C': 12, 'H': 11, 'N': 1}, charge=1) assert f3 == {'C': 12, 'H': 11, 'N': 1} assert f1 == f3 assert f2 == f3 # from kwargs f4 = Formula.from_kwargs(C=12, H=11, N=1, charge=1) assert f1 == f4 assert f2 == f4 assert f3 == f4 # from composition f5 = Formula(f1) assert f5 == f1 assert f5 == f2 for charge in [1, 2, 3]: with pytest.raises( ValueError, match= "Cannot supply 'charge' when the formula already has a charge!", ): Formula.from_string(f"BCHFKOH+{charge:d}", charge + 1)
def test_formula(): # From string f1 = Formula.from_string("(C6H5)2NH") f2 = Formula.from_string("C12H11N") assert f1 == f2 # from dict f3 = Formula({'C': 12, 'H': 11, 'N': 1}) assert f1 == f3 assert f2 == f3 # from kwargs f4 = Formula.from_kwargs(C=12, H=11, N=1) assert f1 == f4 assert f2 == f4 assert f3 == f4 # from composition f5 = Formula(f1) assert f5 == f1 assert f5 == f2 s = Formula.from_string("H+") assert s.charge == 1 assert s.mass == 1.007941
def __init__( self, name: str, formula: Union[str, Formula, None] = None, matches: Optional[Dict[str, "Score"]] = None, ): super().__init__() self.name = str(name) if isinstance(formula, Formula): self.formula = formula elif formula is not None: self.formula = Formula.from_string(formula) else: self.formula = Formula() if isinstance(matches, dict): self.matches = matches elif matches is None: self.matches = {} else: raise TypeError( f"'matches' must be a dictionary, not {type(matches)}")
def test_dict(): assert dict( Molecule( name="Dimethyl Phthalate", formula="C10 H10 O4", matches={ "overall": Score(62.90), "tgt": Score(62.90, flag_string="low score", flag_severity=2), }, ) ) == { "name": "Dimethyl Phthalate", "formula": Formula({'C': 10, 'H': 10, 'O': 4}), "matches": { "overall": Score(62.90), "tgt": Score(62.90, flag_string="low score", flag_severity=2), }, }
def test_calculate_mass(): # Calculate mass by a formula. mass = rounders( Formula.from_string("(C6H5)2NH").monoisotopic_mass, "0.000000") assert mass == decimal.Decimal("169.089149") # Calculate average mass / molecular weight by a formula. assert rounders(Formula.from_string("(C6H5)2NH").average_mass, "0.00") == decimal.Decimal("169.22") assert Formula.from_string( "(C6H5)2NH").average_mass == Formula.from_string( "(C6H5)2NH").average_mass # mz assert Formula.from_string("C12H13N+").get_mz() == Formula.from_string( "C12H13N", charge=1).average_mass
def test_parsing(formula, data): assert Formula.from_string(formula) == data
def test_invalid_formulae_unknown_element(formula): with pytest.raises( ValueError, match=f"Unknown chemical element with symbol {formula}"): Formula.from_string(formula)
def test_invalid_formulae(formula): pattern = f"Unrecognised formula: {re.escape(formula.replace(' ', ''))}" with pytest.raises(ValueError, match=pattern): Formula.from_string(formula)
def test_empirical_formula(formula_1, formula_2): f1 = Formula.from_string(formula_1) f2 = Formula.from_string(formula_2) assert f1.empirical_formula == f2.empirical_formula assert f1.empirical_formula == formula_2
def test_equivalent(formula_1, formula_2): f1 = Formula.from_string(formula_1) f2 = Formula.from_string(formula_2) assert f1 == f2
def test_masses(formula, mass, exact_mass): f = Formula.from_string(formula) print(f) assert rounders(f.mass, "0.00000") == decimal.Decimal(mass) assert rounders(f.exact_mass, "0.00000") == decimal.Decimal(exact_mass)
def C6Br6(): return Formula.from_string("C6Br6")
def Br2(): return Formula.from_string("Br2")
def test_iter_isotopologues_with_abundances(): for state, abundance in Formula.from_string("BCHFKO").iter_isotopologues( elements_with_isotopes='F', report_abundance=True): assert state assert abundance
def test_Composition_sub(): # Test subtraction of Composition objects assert {} - Formula.from_string("C6H12O6") == {'C': -6, 'H': -12, 'O': -6}
def test_Composition_mul(): # Test multiplication of Composition by integers f1 = Formula.from_string("H2O2") assert f1 == {'H': 2, 'O': 2} assert 2 * f1 == {'H': 4, 'O': 4} assert f1 * 2 == {'H': 4, 'O': 4}