def _filter_bp_type(bps: List[Tuple[int, int]], q: str, t: str, qidxpos0: int, tidxpos0: int, bp_type='') -> \ List[Tuple[int, int]]: """ Filters out base pairs of a specified type :param bps: List of tuples in form (q_index, t_index) representing the base pairs :param q: The query sequence :param t: The target sequence :param qidxpos0: Starting index for the query :param tidxpos0: Starting index for the target :param bp_type: The type of base pairs to filter out, for example GU, CG, AU, GC, ... :return: List of tuples in form (q_index, t_index) representing the base pairs >>> q, t = 'GCUACGAUC', 'UUUGCGAGCAGCUAGG' >>> bps = [(1,1),(2,2),(6,13),(8,15),(9,16)] >>> _filter_bp_type(bps, q, t, 1, 1, 'GU') [(2, 2), (9, 16)] >>> _filter_bp_type(bps, q, t, 1, 1, 'GC') [(1, 1), (2, 2), (6, 13), (8, 15)] """ new_bps = [] for q_index, t_index in bps: q_array_index = idx_to_array_index(q_index, qidxpos0) t_array_index = idx_to_array_index(t_index, tidxpos0) if f'{q[q_array_index]}{t[t_array_index]}' in [bp_type, bp_type[::-1]]: continue new_bps.append((q_index, t_index)) return new_bps
def _neighbors_can_pair(q_index: int, t_index: int, q: str, t: str, qidxpos0: int, tidxpos0: int) -> int: """ Counts how many neighboring base pairs can pair :param q_index: Base pair index on the query :param t_index: Base pair index on the target :param q: The query sequence :param t: The target sequence :param qidxpos0: Starting index for the query :param tidxpos0: Starting index for the target :return: Count of neighboring base pairs that can pair >>> _neighbors_can_pair(1, 5, 'GCAUCGAUC', 'CGUACGAUCGAUCC', 1, 1) 0 >>> _neighbors_can_pair(3, 3, 'GCAUCGAUC', 'CGUACGAUCGAUCC', 1, 1) 1 >>> _neighbors_can_pair(6, 9, 'GCAUCGAUC', 'CGUACGAUCGAUCC', 1, 1) 2 """ count = 0 q_array_index = idx_to_array_index(q_index, qidxpos0) t_array_index = idx_to_array_index(t_index, tidxpos0) pairable_bps = ['GC', 'CG', 'AU', 'UA', 'GU', 'UG'] for q_o, t_o in [(+1, -1), (-1, +1)]: # get neighbors by offset if q_array_index + q_o in range( len(q)) and t_array_index + t_o in range(len(t)): if f'{q[q_array_index+q_o]}{t[t_array_index+t_o]}' in pairable_bps: count += 1 return count
def gen_any(query: str, target: str, qidxpos0: int, tidxpos0: int, bp_list: List[Tuple[int, int]], mms: List[str], alpha: float, beta: float) -> List[Mutation]: mutations = [] possible_mutations = { 'AU': ['UA', 'UG', 'CG'], 'UA': ['AU', 'GU', 'GC'], 'GC': ['UA', 'UG', 'CG'], 'CG': ['AU', 'GU', 'GC'], 'GU': ['UA', 'UG', 'CG'], 'UG': ['AU', 'GU', 'GC'] } for q_index, t_index in bp_list: mm = [] for m in mms: mm.append(get_measure(m, alpha, beta)) q_index_a = idx_to_array_index(q_index, qidxpos0) t_index_a = idx_to_array_index(t_index, tidxpos0) pos = possible_mutations[f'{query[q_index_a]}{target[t_index_a]}'] for p in pos: bp_mm = f'{p[0]}{p[1]}' m = Mutation(deepcopy(mm), query, target, qidxpos0, tidxpos0, q_index, t_index, bp_mm) mutations.append(m) return mutations
def __post_init__(self): mut_pos_q = idx_to_array_index(self.query_n, self.qidxpos0) mut_pos_t = idx_to_array_index(self.target_n, self.tidxpos0) self.bp_ww = f'{self.qw[mut_pos_q]}{self.tw[mut_pos_t]}' self.qm = self.qw[0:mut_pos_q] + self.bp_mm[0] + self.qw[mut_pos_q + 1:] self.tm = self.tw[0:mut_pos_t] + self.bp_mm[1] + self.tw[mut_pos_t + 1:]
def calculate_score(self, data: dict, mut_type: str, m: Mutation = None, param_file: str = ''): data = self.read_temp_file()[0] data = data.strip().split('\n') # noinspection PyTypeChecker self.score[mut_type] = float( data[idx_to_array_index(m.query_n, m.qidxpos0) + 2].split('\t')[1])
def gen_flip(query: str, target: str, qidxpos0: int, tidxpos0: int, bp_list: List[Tuple[int, int]], mms: List[str], alpha: float, beta: float) -> List[Mutation]: mutations = [] for q_index, t_index in bp_list: mm = [] for m in mms: mm.append(get_measure(m, alpha, beta)) q_index_a = idx_to_array_index(q_index, qidxpos0) t_index_a = idx_to_array_index(t_index, tidxpos0) bp_mm = f'{target[t_index_a]}{query[q_index_a]}' m = Mutation(deepcopy(mm), query, target, qidxpos0, tidxpos0, q_index, t_index, bp_mm) mutations.append(m) return mutations
def calculate_score(self, data: dict, mut_type: str, m: Mutation = None, param_file: str = ''): data = self.read_temp_file()[0] data = data.strip().split('\n') value = data[idx_to_array_index(m.target_n, m.tidxpos0) + 1].split(';')[2] if value == 'NA': self.score[mut_type] = None else: # noinspection PyTypeChecker self.score[mut_type] = float(value)