Skip to content

Commit

Permalink
object-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
telatin committed May 31, 2024
1 parent 7db3184 commit 50f3e7d
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions first-steps/05-object.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's try to formalise a class for the storing the sequence."
"Now let's try to formalise a class for the storing the sequence. **At the end of this notebook an explanation**"
]
},
{
Expand Down Expand Up @@ -164,7 +164,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"we can also control the equality with `__eq__`..."
"HINT: we can also control the equality with `__eq__`..."
]
},
{
Expand Down Expand Up @@ -206,6 +206,63 @@
"\n",
"Creates and returns a new DNASequence object with the reverse complement sequence."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class DNASequence:\n",
" # Standard genetic code dictionary\n",
" codon_table = {\n",
" 'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',\n",
" 'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',\n",
" 'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',\n",
" 'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',\n",
" 'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',\n",
" 'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',\n",
" 'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',\n",
" 'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',\n",
" 'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',\n",
" 'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',\n",
" 'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',\n",
" 'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',\n",
" 'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',\n",
" 'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',\n",
" 'TAC':'Y', 'TAT':'Y', 'TAA':'*', 'TAG':'*',\n",
" 'TGC':'C', 'TGT':'C', 'TGA':'*', 'TGG':'W',\n",
" }\n",
"\n",
" def __init__(self, sequence, name, description=\"\"):\n",
" self.sequence = sequence.upper()\n",
" self.name = name\n",
" self.description = description\n",
"\n",
" def __len__(self):\n",
" return len(self.sequence)\n",
"\n",
" def __str__(self):\n",
" desc_str = f\" {self.description}\" if self.description else \"\"\n",
" return f\">{self.name}{desc_str}\\n{self.sequence}\"\n",
"\n",
" def __eq__(self, other):\n",
" return self.sequence == other.sequence\n",
" \n",
" def rc(self):\n",
" complement = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}\n",
" rev_comp = ''.join(complement[base] for base in reversed(self.sequence))\n",
" return DNASequence(rev_comp, self.name + \"_rc\", self.description)\n",
" \n",
" def translate(self):\n",
" protein = []\n",
" # Process the DNA sequence in chunks of three (codons)\n",
" for i in range(0, len(self.sequence), 3):\n",
" codon = self.sequence[i:i+3]\n",
" if len(codon) == 3:\n",
" protein.append(self.codon_table.get(codon, 'X')) # 'X' for unknown codons\n",
" return ''.join(protein)"
]
}
],
"metadata": {
Expand Down

0 comments on commit 50f3e7d

Please sign in to comment.