File size: 8,438 Bytes
7cdfe0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
{
 "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
}