def do_generate(args): # Make sure the requested configuration file exists for conf in args.config: if not conf.exists(): exit(msg="No such configuration file: %s" % conf, status=1) # Build but DON'T PROCESS the Config object # This is fast, and gives us enough information to check whether or not try: config = Configuration(configfile=args.config, stdin_data=args.stdin, prior=args.prior, force_glottolog_load=args.report) except wrap_errors as e: # PRAGMA: NO COVER exit(msg="Error encountered while parsing configuration file:", status=2, exception=True) # Make sure we can write to the appropriate output filename output_filename = pathlib.Path( args.output) if args.output else config.admin.path(".xml") if output_filename.exists() and not args.overwrite: exit( msg= "File %s already exists! Run beastling with the --overwrite option if you wish " "to overwrite it." % output_filename, status=4) # Now that we know we will be able to save the resulting XML, we can take # the time to process the config object try: config.process() except wrap_errors as e: # pragma: no cover exit(msg="Error encountered while parsing configuration file:", status=2, exception=True) # Build XML file try: xml = BeastXml(config) except wrap_errors as e: # pragma: no cover exit(msg="Error encountered while building BeastXML object:", status=3, exception=True) # Write XML file xml.write_file(output_filename) # Build and write report if args.report: report = BeastlingReport(config) report.write_file(output_filename.parent / config.admin.path(".md")) geojson = BeastlingGeoJSON(config) geojson.write_file(output_filename.parent / config.admin.path(".geojson")) # Build and write language list if args.language_list: write_language_list(config)
def test_calibration(config_factory): config = _processed_config(config_factory, 'basic', 'calibration') assert {'Cushitic'} == set(config.calibrations) v = config.calibrations['Cushitic'] assert 'DistributionForCushiticMRCA' in BeastXml(config).tostring().decode('utf8') # Now remove one calibration point ... del config.calibrations['Cushitic'] assert 'DistributionForCushiticMRCA' not in BeastXml(config).tostring().decode('utf8') # ... and add it back in with using the glottocode: config.calibrations['cush1243'] = v assert 'DistributionForcush1243MRCA' in BeastXml(config).tostring().decode('utf8')
def test_file_embedding(self): config = self._make_cfg('glottolog_families_from_file', 'embed_data') xml = BeastXml(config).tostring().decode('utf8') # Check for evidence of data self.assertTrue("aari1239,1,1,1,1,1,1,?,1,?,1" in xml) # Check for evidence of families self.assertTrue("Malayo-Polynesian" in xml)
def test_binarisation_ascertainment(self): # Even with ascertainment = False, ascertainment should still # be done for recoded data config = self._make_cfg('covarion_binarised', 'ascertainment_false') config.process() xml = BeastXml(config).tostring().decode('utf8') self.assertTrue("ascertained=\"true\"" in xml) self.assertTrue("excludeto=\"1\"" in xml)
def test_calibration(self): config = self._make_cfg('basic', 'calibration') config.process() self.assertIn('Austronesian', config.calibrations) v = config.calibrations['Austronesian'] xml1 = BeastXml(config).tostring().decode('utf8') # Now remove one calibration point ... del config.calibrations['Austronesian'] xml2 = BeastXml(config).tostring().decode('utf8') self.assertNotEqual(len(xml1.split('CalibrationDistribution.')), len(xml2.split('CalibrationDistribution.'))) # ... and add it back in with using the glottocode: config.calibrations['aust1307'] = v xml2 = BeastXml(config).tostring().decode('utf8') self.assertEqual(len(xml1.split('CalibrationDistribution.')), len(xml2.split('CalibrationDistribution.')))
def do_generate(args): # Build config object for conf in args.config: if not os.path.exists(conf): errmsg("No such configuration file: %s\n" % conf) sys.exit(1) try: config = beastling.configuration.Configuration( configfile=args.config, stdin_data=args.stdin, prior=args.prior) config.process() except Exception as e: errmsg("Error encountered while parsing configuration file:\n") traceback.print_exc() sys.exit(2) # If verbose mode is on, print any messages which were generated while # processing the configuration if args.verbose: for msg in config.messages: errmsg(msg + "\n") # Build XML file try: xml = BeastXml(config) except Exception as e: errmsg("Error encountered while building BeastXML object:\n") traceback.print_exc() sys.exit(3) # Write XML file filename = args.output if args.output else config.basename+".xml" if os.path.exists(filename) and not args.overwrite: errmsg("File %s already exists! Run beastling with the --overwrite option if you wish to overwrite it.\n" % filename) sys.exit(4) xml.write_file(filename) # Build and write report if args.report: report = BeastlingReport(config) report.write_file(config.basename+".md") geojson = BeastlingGeoJSON(config) geojson.write_file(config.basename+".geojson")
def test_validate_ids(config_factory): config = config_factory('basic') bml = BeastXml(config) xml.data(bml.beast, id='theid') xml.data(bml.beast, id='theid') with pytest.raises(ValueError, match='Duplicate'): bml.validate_ids() bml = BeastXml(config) xml.data(bml.beast, idref='theid') with pytest.raises(ValueError, match='missing'): bml.validate_ids()
def test_calibration(self): config = self._make_cfg('basic', 'calibration') config.process() self.assertEqual({'Cushitic'}, set(config.calibrations)) v = config.calibrations['Cushitic'] self.assert_in_xml('DistributionForCushiticMRCA', config) # Now remove one calibration point ... del config.calibrations['Cushitic'] xml2 = BeastXml(config).tostring().decode('utf8') self.assertNotIn('DistributionForCushiticMRCA', xml2) # ... and add it back in with using the glottocode: config.calibrations['cush1243'] = v self.assert_in_xml('DistributionForcush1243MRCA', config)
def test_calibration(self): config = self._make_cfg('basic', 'calibration') config.process() self.assertIn('austronesian', config.calibrations) v = config.calibrations['austronesian'] xml1 = BeastXml(config).tostring().decode('utf8') # Now remove one calibration point ... del config.calibrations['austronesian'] xml2 = BeastXml(config).tostring().decode('utf8') self.assertNotEqual( len(xml1.split('CalibrationDistribution.')), len(xml2.split('CalibrationDistribution.'))) # ... and add it back in with using the glottocode: config.calibrations['aust1307'] = v xml2 = BeastXml(config).tostring().decode('utf8') self.assertEqual( len(xml1.split('CalibrationDistribution.')), len(xml2.split('CalibrationDistribution.')))
def test_random_tree(config_factory): """Load a very basic config file and test that RandomTree from the BEAST core is used.""" config = config_factory('basic') xml = BeastXml(config).tostring().decode('utf8') assert "beast.evolution.tree.RandomTree" in xml
def test_bad_frequencies(config_factory): cfg = _processed_config(config_factory, "bad_frequencies") with pytest.raises(ValueError): # This is an error in a model section, which is only raised # when the model is constructed as XML. BeastXml(cfg)
def test_simple_random_tree(config_factory): """Load a config file with a Uniform calibration and test that SimpleRandomTree from BEASTLabs is used.""" config = config_factory('basic', 'calibration_uniform_range') xml = BeastXml(config).tostring().decode('utf8') assert "beast.evolution.tree.SimpleRandomTree" in xml
def test_constrained_random_tree(config_factory): """Load a config file with monophyly constraints and test that ConstraintedRandomTree from BEASTLabs is used.""" config = config_factory('basic', 'monophyletic') xml = BeastXml(config).tostring().decode('utf8') assert "beast.evolution.tree.ConstrainedRandomTree" in xml
def test_constrained_random_tree(self): """Load a config file with monophyly constraints and test that ConstraintedRandomTree from BEASTLabs is used.""" config = self._make_cfg('basic', 'monophyletic') xml = BeastXml(config).tostring().decode('utf8') self.assertTrue("beast.evolution.tree.ConstrainedRandomTree" in xml)
def test_random_tree(self): """Load a very basic config file and test that RandomTree from the BEAST core is used.""" config = self._make_cfg('basic') xml = BeastXml(config).tostring().decode('utf8') self.assertTrue("beast.evolution.tree.RandomTree" in xml)
def test_simple_random_tree(self): """Load a config file with a Uniform calibration and test that SimpleRandomTree from BEASTLabs is used.""" config = self._make_cfg('basic', 'calibration_uniform_range') xml = BeastXml(config).tostring().decode('utf8') self.assertTrue("beast.evolution.tree.SimpleRandomTree" in xml)
def test_xml(config_factory, cfgs, in_xml): config = _processed_config(config_factory, *cfgs) xml = BeastXml(config).tostring().decode('utf8') for s in in_xml: assert s in xml, s
def test_path_sampling(config_factory): config = config_factory('basic') config.mcmc.path_sampling = True bml = BeastXml(config) bml.validate_ids()
def test_bad_frequencies(self): cfg = self._make_bad_cfg("bad_frequencies") cfg.process() # This is an error in a model section, which is only raised # when the model is constructed as XML. BeastXml(cfg)
def assert_in_xml(self, string, config): xml = BeastXml(config).tostring().decode('utf8') self.assertIn(string, xml)