Repository navigation
Expand file tree
/
Copy pathtool_pathways.py
More file actions
executable file
·155 lines (117 loc) · 5.08 KB
/
Copy pathtool_pathways.py
File metadata and controls
executable file
·155 lines (117 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
import os
import sys
import sqlite3
import numpy as np
import cPickle
import zlib
from collections import defaultdict
from gemini.config import read_gemini_config
import gemini_utils as util
from gemini_constants import *
def get_pathways(args):
version_dic = defaultdict()
version_dic = {'66': 'kegg_pathways_ensembl66', '67': 'kegg_pathways_ensembl67',
'68': 'kegg_pathways_ensembl68', '69': 'kegg_pathways_ensembl69',
'70': 'kegg_pathways_ensembl70', '71': 'kegg_pathways_ensembl71'}
config = read_gemini_config()
path_dirname = config["annotation_dir"]
if args.version in version_dic:
path_file = os.path.join(path_dirname, version_dic[args.version])
else:
sys.exit("Unsupported Ensembl gene version.\n")
agn_paths = defaultdict(list)
hgnc_paths = defaultdict(list)
ensembl_paths = defaultdict(list)
for line in open(path_file, 'r'):
fields=line.strip().split("\t")
uniprot = fields[0]
agn = fields[1]
hgnc = fields[2]
ensid = fields[3]
ens_transcript = fields[4]
hsa = fields[5]
path = fields[6] if fields[6] != 'None' else None
# clean up the pathways such that this:
# path:hsa00260;Glycine_serine_and_threonine_metabolism
# becomes this:
# hsa00260:Glycine_serine_and_threonine_metabolism
if path is not None and path.startswith("path:"):
path = path[5:]
path = path.replace(";", ":")
# build gene/transcript -> pathway mappings using
# all three gene naming conventions
agn_paths[(agn, ens_transcript)].append(path)
hgnc_paths[(hgnc, ens_transcript)].append(path)
ensembl_paths[(ensid, ens_transcript)].append(path)
return agn_paths, hgnc_paths, ensembl_paths
def _get_pathways(gene, transcript, pathways, allow_none=True):
# get distinct pathways
pathways = set(pathways)
if None in pathways:
# remove "None" if a valid pathway exists.
if len(pathways) > 1 or allow_none is False:
pathways.remove(None)
return pathways
def _report_variant_pathways(c, args, idx_to_sample):
(agn_paths, hgnc_paths, ensembl_paths) = get_pathways(args)
for r in c:
gt_types = np.array(cPickle.loads(zlib.decompress(r['gt_types'])))
gts = np.array(cPickle.loads(zlib.decompress(r['gts'])))
gene = str(r['gene'])
trans = str(r['transcript'])
pathways = []
if (gene, trans) in agn_paths:
pathways = _get_pathways(gene, trans, agn_paths[(gene, trans)],
allow_none=False)
elif (gene, trans) in hgnc_paths:
pathways = _get_pathways(gene, trans, hgnc_paths[(gene, trans)],
allow_none=False)
elif (gene, trans) in ensembl_paths:
pathways = _get_pathways(gene, trans, ensembl_paths[(gene, trans)],
allow_none=False)
pathlist = ",".join(pathways)
for idx, gt_type in enumerate(gt_types):
if (gt_type == HET or gt_type == HOM_ALT) and \
len(pathways) > 0:
print "\t".join([r['chrom'], str(r['start']), str(r['end']), \
r['ref'], r['alt'], r['impact'], \
idx_to_sample[idx], gts[idx], gene, trans, \
pathlist])
def get_ind_pathways(c, args):
idx_to_sample = util.map_indicies_to_samples(c)
query = "SELECT v.chrom, v.start, v.end, v.ref, v.alt, \
i.impact, v.gt_types, v.gts, i.gene, \
i.transcript \
FROM variants v, variant_impacts i \
WHERE v.variant_id = i.variant_id"
c.execute(query)
# header
print '\t'.join(['chrom', 'start', 'end', 'ref', 'alt', \
'impact', 'sample', 'genotype', \
'gene', 'transcript', 'pathway'])
_report_variant_pathways(c, args, idx_to_sample)
def get_ind_lof_pathways(c, args):
idx_to_sample = util.map_indicies_to_samples(c)
query = "SELECT v.chrom, v.start, v.end, v.ref, v.alt, \
i.impact, v.gt_types, v.gts, i.gene, \
i.transcript \
FROM variants v, variant_impacts i \
WHERE v.variant_id = i.variant_id \
AND i.is_lof='1'"
c.execute(query)
# header
print '\t'.join(['chrom', 'start', 'end', 'ref', 'alt', \
'impact', 'sample', 'genotype', \
'gene', 'transcript', 'pathway'])
_report_variant_pathways(c, args, idx_to_sample)
def pathways(parser, args):
if os.path.exists(args.db):
conn = sqlite3.connect(args.db)
conn.isolation_level = None
conn.row_factory = sqlite3.Row
c = conn.cursor()
if (not args.lof):
get_ind_pathways(c, args)
else:
get_ind_lof_pathways(c, args)