annotate gstf_preparation.py @ 11:dbe37a658cd2 draft

"planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
author earlhaminst
date Sun, 27 Sep 2020 18:54:31 +0000
parents e8e75a79de59
children 99bae410128c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
1 from __future__ import print_function
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
2
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
3 import json
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
4 import optparse
10
e8e75a79de59 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 9c8611fee927883f50bc6955771aa69df1ce8457"
earlhaminst
parents: 9
diff changeset
5 import os
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
6 import sqlite3
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
7 import sys
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
8
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
9 version = "0.5.0"
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
10 gene_count = 0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
11
8
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
12
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
13 class Sequence(object):
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
14 def __init__(self, header, sequence_parts):
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
15 self.header = header
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
16 self.sequence_parts = sequence_parts
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
17 self._sequence = None
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
18
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
19 @property
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
20 def sequence(self):
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
21 if self._sequence is None:
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
22 self._sequence = ''.join(self.sequence_parts)
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
23 return self._sequence
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
24
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
25 def print(self, fh=sys.stdout):
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
26 print(self.header, file=fh)
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
27 for line in self.sequence_parts:
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
28 print(line, file=fh)
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
29
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
30
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
31 def FASTAReader_gen(fasta_filename):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
32 with open(fasta_filename) as fasta_file:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
33 line = fasta_file.readline()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
34 while True:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
35 if not line:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
36 return
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
37 assert line.startswith('>'), "FASTA headers must start with >"
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
38 header = line.rstrip()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
39 sequence_parts = []
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
40 line = fasta_file.readline()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
41 while line and line[0] != '>':
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
42 sequence_parts.append(line.rstrip())
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
43 line = fasta_file.readline()
8
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
44 yield Sequence(header, sequence_parts)
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
45
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
46
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
47 def create_tables(conn):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
48 cur = conn.cursor()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
49
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
50 cur.execute('''CREATE TABLE meta (
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
51 version VARCHAR PRIMARY KEY NOT NULL)''')
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
52
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
53 cur.execute('INSERT INTO meta (version) VALUES (?)',
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
54 (version, ))
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
55
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
56 cur.execute('''CREATE TABLE gene (
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
57 gene_id VARCHAR PRIMARY KEY NOT NULL,
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
58 gene_symbol VARCHAR,
6
56bbdbfe3eaa planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit fa875eea77a9471acada2b7b8882a0467994c960
earlhaminst
parents: 5
diff changeset
59 seq_region_name VARCHAR NOT NULL,
56bbdbfe3eaa planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit fa875eea77a9471acada2b7b8882a0467994c960
earlhaminst
parents: 5
diff changeset
60 seq_region_start INTEGER NOT NULL,
56bbdbfe3eaa planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit fa875eea77a9471acada2b7b8882a0467994c960
earlhaminst
parents: 5
diff changeset
61 seq_region_end INTEGER NOT NULL,
56bbdbfe3eaa planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit fa875eea77a9471acada2b7b8882a0467994c960
earlhaminst
parents: 5
diff changeset
62 seq_region_strand INTEGER NOT NULL,
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
63 species VARCHAR NOT NULL,
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
64 biotype VARCHAR,
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
65 gene_json VARCHAR NOT NULL)''')
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
66 cur.execute('CREATE INDEX gene_symbol_index ON gene (gene_symbol)')
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
67
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
68 cur.execute('''CREATE TABLE transcript (
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
69 transcript_id VARCHAR PRIMARY KEY NOT NULL,
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
70 transcript_symbol VARCHAR,
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
71 protein_id VARCHAR UNIQUE,
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
72 protein_sequence VARCHAR,
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
73 biotype VARCHAR,
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
74 is_canonical BOOLEAN NOT NULL DEFAULT FALSE,
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
75 gene_id VARCHAR NOT NULL REFERENCES gene(gene_id))''')
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
76
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
77 # The following temporary view is not used in GAFA, so schema changes to it
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
78 # don't require a meta version upgrade.
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
79 cur.execute('''CREATE TEMPORARY VIEW transcript_join_gene AS
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
80 SELECT transcript_id, transcript_symbol, COALESCE(transcript.biotype, gene.biotype) AS biotype, is_canonical, gene_id, gene_symbol, seq_region_name, species
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
81 FROM transcript JOIN gene
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
82 USING (gene_id)''')
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
83
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
84 conn.commit()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
85
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
86
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
87 def fetch_transcript_and_gene(conn, transcript_id):
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
88 cur = conn.cursor()
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
89
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
90 cur.execute('SELECT * FROM transcript_join_gene WHERE transcript_id=?',
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
91 (transcript_id, ))
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
92 return cur.fetchone()
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
93
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
94
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
95 def remove_type_from_list_of_ids(ids):
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
96 return ','.join(remove_type_from_id(id_) for id_ in ids.split(','))
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
97
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
98
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
99 def remove_type_from_id(id_):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
100 colon_index = id_.find(':')
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
101 if colon_index >= 0:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
102 return id_[colon_index + 1:]
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
103 else:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
104 return id_
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
105
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
106
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
107 def feature_to_dict(cols, parent_dict=None):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
108 d = {
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
109 'end': int(cols[4]),
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
110 'start': int(cols[3]),
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
111 }
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
112 for attr in cols[8].split(';'):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
113 if '=' in attr:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
114 (tag, value) = attr.split('=')
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
115 if tag == 'ID':
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
116 tag = 'id'
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
117 value = remove_type_from_id(value)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
118 elif tag == 'Parent':
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
119 value = remove_type_from_list_of_ids(value)
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
120 elif tag == 'representative':
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
121 tag = 'is_canonical'
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
122 d[tag] = value
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
123 if cols[6] == '+':
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
124 d['strand'] = 1
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
125 elif cols[6] == '-':
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
126 d['strand'] = -1
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
127 else:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
128 raise Exception("Unrecognized strand '%s'" % cols[6])
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
129 if parent_dict is not None and 'Parent' in d:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
130 # a 3' UTR can be split among multiple exons
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
131 # a 5' UTR can be split among multiple exons
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
132 # a CDS can be part of multiple transcripts
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
133 for parent in d['Parent'].split(','):
10
e8e75a79de59 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 9c8611fee927883f50bc6955771aa69df1ce8457"
earlhaminst
parents: 9
diff changeset
134 parent_dict.setdefault(parent, []).append(d)
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
135 return d
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
136
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
137
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
138 def add_gene_to_dict(cols, species, gene_dict):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
139 global gene_count
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
140 gene = feature_to_dict(cols)
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
141 if not gene['id']:
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
142 raise Exception("Id not found among column 9 attribute tags: %s" % cols[8])
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
143 gene.update({
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
144 'member_id': gene_count,
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
145 'object_type': 'Gene',
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
146 'seq_region_name': cols[0],
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
147 'species': species,
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
148 'Transcript': [],
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
149 'display_name': gene.get('Name'),
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
150 })
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
151 gene_dict[gene['id']] = gene
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
152 gene_count = gene_count + 1
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
153
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
154
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
155 def add_transcript_to_dict(cols, species, transcript_dict):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
156 transcript = feature_to_dict(cols)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
157 transcript.update({
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
158 'object_type': 'Transcript',
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
159 'seq_region_name': cols[0],
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
160 'species': species,
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
161 'display_name': transcript.get('Name'),
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
162 })
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
163 transcript_dict[transcript['id']] = transcript
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
164
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
165
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
166 def add_exon_to_dict(cols, species, exon_parent_dict):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
167 exon = feature_to_dict(cols, exon_parent_dict)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
168 exon.update({
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
169 'length': int(cols[4]) - int(cols[3]) + 1,
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
170 'object_type': 'Exon',
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
171 'seq_region_name': cols[0],
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
172 'species': species,
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
173 })
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
174 if 'id' not in exon and 'Name' in exon:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
175 exon['id'] = exon['Name']
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
176
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
177
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
178 def add_cds_to_dict(cols, cds_parent_dict):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
179 cds = feature_to_dict(cols, cds_parent_dict)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
180 if 'id' not in cds:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
181 if 'Name' in cds:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
182 cds['id'] = cds['Name']
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
183 elif 'Parent' in cds and ',' not in cds['Parent']:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
184 cds['id'] = cds['Parent']
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
185
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
186
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
187 def join_dicts(gene_dict, transcript_dict, exon_parent_dict, cds_parent_dict, five_prime_utr_parent_dict, three_prime_utr_parent_dict):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
188 for parent, exon_list in exon_parent_dict.items():
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
189 if parent in transcript_dict:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
190 exon_list.sort(key=lambda _: _['start'])
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
191 transcript_dict[parent]['Exon'] = exon_list
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
192
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
193 for transcript_id, transcript in transcript_dict.items():
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
194 translation = {
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
195 'CDS': [],
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
196 'id': None,
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
197 'end': transcript['end'],
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
198 'object_type': 'Translation',
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
199 'species': transcript['species'],
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
200 'start': transcript['start'],
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
201 }
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
202 found_cds = False
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
203 derived_translation_start = None
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
204 derived_translation_end = None
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
205 if transcript_id in cds_parent_dict:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
206 cds_list = cds_parent_dict[transcript_id]
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
207 cds_ids = set(_['id'] for _ in cds_list)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
208 if len(cds_ids) > 1:
5
b3ba0c84667c planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 4
diff changeset
209 raise Exception("Transcript %s has multiple CDSs: this is not supported by Ensembl JSON format" % transcript_id)
b3ba0c84667c planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 4
diff changeset
210 cds_id = cds_ids.pop()
b3ba0c84667c planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 4
diff changeset
211 translation['id'] = cds_id
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
212 cds_list.sort(key=lambda _: _['start'])
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
213 translation['CDS'] = cds_list
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
214 translation['start'] = cds_list[0]['start']
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
215 translation['end'] = cds_list[-1]['end']
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
216 found_cds = True
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
217 if transcript_id in five_prime_utr_parent_dict:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
218 five_prime_utr_list = five_prime_utr_parent_dict[transcript_id]
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
219 five_prime_utr_list.sort(key=lambda _: _['start'])
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
220 if transcript['strand'] == 1:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
221 derived_translation_start = five_prime_utr_list[-1]['end'] + 1
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
222 else:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
223 derived_translation_end = five_prime_utr_list[0]['start'] - 1
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
224 if transcript_id in three_prime_utr_parent_dict:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
225 three_prime_utr_list = three_prime_utr_parent_dict[transcript_id]
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
226 three_prime_utr_list.sort(key=lambda _: _['start'])
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
227 if transcript['strand'] == 1:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
228 derived_translation_end = three_prime_utr_list[0]['start'] - 1
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
229 else:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
230 derived_translation_start = three_prime_utr_list[-1]['end'] + 1
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
231 if derived_translation_start is not None:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
232 if found_cds:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
233 if derived_translation_start > translation['start']:
5
b3ba0c84667c planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 4
diff changeset
234 raise Exception("Transcript %s has the start of CDS %s overlapping with the UTR end" % (transcript_id, cds_id))
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
235 else:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
236 translation['start'] = derived_translation_start
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
237 if derived_translation_end is not None:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
238 if found_cds:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
239 if derived_translation_end < translation['end']:
5
b3ba0c84667c planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 4
diff changeset
240 raise Exception("Transcript %s has the end of CDS %s overlapping with the UTR start" % (transcript_id, cds_id))
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
241 else:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
242 translation['end'] = derived_translation_end
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
243 if found_cds or derived_translation_start is not None or derived_translation_end is not None:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
244 transcript['Translation'] = translation
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
245
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
246 for transcript in transcript_dict.values():
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
247 if 'Parent' in transcript:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
248 # A polycistronic transcript can have multiple parents
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
249 for parent in transcript['Parent'].split(','):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
250 if parent in gene_dict:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
251 gene_dict[parent]['Transcript'].append(transcript)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
252
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
253
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
254 def write_gene_dict_to_db(conn, gene_dict):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
255 cur = conn.cursor()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
256
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
257 for gene in gene_dict.values():
3
7e11a7f4bdba planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 77ed525b753e34d3e9dd4f07a239592ce764f7e6-dirty
earlhaminst
parents: 1
diff changeset
258 if gene is None:
7e11a7f4bdba planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 77ed525b753e34d3e9dd4f07a239592ce764f7e6-dirty
earlhaminst
parents: 1
diff changeset
259 # This can happen when loading a JSON file from Ensembl
7e11a7f4bdba planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 77ed525b753e34d3e9dd4f07a239592ce764f7e6-dirty
earlhaminst
parents: 1
diff changeset
260 continue
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
261 if 'confidence' in gene and gene['confidence'] != 'high':
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
262 print("Gene %s has confidence %s (not high), discarding" % (gene['id'], gene['confidence']), file=sys.stderr)
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
263 continue
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
264 gene_id = gene['id']
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
265 cur.execute('INSERT INTO gene (gene_id, gene_symbol, seq_region_name, seq_region_start, seq_region_end, seq_region_strand, species, biotype, gene_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
266 (gene_id, gene.get('display_name'), gene['seq_region_name'], gene['start'], gene['end'], gene['strand'], gene['species'], gene.get('biotype'), json.dumps(gene)))
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
267
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
268 if "Transcript" in gene:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
269 for transcript in gene["Transcript"]:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
270 transcript_id = transcript['id']
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
271 transcript_symbol = transcript.get('display_name')
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
272 protein_id = transcript.get('Translation', {}).get('id')
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
273 biotype = transcript.get('biotype')
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
274 is_canonical = transcript.get('is_canonical', False)
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
275 to_insert = (transcript_id, transcript_symbol, protein_id, biotype, is_canonical, gene_id)
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
276 try:
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
277 cur.execute('INSERT INTO transcript (transcript_id, transcript_symbol, protein_id, biotype, is_canonical, gene_id) VALUES (?, ?, ?, ?, ?, ?)',
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
278 to_insert)
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
279 except Exception as e:
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
280 raise Exception("Error while inserting %s into transcript table: %s" % (str(to_insert), e))
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
281
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
282 conn.commit()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
283
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
284
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
285 def remove_id_version(s, force=False):
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
286 """
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
287 Remove the optional '.VERSION' from an id if it's an Ensembl id or if
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
288 `force` is True.
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
289 """
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
290 if force or s.startswith('ENS'):
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
291 return s.split('.')[0]
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
292 else:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
293 return s
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
294
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
295
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
296 def __main__():
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
297 parser = optparse.OptionParser()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
298 parser.add_option('--gff3', action='append', default=[], help='GFF3 file to convert, in SPECIES:FILENAME format. Use multiple times to add more files')
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
299 parser.add_option('--json', action='append', default=[], help='JSON file to merge. Use multiple times to add more files')
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
300 parser.add_option('--fasta', action='append', default=[], help='Path of the input FASTA files')
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
301 parser.add_option('--filter', type='choice', choices=['canonical', 'coding', ''], default='', help='Which transcripts to keep')
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
302 parser.add_option('--headers', type='choice',
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
303 choices=['TranscriptId_species', 'GeneSymbol-TranscriptID_species', 'TranscriptSymbol-TranscriptID_species', ''],
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
304 default='', help='Change the header line of the FASTA sequences to this format')
6
56bbdbfe3eaa planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit fa875eea77a9471acada2b7b8882a0467994c960
earlhaminst
parents: 5
diff changeset
305 parser.add_option('--regions', default="", help='Comma-separated list of region IDs for which FASTA sequences should be filtered')
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
306 parser.add_option('-o', '--output', help='Path of the output SQLite file')
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
307 parser.add_option('--of', help='Path of the output FASTA file')
10
e8e75a79de59 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 9c8611fee927883f50bc6955771aa69df1ce8457"
earlhaminst
parents: 9
diff changeset
308 parser.add_option('--ff', default=os.devnull, help='Path of the filtered sequences output FASTA file')
6
56bbdbfe3eaa planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit fa875eea77a9471acada2b7b8882a0467994c960
earlhaminst
parents: 5
diff changeset
309
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
310 options, args = parser.parse_args()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
311 if args:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
312 raise Exception('Use options to provide inputs')
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
313
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
314 conn = sqlite3.connect(options.output)
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
315 conn.row_factory = sqlite3.Row
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
316 conn.execute('PRAGMA foreign_keys = ON')
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
317 create_tables(conn)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
318
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
319 for gff3_arg in options.gff3:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
320 try:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
321 (species, filename) = gff3_arg.split(':')
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
322 except ValueError:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
323 raise Exception("Argument for --gff3 '%s' is not in the SPECIES:FILENAME format" % gff3_arg)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
324 gene_dict = dict()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
325 transcript_dict = dict()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
326 exon_parent_dict = dict()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
327 cds_parent_dict = dict()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
328 five_prime_utr_parent_dict = dict()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
329 three_prime_utr_parent_dict = dict()
5
b3ba0c84667c planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 4
diff changeset
330 unimplemented_feature_nlines_dict = dict()
4
284f64ad9d43 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit cda3ecab1a34376cc7d4d392a34dc810847cbf0b-dirty
earlhaminst
parents: 3
diff changeset
331
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
332 with open(filename) as f:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
333 for i, line in enumerate(f, start=1):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
334 line = line.strip()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
335 if not line:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
336 # skip empty lines
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
337 continue
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
338 if line[0] == '#':
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
339 # skip comment lines
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
340 continue
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
341 cols = line.split('\t')
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
342 if len(cols) != 9:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
343 raise Exception("Line %i in file '%s': '%s' does not have 9 columns" % (i, filename, line))
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
344 feature_type = cols[2]
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
345 try:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
346 if feature_type == 'gene':
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
347 add_gene_to_dict(cols, species, gene_dict)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
348 elif feature_type in ('mRNA', 'transcript'):
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
349 add_transcript_to_dict(cols, species, transcript_dict)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
350 elif feature_type == 'exon':
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
351 add_exon_to_dict(cols, species, exon_parent_dict)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
352 elif feature_type == 'five_prime_UTR':
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
353 feature_to_dict(cols, five_prime_utr_parent_dict)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
354 elif feature_type == 'three_prime_UTR':
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
355 feature_to_dict(cols, three_prime_utr_parent_dict)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
356 elif feature_type == 'CDS':
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
357 add_cds_to_dict(cols, cds_parent_dict)
5
b3ba0c84667c planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 4
diff changeset
358 elif feature_type in unimplemented_feature_nlines_dict:
b3ba0c84667c planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 4
diff changeset
359 unimplemented_feature_nlines_dict[feature_type] += 1
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
360 else:
5
b3ba0c84667c planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 4
diff changeset
361 unimplemented_feature_nlines_dict[feature_type] = 0
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
362 except Exception as e:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
363 print("Line %i in file '%s': %s" % (i, filename, e), file=sys.stderr)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
364
5
b3ba0c84667c planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 4
diff changeset
365 for unimplemented_feature, nlines in unimplemented_feature_nlines_dict.items():
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
366 print("Skipped %d lines in GFF3 file '%s': '%s' is not an implemented feature type" % (nlines, filename, unimplemented_feature), file=sys.stderr)
5
b3ba0c84667c planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 95bab1105cf8a7b07c668f08f712399e8775a4ae
earlhaminst
parents: 4
diff changeset
367
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
368 join_dicts(gene_dict, transcript_dict, exon_parent_dict, cds_parent_dict, five_prime_utr_parent_dict, three_prime_utr_parent_dict)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
369 write_gene_dict_to_db(conn, gene_dict)
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
370
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
371 for json_arg in options.json:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
372 with open(json_arg) as f:
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
373 write_gene_dict_to_db(conn, json.load(f))
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
374
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
375 # Read the FASTA files a first time to:
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
376 # - determine for each file if we need to force the removal of the version
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
377 # from the transcript id
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
378 # - fill gene_transcripts_dict when keeping only the canonical transcripts
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
379 force_remove_id_version_file_list = []
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
380 gene_transcripts_dict = dict()
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
381 for fasta_arg in options.fasta:
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
382 force_remove_id_version = False
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
383 found_gene_transcript = False
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
384 for entry in FASTAReader_gen(fasta_arg):
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
385 # Extract the transcript id by removing everything after the first space and then removing the version if needed
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
386 transcript_id = remove_id_version(entry.header[1:].lstrip().split(' ')[0], force_remove_id_version)
4
284f64ad9d43 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit cda3ecab1a34376cc7d4d392a34dc810847cbf0b-dirty
earlhaminst
parents: 3
diff changeset
387
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
388 transcript = fetch_transcript_and_gene(conn, transcript_id)
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
389 if not transcript and not found_gene_transcript:
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
390 # We have not found a proper gene transcript in this file yet,
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
391 # try to force the removal of the version from the transcript id
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
392 transcript_id = remove_id_version(entry.header[1:].lstrip().split(' ')[0], True)
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
393 transcript = fetch_transcript_and_gene(conn, transcript_id)
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
394 # Remember that we need to force the removal for this file
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
395 if transcript:
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
396 force_remove_id_version = True
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
397 force_remove_id_version_file_list.append(fasta_arg)
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
398 print("Forcing removal of id version in FASTA file '%s'" % fasta_arg, file=sys.stderr)
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
399 if not transcript:
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
400 print("Transcript '%s' in FASTA file '%s' not found in the gene feature information" % (transcript_id, fasta_arg), file=sys.stderr)
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
401 continue
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
402 if options.filter != 'canonical':
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
403 break
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
404 found_gene_transcript = True
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
405
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
406 if len(entry.sequence) % 3 != 0:
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
407 continue
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
408 transcript_biotype = transcript['biotype'] # This is the biotype of the transcript or, if that is NULL, the one of the gene
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
409 if transcript_biotype and transcript_biotype != 'protein_coding':
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
410 continue
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
411 gene_transcripts_dict.setdefault(transcript['gene_id'], []).append((transcript_id, transcript['is_canonical'], len(entry.sequence)))
4
284f64ad9d43 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit cda3ecab1a34376cc7d4d392a34dc810847cbf0b-dirty
earlhaminst
parents: 3
diff changeset
412
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
413 if options.filter == 'canonical':
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
414 selected_transcript_ids = []
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
415 for gene_id, transcript_tuples in gene_transcripts_dict.items():
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
416 canonical_transcript_ids = [id_ for (id_, is_canonical, _) in transcript_tuples if is_canonical]
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
417 if not canonical_transcript_ids:
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
418 # Select the transcript with the longest sequence. If more than
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
419 # one transcripts have the same longest sequence for a gene, the
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
420 # first one to appear in the FASTA file is selected.
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
421 selected_transcript_id = max(transcript_tuples, key=lambda transcript_tuple: transcript_tuple[2])[0]
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
422 elif len(canonical_transcript_ids) > 1:
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
423 raise Exception("Gene %s has more than 1 canonical transcripts" % (gene_id))
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
424 else:
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
425 selected_transcript_id = canonical_transcript_ids[0]
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
426 selected_transcript_ids.append(selected_transcript_id)
4
284f64ad9d43 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit cda3ecab1a34376cc7d4d392a34dc810847cbf0b-dirty
earlhaminst
parents: 3
diff changeset
427
6
56bbdbfe3eaa planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit fa875eea77a9471acada2b7b8882a0467994c960
earlhaminst
parents: 5
diff changeset
428 regions = [_.strip().lower() for _ in options.regions.split(",")]
56bbdbfe3eaa planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit fa875eea77a9471acada2b7b8882a0467994c960
earlhaminst
parents: 5
diff changeset
429 with open(options.of, 'w') as output_fasta_file, open(options.ff, 'w') as filtered_fasta_file:
4
284f64ad9d43 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit cda3ecab1a34376cc7d4d392a34dc810847cbf0b-dirty
earlhaminst
parents: 3
diff changeset
430 for fasta_arg in options.fasta:
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
431 force_remove_id_version = fasta_arg in force_remove_id_version_file_list
4
284f64ad9d43 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit cda3ecab1a34376cc7d4d392a34dc810847cbf0b-dirty
earlhaminst
parents: 3
diff changeset
432 for entry in FASTAReader_gen(fasta_arg):
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
433 transcript_id = remove_id_version(entry.header[1:].lstrip().split(' ')[0], force_remove_id_version)
4
284f64ad9d43 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit cda3ecab1a34376cc7d4d392a34dc810847cbf0b-dirty
earlhaminst
parents: 3
diff changeset
434
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
435 transcript = fetch_transcript_and_gene(conn, transcript_id)
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
436 if not transcript:
9
f4acbfe8d6fe planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 2f56285b1ef694d732c8b2637e3e924f8a626e55
earlhaminst
parents: 8
diff changeset
437 print("Transcript '%s' in FASTA file '%s' not found in the gene feature information" % (transcript_id, fasta_arg), file=sys.stderr)
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
438 continue
4
284f64ad9d43 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit cda3ecab1a34376cc7d4d392a34dc810847cbf0b-dirty
earlhaminst
parents: 3
diff changeset
439
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
440 if options.filter == 'canonical':
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
441 # We already filtered out non-protein-coding transcripts when populating gene_transcripts_dict
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
442 if transcript_id not in selected_transcript_ids:
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
443 continue
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
444 elif options.filter == 'coding':
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
445 if len(entry.sequence) % 3 != 0:
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
446 print("Transcript '%s' in FASTA file '%s' has a coding sequence length which is not multiple of 3, removing from FASTA output" % (transcript_id, fasta_arg), file=sys.stderr)
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
447 continue
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
448 transcript_biotype = transcript['biotype'] # This is the biotype of the transcript or, if that is NULL, the one of the gene
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
449 if transcript_biotype and transcript_biotype != 'protein_coding':
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
450 print("Transcript %s has biotype %s (not protein-coding), removing from FASTA output" % (transcript_id, transcript_biotype), file=sys.stderr)
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
451 continue
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
452
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
453 if options.headers == "TranscriptId_species":
4
284f64ad9d43 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit cda3ecab1a34376cc7d4d392a34dc810847cbf0b-dirty
earlhaminst
parents: 3
diff changeset
454 # Change the FASTA header to '>TranscriptId_species', as required by TreeBest
284f64ad9d43 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit cda3ecab1a34376cc7d4d392a34dc810847cbf0b-dirty
earlhaminst
parents: 3
diff changeset
455 # Remove any underscore in the species
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
456 entry.header = ">%s_%s" % (transcript_id, transcript['species'].replace('_', ''))
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
457 elif options.headers == "GeneSymbol-TranscriptID_species":
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
458 # Remove any underscore in the species
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
459 entry.header = ">%s-%s_%s" % (transcript['gene_symbol'], transcript_id, transcript['species'].replace('_', ''))
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
460 elif options.headers == "TranscriptSymbol-TranscriptID_species":
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
461 # Remove any underscore in the species
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
462 entry.header = ">%s-%s_%s" % (transcript['transcript_symbol'], transcript_id, transcript['species'].replace('_', ''))
4
284f64ad9d43 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit cda3ecab1a34376cc7d4d392a34dc810847cbf0b-dirty
earlhaminst
parents: 3
diff changeset
463
11
dbe37a658cd2 "planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 133a11e7195f8da83c5b661d8babb3f6d9e18812"
earlhaminst
parents: 10
diff changeset
464 if transcript['seq_region_name'].lower() in regions:
8
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
465 entry.print(filtered_fasta_file)
6
56bbdbfe3eaa planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit fa875eea77a9471acada2b7b8882a0467994c960
earlhaminst
parents: 5
diff changeset
466 else:
8
92f3966d5bc3 planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 88ba62ae8c3d9587a0015c72209242ad0c1df0c2
earlhaminst
parents: 6
diff changeset
467 entry.print(output_fasta_file)
0
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
468
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
469 conn.close()
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
470
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
471
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
472 if __name__ == '__main__':
28879ca33b5f planemo upload for repository https://github.com/TGAC/earlham-galaxytools/tree/master/tools/gstf_preparation commit 651fae48371f845578753052c6fe173e3bb35670
earlhaminst
parents:
diff changeset
473 __main__()