def MutsFromTrees(Trees): print('read trees') ts = pyslim.load(Trees).simplify() variants = [] # for variant in pyslim.extract_mutation_metadata(ts.tables): # print(variant) # return for variant, mut, node in zip(ts.variants(), pyslim.extract_mutation_metadata(ts.tables), pyslim.extract_node_metadata(ts.tables)): # print () # print (variant) # print (mut) # print (node) freq = sum(variant.genotypes) / len(variant.genotypes) # print (freq) if freq >= 1: continue if len(mut) > 1: pass variants.append([variant.site.position, freq, mut[0].selection_coeff]) return variants
def test_reload_annotate(self): """ Test the ability of SLiM to load our files after annotation. """ for ts, basename in self.get_slim_restarts(): tables = ts.tables metadata = list(pyslim.extract_mutation_metadata(tables)) has_nucleotides = (metadata[0][0].nucleotide >= 0) if has_nucleotides: nucs = [random.choice([0, 1, 2, 3]) for _ in metadata] refseq = "".join( random.choices(pyslim.NUCLEOTIDES, k=int(ts.sequence_length))) for n, md in zip(nucs, metadata): for j in range(len(md)): md[j].nucleotide = n else: refseq = None for md in metadata: for j in range(len(md)): md[j].selection_coeff = random.random() pyslim.annotate_mutation_metadata(tables, metadata) in_ts = pyslim.load_tables(tables, reference_sequence=refseq) # put it through SLiM (which just reads in and writes out) out_ts = self.run_slim_restart(in_ts, basename) # check for equality, in everything but the last provenance self.verify_slim_restart_equality(in_ts, out_ts)
def test_annotate_mutations(self): for ts in get_msprime_examples(): slim_ts = pyslim.annotate_defaults(ts, model_type="nonWF", slim_generation=1) tables = slim_ts.tables metadata = list(pyslim.extract_mutation_metadata(tables)) self.assertEqual(len(metadata), slim_ts.num_mutations) selcoefs = [random.uniform(0, 1) for _ in metadata] for j in range(len(metadata)): metadata[j].selection_coeff = selcoefs[j] pyslim.annotate_mutation_metadata(tables, metadata) new_ts = pyslim.load_tables(tables) for j, x in enumerate(new_ts.mutations()): md = pyslim.decode_mutation(x.metadata) self.assertEqual(md.selection_coeff, selcoefs[j])
def verify_defaults(self, ts): ''' Verify the default values have been entered into metadata. ''' mut_md = pyslim.extract_mutation_metadata(ts.tables) for md in mut_md: self.assertEqual(md.mutation_type, 1) self.assertEqual(md.selection_coeff, 0.0) self.assertEqual(md.population, tskit.NULL) self.assertEqual(md.slim_time, 0) node_md = pyslim.extract_node_metadata(ts.tables) for md, node in zip(node_md, ts.nodes()): if not node.is_sample(): self.assertEqual(md, None) else: self.assertEqual(md.is_null, False) self.assertEqual(md.genome_type, pyslim.GENOME_TYPE_AUTOSOME) for ind in ts.individuals(): self.assertArrayEqual(ind.location, [0, 0, 0]) self.assertEqual(ind.flags, pyslim.INDIVIDUAL_ALIVE) ind_md = pyslim.extract_individual_metadata(ts.tables) for md in ind_md: self.assertEqual(md.sex, pyslim.INDIVIDUAL_TYPE_HERMAPHRODITE) self.assertEqual(md.flags, 0) pop_md = pyslim.extract_population_metadata(ts.tables) for md in pop_md: self.assertEqual(md.selfing_fraction, 0.0) self.assertEqual(md.female_cloning_fraction, 0.0) self.assertEqual(md.male_cloning_fraction, 0.0) self.assertEqual(md.sex_ratio, 0.5) self.assertEqual(md.bounds_x0, 0.0) self.assertEqual(md.bounds_x1, 0.0) self.assertEqual(md.bounds_y0, 0.0) self.assertEqual(md.bounds_y1, 0.0) self.assertEqual(md.bounds_z0, 0.0) self.assertEqual(md.bounds_z1, 0.0) self.assertEqual(len(md.migration_records), 0)