{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "d3dfbcc5", "metadata": {}, "outputs": [], "source": [ "%cd ~/projects/cmdb-quantbio/assignments/bootcamp/dictionaries_file_io/" ] }, { "cell_type": "markdown", "id": "5de1e054", "metadata": {}, "source": [ "- Open a filestream for the file \"CDS.fa\"" ] }, { "cell_type": "code", "execution_count": null, "id": "e694b5c2", "metadata": {}, "outputs": [], "source": [ "fa_fname = \"actin.fa\"\n", "fs = open(fa_fname)" ] }, { "cell_type": "markdown", "id": "cefb411d", "metadata": {}, "source": [ "- Create a list to hold the sequence being read in\n", "- Use a for loop to read the file, line by line\n", "- Capture the sequence name with a conditional\n", "- Before adding each sequence line to the list, remove any newline or space characters" ] }, { "cell_type": "code", "execution_count": null, "id": "c3b10cb8", "metadata": {}, "outputs": [], "source": [ "sequence = []\n", "for line in fs:\n", " if line.startswith('>'):\n", " name = line.rstrip().lstrip('> ')\n", " continue\n", " sequence.append(line.rstrip())\n", "fs.close()" ] }, { "cell_type": "markdown", "id": "2580a9f4", "metadata": {}, "source": [ "- Concatenate the list of sequences into a single sequence string" ] }, { "cell_type": "code", "execution_count": null, "id": "03306c81", "metadata": {}, "outputs": [], "source": [ "sequence = ''.join(sequence)" ] }, { "cell_type": "markdown", "id": "10fdf061", "metadata": {}, "source": [ "- Open a filestream to the codon table file\n", "- Create a dictionary to hold the codon/amino acid pairs\n", "- Read in the file, line by line\n", "- Strip newline and space characters from line and split by tabs to create a list of key and value\n", "- Add the key/value pair to the dictionary" ] }, { "cell_type": "code", "execution_count": null, "id": "073176bb", "metadata": {}, "outputs": [], "source": [ "codon_fname = \"codons.tsv\"\n", "codons = {}\n", "for line in open(codon_fname):\n", " codon, aa = line.rstrip().split('\\t')\n", " codons[codon] = aa" ] }, { "cell_type": "markdown", "id": "04765a58", "metadata": {}, "source": [ "- Create a list to hold the translated sequence\n", "- For each codon, look up its corresponding amino acid and add it to the amino acid list" ] }, { "cell_type": "code", "execution_count": null, "id": "4e053af4", "metadata": {}, "outputs": [], "source": [ "AAs = []\n", "for i in range(0, len(sequence), 3):\n", " AAs.append(codons.get(sequence[i:i+3], ' '))" ] }, { "cell_type": "markdown", "id": "c190bc96", "metadata": {}, "source": [ "- Open a filestream to write the results to\n", "- Write the sequence name with the greater than symbol (fasta format) (don't forget the newline character)\n", "- Join the amino acid letters to create an output line and write it to the file (don't forget the newline character)\n", "- Close the output filestream" ] }, { "cell_type": "code", "execution_count": null, "id": "49cb1fca", "metadata": {}, "outputs": [], "source": [ "out_fname = \"amino_acids.fa\"\n", "output = open(out_fname, 'w')\n", "output.write(f\">{name}\\n\")\n", "# output.write(''.join(AAs))\n", "for i in range(0, len(AAs), 40):\n", " output.write(''.join(AAs[i:i+40]) + '\\n')\n", "output.close()" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.14.7" } }, "nbformat": 4, "nbformat_minor": 5 }