def test_str(self): assert str(Score(7)) == "7.0" assert str(Score(7, flag_string="Hello", flag_severity=8)) == "7.0" assert repr(Score(7)) == "Score(7.0)" assert repr( Score(7, flag_string="Hello", flag_severity=8)) == "Score(7.0, Flag('Hello', severity=8))"
def test_repr(): molecule = Molecule( name="Dimethyl Phthalate", formula="C10 H10 O4", matches={ "overall": Score(62.90), "tgt": Score(62.90, flag_string="low score", flag_severity=2), }, ) assert str(molecule) == "Molecule(Dimethyl Phthalate, C10H10O4)" assert repr(molecule) == "<Molecule(Dimethyl Phthalate, Formula({'C': 10, 'H': 10, 'O': 4}))>"
def test_matches(self): molecule = Molecule( name="Dimethyl Phthalate", matches={ "overall": Score(62.90), "tgt": Score(62.90, flag_string="low score", flag_severity=2), }, ) assert molecule.matches == { "overall": Score(62.90), "tgt": Score(62.90, flag_string="low score", flag_severity=2), } with pytest.raises(TypeError, match="'matches' must be a dictionary, not"): Molecule(name="Dimethyl Phthalate", matches="Hello World") # type: ignore
def compound(spectrum, molecule): return Compound( algo="FindByFormula", location={'m': 169.0893, "rt": 13.649, 'a': 29388223, 'y': 3377289}, results=[molecule], spectra=[spectrum], compound_scores={"fbf": Score(62.90, flag_string="low score", flag_severity=2)}, )
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_dict(spectrum, molecule, compound): as_dict = { "algo": "FindByFormula", "location": {'m': 169.0893, "rt": 13.649, 'a': 29388223, 'y': 3377289}, "results": [molecule], "spectra": [spectrum], "compound_scores": {"fbf": Score(62.90, flag_string="low score", flag_severity=2)}, } assert dict(compound) == as_dict
class TestCreation: def test_algo(self): assert Compound(algo="FindByFormula").algo == "FindByFormula" def test_location(self): assert Compound( algo="FindByFormula", location={'m': 169.0893, "rt": 13.649, 'a': 29388223, 'y': 3377289} ).location == {'m': 169.0893, "rt": 13.649, 'a': 29388223, 'y': 3377289} assert Compound( algo="FindByFormula", location={'m': 169.0893, "rt": 13.649, 'a': 29388223, 'y': 3377289} ).location == {'m': 169.0893, "rt": 13.649, 'a': 29388223, 'y': 3377289} assert Compound(algo="FindByFormula").location == {} @pytest.mark.parametrize( "scores", [ {"fbf": Score(56.24, flag_string="low score; No H adduct", flag_severity=2)}, {"fbf": Score(82.53, flag_string="No H adduct", flag_severity=2)}, {"fbf": Score(60.62, flag_string="low score", flag_severity=2)}, {"fbf": Score(62.90, flag_string="low score", flag_severity=2)}, ] ) def test_compound_scores(self, scores): assert Compound(algo="FindByFormula", compound_scores=scores).compound_scores == copy.deepcopy(scores) assert Compound(algo="FindByFormula").compound_scores == {} def test_results(self, molecule): results = [molecule, molecule] assert Compound(algo="FindByFormula", results=results).results == copy.deepcopy(results) assert Compound(algo="FindByFormula", results=()).results == [] assert Compound(algo="FindByFormula", results=[]).results == [] assert Compound(algo="FindByFormula").results == [] def test_spectra(self, spectrum): assert Compound(algo="FindByFormula", spectra=[spectrum]).spectra == [spectrum] assert Compound(algo="FindByFormula", spectra=(spectrum, )).spectra == [spectrum] assert Compound(algo="FindByFormula", spectra=()).spectra == [] assert Compound(algo="FindByFormula", spectra=[]).spectra == [] assert Compound(algo="FindByFormula").spectra == []
def test_repr(compound, spectrum, molecule, file_regression: FileRegressionFixture): assert str(compound) == "Compound([Molecule(Dimethyl Phthalate, C10H10O4)])" assert repr(compound) == "<Compound([<Molecule(Dimethyl Phthalate, Formula({'C': 10, 'H': 10, 'O': 4}))>])>" compound = Compound( algo="FindByFormula", location={'m': 169.0893, "rt": 13.649, 'a': 29388223, 'y': 3377289}, results=[molecule, molecule, molecule, molecule, molecule], spectra=[spectrum], compound_scores={"fbf": Score(62.90, flag_string="low score", flag_severity=2)}, ) file_regression.check(str(compound), encoding="UTF-8", extension="_str.txt") file_regression.check(repr(compound), encoding="UTF-8", extension="_repr.txt")
def expected_compound(fbf_spectrum, tof_spectrum): score = Score(99.79) return Compound( algo="FindByFormula", location={'m': 169.0893, "rt": 13.649, 'a': 29388223, 'y': 3377289}, compound_scores={"fbf": score}, results=[ Molecule( name="Diphenylamine", formula="C12 H11 N", matches={ "overall": score, "tgt": score, } ) ], spectra=[fbf_spectrum, tof_spectrum], )
raw_xml = """ <Molecule name="Dimethyl Phthalate" formula="C10 H10 O4"> <MatchScores> <Match algo="overall" score="62.90" /> <Match algo="tgt" score="62.90" tgtFlagsString="low score" tgtFlagsSeverity="2" /> </MatchScores> </Molecule> """ expects = Molecule( name="Dimethyl Phthalate", formula="C10 H10 O4", matches={ "overall": Score(62.90), "tgt": Score(62.90, flag_string="low score", flag_severity=2), }, ) @pytest.mark.parametrize("raw_xml, expects", [(raw_xml, expects)]) def test_from_xml(raw_xml, expects): tree = lxml.objectify.fromstring(raw_xml) spec = Molecule.from_xml(tree) assert spec == expects raw_xml_1 = '<MatchScores><Match algo="fbf" score="86.30" /></MatchScores>' raw_xml_2 = '<MatchScores><Match algo="fbf" score="56.24" tgtFlagsString="low score; No H adduct" tgtFlagsSeverity="2" /></MatchScores>' raw_xml_3 = '<MatchScores><Match algo="fbf" score="82.53" tgtFlagsString="No H adduct" tgtFlagsSeverity="2" /></MatchScores>'
def test_equals(self): assert Score(7) == Score(7) assert Score(7) != Score(8) assert Score(7, flag_string="Hello") != Score(8) assert Score(7, flag_string="Hello", flag_severity=3) != Score(8) assert Score(7, flag_string="Hello") == Score(7, flag_string="Hello") assert Score(7, flag_string="Hello", flag_severity=3) == Score(7, flag_string="Hello", flag_severity=3) assert Score(7.5, flag_string="Hello", flag_severity=3) == Score(7.5, flag_string="Hello", flag_severity=3) assert Score(7, flag_string="Hello") != Score(7, flag_string="World") assert Score(7, flag_string="Hello", flag_severity=2) != Score( 7, flag_string="Hello", flag_severity=3) score = Score(56.24, Flag("low score; No H adduct", severity=2)) assert score == Score(56.24, Flag("low score; No H adduct", severity=2))
def test_creation(self): assert Score(7) == 7 assert Score(7, flag_string="Hello") == 7 assert Score(7, flag_string="Hello", flag_severity=3) == 7
def test_float(self): assert float(Score(7)) == 7.0 assert float(Score(7, flag_string="Hello", flag_severity=8)) == 7.0
}), (raw_xml_3, { "fbf": 82.53 }), (raw_xml_4, { "fbf": 60.62 }), (raw_xml_5, { "fbf": 62.90 }), (raw_xml_multiline, { "fbf": 62.90, "abc": 12.34 }), (raw_xml_1, { "fbf": Score(86.3) }), (raw_xml_2, { "fbf": Score(56.24, flag_string="low score; No H adduct", flag_severity=2) }), (raw_xml_3, { "fbf": Score(82.53, flag_string="No H adduct", flag_severity=2) }), (raw_xml_4, { "fbf": Score(60.62, flag_string="low score", flag_severity=2) }), (raw_xml_5, { "fbf": Score(62.90, flag_string="low score", flag_severity=2) }), (raw_xml_multiline, {