def mutant_string(self, loc, tup): ''' given a location and a set of mutant locations (loc/str tuples), give a mutant string of length location[1] - location[0] that make the changes specified. ''' seq_str = str(self.seq)[slice(*loc)] return mutate.tups_to_str(seq_str, loc, tup)
def mutant_locations(self, loc, count=1, max=False): ''' this function takes from the dict of all potential _mutation_locations a set of mutations that fall within a loc tuple. It returns an iterator that spits out potential mutants at these locations; it is randomized by position first, then by mutation. count is the number of mutations to return, setting to one returns all possible sequences off by one, setting to two returns all sequences with two mutations made, etc, etc. ''' #first, make sure my self._mutant_locations dict is instantiatied if not hasattr(self, '_mutant_locations'): self._mutant_locations = mutate.mutant_locations(self) #deal with interval() versus tuple inputs if isinstance(loc, interval): if len(loc) == 0: return iter([]) loc_ivl = loc loc = interval.hull([loc_ivl]).to_tuple() else: loc_ivl = interval(loc) #create an iterator that returns all keys for _mutant_locations that are #in this location range mut_ivls = (interval(ml) for ml in self._mutant_locations.keys()) #now loc iter will output a non-random set of mutation locations which are #keys to the _mutation_locations dict loc_iter = ifilter(itemgetter(1), ((ivl, ivl.overlaps(loc_ivl)) for \ ivl in mut_ivls)) #change interval obj into loc tuple loc_tup = lambda loc: loc[0].to_tuple() #get the mutation set (the values) for a loc tuple loc_muts = lambda loc: self._mutant_locations[loc_tup(loc)] #expand the mutation set into individual mutations for a loc tuple loc_mset = lambda loc: ((loc_tup(loc), i) for i in loc_muts(loc)) #put them all together for a randomized list of generators, one generator #for each loc tuple pos_mut_sets = map(lambda loc: (loc_mset(loc)), loc_iter) emit_sets = combinations(util.irandomize( chain.from_iterable(pos_mut_sets), seed=random_seed), count) emit_sets = imap(frozenset, emit_sets) is_unique_pos = \ lambda mset: ( len([m[0] for m in mset]) == len(set([m[0] for m in mset])) and set(mset) not in self.mut_sets) mut_iter = util.irandomize( ifilter(is_unique_pos, util.irandomize(emit_sets, seed=random_seed)), seed=random_seed) # if this feature overlaps exons #expand the motif to codons, so that we can check that mutants are # synonymous if interval(self.exon_list[0].extract_pos()).overlaps(loc_ivl): codon_loc = \ (interval(mutate.expand_motif_to_codons(self, loc)) \ & interval(self.exon_list[0].extract_pos())).to_tuple() #check all mutations for synonymousness seq_str = str(self.seq)[slice(*codon_loc)] is_synon = lambda seq_str, codon_loc: lambda mut_tups: \ mutate.check_translation(\ string.upper(mutate.tups_to_str(seq_str, codon_loc, mut_tups)), seq_str) is_synon = is_synon(seq_str, codon_loc) return util.irandomize(ifilter(lambda mut: is_synon(mut), mut_iter), seed=random_seed) else: return mut_iter