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, count=0): """ Duplicates a segment of a sequence and inserts it right after the end of the segment. :param dna_seq: The sequence from which a segment of elements will be copied. :param idx: The index at which the segment to be duplicated begins. :param segment_size: The number of elements to be duplicated. :param count: Way of keeping track of what processes have been done. :return: A new linked sequence that represents the resulting DNA strand after the specified segment of elements from dna_seq have been copied. """ leng = lc.length_rec(dna_seq) if segment_size == 0 and idx > leng: return dna_seq elif idx + segment_size > leng: raise IndexError("Error: Index out of range.") elif segment_size == 0 and count == 0: return dna_seq elif idx == 0 and count == 0: return insertion(dna_seq, duplication(dna_seq, idx, segment_size, 1), idx) elif idx == 0: if segment_size != 0: return LinkNode( dna_seq.value, duplication(dna_seq.rest, idx, segment_size - 1, 1)) else: pass else: if count == 0: return insertion( dna_seq, duplication(dna_seq.rest, idx - 1, segment_size, 1), idx) else: return duplication(dna_seq.rest, idx - 1, segment_size, 1)
def substitution(dna_seq, index, base): """ This function takes the input of the DNA sequence, index at which the base has to be replaced and the base by which the original sequence will be replaced """ if linked_code.length_rec(dna_seq) < index: raise IndexError("Invalid Insertion Index") else: return linked_code.insert_at(index, base, linked_code.remove_at(index, dna_seq))
def test7(): """ Tests insertion function. :return: None """ print("Test7: testing insertion") dna_seq1 = None dna_seq2 = None dna_seq3 = dna.insertion(dna_seq1, dna_seq2, 0) print(dna_seq3 == None, end=" ") try: dna_seq3 = dna.insertion(dna_seq1, dna_seq2, 1) print("False", end=" ") # failed to catch exception except: print("True", end=" ") dna_seq1 = linked_code.insert_at(0, "A", dna_seq1) dna_seq3 = dna.insertion(dna_seq1, dna_seq2, 0) print(dna_seq3.value == "A" \ and linked_code.length_rec(dna_seq3) == 1, end=" ") dna_seq3 = dna.insertion(dna_seq1, dna_seq2, 1) print(dna_seq3.value == "A" \ and linked_code.length_rec(dna_seq3) == 1, end=" ") dna_seq2 = linked_code.insert_at(0, "C", dna_seq2) dna_seq3 = dna.insertion(dna_seq1, dna_seq2, 0) print(dna_seq3.value == "C" and dna_seq3.rest.value == "A" \ and linked_code.length_rec(dna_seq3) == 2, end=" ") dna_seq3 = dna.insertion(dna_seq1, dna_seq2, 1) print(dna_seq3.value == "A" and dna_seq3.rest.value == "C" \ and linked_code.length_rec(dna_seq3) == 2, end=" ") dna_seq1 = linked_code.insert_at(0, "T", dna_seq1) # now TA dna_seq2 = linked_code.insert_at(0, "G", dna_seq2) # now GC dna_seq3 = dna.insertion(dna_seq1, dna_seq2, 0) print(dna_seq3.value == "G" and dna_seq3.rest.value == "C" \ and dna_seq3.rest.rest.value == "T" \ and dna_seq3.rest.rest.rest.value == "A" and linked_code.length_rec(dna_seq3) == 4, end=" ") dna_seq3 = dna.insertion(dna_seq1, dna_seq2, 1) print(dna_seq3.value == "T" and dna_seq3.rest.value == "G" \ and dna_seq3.rest.rest.value == "C" \ and dna_seq3.rest.rest.rest.value == "A" and linked_code.length_rec(dna_seq3) == 4, end=" ") dna_seq3 = dna.insertion(dna_seq1, dna_seq2, 2) print(dna_seq3.value == "T" and dna_seq3.rest.value == "A" \ and dna_seq3.rest.rest.value == "G" \ and dna_seq3.rest.rest.rest.value == "C" and linked_code.length_rec(dna_seq3) == 4)
def deletion(dna_seq, idx, segment_size): """ This function takes the DNA sequence , index at which the element has to deleted and the segment size that has to be deleted """ if (segment_size == 0): return dna_seq elif (segment_size + idx > linked_code.length_rec(dna_seq)): raise IndexError("Sequence out of range") else: return deletion(linked_code.remove_at(idx, dna_seq), idx, 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 deletion(dna_seq, idx, segment_size): """ This function deletes a segment of chosen size, beginning at a chosen index, in a linked list. Pre: dna_seq must be a linked list, idx must be an integer, and segment_size should be an integer. Post: a linked list with deleted values is returned :param dna_seq: a linked list :param idx: index at which deletion occurs :param segment_size: size of segment being deleted :return: a linked list with a correctly deleted segment """ if segment_size == 0: return dna_seq elif linked_code.length_rec(dna_seq) < idx + segment_size: raise IndexError('Index out of range') else: if idx == 0: dna_seq = linked_code.remove_at(0, dna_seq) return deletion(dna_seq, idx, segment_size - 1) else: return LinkNode(dna_seq.value, deletion(dna_seq.rest, idx - 1, segment_size))
def duplication(dna_seq, idx, segment_size): """ This function duplicates a segemnt of length segment_size at a chosen index in a linked list. Pre: dna_seq must be a valid linked list, idx must be an integer, and segment_size should be an integer. Post: a linked list with duplicated segment in place is returned :param dna_seq: linked list :param idx: index at which duplicated segment begins :param segment_size: length of duplicated segment :return: a linked list with correctly duplicated segment in place """ if segment_size == 0: return dna_seq elif linked_code.length_rec(dna_seq) < idx + segment_size: raise IndexError('Index out of range') else: if idx > 0: return LinkNode(dna_seq.value, duplication(dna_seq.rest, idx - 1, segment_size)) if idx == 0: dna_seq2 = find_values_sequence(dna_seq, segment_size) dna_seq3 = linked_code.concatenate(dna_seq2, dna_seq) return dna_seq3
def test9(): """ Tests duplication function. :return: None """ print("Test9: testing duplication") dna_seq1 = None dna_seq2 = dna.duplication(dna_seq1, 0, 0) print(dna_seq2 == None, end=" ") try: dna_seq2 = dna.duplication(dna_seq1, 0, 1) print("False", end=" ") # failed to catch exception except: print("True", end=" ") dna_seq1 = linked_code.insert_at(0, "A", dna_seq1) dna_seq2 = dna.duplication(dna_seq1, 2, 0) print(dna_seq2.value == "A" and \ linked_code.length_rec(dna_seq2) == 1, end=" ") dna_seq2 = dna.duplication(dna_seq1, 0, 1) print(dna_seq2.value == "A" and \ dna_seq2.rest.value == "A" and \ linked_code.length_rec(dna_seq2) == 2, end=" ") dna_seq1 = linked_code.insert_at(1, "T", dna_seq1) dna_seq1 = linked_code.insert_at(2, "C", dna_seq1) dna_seq2 = dna.duplication(dna_seq1, 0, 1) print(dna_seq2.value == "A" and \ dna_seq2.rest.value == "A" and \ linked_code.length_rec(dna_seq2) == 4, end=" ") dna_seq2 = dna.duplication(dna_seq1, 1, 1) print(dna_seq2.value == "A" and \ dna_seq2.rest.value == "T" and \ dna_seq2.rest.rest.value == "T" and \ linked_code.length_rec(dna_seq2) == 4, end=" ") dna_seq2 = dna.duplication(dna_seq1, 2, 1) print(dna_seq2.value == "A" and \ dna_seq2.rest.value == "T" and \ dna_seq2.rest.rest.value == "C" and \ dna_seq2.rest.rest.rest.value == "C" and \ linked_code.length_rec(dna_seq2) == 4, end=" ") dna_seq2 = dna.duplication(dna_seq1, 0, 2) print(dna_seq2.value == "A" and \ dna_seq2.rest.value == "T" and \ dna_seq2.rest.rest.value == "A" and \ dna_seq2.rest.rest.rest.value == "T" and \ dna_seq2.rest.rest.rest.rest.value == "C" and \ linked_code.length_rec(dna_seq2) == 5, end=" ") dna_seq2 = dna.duplication(dna_seq1, 1, 2) print(dna_seq2.value == "A" and \ dna_seq2.rest.value == "T" and \ dna_seq2.rest.rest.value == "C" and \ dna_seq2.rest.rest.rest.value == "T" and \ dna_seq2.rest.rest.rest.rest.value == "C" and \ linked_code.length_rec(dna_seq2) == 5, end=" ") try: dna_seq2 = dna.duplication(dna_seq1, 2, 2) print("False", end=" ") # failed to catch exception except: print("True", end=" ") print()