-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUCSC.py
241 lines (201 loc) · 7.21 KB
/
UCSC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# Authors: Dustin Schones and Keji Zhao
#
# This software is distributable under the terms of the GNU General
# Public License (GPL) v2, the text of which can be found at
# http://www.gnu.org/copyleft/gpl.html. Installing, importing or
# otherwise using this module constitutes acceptance of the terms of
# this License.
#
# Disclaimer
#
# This software is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# Comments and/or additions are welcome (send e-mail to:
"""
This module contains the classes to deal with UCSC
known gene files
"""
import re, os, shutil, time, sys
from math import *
from string import *
plus = re.compile('\+');
minus = re.compile('\-');
import BED;
UCSCError = "Error in UCSC class";
class UCSC:
"""
Class for keeping known gene information. This might be too much
information to carry around when the most usefull parts are really
the chrom, strand, txStart and txEnd.
"""
def __init__(self, name, chrom, strand, txStart, txEnd, cdsStart,
cdsEnd, exonCount, exonStarts, exonEnds):
self.name = name;
self.chrom = chrom;
self.strand = strand;
self.txStart = txStart;
self.txEnd = txEnd;
self.cdsStart = cdsStart;
self.cdsEnd = cdsEnd;
self.exonCount = exonCount;
self.exonStarts = exonStarts;
self.exonEnds = exonEnds;
def __set__(self, name, chrom, strand, txStart, txEnd, cdsStart,
cdsEnd, exonCount, exonStarts, exonEnds):
self.name = name;
self.chrom = chrom;
self.strand = strand;
self.txStart = txStart;
self.txEnd = txEnd;
self.cdsStart = cdsStart;
self.cdsEnd = cdsEnd;
self.exonCount = exonCount;
self.exonStarts = exonStarts;
self.exonEnds = exonEnds;
def getAll(self):
outstring = self.name + " " + self.chrom + " " + self.strand + " " + \
str(self.txStart) + \
" " + str(self.txEnd) + " " + str(self.cdsStart) + " " + \
str(self.cdsEnd) + " " + str(self.exonCount) + " " + \
str(self.exonStarts) + " " + str(self.exonEnds);
try:
return outstring;
except:
sys.stderr.write("No UCSC known gene information for %s\n" % self)
return ''
#----------------------------
#----------------------------
class UCSC_lite:
"""
Class for keeping known gene information. Only partial (most
usefull) information stored.
"""
def __init__(self, name, chrom, strand, txStart, txEnd):
self.name = name;
self.chrom = chrom;
self.strand = strand;
self.txStart = txStart;
self.txEnd = txEnd;
def __set__(self, name, chrom, strand, txStart, txEnd):
self.name = name;
self.chrom = chrom;
self.strand = strand;
self.txStart = txStart;
self.txEnd = txEnd;
def getAll(self):
outstring = self.name + " " + self.chrom + " " + self.strand + \
" " + str(self.txStart) + " " + str(self.txEnd);
try:
return outstring;
except:
sys.stderr.write("No UCSC known gene information for %s\n" % self)
return ''
#----------------------------
#----------------------------
class KnownGenes:
"""
Class to read in UCSC known gene files and get all the info
"""
def __init__(self, file=None):
"""
Reads in a UCSC known gene file and builds a dictionary
with chromosomes as keys and lists of bed vals representing
the genes on each chromosome as values.
"""
self.gene_coords = {}
infile = open(file);
for line in infile:
""" check to make sure not a header line """
if not re.match("#", line):
line = line.strip();
sline = line.split();
if sline[1] not in self.gene_coords.keys():
self.gene_coords[sline[1]] = [];
coord = UCSC(sline[0], sline[1], sline[2], atoi(sline[3]),
atoi(sline[4]), atoi(sline[5]), atoi(sline[6]),
atoi(sline[7]), sline[8], sline[9]);
self.gene_coords[sline[1]].append(coord);
def getPromoters(self, upstream, downstream):
"""
Return the promoter coordinates defined by given upstream and
downstream distances in a bed dictionary
NOTICE: if gene is on negative strand, the promoter start
and end are still sequential
"""
self.prom_coords = {};
for chrom in self.gene_coords.keys():
chrom_coords = self.gene_coords[chrom];
for g in chrom_coords:
if plus.match(g.strand):
prom_start = g.txStart - upstream;
prom_end = g.txStart + downstream;
elif minus.match(g.strand):
prom_start = g.txEnd - downstream;
prom_end = g.txEnd + upstream;
if prom_start > 0:
ucsc = UCSC_lite(g.name, chrom, g.strand,
prom_start, prom_end);
else:
ucsc = UCSC_lite(g.name, chrom, g.strand,
0, prom_end);
if chrom not in self.prom_coords.keys():
self.prom_coords[chrom] = [];
self.prom_coords[chrom].append(ucsc);
return self.prom_coords;
def keys(self):
"""
Return a list of the keys - duplicating the function of a dictionary
"""
return self.gene_coords.keys()
def getNumGenes(self):
"""
Return the number of genes
"""
num = 0;
for c in self.gene_coords.keys():
num += len(self.gene_coords[c]);
return num;
def __setitem__(self, name, bedcoord):
"""
Sets a new gene coord
"""
self.gene_coords[name] = bedcoord
def __getitem__(self, name):
"""
Returns a bed_val indexed by its name or None if no such bed_val exists
"""
if self.gene_coords.has_key(name):
return self.gene_coords[name]
else: raise UCSCError
def __del__(self):
"""
Delete, delete;
"""
self.gene_coords.clear()
def __contains__(self, item):
"""
Returns mapping iterator
"""
return self.gene_coords.has_key(item)
def __iter__(self):
"""
Returns mapping iterator
"""
return self.gene_coords.iterkeys()
def __len__(self):
"""
Returns number of gene_coords
"""
return len(self.gene_coords)
def __delitem__(self, name):
"""
removes a chrom if its name exists in the dictionary
-- I guess this could possibly be useful at some point
"""
if self.gene_coords.has_key(name):
del self.gene_coords[name]
else: raise bedError