|
0
|
1 #!/usr/bin/env python
|
|
|
2
|
|
|
3 class RoiRecord(object):
|
|
|
4 """Represent one record in a ROI file."""
|
|
|
5
|
|
|
6 def __init__(self, ref, start_pos, end_pos, region_name, region_length,
|
|
|
7 strand, max_count, data, points):
|
|
|
8 """Initialize RoiRecord."""
|
|
|
9 self.ref = ref
|
|
|
10 self.start_pos = start_pos
|
|
|
11 self.end_pos = end_pos
|
|
|
12 self.strand = strand
|
|
|
13 self.region_length = region_length
|
|
|
14 self.region_name = region_name
|
|
|
15 self.max_count = max_count
|
|
|
16 self.data = data
|
|
|
17 self.points = points
|
|
|
18
|
|
|
19 def __str__(self):
|
|
|
20 return 'RoiRecord(%s, %s, %s, %s, %s, %s, %s, %s, len([...])==%s)' % \
|
|
|
21 (repr(self.ref), self.start_pos, self.end_pos,
|
|
|
22 self.region_name, self.region_length, repr(self.strand),
|
|
|
23 self.max_count, self.data, len(self.points))
|
|
|
24 def __repr__(self):
|
|
|
25 return self.__str__()
|
|
|
26
|
|
|
27
|
|
|
28 def loadRoi(path, max_count=0):
|
|
|
29 """Load ROI file and return it as a list of RoiRecord objects.
|
|
|
30
|
|
|
31 NA values are translated to 0.
|
|
|
32 """
|
|
|
33 data_keys = []
|
|
|
34 result = []
|
|
|
35 i = 0
|
|
|
36 with open(path, 'rb') as f:
|
|
|
37 for line in f:
|
|
|
38 if line.startswith('##'):
|
|
|
39 data_keys = line[2:].split('\t')[7:-1]
|
|
|
40 if line[0] == '#':
|
|
|
41 continue
|
|
|
42 if max_count > 0 and i >= max_count:
|
|
|
43 break
|
|
|
44 i += 1
|
|
|
45 vals = line.split()
|
|
|
46 region_length = int(vals[4])
|
|
|
47 data = vals[7:-1]
|
|
|
48 points = [int(x) for x in vals[-1].split(',')]
|
|
|
49 r = RoiRecord(vals[0], int(vals[1]) - 1, int(vals[2]), vals[3],
|
|
|
50 region_length, vals[5], int(vals[6]), data, points)
|
|
|
51 result.append(r)
|
|
|
52 #print ' => Loaded %d records.' % len(result)
|
|
|
53 return data_keys, result
|