def create_new_apple(self, position=None): if position: if not self._is_captured_by_snake(position): self._apple_pos = position return # else: a new apple has been created because a snake bytes the old one while True: apple = (random_range(0, BoardState.get_cols() - 1), random_range(0, BoardState.get_rows() - 1)) if not self._is_captured_by_snake(apple): self._apple_pos = apple break self.leave_tail = True # for the next generations
def init_random_snake(self): apple = (random_range(0, BoardState.get_cols() - 1), random_range(0, BoardState.get_rows() - 1)) self._apple_pos = apple snake_head = None while True: snake_head = ( random_range(0, BoardState.get_cols() - 1), random_range(0, BoardState.get_rows() - 1)) if apple != snake_head: self._snake_positions_list.append(snake_head) break snake_cur_pos = snake_head for snake_body_depth in range(BoardState.TILE_SNAKE_BODY_DEFAULT, SNAKE_INIT_LENGTH+1): snake_next_pos = random_from_seq(list(self._get_closest_blank_points(snake_cur_pos))) self._snake_positions_list.append(snake_next_pos) snake_cur_pos = snake_next_pos
def random(image, clength, **kwargs): intervals = np.array([]) for y in range(image.shape[1]): intervals.append([]) x = 0 while True: x += int(clength * random_range()) if x > image.shape[0]: break else: intervals[y].append(x) return intervals
def build_fwtrack(self, opt): """Build FWTrackII from all lines, return a FWTrackII object. Handle multi-reads here by building a probability and enrichment index or select only one alignment from each multi-read. Initial alignment probabilities are set from read/mismatch qualities or from a uniform distribution. """ fwtrack = FWTrackII() i = 0 m = 0 read_total = 0 recent_tags = [] random_select_one_multi = opt.random_select_one_multi no_multi_reads = opt.no_multi_reads min_score = opt.min_score prior_prob_snp = opt.prior_prob_snp no_prior_prob_map = opt.no_prior_prob_map if opt.qual_scale == 'auto': opt.qual_scale = self._guess_qual_scale() if opt.qual_scale == 'sanger+33': qual_offset = 33 elif opt.qual_scale == 'illumina+64': qual_offset = 64 group_starts_append = fwtrack.group_starts.append fwtrack_add_loc = fwtrack.add_loc match_probs = { } # {(1,30):p(match|phred=30), (0,30):p(mismatch|phred=30)} for grouplines in self._group_by_name(self.fhd): read_total += 1 # in ratios, only count reads, not total alignments if len(grouplines) == 1: # uniquely mapping reads i += 1 if i == 1000000: m += 1 logging.info(" %d alignments read." % (m * 1000000)) i = 0 chromosome, fpos, strand, qualstr, mismatches = grouplines[0] fwtrack_add_loc(chromosome, fpos, strand, 0) # 0'th index => unique else: if no_multi_reads: # throw away multi-reads fwtrack.total -= 1 continue elif random_select_one_multi: # choose one alignment at random i += 1 if i == 1000000: m += 1 logging.info(" %d alignments read." % (m * 1000000)) i = 0 randline = grouplines[random_range(len(grouplines))] chromosome, fpos, strand, qualstr, mismatches = randline fwtrack_add_loc(chromosome, fpos, strand, 0) else: # use all alignments probabilistically group_starts_append( fwtrack.total_multi + 1) # starts at 1 (0 reserved for unique reads) if no_prior_prob_map: # don't use map quality; just assume uniform priors for (chromosome, fpos, strand, qualstr, mismatches) in grouplines: i += 1 if i == 1000000: m += 1 logging.info(" %d alignments read." % (m * 1000000)) i = 0 fwtrack.total_multi += 1 fwtrack_add_loc(chromosome, fpos, strand, fwtrack.total_multi) normed_probs = [1. / len(grouplines)] * len(grouplines) else: # TODO: might want to be working in log space-- if many mismatches, we'll lose precision qualstr = grouplines[0][ 3] # all quality strings are shared across the group group_total_prob = 0. group_probs = [] group_probs_append = group_probs.append for (chromosome, fpos, strand, qualstr, mismatches) in grouplines: i += 1 if i == 1000000: m += 1 logging.info(" %d alignments read." % (m * 1000000)) i = 0 fwtrack.total_multi += 1 fwtrack_add_loc(chromosome, fpos, strand, fwtrack.total_multi) mismatches = set(mismatches) read_prob = 1. # P(SNP) = prior probability a SNP occurs at any base # P(SE) = probability there was a sequencing error (from PHRED) # _P(Map|SNP,SE)__MATCH__SNP__SE_ # 0 0 0 0 # can't map here without explanation # 1 0 0 1 # 1 0 1 0 # 1 0 1 1 # 1 1 0 0 # 1 1 0 1 # 0 1 1 0 # wouldn't map here if SNP, but sequencer read reference # 1 1 1 1 # we are interested in P(Mapping | Match), which is equivalent to: # \Sum_{SNP \in {0,1}, SE \in {0,1}} p(SNP) * p(SE) * p(Map|SE,SNP), or: # p(Map|match = 0): # p(SE) + p(SNP) + p(SE)*p(SNP) # p(Map|match = 1): # 1 - (p(SE) + p(SE)*p(SNP)) for b in xrange(len(qualstr)): tup = (b in mismatches, qualstr[b]) if tup in match_probs: prob = match_probs[tup] elif tup[0]: # mismatch p_seq_error = 10.**( (qualstr[b] - qual_offset) / -10.) prob = p_seq_error + prior_prob_snp + p_seq_error * prior_prob_snp match_probs[tup] = prob else: # match p_seq_error = 10.**( (qualstr[b] - qual_offset) / -10.) prob = 1. - (p_seq_error + p_seq_error * prior_prob_snp) match_probs[tup] = prob read_prob *= prob # quick & dirty check-- only looking at last base assert qualstr[ b] >= qual_offset # Specified quality scale yielded a negative phred score! You probably have the wrong PHRED scale! assert 0. <= read_prob <= 1. # error with map qualities #raise BaseQualityError("Specified quality scale yielded a negative phred score! You probably have the wrong PHRED scale!") group_probs_append(read_prob) group_total_prob += read_prob normed_probs = [ p / group_total_prob for p in group_probs ] fwtrack.prob_aligns.extend(normed_probs) fwtrack.prior_aligns.extend(normed_probs) fwtrack.enrich_scores.extend([min_score] * len(grouplines)) fwtrack.total = read_total # overwrite the running total, counting each read once return fwtrack
def generate_pseudo_random_dates_and_times( self, num_pseudo_random_dates_and_times_to_generate: int, weeks_max_delta: int, days_max_delta: int, hours_max_delta: int, minutes_max_delta: int, seconds_max_delta: int, milliseconds_max_delta: int, microseconds_max_delta: int) -> list: """ Generate and return a given number of Pseudo-Random Dates and Times, starting from a given delta Date and Time, for a generator/list. :param num_pseudo_random_dates_and_times_to_generate: the number of Pseudo-Random Dates and Times to be generated. :param weeks_max_delta: the delta number for weeks. :param days_max_delta: the delta number for days. :param hours_max_delta: the delta number for hours. :param minutes_max_delta: the delta number for minutes. :param seconds_max_delta: the delta number for seconds. :param milliseconds_max_delta: the delta number for milliseconds. :param microseconds_max_delta: the delta number for microseconds. :return date_and_time_initialisation_plus_pseudo_random_delta: the Pseudo-Random Timestamps, starting from a given delta Date and Time, for a generator/list. """ date_and_time_initialisation = \ self.get_date_and_time_initialisation() """ Retrieve the date and time of initialisation of the Qiskrypt's Timestamp Generator. """ while num_pseudo_random_dates_and_times_to_generate > 0: """ While there are still Pseudo-Random Dates and Times to be generated. """ if weeks_max_delta == 0: """ If the maximum delta number for weeks is not set up, will not be defined any pseudo-random delta for weeks. """ weeks_random_delta = 0 """ Set the pseudo-random delta for weeks as zero (0). """ else: """ If the maximum delta number for weeks is set up, will be defined a pseudo-random delta for the number of weeks. """ weeks_random_delta = random_range(weeks_max_delta) """ Set the pseudo-random delta for the number of weeks, between zero (0) and the maximum delta number for weeks. """ if days_max_delta == 0: """ If the maximum delta number for days is not set up, will not be defined any pseudo-random delta for days. """ days_random_delta = 0 """ Set the pseudo-random delta for days as zero (0). """ else: """ If the maximum delta number for days is set up, will be defined a pseudo-random delta for the number of days. """ days_random_delta = random_range(days_max_delta) """ Set the pseudo-random delta for the number of days, between zero (0) and the maximum delta number for days. """ if hours_max_delta == 0: """ If the maximum delta number for hours is not set up, will not be defined any pseudo-random delta for hours. """ hours_random_delta = 0 """ Set the pseudo-random delta for hours as zero (0). """ else: """ If the maximum delta number for hours is set up, will be defined a pseudo-random delta for the number of hours. """ hours_random_delta = random_range(days_max_delta) """ Set the pseudo-random delta for the number of hours, between zero (0) and the maximum delta number for hours. """ if minutes_max_delta == 0: """ If the maximum delta number for minutes is not set up, will not be defined any pseudo-random delta for minutes. """ minutes_random_delta = 0 """ Set the pseudo-random delta for minutes as zero (0). """ else: """ If the maximum delta number for minutes is set up, will be defined a pseudo-random delta for the number of minutes. """ minutes_random_delta = random_range(minutes_max_delta) """ Set the pseudo-random delta for the number of minutes, between zero (0) and the maximum delta number for minutes. """ if seconds_max_delta == 0: """ If the maximum delta number for seconds is not set up, will not be defined any pseudo-random delta for seconds. """ seconds_random_delta = 0 """ Set the pseudo-random delta for seconds as zero (0). """ else: """ If the maximum delta number for seconds is set up, will be defined a pseudo-random delta for the number of seconds. """ seconds_random_delta = random_range(seconds_max_delta) """ Set the pseudo-random delta for the number of seconds, between zero (0) and the maximum delta number for seconds. """ if milliseconds_max_delta == 0: """ If the maximum delta number for milliseconds is not set up, will not be defined any pseudo-random delta for milliseconds. """ milliseconds_random_delta = 0 """ Set the pseudo-random delta for milliseconds as zero (0). """ else: """ If the maximum delta number for milliseconds is set up, will be defined a pseudo-random delta for the number of milliseconds. """ milliseconds_random_delta = random_range( milliseconds_max_delta) """ Set the pseudo-random delta for the number of milliseconds, between zero (0) and the maximum delta number for milliseconds. """ if microseconds_max_delta == 0: """ If the maximum delta number for microseconds is not set up, will not be defined any pseudo-random delta for microseconds. """ microseconds_random_delta = 0 """ Set the pseudo-random delta for microseconds as zero (0). """ else: """ If the maximum delta number for microseconds is set up, will be defined a pseudo-random delta for the number of microseconds. """ microseconds_random_delta = random_range( microseconds_max_delta) """ Set the pseudo-random delta for the number of microseconds, between zero (0) and the maximum delta number for microseconds. """ date_and_time_initialisation_plus_pseudo_random_delta = date_and_time_initialisation + \ time_delta(weeks=weeks_random_delta, days=days_random_delta, hours=hours_random_delta, minutes=minutes_random_delta, seconds=seconds_random_delta, milliseconds=milliseconds_random_delta, microseconds=microseconds_random_delta) """ Compute the current date and time of initialisation of the Qiskrypt's Timestamp Generator, with a given delta Date and Time, considering the pseudo-random weeks, days, hours, minutes, seconds, milliseconds and microseconds computed previously. """ yield date_and_time_initialisation_plus_pseudo_random_delta """ Yield the current date and time of initialisation of the Qiskrypt's Timestamp Generator, with a given delta Date and Time, considering the pseudo-random weeks, days, hours, minutes, seconds, milliseconds and microseconds computed previously, for a generator/list. """ num_pseudo_random_dates_and_times_to_generate -= 1 """
def build_fwtrack (self, opt): """Build FWTrackII from all lines, return a FWTrackII object. Handle multi-reads here by building a probability and enrichment index or select only one alignment from each multi-read. Initial alignment probabilities are set from read/mismatch qualities or from a uniform distribution. """ fwtrack = FWTrackII() i = 0 m = 0 read_total = 0 recent_tags = [] random_select_one_multi = opt.random_select_one_multi no_multi_reads = opt.no_multi_reads min_score = opt.min_score prior_prob_snp = opt.prior_prob_snp no_prior_prob_map = opt.no_prior_prob_map if opt.qual_scale == 'auto': opt.qual_scale = self._guess_qual_scale() if opt.qual_scale == 'sanger+33': qual_offset = 33 elif opt.qual_scale == 'illumina+64': qual_offset = 64 group_starts_append = fwtrack.group_starts.append fwtrack_add_loc = fwtrack.add_loc match_probs = {} # {(1,30):p(match|phred=30), (0,30):p(mismatch|phred=30)} for grouplines in self._group_by_name(self.fhd): read_total += 1 # in ratios, only count reads, not total alignments if len(grouplines) == 1: # uniquely mapping reads i+=1 if i == 1000000: m += 1 logging.info(" %d alignments read." % (m*1000000)) i=0 chromosome, fpos, strand, qualstr, mismatches = grouplines[0] fwtrack_add_loc(chromosome,fpos,strand,0) # 0'th index => unique else: if no_multi_reads: # throw away multi-reads fwtrack.total -= 1 continue elif random_select_one_multi: # choose one alignment at random i+=1 if i == 1000000: m += 1 logging.info(" %d alignments read." % (m*1000000)) i=0 randline = grouplines[random_range(len(grouplines))] chromosome,fpos,strand,qualstr,mismatches = randline fwtrack_add_loc(chromosome,fpos,strand,0) else: # use all alignments probabilistically group_starts_append(fwtrack.total_multi + 1) # starts at 1 (0 reserved for unique reads) if no_prior_prob_map: # don't use map quality; just assume uniform priors for (chromosome,fpos,strand, qualstr,mismatches) in grouplines: i+=1 if i == 1000000: m += 1 logging.info(" %d alignments read." % (m*1000000)) i=0 fwtrack.total_multi += 1 fwtrack_add_loc(chromosome,fpos,strand, fwtrack.total_multi) normed_probs = [1./len(grouplines)] * len(grouplines) else: # TODO: might want to be working in log space-- if many mismatches, we'll lose precision qualstr = grouplines[0][3] # all quality strings are shared across the group group_total_prob = 0. group_probs = [] group_probs_append = group_probs.append for (chromosome,fpos,strand, qualstr, mismatches) in grouplines: i+=1 if i == 1000000: m += 1 logging.info(" %d alignments read." % (m*1000000)) i=0 fwtrack.total_multi += 1 fwtrack_add_loc(chromosome,fpos,strand, fwtrack.total_multi) mismatches = set(mismatches) read_prob = 1. # P(SNP) = prior probability a SNP occurs at any base # P(SE) = probability there was a sequencing error (from PHRED) # _P(Map|SNP,SE)__MATCH__SNP__SE_ # 0 0 0 0 # can't map here without explanation # 1 0 0 1 # 1 0 1 0 # 1 0 1 1 # 1 1 0 0 # 1 1 0 1 # 0 1 1 0 # wouldn't map here if SNP, but sequencer read reference # 1 1 1 1 # we are interested in P(Mapping | Match), which is equivalent to: # \Sum_{SNP \in {0,1}, SE \in {0,1}} p(SNP) * p(SE) * p(Map|SE,SNP), or: # p(Map|match = 0): # p(SE) + p(SNP) + p(SE)*p(SNP) # p(Map|match = 1): # 1 - (p(SE) + p(SE)*p(SNP)) for b in xrange(len(qualstr)): tup = (b in mismatches,qualstr[b]) if tup in match_probs: prob = match_probs[tup] elif tup[0]: # mismatch p_seq_error = 10. ** ((qualstr[b]-qual_offset)/-10.) prob = p_seq_error + prior_prob_snp + p_seq_error * prior_prob_snp match_probs[tup] = prob else: # match p_seq_error = 10. ** ((qualstr[b]-qual_offset)/-10.) prob = 1. - (p_seq_error + p_seq_error * prior_prob_snp) match_probs[tup] = prob read_prob *= prob # quick & dirty check-- only looking at last base assert qualstr[b] >= qual_offset # Specified quality scale yielded a negative phred score! You probably have the wrong PHRED scale! assert 0.<=read_prob<=1. # error with map qualities #raise BaseQualityError("Specified quality scale yielded a negative phred score! You probably have the wrong PHRED scale!") group_probs_append(read_prob) group_total_prob += read_prob normed_probs = [p / group_total_prob for p in group_probs] fwtrack.prob_aligns.extend(normed_probs) fwtrack.prior_aligns.extend(normed_probs) fwtrack.enrich_scores.extend([min_score] * len(grouplines)) fwtrack.total = read_total # overwrite the running total, counting each read once return fwtrack