forked from ucscCancer/cgData
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
372 lines (289 loc) · 10.4 KB
/
__init__.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import os
import re
import json
import functools
from zipfile import ZipFile
import sys
"""
CGData object style:
Every file type documented in the CGData specification has an equivilent object
to parse and manipulate the contents of that file type. For <dataType> there
should be a CGData.<dataType> object with a <CGData> class. These classes
should extend the baseObject class. For loading they implement the 'read'
function which will parse the contents of a file from a passed file handle.
"""
OBJECT_MAP = {
'genomicSegment': ('CGData.GenomicSegment', 'GenomicSegment'),
'genomicMatrix': ('CGData.GenomicMatrix', 'GenomicMatrix'),
'probeMap': ('CGData.ProbeMap', 'ProbeMap'),
'sampleMap': ('CGData.SampleMap', 'SampleMap'),
'clinicalMatrix': ('CGData.ClinicalMatrix', 'ClinicalMatrix'),
'dataSubType': ('CGData.DataSubType', 'DataSubType'),
'trackGenomic': ('CGData.TrackGenomic', 'TrackGenomic'),
'trackClinical': ('CGData.TrackClinical', 'TrackClinical'),
'assembly': ('CGData.Assembly', 'Assembly'),
'clinicalFeature': ('CGData.ClinicalFeature', 'ClinicalFeature')
}
MERGE_OBJECTS = [ 'trackClinical', 'trackGenomic' ]
class FormatException(Exception):
def __init__(self, str):
Exception.__init__(self, str)
def has_type(type_str):
return type_str in OBJECT_MAP
def get_type(type_str):
mod_name, cls_name = OBJECT_MAP[type_str]
module = __import__(mod_name, globals(), locals(), [ cls_name ])
cls = getattr(module, cls_name)
return cls
class CGGroupMember(object):
pass
class CGGroupBase(object):
DATA_FORM = None
def __init__(self, group_name):
self.members = {}
self.name = group_name
def __setitem__(self, name, item):
self.members[ name ] = item
def __getitem__(self, name):
return self.members[ name ]
def put(self, obj):
self.members[ obj.get_name() ] = obj
def is_link_ready(self):
for name in self.members:
if not self.members[name].is_link_ready():
return False
return True
def get_name(self):
return self.name
def unload(self):
for name in self.members:
self.members[name].unload()
def get(self, **kw):
for elem in self.members:
found = True
obj = self.members[ elem ]
for key in kw:
if obj.attrs.get( key, None ) != kw[key]\
and obj.attrs.get( ":" + key, None ) != kw[key]:
found = False
if found:
return obj
def get_link_map(self):
out = {}
for name in self.members:
lmap = self.members[ name ].get_link_map()
for ltype in lmap:
if ltype not in out:
out[ ltype ] = []
for lname in lmap[ltype]:
if lname not in out[ltype]:
out[ltype].append( lname )
return out
class UnimplementedException(Exception):
def __init__(self, str):
Exception.__init__(self, str)
class CGObjectBase(object):
"""
This is the base object for CGData loadable objects.
The methods covered in the base case cover usage meta-information
loading/unloading and manipulation as well as zip (cgz) file access.
"""
def __init__(self):
self.attrs = {}
self.path = None
self.zip = None
self.light_mode = False
def load(self, path=None, **kw):
if path is None and self.path is not None:
path = self.path
if path is None:
raise OSError( "Path not defined" )
if self.zip is None:
dhandle = open(path)
self.read(dhandle, **kw)
dhandle.close()
else:
z = ZipFile(self.zip)
dhandle = z.open(self.path)
self.read(dhandle, **kw)
dhandle.close()
z.close()
self.path = path
if (os.path.exists(path + ".json")):
mhandle = open(path + ".json")
self.set_attrs(json.loads(mhandle.read()))
mhandle.close()
def unload(self):
pass
def is_link_ready(self):
return True
def store(self, path=None):
if path is None and self.path is not None:
path = self.path
if path is None:
raise OSError( "Path not defined" )
mHandle = open(path + ".json", "w")
mHandle.write(json.dumps(self.attrs))
mHandle.close()
if not self.light_mode:
self.path = path
dhandle = open(path, "w")
self.write(dhandle)
dhandle.close()
def read(self, handle):
"""
The read method is implemented by the subclass that
inherits from CGObjectBase. It is passed a handle
to a file (which may be on file, in a compressed object, or
from a network source). The implementing class then uses his handle
to populate it's data structures.
"""
raise UnimplementedException()
def write(self, handle):
"""
The write method is implemented by the subclass that
inherits from CGObjectBase. It is passed a handle to an
output file, which it can use 'write' method calls to emit
it's data.
"""
raise UnimplementedException()
def get_attrs(self):
return self.attrs
def get_attr(self, name):
return self.attrs.get(name,None)
def set_attrs(self, attrs):
self.attrs.update(attrs)
def is_group_member(self):
if 'group' in self.attrs:
return True
return False
def get_group(self):
return self.attrs.get( 'group', self.attrs.get('name', None))
def get_name(self):
return self.attrs.get( 'name', None )
def get_link_map(self):
out = {}
for key in self.attrs:
if key.startswith(':'):
if isinstance( self.attrs[ key ], list ):
out[ key[1:] ] = self.attrs[ key ]
elif self.attrs[ key ] is not None:
out[ key[1:] ] = [ self.attrs[ key ] ]
return out
def add_history(self, desc):
if not 'history' in self.attrs:
self.attrs[ 'history' ] = []
self.attrs[ 'history' ].append( desc )
class CGMergeObject(object):
typeSet = {}
def __init__(self):
self.members = {}
def merge(self, **kw):
self.members = kw
def __iter__(self):
return self.members.keys().__iter__()
def __getitem__(self, item):
return self.members[item]
def unload(self):
pass
def sql_pass(self, id_table, method):
for t in self.members:
if hasattr(self.members[t], "gen_sql_" + method):
f = getattr(self.members[t], "gen_sql_" + method)
for line in f(id_table):
yield line
class CGDataSetObject(CGObjectBase):
def __init__(self):
CGObjectBase.__init__(self)
class CGDataMatrixObject(CGObjectBase):
def __init__(self):
CGObjectBase.__init__(self)
def cg_new(type_str):
"""
cg_new takes a type string and creates a new object from the
class named, it uses an internally defined map to find all
official CGData data types. So if a 'genomicMatrix' is requested
a CGData.GenomicMatrix.GenomicMatrix is initialized.
type_str -- A string name of a CGData type, ie 'genomicMatrix'
"""
mod_name, cls_name = OBJECT_MAP[type_str]
module = __import__(mod_name, globals(), locals(), [ cls_name ])
cls = getattr(module, cls_name)
out = cls()
return out
def load(path, zip=None):
"""
load is a the automatic CGData loading function. There has to
be a '.json' file for this function to work. It inspects the
'.json' file and uses the 'type' field to determine the
appropriate object loader to use. The object is created
(using the cg_new function) and the 'read' method is passed
a handle to the data file. If the 'zip' parameter is not None,
then it is used as the path to a zipfile, and the path parameter
is used as an path inside the zip file to the object data
path -- path to file (in file system space if zip is None, otherwise
it is the location in the zip file)
zip -- path to zip file (None by default)
"""
if not path.endswith(".json"):
path = path + ".json"
data_path = re.sub(r'.json$', '', path)
try:
handle = open(path)
meta = json.loads(handle.read())
except IOError:
raise FormatException("Meta-info (%s) file not found" % (path))
if meta['type'] in OBJECT_MAP:
out = cg_new(meta['type'])
out.set_attrs( meta )
out.path = data_path
out.load(data_path)
return out
else:
raise FormatException("%s class not found" % (meta['type']))
def light_load(path, zip=None):
if not path.endswith(".json"):
path = path + ".json"
data_path = re.sub(r'.json$', '', path)
if zip is None:
try:
handle = open(path)
meta = json.loads(handle.read())
except IOError:
raise FormatException("Meta-info (%s) file not found" % (path))
else:
z = ZipFile(zip)
handle = z.open(path)
meta = json.loads(handle.read())
handle.close()
z.close()
if meta['type'] in OBJECT_MAP:
out = cg_new(meta['type'])
out.set_attrs( meta )
out.path = data_path
out.zip = zip
out.light_mode = True
return out
else:
raise FormatException("%s class not found" % (meta['type']))
global LOG_LEVEL
LOG_LEVEL = 2
def log(eStr):
if LOG_LEVEL < 2:
sys.stderr.write("LOG: %s\n" % (eStr))
#errorLogHandle.write("LOG: %s\n" % (eStr))
def warn(eStr):
if LOG_LEVEL < 1:
sys.stderr.write("WARNING: %s\n" % (eStr))
#errorLogHandle.write("WARNING: %s\n" % (eStr))
def error(eStr):
sys.stderr.write("ERROR: %s\n" % (eStr))
#errorLogHandle.write("ERROR: %s\n" % (eStr))
#####################
TABLE = "table"
MATRIX = "matrix"
class Column(object):
def __init__(self, name, type, primary_key=False):
self.name = name
self.type = type
self.primary_key = primary_key