From f0499afb5b5470eeb1060bb1054608ca85a8c287 Mon Sep 17 00:00:00 2001 From: Nico Maillet <nicolas.maillet@pasteur.fr> Date: Wed, 27 Sep 2023 12:07:53 +0200 Subject: [PATCH] Add TP 3 skeleton --- 3-Needleman-Wunsch/tp3_skeleton.py | 158 +++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 3-Needleman-Wunsch/tp3_skeleton.py diff --git a/3-Needleman-Wunsch/tp3_skeleton.py b/3-Needleman-Wunsch/tp3_skeleton.py new file mode 100644 index 0000000..d10ef52 --- /dev/null +++ b/3-Needleman-Wunsch/tp3_skeleton.py @@ -0,0 +1,158 @@ +""" TP3 where we code a global alignment program + based on Needleman-Wunsch algorithm """ + +def simple_display(seq_top, seq_left, score): + """ Do a simple display """ + print("Seq_top: {}\nSeq_left: {}\nScore: {}".format(seq_top[::-1], seq_left[::-1], score)) + +def nice_display(seq_top, seq_left, score): + """ Do a nice display """ + # What will be printed + to_print = "" + + # Print seq_top + # For each positions in a sequence + for i, _ in enumerate(seq_top): + # Add the corresponding letter (reverse order) + to_print += seq_top[-(i+1)] + # Print a next line after printing the seq_top + to_print += "\n" + + # Print middle line + # For each positions in a sequence + for i, _ in enumerate(seq_top): + # If it is a match between the two sequences + if seq_top[-(i+1)] == seq_left[-(i+1)]: + # Print a pipe + to_print += "|" + # Otherwise + else: + # Print a space + to_print += " " + # Print a next line after printing the middle line + to_print += "\n" + + # Print seq_left + # For each positions in a sequence + for i, _ in enumerate(seq_top): + # Add the corresponding letter (reverse order) + to_print += seq_left[-(i+1)] + + # Add the score at the end + to_print += "\nScore: {}\n".format(score) + # Print everything! + print(to_print) + + +class DynamicMatrix: + """ Class to generate an empty matrix """ + def __init__(self, seq_top, seq_left, match, mismatch, indel): + # Init all "self" variables + + # Create the matrix of Cell() + + # self representation for print + def __repr__(self): + # What will be returned + return "Scores:\n{}\nPrev_pos:\n{}\n\n".format(self.print_scores(), self.print_prev_pos()) + + # self representation for print + def print_scores(self): + """ Output the values of the matrix """ + # What will be returned + ret_scores = ". . " + # Print top_seq + for i in self.seq_top: + ret_scores += " {} ".format(i) + # New line + ret_scores += "\n" + # For each line + for ind, i in enumerate(self.matrix): + # Print seq_left + if ind > 0: + ret_scores += "{} ".format(self.seq_left[ind-1]) + else: + ret_scores += ". " + # For each column + for j in i: + # If this cell has no value + if j.score is None: + # Add a dot to the return + ret_scores += (" . ") + # If this cell is not empty + else: + # Add its content to the return + tmp_val = str(j.score) + if len(tmp_val) == 1: + ret_scores += " " + tmp_val + " " + if len(tmp_val) == 2: + ret_scores += tmp_val + " " + if len(tmp_val) == 3: + ret_scores += tmp_val + # Always add a space after the value we add + ret_scores += " " + # End of this line, go to next line + ret_scores += "\n" + # Return the content of the Matrix + return ret_scores + + # self representation for print + def print_prev_pos(self): + """ Output the values of the matrix """ + # What will be returned + ret_prev_pos = ". . " + # Print top_seq + for i in self.seq_top: + ret_prev_pos += " {} ".format(i) + # New line + ret_prev_pos += "\n" + # For each line + for ind, i in enumerate(self.matrix): + # Print seq_left + if ind > 0: + ret_prev_pos += "{} ".format(self.seq_left[ind-1]) + else: + ret_prev_pos += ". " + # For each column + for j in i: + # If this cell has no value + if j.prev_pos is None: + # Add a dot to the return + ret_prev_pos += (". ") + # If this cell is not empty + else: + # Add its content to the return + tmp_val = str(j.prev_pos) + ret_prev_pos += tmp_val + # Always add a space after the value we add + ret_prev_pos += " " + # End of this line, go to next line + ret_prev_pos += "\n" + # Return the content of the Matrix + return ret_prev_pos + + def initialize(self): + """ Initialize the matrix, i.e. fill the first line and column """ + # First cell is 0 + + def fill_matrix(self): + """ Fill-up the matrix """ + + def global_alignment(self): + """ Make a global alignment of two sequences """ + + +def main(): + """ The main of TP3""" + mat = DynamicMatrix("ACGGCTAT", "ACTGTAG", 2, -1, -2) + #mat.initialize() + #mat.fill_matrix() + #print(mat) + #al_seq_top, al_seq_left, score = mat.global_alignment() + #nice_display(al_seq_top, al_seq_left, score) + +# Launch the main +main() +# Exit without error +exit(0) +# Always put one extra return line -- GitLab