|
| 1 | + |
| 2 | +""" |
| 3 | +Exercici 5: Andrea Morales |
| 4 | +
|
| 5 | +- Fes un programa que mostri mutacions aleatòries d'una seqüència d'ADN a una pàgina html. |
| 6 | +- La seqüència està al fitxer dna.fasta. |
| 7 | +- Llegiu la seqüència del fitxer i genereu seqüències que tinguin una base (lletra) mutada aleatòriament. |
| 8 | +- Les seqüències han de ser úniques, no hi pot haver repetides. |
| 9 | +- Per generar la pàgina html, utilitzeu Jinja. |
| 10 | +- A la pàgina html la mutació de cada seqüència ha d'estar en vermell, |
| 11 | + i la resta de lletres en color negre. |
| 12 | +- El programa només rep un paràmetre: el número de seqüències a generar. |
| 13 | +- El programa ha de rebre el número de seqüències des de la línia d'ordres. |
| 14 | +- **Poseu el vostre nom i número d'exercici al principi del vostre codi.** |
| 15 | +
|
| 16 | +""" |
| 17 | +#FINAL VERSION |
| 18 | +#ONLY LAST TO DO THE COMPROBATION OF REPEATED SEQUENCE |
| 19 | +#-------------------------------------------------------------------------------------------------------------------------------------- |
| 20 | +#IMPORTS |
| 21 | +#-------------------------------------------------------------------------------------------------------------------------------------- |
| 22 | +import sys |
| 23 | +import random |
| 24 | +import engine |
| 25 | +from pathlib import Path |
| 26 | +#-------------------------------------------------------------------------------------------------------------------------------------- |
| 27 | + |
| 28 | +#-------------------------------------------------------------------------------------------------------------------------------------- |
| 29 | +#THE SEQUENCE |
| 30 | +#-------------------------------------------------------------------------------------------------------------------------------------- |
| 31 | +def get_fasta(filename: str) -> str: |
| 32 | + '''Input: The path to a .fasta file. |
| 33 | + Output: The contents of th .fasta file as a single string''' |
| 34 | + |
| 35 | + all_lines: str = Path(filename).read_text().strip() |
| 36 | + seq: str = ''.join(all_lines.split('\n')[1:]) |
| 37 | + |
| 38 | + return seq |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +def mutate_seq(seq: str) -> dict: |
| 43 | + '''INPUT: A string of the hole sequences |
| 44 | + OUTPUT: A dict with before, snp ,after''' |
| 45 | + |
| 46 | + |
| 47 | + random_index: int = random.randint(0, len(seq)-1) |
| 48 | + new_letter: str = random.choice("ATCG") |
| 49 | + |
| 50 | + before: str = seq[0:random_index] |
| 51 | + after: str = seq[random_index+1:] |
| 52 | + |
| 53 | + mutated_seq: dict = {'before':before, 'new_letter': new_letter, 'after': after} |
| 54 | + |
| 55 | + return mutated_seq |
| 56 | + |
| 57 | +def get_mutated_seq_list(num_seqs:int, seq:str)-> list[dict]: |
| 58 | + '''INPUT: |
| 59 | + num_seqs: Number of sequences |
| 60 | + seq: The string of the sequence untoched |
| 61 | + OUTPUT: A list of the number of the sequences given''' |
| 62 | + |
| 63 | + |
| 64 | + mutated_seq_list: list[dict] = [mutate_seq(seq) for _ in range(num_seqs)] |
| 65 | + |
| 66 | + return mutated_seq_list |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +#-------------------------------------------------------------------------------------------------------------------------------------- |
| 71 | +#MAIN |
| 72 | +#-------------------------------------------------------------------------------------------------------------------------------------- |
| 73 | + |
| 74 | +def make_html_seq_list (num_seqs : int, |
| 75 | + template_filename: str, |
| 76 | + html_filename: str): |
| 77 | + '''INPUT: |
| 78 | + num_seq: The number of sequences to create |
| 79 | + template_filename: A path to the the template |
| 80 | + html_filename: The name of the html file that will be written in disk |
| 81 | + OUTPUT''' |
| 82 | + |
| 83 | + #Variables |
| 84 | + original: str = get_fasta('dna.fasta') |
| 85 | + mutated_seq_list: list[dict] = get_mutated_seq_list(num_seqs, seq) |
| 86 | + |
| 87 | + #Fill template |
| 88 | + template_str: str = Path(template_filename).read_text() |
| 89 | + vars_dict: dict = {'original':original,'mutated_seq_list': mutated_seq_list} |
| 90 | + html_seq_list: str = engine.fill_template_str(template_str,vars_dict) |
| 91 | + |
| 92 | + #Write html in disk |
| 93 | + Path(html_filename).write_text(html_seq_list) |
| 94 | + |
| 95 | +def parse_command_line (command_line: list[str])-> tuple[str, str, str]: |
| 96 | + |
| 97 | + |
| 98 | + #Program name & program parameter |
| 99 | + program_name: str = sys.argv[0] |
| 100 | + program_parameters: list[str] = sys.argv[1:] |
| 101 | + |
| 102 | + #Restriction |
| 103 | + assert len(program_parameters) == 3 |
| 104 | + |
| 105 | + #List decostruction |
| 106 | + num_seqs, template_filename, html_filename = program_parameters |
| 107 | + |
| 108 | + |
| 109 | + return num_seqs, template_filename, html_filename |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +#-------------------------------------------------------------------------------------------------------------------------------------- |
| 116 | +if __name__ == '__main__': |
| 117 | + |
| 118 | + seq: str = get_fasta('dna.fasta') |
| 119 | + mutated_seq: dict = mutate_seq(seq) |
| 120 | + |
| 121 | + #Terminal |
| 122 | + #num_seqs, template_filename, html_filename = parse_command_line(sys.argv) |
| 123 | + # make_html_seq_list(num_seqs, template_filename, html_filename) |
| 124 | + |
| 125 | + |
| 126 | + #VSCODE |
| 127 | + make_html_seq_list(3, 'template2.html', 'index2.html') |
| 128 | + |
| 129 | + |
| 130 | + |
0 commit comments