-
Notifications
You must be signed in to change notification settings - Fork 0
Review CNTN6 #21
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?
Review CNTN6 #21
Changes from all commits
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,88 @@ | ||||||||||||||||||
class BiologicalSequence: | ||||||||||||||||||
def __init__(self, sequence): | ||||||||||||||||||
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. не должно быть инита в абстрактном классе |
||||||||||||||||||
self.sequence = sequence | ||||||||||||||||||
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. Суть абстрактного класса - не держать в себе готовые атрибуты и методы, а только говорить какие атрибуты и/или методы должны обязательно быть у его наследников. Здесь выходит так, что у наследуемых классов с нуклеотидными последовательностями будет алфавит белков по умолчанию :( |
||||||||||||||||||
self.ALPHABET = {'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y'} | ||||||||||||||||||
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 __len__(self): | ||||||||||||||||||
return len(self.sequence) | ||||||||||||||||||
|
||||||||||||||||||
def __getitem__(self, idx): | ||||||||||||||||||
return self.sequence[idx] | ||||||||||||||||||
|
||||||||||||||||||
def __get_slice__(self, start, end): | ||||||||||||||||||
if start < 0 or start > len(self.sequence): | ||||||||||||||||||
raise IndexError("start index out of range") | ||||||||||||||||||
|
||||||||||||||||||
if end < 0 or end > len(self.sequence): | ||||||||||||||||||
raise IndexError("end index out of range") | ||||||||||||||||||
|
||||||||||||||||||
if start > end: | ||||||||||||||||||
raise ValueError("start index must be less than or equal to end index") | ||||||||||||||||||
|
||||||||||||||||||
return self.sequence[start:end] | ||||||||||||||||||
|
||||||||||||||||||
def __str__(self): | ||||||||||||||||||
return self.sequence | ||||||||||||||||||
|
||||||||||||||||||
def __repr__(self): | ||||||||||||||||||
return f'{self.__class__.__name__}({self.sequence!r})' | ||||||||||||||||||
Comment on lines
+27
to
+28
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. Класс, даже repr не забыт! И все методы строки есть! Супер! |
||||||||||||||||||
|
||||||||||||||||||
def is_valid_bioseq(self): | ||||||||||||||||||
for char in self.sequence: | ||||||||||||||||||
if char not in self.ALPHABET: | ||||||||||||||||||
return False | ||||||||||||||||||
return True | ||||||||||||||||||
Comment on lines
+31
to
+34
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
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. Можно избежать лишних циелов используя set :) Это не обязательно, но кажется будет работать чуть быстрее. Особенно если алфивиты тоже сразу сделаны сетами - удобная вещь!) |
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class NucleicAcidSequence(BiologicalSequence): | ||||||||||||||||||
def complement(self, COMPLEMENT_NUCLEOTIDES=None): | ||||||||||||||||||
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
|
||||||||||||||||||
if COMPLEMENT_NUCLEOTIDES is None: | ||||||||||||||||||
COMPLEMENT_NUCLEOTIDES = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'} | ||||||||||||||||||
Comment on lines
+39
to
+40
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
Кажется, комплементарные нуклеотиды у РНК и ДНК, которые от этого класса наследуются, будут разные. Так что лучше это определить в дочерних классах - или еще лучше - получать это из дочерних классов! 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. Так как это сильно поменяет структура дальнейшего кода - внесу только маленькое изменение - если есть какие-то константы, то их не надо объявлять внутри методов - лучше вынести в атрибут класса 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. Наверное эта часть должна быть в ДНК и РНК отдельно, так как это позволит сразу создать их с верным алфавитом |
||||||||||||||||||
|
||||||||||||||||||
complement_seq = '' | ||||||||||||||||||
for nucleotide in self.sequence: | ||||||||||||||||||
complement_seq += COMPLEMENT_NUCLEOTIDES.get(nucleotide) | ||||||||||||||||||
return complement_seq | ||||||||||||||||||
|
||||||||||||||||||
def gc_content(self): | ||||||||||||||||||
return (self.sequence.count('G') + self.sequence.count('C'))/len(self.sequence) | ||||||||||||||||||
Comment on lines
+47
to
+48
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. О, супер! Правда очень логично держать эту функцию тут - мы ведь ее можем и для РНК, и для ДНК использовать! |
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class RNASequence(NucleicAcidSequence): | ||||||||||||||||||
REV_TRANSCRTPTION_COMPLEMENT = {'A': 'T', 'U': 'A', 'C': 'G', 'G': 'C'} | ||||||||||||||||||
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. о! Класс! 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 __init__(self, sequence): | ||||||||||||||||||
super().__init__(sequence) | ||||||||||||||||||
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. если я не ошибаюсь, получается что все |
||||||||||||||||||
self.ALPHABET = {'A','U','G','C'} | ||||||||||||||||||
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 reverse_transribe(self): | ||||||||||||||||||
return DnaSequence(self.complement(self.REV_TRANSCRTPTION_COMPLEMENT)) | ||||||||||||||||||
Comment on lines
+58
to
+59
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. Супер! И дополнительная функция, и еще и может возвращать другой класс!!! 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. верно, что возвращает объект правильного класса |
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class DnaSequence(NucleicAcidSequence): | ||||||||||||||||||
TRANSCRTPTION_COMPLEMENT = {'A': 'U', 'T': 'A', 'C': 'G', 'G': 'C'} | ||||||||||||||||||
def __init__(self, sequence): | ||||||||||||||||||
super().__init__(sequence) | ||||||||||||||||||
self.ALPHABET = {'A', 'T', 'G', 'C'} | ||||||||||||||||||
Comment on lines
+63
to
+66
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 transcribe(self): | ||||||||||||||||||
return RNASequence(self.complement(self.TRANSCRTPTION_COMPLEMENT)) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class AminoAcidSequence(BiologicalSequence): | ||||||||||||||||||
AMINOACID_ALPHABET_1TO3 = {'A': 'Ala', 'R': 'Arg', 'N': 'Asn', 'D': 'Asp', 'C': 'Cys', | ||||||||||||||||||
'Q': 'Gln', 'E': 'Glu', 'G': 'Gly', 'H': 'His', 'I': 'Ile', | ||||||||||||||||||
'L': 'Leu', 'K': 'Lys', 'M': 'Met', 'F': 'Phe', 'P': 'Pro', | ||||||||||||||||||
'S': 'Ser', 'T': 'Thr', 'W': 'Trp', 'Y': 'Tyr', 'V': 'Val'} | ||||||||||||||||||
|
||||||||||||||||||
def __init__(self, sequence): | ||||||||||||||||||
super().__init__(sequence) | ||||||||||||||||||
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 convert_1_to_str3(self) -> str: | ||||||||||||||||||
seq3 = '' | ||||||||||||||||||
if len(self.sequence) > 0: | ||||||||||||||||||
for aminoacid in self.sequence: | ||||||||||||||||||
if aminoacid in self.AMINOACID_ALPHABET_1TO3: | ||||||||||||||||||
seq3 += self.AMINOACID_ALPHABET_1TO3[aminoacid] | ||||||||||||||||||
|
||||||||||||||||||
return seq3 |
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.
В целом, как говорили на паре, идеальный абстрактный класс init не содержит (чтобы нельзя было создавать его экзмепляры). Но тут конечно логично, что у каждого наследника должна быть последовательность, но лучше бы переписать это все в наследниках.