def test_phred_conversions(): assert util.phred_to_prob(40) == 0.9999 assert util.phred_to_prob(30) == 0.999 assert util.phred_to_prob(20) == 0.99 assert util.phred_to_prob(10) == 0.9 assert util.prob_to_phred(0.9999) == 40 assert util.prob_to_phred(0.999) == 30 assert util.prob_to_phred(0.99) == 20 assert util.prob_to_phred(0.9) == 10
def mut_sequence(self, record, orientation): """Introduce substitution errors to a sequence If a random probability is higher than the probability of the basecall being correct, introduce a substitution error Args: record (SeqRecord): a read record with error scores orientation (string): orientation of the read. Can be 'forward' or 'reverse' Returns: Seq: a sequence """ # get the right subst_matrix if orientation == 'forward': nucl_choices = self.subst_choices_for elif orientation == 'reverse': nucl_choices = self.subst_choices_rev mutable_seq = record.seq.tomutable() quality_list = record.letter_annotations["phred_quality"] position = 0 for nucl, qual in zip(mutable_seq, quality_list): if random.random() > util.phred_to_prob(qual) \ and nucl.upper() not in 'RYWSMKHBVDN': mutable_seq[position] = str( np.random.choice( nucl_choices[position][nucl.upper()][0], p=nucl_choices[position][nucl.upper()][1])) position += 1 return mutable_seq.toseq()
def gen_phred_scores(self, mean_quality, orientation): """Generate a normal distribution, transform to phred scores Generate a list of phred score according to a normal distribution centered around the ErrorModel quality Args: mean_quality (int): mean phred score Returns: list: list of phred scores following a normal distribution """ norm = [min(q, 0.9999) for q in np.random.normal( util.phred_to_prob(mean_quality), 0.01, self.read_length)] phred = [util.prob_to_phred(p) for p in norm] return phred