{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "b9c59e0e", "metadata": {}, "outputs": [], "source": [ "import re\n", "from pathlib import Path\n", "\n", "# ============================================================\n", "# Input / output\n", "# ============================================================\n", "\n", "GTF_FILE = Path(\n", " \"/s/chromatin/m/nobackup/ahmed/GitRepositries/\"\n", " \"DeepPlant/data/arabidopsis/gtf/Arabidopsis.gtf\"\n", ")\n", "\n", "OUTPUT_DIR = GTF_FILE.parent\n", "\n", "CODING_FILE = OUTPUT_DIR / \"coding_genes.txt\"\n", "NONCODING_FILE = OUTPUT_DIR / \"noncoding_genes.txt\"\n", "NONTRANSLATING_FILE = OUTPUT_DIR / \"nontranslating_CDS_genes.txt\"\n", "\n", "\n", "# ============================================================\n", "# Biotype definitions\n", "# ============================================================\n", "\n", "NONCODING_BIOTYPES = {\n", " \"lncRNA\",\n", " \"miRNA\",\n", " \"ncRNA\",\n", " \"rRNA\",\n", " \"snRNA\",\n", " \"snoRNA\",\n", " \"tRNA\",\n", "}\n", "\n", "PROTEIN_CODING_BIOTYPE = \"protein_coding\"\n", "\n", "SPECIAL_BIOTYPE = \"nontranslating_CDS\"\n", "\n", "\n", "# ============================================================\n", "# Parse GTF attributes\n", "# ============================================================\n", "\n", "def parse_attributes(attribute_string):\n", " \"\"\"\n", " Parse the attributes field of a GTF line.\n", "\n", " Example:\n", " gene_id \"AT1G01010\";\n", " gene_biotype \"protein_coding\";\n", " \"\"\"\n", "\n", " attributes = {}\n", "\n", " for match in re.finditer(r'(\\S+)\\s+\"([^\"]*)\"', attribute_string):\n", " key, value = match.groups()\n", " attributes[key] = value\n", "\n", " return attributes\n", "\n", "\n", "# ============================================================\n", "# Main\n", "# ============================================================\n", "\n", "def main():\n", "\n", " if not GTF_FILE.exists():\n", " raise FileNotFoundError(\n", " f\"GTF file not found:\\n{GTF_FILE}\"\n", " )\n", "\n", " coding_genes = set()\n", " noncoding_genes = set()\n", " nontranslating_genes = set()\n", "\n", " biotype_counts = {}\n", " mitochondrial_or_chloroplast_genes = set()\n", "\n", " with open(GTF_FILE, \"r\") as f:\n", "\n", " for line_number, line in enumerate(f, start=1):\n", "\n", " line = line.strip()\n", "\n", " if not line or line.startswith(\"#\"):\n", " continue\n", "\n", " fields = line.split(\"\\t\")\n", "\n", " if len(fields) != 9:\n", " print(\n", " f\"Warning: skipping malformed line {line_number}\"\n", " )\n", " continue\n", "\n", " feature = fields[2]\n", " attributes_string = fields[8]\n", "\n", " # We only need gene-level annotations\n", " if feature != \"gene\":\n", " continue\n", "\n", " attributes = parse_attributes(attributes_string)\n", "\n", " gene_id = attributes.get(\"gene_id\")\n", "\n", " if gene_id is None:\n", " print(\n", " f\"Warning: no gene_id found on line {line_number}\"\n", " )\n", " continue\n", "\n", " # Remove version suffix if present\n", " # Example: AT1G01010.1 -> AT1G01010\n", " gene_id = gene_id.split(\".\")[0]\n", "\n", " # =================================================\n", " # Remove mitochondrial genes\n", " # =================================================\n", "\n", " if gene_id.upper().startswith((\"ATMG\", \"ATCG\")):\n", " mitochondrial_or_chloroplast_genes.add(gene_id)\n", " continue\n", "\n", " # =================================================\n", " # Get gene biotype\n", " # =================================================\n", "\n", " biotype = (\n", " attributes.get(\"gene_biotype\")\n", " or attributes.get(\"gene_type\")\n", " or attributes.get(\"biotype\")\n", " or \"unknown\"\n", " )\n", "\n", " biotype_counts[biotype] = (\n", " biotype_counts.get(biotype, 0) + 1\n", " )\n", "\n", " # =================================================\n", " # Classify gene\n", " # =================================================\n", "\n", " if biotype == PROTEIN_CODING_BIOTYPE:\n", "\n", " coding_genes.add(gene_id)\n", "\n", " elif biotype in NONCODING_BIOTYPES:\n", "\n", " noncoding_genes.add(gene_id)\n", "\n", " elif biotype == SPECIAL_BIOTYPE:\n", "\n", " nontranslating_genes.add(gene_id)\n", "\n", " # =========================================================\n", " # Sort\n", " # =========================================================\n", "\n", " coding_genes = sorted(coding_genes)\n", " noncoding_genes = sorted(noncoding_genes)\n", " nontranslating_genes = sorted(nontranslating_genes)\n", " mitochondrial_or_chloroplast_genes = sorted(mitochondrial_or_chloroplast_genes)\n", "\n", " # =========================================================\n", " # Write output files\n", " # =========================================================\n", "\n", " with open(CODING_FILE, \"w\") as f:\n", " for gene_id in coding_genes:\n", " f.write(gene_id + \"\\n\")\n", "\n", " with open(NONCODING_FILE, \"w\") as f:\n", " for gene_id in noncoding_genes:\n", " f.write(gene_id + \"\\n\")\n", "\n", " with open(NONTRANSLATING_FILE, \"w\") as f:\n", " for gene_id in nontranslating_genes:\n", " f.write(gene_id + \"\\n\")\n", "\n", " # =========================================================\n", " # Summary\n", " # =========================================================\n", "\n", " print(\"\\n========================================\")\n", " print(\"Arabidopsis gene classification\")\n", " print(\"========================================\")\n", "\n", " print(f\"\\nProtein-coding genes : {len(coding_genes):,}\")\n", " print(f\"Non-coding genes : {len(noncoding_genes):,}\")\n", " print(f\"Nontranslating CDS : {len(nontranslating_genes):,}\")\n", " print(f\"Mitochondrial or Chloroplast genes : {len(mitochondrial_or_chloroplast_genes):,}\")\n", "\n", " print(\"\\nBiotypes retained:\")\n", " for biotype, count in sorted(biotype_counts.items()):\n", " print(f\" {biotype:25s} {count:,}\")\n", "\n", " print(\"\\nOutput files:\")\n", " print(f\" Coding : {CODING_FILE}\")\n", " print(f\" Non-coding : {NONCODING_FILE}\")\n", " print(f\" Nontranslating: {NONTRANSLATING_FILE}\")\n", "\n", " print(\"\\nFirst mitochondrial genes removed:\")\n", " for gene_id in mitochondrial_or_chloroplast_genes[:10]:\n", " print(f\" {gene_id}\")\n", "\n", " if len(mitochondrial_or_chloroplast_genes) > 10:\n", " print(f\" ... and {len(mitochondrial_or_chloroplast_genes) - 10:,} more\")\n", "\n", "\n", "if __name__ == \"__main__\":\n", " main()" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }