Debian Codegraph @

unit_tests/input/Get Subseq.ipynb

raw
1 {
2 "metadata": {
3 "name": "",
4 "signature": "sha256:8085e97a331345d09d79abcf05d188bbd241f68cab4a5f4fa70e56f954131e92"
5 },
6 "nbformat": 3,
7 "nbformat_minor": 0,
8 "worksheets": [
9 {
10 "cells": [
11 {
12 "cell_type": "code",
13 "collapsed": false,
14 "input": [
15 "from pyfasta import Fasta\n",
16 "from Bio import SeqIO\n",
17 "from Bio.Seq import Seq\n",
18 "from Bio.SeqRecord import SeqRecord"
19 ],
20 "language": "python",
21 "metadata": {},
22 "outputs": [],
23 "prompt_number": 29
24 },
25 {
26 "cell_type": "code",
27 "collapsed": false,
28 "input": [
29 "# Requires this file to be in the current working directory\n",
30 "all_trans = Fasta(\"Homo_sapiens.GRCh38.cdna.all.fa\")"
31 ],
32 "language": "python",
33 "metadata": {},
34 "outputs": [],
35 "prompt_number": 36
36 },
37 {
38 "cell_type": "markdown",
39 "metadata": {},
40 "source": [
41 "# Get the first `max_trans` transcripts"
42 ]
43 },
44 {
45 "cell_type": "code",
46 "collapsed": false,
47 "input": [
48 "min_len = 500\n",
49 "count = 0\n",
50 "max_trans = 10\n",
51 "trans_to_keep = {}\n",
52 "for tname in all_trans.keys():\n",
53 " if count == max_trans:\n",
54 " break\n",
55 " if len(all_trans[tname]) > min_len:\n",
56 " trans_to_keep[tname] = str(all_trans[tname])\n",
57 " count += 1"
58 ],
59 "language": "python",
60 "metadata": {},
61 "outputs": [],
62 "prompt_number": 14
63 },
64 {
65 "cell_type": "markdown",
66 "metadata": {},
67 "source": [
68 "# Now output them"
69 ]
70 },
71 {
72 "cell_type": "code",
73 "collapsed": false,
74 "input": [
75 "trans_recs = []\n",
76 "for tname in trans_to_keep.keys():\n",
77 " trans_recs.append(SeqRecord(seq = Seq(trans_to_keep[tname]),\n",
78 " id = tname.split()[0],\n",
79 " description = \"\"))"
80 ],
81 "language": "python",
82 "metadata": {},
83 "outputs": [],
84 "prompt_number": 33
85 },
86 {
87 "cell_type": "code",
88 "collapsed": false,
89 "input": [
90 "out_fname = \"{0}_trans_gt_{1}_bp.fasta\".format(max_trans, min_len)"
91 ],
92 "language": "python",
93 "metadata": {},
94 "outputs": [],
95 "prompt_number": 34
96 },
97 {
98 "cell_type": "code",
99 "collapsed": false,
100 "input": [
101 "with open(out_fname, \"w\") as out_fhandle:\n",
102 " SeqIO.write(trans_recs, out_fhandle, \"fasta\")"
103 ],
104 "language": "python",
105 "metadata": {},
106 "outputs": [],
107 "prompt_number": 35
108 },
109 {
110 "cell_type": "code",
111 "collapsed": false,
112 "input": [],
113 "language": "python",
114 "metadata": {},
115 "outputs": []
116 }
117 ],
118 "metadata": {}
119 }
120 ]
121 }