def get_approximate_frequent_words(text, k, d): # Flag indicies of candidate kmers from neighborhoods is_neighbor_flags = [0] * pow(4, k) for position, kmer in ApproximatePatternMatching_1F.yield_kmers_by_position(text, k): neighbors = calculate_neighbors(kmer, d) for neighbor in neighbors: number = frequency_array_lib.pattern_to_number(neighbor) is_neighbor_flags[number] = 1 # Count approximate occurences of candidates chunk_size = 5 translation_table = build_translation_table(chunk_size) frequency_array = [0] * pow(4, k) for i in range(len(is_neighbor_flags)): if 1 == is_neighbor_flags[i]: kmer = frequency_array_lib.number_to_pattern(i, k) reverse_complement = generate_strand_complement(kmer, translation_table, chunk_size) frequency = ApproximatePatternMatching_1F.count_approximate_matches(kmer, text, d) + ApproximatePatternMatching_1F.count_approximate_matches(reverse_complement, text, d) frequency_array[i] = frequency # Find max count max_count = -1 for count in frequency_array: if count > max_count: max_count = count # Find most frequent kmers frequent = [] for i in range(len(frequency_array)): if frequency_array[i] == max_count: kmer = frequency_array_lib.number_to_pattern(i, k) frequent.append(kmer) return frequent
def compute_frequency_array(text, k): fa = [0] * pow(4, k) for kmer in frequency_array_lib.yield_kmers(text, k): number = frequency_array_lib.pattern_to_number(kmer) fa[number] = fa[number] + 1 return fa
def main(): with fileinput.input() as fi: genome = fi.readline().rstrip() number = frequency_array_lib.pattern_to_number(genome) print(number)