Mercurial > repos > iuc > ngsutils_bam_filter
annotate ngsutils/bed/__init__.py @ 1:8187a729d9f4 draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit bbabad202be09c91e66c94bcbfd057e49f57c841
author | iuc |
---|---|
date | Sun, 06 Dec 2015 05:03:12 -0500 |
parents | 4e4e4093d65d |
children | 7a68005de299 |
rev | line source |
---|---|
0
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
1 import os |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
2 import ngsutils.support.ngs_utils |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
3 import pysam |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
4 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
5 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
6 class BedStreamer(object): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
7 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
8 Streams BedRegions from a BED file |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
9 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
10 Note - this can only be used once! There is no mechanism to seek the stream. |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
11 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
12 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
13 def __init__(self, fname=None, fileobj=None, quiet=False): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
14 if not fname and not fileobj: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
15 raise ValueError("You must specify either fname or fileobj!") |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
16 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
17 self.reader = ngsutils.support.gzip_reader(fname=fname, quiet=quiet, fileobj=fileobj) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
18 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
19 def __iter__(self): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
20 return self |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
21 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
22 def next(self): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
23 try: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
24 while True: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
25 line = self.reader.next().strip() |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
26 if line and line[0] != '#': |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
27 cols = line.split('\t') |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
28 while len(cols) < 6: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
29 cols.append('') |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
30 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
31 return BedRegion(*cols) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
32 except: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
33 raise StopIteration |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
34 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
35 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
36 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
37 class BedFile(object): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
38 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
39 BED files are read in their entirety memory, in a series of bins. Each bin |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
40 is ~100kb in size. Each bin can then be iterated over. |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
41 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
42 This is less efficient than using a proper index, but in reality, this |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
43 usually isn't an issue. However, if the BED file has been Tabix indexed, |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
44 that index will be used for random access. |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
45 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
46 NOTE: This isn't very efficient, so perhaps this can be remodeled into a BedFile |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
47 and a BedFileIndex where the file is indexed only if random access is requested. |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
48 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
49 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
50 _bin_const = 100000 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
51 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
52 def __init__(self, fname=None, fileobj=None, region=None): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
53 self._bins = {} |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
54 self._bin_list = [] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
55 self._cur_bin_idx = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
56 self._cur_bin_pos = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
57 self._tellpos = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
58 self._total = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
59 self._length = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
60 self.__tabix = None |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
61 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
62 self.filename = fname |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
63 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
64 if os.path.exists('%s.tbi' % fname): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
65 self.__tabix = pysam.Tabixfile(fname) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
66 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
67 if fileobj: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
68 self.__readfile(fileobj) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
69 elif fname: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
70 with ngsutils.support.ngs_utils.gzip_opener(fname) as fobj: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
71 self.__readfile(fobj) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
72 elif region: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
73 chrom, startend = region.split(':') |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
74 if '-' in startend: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
75 start, end = [int(x) for x in startend.split('-')] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
76 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
77 start = int(startend) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
78 end = start |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
79 start -= 1 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
80 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
81 self.__add_region(BedRegion(chrom, start, end)) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
82 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
83 raise ValueError("Must specify either filename, fileobj, or region") |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
84 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
85 def __readfile(self, fobj): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
86 for line in fobj: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
87 line = line.strip() |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
88 if line and line[0] != '#': |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
89 cols = line.split('\t') |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
90 while len(cols) < 6: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
91 cols.append('') |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
92 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
93 region = BedRegion(*cols) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
94 self.__add_region(region) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
95 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
96 self._bin_list.sort() |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
97 for bin in self._bins: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
98 self._bins[bin].sort() |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
99 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
100 def __add_region(self, region): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
101 self._total += region.end - region.start |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
102 self._length += 1 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
103 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
104 startbin = region.start / BedFile._bin_const |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
105 endbin = region.end / BedFile._bin_const |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
106 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
107 for bin in xrange(startbin, endbin + 1): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
108 if not (region.chrom, bin) in self._bins: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
109 self._bin_list.append((region.chrom, bin)) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
110 self._bins[(region.chrom, bin)] = [] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
111 self._bins[(region.chrom, bin)].append(region) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
112 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
113 def fetch(self, chrom, start, end, strand=None): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
114 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
115 For TABIX indexed BED files, find all regions w/in a range |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
116 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
117 For non-TABIX index BED files, use the calculated bins, and |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
118 output matching regions |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
119 ''' |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
120 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
121 if self.__tabix: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
122 for match in self.__tabix.fetch(chrom, start, end): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
123 region = BedRegion(*match.split('\t')) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
124 if not strand or (strand and region.strand == strand): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
125 yield region |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
126 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
127 startbin = start / BedFile._bin_const |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
128 endbin = end / BedFile._bin_const |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
129 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
130 buf = set() |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
131 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
132 for bin in xrange(startbin, endbin + 1): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
133 if (chrom, bin) in self._bins: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
134 for region in self._bins[(chrom, bin)]: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
135 if strand and strand != region.strand: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
136 continue |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
137 if start <= region.start <= end or start <= region.end <= end: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
138 if not region in buf: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
139 yield region |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
140 buf.add(region) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
141 elif region.start < start and region.end > end: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
142 if not region in buf: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
143 yield region |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
144 buf.add(region) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
145 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
146 def tell(self): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
147 return self._tellpos |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
148 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
149 def close(self): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
150 pass |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
151 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
152 @property |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
153 def length(self): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
154 return self._length |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
155 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
156 @property |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
157 def total(self): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
158 return self._total |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
159 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
160 def __iter__(self): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
161 self._cur_bin_idx = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
162 self._cur_bin_pos = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
163 self._tellpos = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
164 return self |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
165 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
166 def next(self): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
167 if self._cur_bin_idx >= len(self._bin_list): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
168 raise StopIteration |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
169 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
170 binvals = self._bins[self._bin_list[self._cur_bin_idx]] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
171 while self._cur_bin_pos < len(binvals): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
172 val = binvals[self._cur_bin_pos] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
173 self._cur_bin_pos += 1 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
174 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
175 startbin = (val.chrom, val.start / BedFile._bin_const) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
176 if startbin == self._bin_list[self._cur_bin_idx]: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
177 self._tellpos += 1 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
178 return val |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
179 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
180 self._cur_bin_idx += 1 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
181 self._cur_bin_pos = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
182 return self.next() |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
183 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
184 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
185 class BedRegion(object): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
186 def __init__(self, chrom, start, end, name='', score='', strand='', thickStart='', thickEnd='', rgb='', *args): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
187 self.chrom = chrom |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
188 self.start = int(start) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
189 self.end = int(end) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
190 self.name = name |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
191 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
192 if score == '': |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
193 self.score = 0 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
194 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
195 self.score = float(score) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
196 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
197 if strand == '': |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
198 self.strand = None |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
199 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
200 self.strand = strand |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
201 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
202 if thickStart == '': |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
203 self.thickStart = None |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
204 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
205 self.thickStart = thickStart |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
206 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
207 if thickEnd == '': |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
208 self.thickEnd = None |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
209 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
210 self.thickEnd = thickEnd |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
211 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
212 if rgb == '': |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
213 self.rgb = None |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
214 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
215 self.rgb = rgb |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
216 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
217 self.extras = args |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
218 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
219 def clone(self, chrom=None, start=None, end=None, name=None, score=None, strand=None, thickStart=None, thickEnd=None, rgb=None, *args): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
220 cols = [] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
221 cols.append(self.chrom if chrom is None else chrom) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
222 cols.append(self.start if start is None else start) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
223 cols.append(self.end if end is None else end) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
224 cols.append(self.name if name is None else name) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
225 cols.append(self.score if score is None else score) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
226 cols.append(self.strand if strand is None else strand) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
227 cols.append(self.thickStart if thickStart is None else thickStart) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
228 cols.append(self.thickEnd if thickEnd is None else thickEnd) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
229 cols.append(self.rgb if rgb is None else rgb) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
230 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
231 for i, val in enumerate(self.extras): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
232 if len(args) > i: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
233 cols.append(args[i]) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
234 else: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
235 cols.append(val) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
236 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
237 return BedRegion(*cols) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
238 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
239 @property |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
240 def score_int(self): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
241 score = str(self.score) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
242 if score[-2:] == '.0': |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
243 score = score[:-2] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
244 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
245 return score |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
246 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
247 def __key(self): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
248 return (self.chrom, self.start, self.end, self.strand, self.name) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
249 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
250 def __lt__(self, other): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
251 return self.__key() < other.__key() |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
252 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
253 def __gt__(self, other): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
254 return self.__key() > other.__key() |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
255 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
256 def __eq__(self, other): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
257 return self.__key() == other.__key() |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
258 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
259 def write(self, out): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
260 out.write('%s\n' % self) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
261 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
262 def __repr__(self): |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
263 outcols = [] |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
264 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
265 if self.rgb: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
266 outcols.append(self.rgb) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
267 if self.thickEnd or outcols: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
268 outcols.append(self.thickEnd if self.thickEnd else self.end) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
269 if self.thickStart or outcols: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
270 outcols.append(self.thickStart if self.thickStart else self.start) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
271 if self.strand or outcols: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
272 outcols.append(self.strand) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
273 if self.score_int != '' or outcols: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
274 outcols.append(self.score_int) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
275 if self.name or outcols: |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
276 outcols.append(self.name) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
277 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
278 outcols.append(self.end) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
279 outcols.append(self.start) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
280 outcols.append(self.chrom) |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
281 |
4e4e4093d65d
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/ngsutils commit 09194687c74a424732f8b0c017cbb942aad89068
iuc
parents:
diff
changeset
|
282 return '\t'. join([str(x) for x in outcols[::-1]]) |