-
Notifications
You must be signed in to change notification settings - Fork 45
HW4 Zolotikov #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
HW4 Zolotikov #10
Changes from all commits
b22ea48
69aab65
ae5c637
9b314eb
86127fe
86d1d11
59e1fdc
1b80718
2a9e3b4
2c68ee9
25e3f80
65d915f
b0a4d82
952fae5
185e764
4efc0e8
5c0687a
b6c003c
85382ee
df3007d
648e276
0e82b72
e193533
f76d7a2
9e59966
a537b98
d5716f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,200 @@ | ||||||||||||||||||
from typing import Dict, List, Union | ||||||||||||||||||
|
||||||||||||||||||
# Dorzhi | ||||||||||||||||||
def to_rna(seq: str, rna_dict: Dict[str, str] = {'F': 'UUY', 'L': 'YUN', 'I': 'AUH', 'M': 'AUG', | ||||||||||||||||||
'V': 'GUN', 'S': 'WSN', 'P': 'CCN', 'T': 'ACN', | ||||||||||||||||||
'A': 'GCN', 'Y': 'UAY', 'H': 'CAY', 'Q': 'CAR', | ||||||||||||||||||
'N': 'AAY', 'K': 'AAR', 'D': 'GAY', 'E': 'GAR', | ||||||||||||||||||
'C': 'UGY', 'R': 'MGN', 'G': 'GGN', 'W': 'UGG'}) -> str: | ||||||||||||||||||
""" | ||||||||||||||||||
Converts an amino acid sequence into an RNA sequence. | ||||||||||||||||||
|
||||||||||||||||||
Parameters | ||||||||||||||||||
---------- | ||||||||||||||||||
seq : str | ||||||||||||||||||
Amino acid sequence. | ||||||||||||||||||
rna_dict : dict | ||||||||||||||||||
Dictionary defining the correspondence of amino acids | ||||||||||||||||||
to RNA triplets (default, standard code). | ||||||||||||||||||
Returns | ||||||||||||||||||
------- | ||||||||||||||||||
str | ||||||||||||||||||
RNA sequence. | ||||||||||||||||||
|
||||||||||||||||||
""" | ||||||||||||||||||
result = ''.join(rna_dict[base] for base in seq) | ||||||||||||||||||
return result | ||||||||||||||||||
Comment on lines
+5
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍👍👍 за суть операции, |
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def define_charge(seq: str, positive_charge: List[str] = ['R', 'K', 'H'], | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут также словарь является константой, зачем его переопределять при каждом вызове функции? |
||||||||||||||||||
negative_charge: List[str] = ['D', 'E']) -> Dict[str, int]: | ||||||||||||||||||
""" | ||||||||||||||||||
Counts the number of amino acids with positive charge, negative charge, | ||||||||||||||||||
and neutral amino acids in the sequence. | ||||||||||||||||||
|
||||||||||||||||||
Parameters | ||||||||||||||||||
---------- | ||||||||||||||||||
seq : str | ||||||||||||||||||
Amino acid sequence (string). | ||||||||||||||||||
positive_charge : list | ||||||||||||||||||
List of amino acids with positive charge (default is ['R', 'K', 'H']). | ||||||||||||||||||
negative_charge : list | ||||||||||||||||||
List of amino acids with negative charge (default is ['D', 'E']). | ||||||||||||||||||
Comment on lines
+40
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Использование списка как типа данных для поиска менее эффективно по скорости, чем множества. К тому же непонятно, зачем делать его аргументом функции - вряд ли пользователь захочет ввести свои, другие данные |
||||||||||||||||||
|
||||||||||||||||||
Returns | ||||||||||||||||||
------- | ||||||||||||||||||
dict | ||||||||||||||||||
A dictionary containing the counts of amino acids and their labels: | ||||||||||||||||||
- 'Positive' for amino acids with positive charge. | ||||||||||||||||||
- 'Negative' for amino acids with negative charge. | ||||||||||||||||||
- 'Neutral' for neutral amino acids. | ||||||||||||||||||
""" | ||||||||||||||||||
positive_count = 0 | ||||||||||||||||||
negative_count = 0 | ||||||||||||||||||
neutral_count = 0 | ||||||||||||||||||
|
||||||||||||||||||
for aa in seq: | ||||||||||||||||||
if aa in positive_charge: | ||||||||||||||||||
positive_count += 1 | ||||||||||||||||||
elif aa in negative_charge: | ||||||||||||||||||
negative_count += 1 | ||||||||||||||||||
else: | ||||||||||||||||||
neutral_count += 1 | ||||||||||||||||||
|
||||||||||||||||||
result = { | ||||||||||||||||||
'Positive': positive_count, | ||||||||||||||||||
'Negative': negative_count, | ||||||||||||||||||
'Neutral': neutral_count | ||||||||||||||||||
} | ||||||||||||||||||
return result | ||||||||||||||||||
Comment on lines
+52
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Хорошее разбиение кода пустыми строками на смысловые участки, хороший вывод |
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
# Ustin | ||||||||||||||||||
POLAR_AA = {'D', 'E', 'R', 'K', 'H', 'N', 'Q', 'S', 'T', 'Y', 'C'} | ||||||||||||||||||
NONPOLAR_AA = {'A', 'G', 'V', 'L', 'I', 'P', 'F', 'M', 'W'} | ||||||||||||||||||
Comment on lines
+73
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Здорово, что тут переменные являются константами, и названы корректно! 🔥 На самом деле, можно было бы просто задать множества положительно, отрицательно и нейтрально заряженных полярны аминокислот, и отдельно неполярных аминокислот как константы Для предыдущей функции эти константы можно было бы тоже эффективно использовать, и не множить сущности в коде |
||||||||||||||||||
DNA_AA = {'F': 'TTY', 'L': '(TTR or CTN)', 'I': 'ATH', 'M': 'ATG', 'V': 'GTN', 'S': '(TCN or AGY)', 'P': 'CCN', 'T': 'ACN', 'A': 'GCN', | ||||||||||||||||||
'Y': 'TAY', 'H': 'CAY', 'Q': 'CAR', 'N': 'AAY', 'K': 'AAR', 'D': 'GAY', 'E': 'GAR', 'C': 'TGY', 'W': '(CGN or AGR)', 'R': 'AGY', 'G': 'GGN'} | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def define_polarity(seq: str) -> Dict[str, int]: | ||||||||||||||||||
""" | ||||||||||||||||||
Counts polar and nonpolar aminoacids in aminoacid sequences. | ||||||||||||||||||
|
||||||||||||||||||
Arguments: | ||||||||||||||||||
str: sequence to count polar and nonpolar aminoacids. | ||||||||||||||||||
|
||||||||||||||||||
Return: | ||||||||||||||||||
Dict[str, int]: | ||||||||||||||||||
Dictionary with keys 'Polar', 'Nonpolar' and values of quantity of according groups in sequence. | ||||||||||||||||||
""" | ||||||||||||||||||
polarity_count = {'Polar': 0, 'Nonpolar': 0} | ||||||||||||||||||
for aminoacid in seq: | ||||||||||||||||||
if aminoacid in POLAR_AA: | ||||||||||||||||||
polarity_count['Polar'] += 1 | ||||||||||||||||||
else: | ||||||||||||||||||
polarity_count['Nonpolar'] += 1 | ||||||||||||||||||
return polarity_count | ||||||||||||||||||
Comment on lines
+90
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔥 |
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def to_dna(seq: str) -> str: | ||||||||||||||||||
""" | ||||||||||||||||||
Transforms aminoacid sequence to DNA sequence | ||||||||||||||||||
|
||||||||||||||||||
Arguments | ||||||||||||||||||
--------- | ||||||||||||||||||
str: aminoacid sequence to transform to DNA sequence. | ||||||||||||||||||
|
||||||||||||||||||
Return | ||||||||||||||||||
------ | ||||||||||||||||||
str: according DNA sequence. | ||||||||||||||||||
""" | ||||||||||||||||||
sequence_dna = [] | ||||||||||||||||||
for aminoacid in seq: | ||||||||||||||||||
sequence_dna.append(DNA_AA[aminoacid]) | ||||||||||||||||||
return ''.join(sequence_dna) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
#Margarita | ||||||||||||||||||
ABBREVIATION_THREE_TO_ONE = {'ALA':'A', 'CYS':'C', 'ASP':'D', 'GLU':'E', 'PHE':'F', | ||||||||||||||||||
'GLY':'G', 'HIS':'H', 'ILE':'I', 'LYS':'K', 'LEU':'L', | ||||||||||||||||||
'MET':'M', 'ASN':'N', 'PRO':'P', 'GLN':'Q', 'ARG':'R', | ||||||||||||||||||
'SER':'S', 'TRE':'T', 'VAL':'V', 'TRP':'W', 'TYR':'Y'} | ||||||||||||||||||
AMINO_ACIDS_ONE_LETTER = {'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А можно было бы объединить сеты :)
Suggested change
|
||||||||||||||||||
'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', | ||||||||||||||||||
'W', 'Y'} | ||||||||||||||||||
AMINO_ACIDS_THREE_LETTER = {'ALA', 'CYS', 'ASP', 'GLU', 'PHE', | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ну или же для получения этого множества и множества выше, взять отдельно ключи и значенися словаря ABBREVIATION_THREE_TO_ONE Зачем вводить все вручную, если можно накодить?) |
||||||||||||||||||
'GLY', 'HIS', 'ILE', 'LYS', 'LEU', | ||||||||||||||||||
'MET', 'ASN', 'PRO', 'GLN', 'ARG', | ||||||||||||||||||
'SER', 'TRE', 'VAL', 'TRP', 'TYR'} | ||||||||||||||||||
|
||||||||||||||||||
import sys | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Все импорты должны быть вверху |
||||||||||||||||||
|
||||||||||||||||||
def change_abbreviation(seq: str) -> str: | ||||||||||||||||||
""" | ||||||||||||||||||
Changes the amino acid abbreviation from three-letter to one-letter. | ||||||||||||||||||
|
||||||||||||||||||
Parametrs | ||||||||||||||||||
---------- | ||||||||||||||||||
seq : str | ||||||||||||||||||
Amino acid sequence in three-letter form. | ||||||||||||||||||
Returns | ||||||||||||||||||
------- | ||||||||||||||||||
str | ||||||||||||||||||
Amino acid sequence in one-letter form | ||||||||||||||||||
|
||||||||||||||||||
""" | ||||||||||||||||||
one_letter_seq = [ABBREVIATION_THREE_TO_ONE[amino_acid] for amino_acid in seq.split("-")] | ||||||||||||||||||
return "".join(one_letter_seq) | ||||||||||||||||||
|
||||||||||||||||||
def is_correct_seq(seq: str) -> bool: | ||||||||||||||||||
""" | ||||||||||||||||||
Check the sequence for extraneous characters. | ||||||||||||||||||
|
||||||||||||||||||
Parametrs | ||||||||||||||||||
---------- | ||||||||||||||||||
seq : str | ||||||||||||||||||
Amino acid sequence. | ||||||||||||||||||
Returns | ||||||||||||||||||
------- | ||||||||||||||||||
bool | ||||||||||||||||||
TRUE - if there is no extraneous characters, FALSE - if there is extraneous characters. | ||||||||||||||||||
|
||||||||||||||||||
""" | ||||||||||||||||||
unique_amino_acids = set(seq) | ||||||||||||||||||
unique_amino_acids_three = set(seq.split("-")) | ||||||||||||||||||
check = unique_amino_acids <= AMINO_ACIDS_ONE_LETTER or unique_amino_acids_three <= AMINO_ACIDS_THREE_LETTER | ||||||||||||||||||
return check | ||||||||||||||||||
Comment on lines
+163
to
+166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Проблема с отступами;
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
def protein_tool(*args: str) -> Union[str, List[Union[Dict[str, int], str]]]: | ||||||||||||||||||
""" | ||||||||||||||||||
Receives a request from the user and runs the desired function. | ||||||||||||||||||
|
||||||||||||||||||
Parametrs | ||||||||||||||||||
---------- | ||||||||||||||||||
seq : str | ||||||||||||||||||
Amino acid sequences. | ||||||||||||||||||
operation : str | ||||||||||||||||||
Type of user's request. | ||||||||||||||||||
Returns | ||||||||||||||||||
------- | ||||||||||||||||||
str | ||||||||||||||||||
If a single sequence is supplied, outputs the result as a string or or identify a problem with a specific sequence. | ||||||||||||||||||
list | ||||||||||||||||||
If several sequences are supplied, outputs the result as a list. | ||||||||||||||||||
|
||||||||||||||||||
""" | ||||||||||||||||||
*seqs, operation = args | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Операцию над последовательностью в идеале можно сделать именованным аргументом, и тогда можно обойтись без усложнения в виде распаковки) |
||||||||||||||||||
operations = {'one letter':change_abbreviation, 'RNA':to_rna, 'DNA':to_dna, 'charge':define_charge, 'polarity':define_polarity} | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тоже стоит вынести как константу (отступы грустят) |
||||||||||||||||||
output = [] | ||||||||||||||||||
for seq in seqs: | ||||||||||||||||||
answer = is_correct_seq(seq.upper()) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нейминг: скорее это не ответ, а проверка, лучше назвать переменную input_check |
||||||||||||||||||
if answer: | ||||||||||||||||||
function_output = operations[operation](seq.upper()) | ||||||||||||||||||
output.append(function_output) | ||||||||||||||||||
else: | ||||||||||||||||||
print(f'Something wrong with {seq}', file=sys.stderr) | ||||||||||||||||||
continue | ||||||||||||||||||
if len(output) == 1 and (operation == 'RNA' or operation == 'DNA' or operation == 'one letter'): | ||||||||||||||||||
return ''.join(output) | ||||||||||||||||||
else: | ||||||||||||||||||
return output |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Словарь стоило бы вынести как константу