def is_pairing(DNA_1, DNA_2): """ Determine if the DNA bases are paired correctly :param DNA_1: dna sequence 1 :param DNA_2: dna sequence 2 :return: true if dna sequences are paired correctly and are same length, else false """ if DNA_1 is None and DNA_2 is None: return True if DNA_1 is None or DNA_2 is None: return False # valid base pairing are A-T and C-G if linked_code.value_at( DNA_1, 0) == 'A' and linked_code.value_at(DNA_2, 0) != 'T': return False elif linked_code.value_at( DNA_1, 0) == 'T' and linked_code.value_at(DNA_2, 0) != 'A': return False elif linked_code.value_at( DNA_1, 0) == 'C' and linked_code.value_at(DNA_2, 0) != 'G': return False elif linked_code.value_at( DNA_1, 0) == 'G' and linked_code.value_at(DNA_2, 0) != 'C': return False else: return is_pairing(DNA_1.rest, DNA_2.rest)
def is_palindrome(dna): """ Determines if dna sequence is a palindrome :param dna: dna sequence :return: true if dna sequence is a palindrome, false otherwise """ if dna is None: return True first_idx = 0 last_idx = length_iter(dna) - 1 while dna is not None and last_idx >= first_idx: # determine if opposite indices are the same if linked_code.value_at(dna, first_idx) == linked_code.value_at( dna, last_idx): first_idx += 1 last_idx -= 1 else: return False return True
def find_values_sequence(dna_seq, segment_size): """ This is a helper function for the duplication function. It finds and returns the segment which will be added into the linked list. Pre: dna_seq must be a linked list, while segment_size must be an integer Post: a segment of length segment_size from dna_seq is returned :param dna_seq: a linked list :param segment_size: length of segment :return: a segment of length segment_size """ if dna_seq is None: return dna_seq if segment_size == 1: x = linked_code.value_at(dna_seq, 0) return convert_to_nodes(x) else: x = linked_code.value_at(dna_seq, 0) return LinkNode(x, find_values_sequence(dna_seq.rest, segment_size - 1))
def pretty_print(lnk): """ Print the contents of a linked list in standard Python format. [value, value, value] (Note the spaces.) :param lnk: the node at the head of the provided list :return: None """ lst = [] for x in range(linked_code.length_rec(lnk)): lst.append(linked_code.value_at(lnk, x)) print(lst)
def is_palindrome(dna_seq): """ This function takes the DNA sequence in the form of Node data structure and checks whether it is palindrome or not """ if (dna_seq == None): return True elif (linked_code.length_rec(dna_seq) == 1): return True elif (dna_seq.value == linked_code.value_at( dna_seq, (linked_code.length_tail_rec(dna_seq) - 1))): return is_palindrome( linked_code.remove_at( linked_code.length_rec(dna_seq.rest) - 1, dna_seq.rest)) else: return False
def duplication(dna_seq, idx, segment_size): """ This function takes the dna_seq, index a and the segment size that has to be copied and added after that """ if segment_size != 0: a = segment_size while (segment_size > 0): v = linked_code.value_at(dna_seq, idx) dna_seq = linked_code.insert_at(idx + a, v, dna_seq) idx += 1 segment_size -= 1 return dna_seq else: return dna_seq
def convert_to_string(dna_seq): """ Convert a linked-node structure of DNA bases into a string :param dna_seq: linked-node dna sequence :return: string of DNA bases """ st = '' idx = 0 # if link is none return empty string if dna_seq is None: return st # iterate through linked node and convert each value into a string and concatenate it while dna_seq is not None and idx <= (length_iter(dna_seq) - 1): value = linked_code.value_at(dna_seq, idx) value = str(value) st = st + value idx += 1 return st
def duplication(DNA, idx, size): """ Insert duplicate segment of specific size at index of idx + size :param DNA: dna sequence :param idx: index where dna sequence starts to be copied :param size: size of segment copied :return: new linked-node with duplicate segment of specific size inserted at index of idx + size """ if size == 0: return DNA elif DNA is None: raise IndexError("Out of Bounds") lnk = None for i in range(size): val = linked_code.value_at(DNA, i + idx) lnk = linked_code.insert_at(i, val, lnk) new_link = insertion(DNA, lnk, idx + size) return new_link