Skip to content

Commit 570b448

Browse files
author
Andrea
committed
Main code, two versions, 2 final version
1 parent 2879386 commit 570b448

File tree

2 files changed

+256
-0
lines changed

2 files changed

+256
-0
lines changed

e5v1.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
#--------------------------------------------------------------------------------------------------------------------------------------
18+
#IMPORTS
19+
#--------------------------------------------------------------------------------------------------------------------------------------
20+
import sys
21+
import random
22+
import engine
23+
from pathlib import Path
24+
#--------------------------------------------------------------------------------------------------------------------------------------
25+
26+
#--------------------------------------------------------------------------------------------------------------------------------------
27+
#THE SEQUENCE
28+
#--------------------------------------------------------------------------------------------------------------------------------------
29+
def get_fasta(filename: str) -> str:
30+
'''Input: The path to a .fasta file.
31+
Output: The contents of th .fasta file as a single string'''
32+
33+
all_lines: str = Path(filename).read_text().strip()
34+
seq: str = ''.join(all_lines.split('\n')[1:])
35+
36+
return seq
37+
38+
39+
40+
def mutate_seq(seq: str) -> str:
41+
'''INPUT: A string of the hole sequence unchanged
42+
OUTPUT: A str with the mutation'''
43+
44+
45+
random_index: int = random.randint(0, len(seq)-1)
46+
new_letter: str = random.choice("ATCG")
47+
48+
before: str = seq[0:random_index]
49+
after: str = seq[random_index+1:]
50+
51+
mutated_seq: str = f"{before}{new_letter}{after}"
52+
53+
return mutated_seq
54+
55+
def get_mutated_seq_list(num_seqs:int, seq:str)-> list[str]:
56+
'''INPUT:
57+
num_seqs: Number of sequences
58+
seq: The string of the sequence untoched
59+
OUTPUT: A list of the number of the sequences given'''
60+
61+
62+
mutated_seq_list: list[str] = [mutate_seq(seq) for _ in range(num_seqs)]
63+
64+
return mutated_seq_list
65+
66+
67+
68+
#--------------------------------------------------------------------------------------------------------------------------------------
69+
#MAIN
70+
#--------------------------------------------------------------------------------------------------------------------------------------
71+
72+
def make_html_seq_list (num_seqs : int,
73+
template_filename: str,
74+
html_filename: str):
75+
'''INPUT:
76+
num_seq: The number of sequences to create
77+
template_filename: A path to the the template
78+
html_filename: The name of the html file that will be written in disk
79+
OUTPUT'''
80+
81+
82+
#Variables
83+
original: str = get_fasta('example.fasta')
84+
mutated_seq_list: list[str] = get_mutated_seq_list(num_seqs, seq)
85+
86+
#Fill template
87+
template_str: str = Path(template_filename).read_text()
88+
vars_dict: dict = {'original':original,'mutated_seq_list': mutated_seq_list}
89+
html_seq_list: str = engine.fill_template_str(template_str,vars_dict)
90+
91+
#Write html in disk
92+
Path(html_filename).write_text(html_seq_list)
93+
94+
def parse_command_line (command_line: list[str])-> tuple[str, str, str]:
95+
96+
#Program name & program parameter
97+
program_name: str = sys.argv[0]
98+
program_parameters: list[str] = sys.argv[1:]
99+
100+
#Restriction
101+
assert len(program_parameters) == 3
102+
103+
#List decostruction
104+
num_seqs, template_filename, html_filename = program_parameters
105+
106+
107+
return num_seqs, template_filename, html_filename
108+
109+
110+
111+
#--------------------------------------------------------------------------------------------------------------------------------------
112+
if __name__ == '__main__':
113+
114+
seq: str = get_fasta('example.fasta')
115+
mutated_seq: str = mutate_seq(seq)
116+
117+
#Terminal
118+
#num_seqs, template_filename, html_filename = parse_command_line(sys.argv)
119+
# make_html_seq_list(num_seqs, template_filename, html_filename)
120+
121+
122+
#VSCODE
123+
make_html_seq_list(3, 'template.html', 'index.html')
124+
125+
126+

e5v2.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)