Created
March 25, 2024 20:46
-
-
Save huddlej/b35921582f41b44a944ce9f4fb1be6ae to your computer and use it in GitHub Desktop.
Simple example of how to calculate coverage per position of a multiple sequence alignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "b2a632f3-1ff5-4338-89e7-1a82e6eb670e", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import Bio.AlignIO\n", | |
| "from matplotlib import pyplot as plt\n", | |
| "import re" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "849e2e37-237d-4482-8923-30340ba464c6", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "alignment_path = \"builds/loes-2024/ha/aligned.fasta\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "cba57488-148a-4906-ab36-d5e9c2c8e98c", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "alignment = Bio.AlignIO.read(alignment_path, \"fasta\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "a0b247c1-b7a4-46f2-a42d-ce4291d4fb11", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "number_of_records = len(alignment)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "ea9d777e-705d-4a5e-b9b2-f22c52b43ca0", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "coverages = []\n", | |
| "for position in range(alignment.get_alignment_length()):\n", | |
| " coverage = len(re.findall(r\"[ACGT]\", alignment[:, position]))\n", | |
| " coverages.append(coverage)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "f48189ff-1091-43ed-b9f4-c8ddd5eb1b7c", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "fig, ax = plt.subplots(1, 1, figsize=(8, 4), dpi=300)\n", | |
| "ax.plot(\n", | |
| " coverages,\n", | |
| ")\n", | |
| "\n", | |
| "ax.set_ylim(bottom=0)\n", | |
| "\n", | |
| "ax.set_xlabel(\"Alignment position\")\n", | |
| "ax.set_ylabel(\"Number of records\")" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3 (ipykernel)", | |
| "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.10.13" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment