{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# BioPython fashioned after BioPerl\n",
    "# Provides a collection of bioinformatic utilities\n",
    "import Bio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Bio.Seq import Seq\n",
    "\n",
    "#create a sequence object\n",
    "my_seq = Seq('CATGTAGACTAG')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CATGTAGACTAG\n"
     ]
    }
   ],
   "source": [
    "print(my_seq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sequence CATGTAGACTAG is 12 bases long\n"
     ]
    }
   ],
   "source": [
    "print('sequence %s is %i bases long' % (my_seq, len(my_seq)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "reverse complement is CTAGTCTACATG\n"
     ]
    }
   ],
   "source": [
    "print('reverse complement is %s' % my_seq.reverse_complement())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "protein translation is HVD*\n"
     ]
    }
   ],
   "source": [
    "print('protein translation is %s' % my_seq.translate())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Seq('CATGTAGACTAG')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_seq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gi|2765658|emb|Z78533.1|CIZ78533\n",
      "Seq('CGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGATGAGACCGTGG...GGG', SingleLetterAlphabet())\n",
      "140\n"
     ]
    }
   ],
   "source": [
    "from Bio import SeqIO\n",
    "handle = open(\"../data/ls_orchid.fasta\")\n",
    "for seq_record in SeqIO.parse(handle, \"fasta\") :\n",
    "    print(seq_record.id)\n",
    "    print(repr(seq_record.seq))\n",
    "    print(len(seq_record))\n",
    "handle.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Bio import Entrez\n",
    "from Bio import SeqIO\n",
    "Entrez.email = \"giri.n.fiu@gmail.com\"\n",
    "handle = Entrez.efetch(db=\"nucleotide\", rettype=\"gb\", id=\"EU490707\", retmode=\"text\")\n",
    "# print(handle.read())\n",
    "# handle = Entrez.efetch(db=\"protein\", rettype=\"gb\", id=\"6273291\", retmode=\"text\")\n",
    "# handle = Entrez.efetch(db=\"protein\", rettype=\"fasta\", id=\"6273291\")\n",
    "# seq_record = SeqIO.read(handle, \"gb\")\n",
    "print(handle.read())\n",
    "handle.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "handle = Entrez.efetch(db=\"nucleotide\", rettype=\"gb\", id=\"EU490707,EU490707\", retmode=\"text\")\n",
    "print(handle.read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Convert GenBank format to fasta\n",
    "from Bio import SeqIO\n",
    "records = SeqIO.parse(\"../Data/ls_orchid.gbk\", \"genbank\")\n",
    "count = SeqIO.write(records, \"my_example.fasta\", \"fasta\")\n",
    "print(\"Converted %i records\" % count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for record in SeqIO.parse(\"my_example.fasta\", \"fasta\"):\n",
    "    print(record.id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Bio import SeqIO\n",
    "with open(\"my_example.fasta\", \"r\") as handle:\n",
    "    for record in SeqIO.parse(handle, \"fasta\"):\n",
    "        print(record.id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Bio import SeqIO\n",
    "records = list(SeqIO.parse(\"my_example.fasta\", \"fasta\"))\n",
    "print(records[0].id)  # first record\n",
    "print(records[-1].id)  # last record\n",
    "print(records[7].id)  # 8th record"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Bio import SeqIO\n",
    "record_dict = SeqIO.to_dict(SeqIO.parse(\"my_example.fasta\", \"fasta\"))\n",
    "print(record_dict[\"Z78525.1\"])  # use the record ID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Bio import SeqIO\n",
    "with open(\"../Data/opuntia.aln\", \"r\") as handle:\n",
    "    for record in SeqIO.parse(handle, \"clustal\") :\n",
    "        print(record.id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Tutorial: http://biopython.org/DIST/docs/tutorial/Tutorial.html\n",
    "# Documentation: https://biopython.org/wiki/Category%3AWiki_Documentation\n",
    "# Seq object: https://biopython.org/wiki/Seq\n",
    "# SeqIO object: https://biopython.org/wiki/SeqIO\n",
    "# AlignIO: https://biopython.org/wiki/AlignIO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Bio import AlignIO\n",
    "alignment = AlignIO.read(open(\"../Data/PF09395_seed.sth\"), \"stockholm\")\n",
    "print(\"Alignment length %i\" % alignment.get_alignment_length())\n",
    "for record in alignment :\n",
    "    print(record.seq + \" \" + record.id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
