annotate genetrack_util.py @ 0:25cd59a002d9 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
author iuc
date Tue, 22 Dec 2015 17:03:27 -0500
parents
children df7ac50ade5d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
1 import bisect
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
2 import math
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
3 import numpy
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
4 import re
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
5 import subprocess
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
6 import sys
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
7 import tempfile
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
8
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
9 GFF_EXT = 'gff'
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
10 SCIDX_EXT = 'scidx'
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
11
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
12
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
13 def noop(data):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
14 return data
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
15
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
16
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
17 def zeropad_to_numeric(data):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
18 return re.sub(r'chr0(\d)', r'chr\1', data)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
19
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
20
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
21 def numeric_to_zeropad(data):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
22 return re.sub(r'chr(\d([^\d]|$))', r'chr0\1', data)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
23
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
24
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
25 FORMATS = ['zeropad', 'numeric']
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
26 IN_CONVERT = {'zeropad': zeropad_to_numeric, 'numeric': noop}
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
27 OUT_CONVERT = {'zeropad': numeric_to_zeropad, 'numeric': noop}
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
28
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
29
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
30 def conversion_functions(in_fmt, out_fmt):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
31 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
32 Returns the proper list of functions to apply to perform a conversion
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
33 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
34 return [IN_CONVERT[in_fmt], OUT_CONVERT[out_fmt]]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
35
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
36
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
37 def convert_data(data, in_fmt, out_fmt):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
38 for fn in conversion_functions(in_fmt, out_fmt):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
39 data = fn(data)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
40 return data
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
41
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
42
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
43 class ChromosomeManager(object):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
44 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
45 Manages a CSV reader of an index file to only load one chrom at a time
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
46 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
47
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
48 def __init__(self, reader):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
49 self.done = False
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
50 self.reader = reader
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
51 self.processed_chromosomes = []
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
52 self.current_index = 0
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
53 self.next_valid()
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
54
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
55 def next(self):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
56 self.line = self.reader.next()
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
57
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
58 def is_valid(self, line):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
59 if len(line) not in [4, 5, 9]:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
60 return False
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
61 try:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
62 [int(i) for i in line[1:]]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
63 self.format = SCIDX_EXT
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
64 return True
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
65 except ValueError:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
66 try:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
67 if len(line) < 6:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
68 return False
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
69 [int(line[4]), int(line[5])]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
70 self.format = GFF_EXT
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
71 return True
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
72 except ValueError:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
73 return False
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
74
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
75 def next_valid(self):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
76 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
77 Advance to the next valid line in the reader
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
78 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
79 self.line = self.reader.next()
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
80 s = 0
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
81 while not self.is_valid(self.line):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
82 self.line = self.reader.next()
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
83 s += 1
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
84 if s > 0:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
85 # Skip initial line(s) of file
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
86 pass
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
87
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
88 def parse_line(self, line):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
89 if self.format == SCIDX_EXT:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
90 return [int(line[1]), int(line[2]), int(line[3])]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
91 else:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
92 return [int(line[3]), line[6], line[5]]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
93
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
94 def chromosome_name(self):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
95 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
96 Return the name of the chromosome about to be loaded
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
97 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
98 return self.line[0]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
99
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
100 def load_chromosome(self, collect_data=True):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
101 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
102 Load the current chromosome into an array and return it
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
103 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
104 cname = self.chromosome_name()
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
105 if cname in self.processed_chromosomes:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
106 stop_err('File is not grouped by chromosome')
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
107 self.data = []
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
108 while self.line[0] == cname:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
109 if collect_data:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
110 read = self.parse_line(self.line)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
111 if read[0] < self.current_index:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
112 msg = 'Reads in chromosome %s are not sorted by index. (At index %d)' % (cname, self.current_index)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
113 stop_err(msg)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
114 self.current_index = read[0]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
115 self.add_read(read)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
116 try:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
117 self.next()
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
118 except StopIteration:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
119 self.done = True
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
120 break
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
121 self.processed_chromosomes.append(cname)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
122 self.current_index = 0
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
123 data = self.data
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
124 # Don't retain reference anymore to save memory
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
125 del self.data
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
126 return data
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
127
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
128 def add_read(self, read):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
129 if self.format == SCIDX_EXT:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
130 self.data.append(read)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
131 else:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
132 index, strand, value = read
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
133 if value == '' or value == '.':
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
134 value = 1
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
135 else:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
136 value = int(value)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
137 if not self.data:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
138 self.data.append([index, 0, 0])
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
139 current_read = self.data[-1]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
140 if self.data[-1][0] == index:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
141 current_read = self.data[-1]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
142 elif self.data[-1][0] < index:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
143 self.data.append([index, 0, 0])
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
144 current_read = self.data[-1]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
145 else:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
146 msg = 'Reads in chromosome %s are not sorted by index. (At index %d)' % (self.chromosome_name(), index)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
147 stop_err(msg)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
148 if strand == '+':
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
149 current_read[1] += value
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
150 elif strand == '-':
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
151 current_read[2] += value
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
152 else:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
153 msg = 'Strand "%s" at chromosome "%s" index %d is not valid.' % (strand, self.chromosome_name(), index)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
154 stop_err(msg)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
155
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
156 def skip_chromosome(self):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
157 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
158 Skip the current chromosome, discarding data
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
159 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
160 self.load_chromosome(collect_data=False)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
161
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
162
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
163 class Peak(object):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
164 def __init__(self, index, pos_width, neg_width):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
165 self.index = index
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
166 self.start = index - neg_width
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
167 self.end = index + pos_width
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
168 self.value = 0
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
169 self.deleted = False
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
170 self.safe = False
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
171
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
172 def __repr__(self):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
173 return '[%d] %d' % (self.index, self.value)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
174
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
175
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
176 def gff_row(cname, start, end, score, source, type='.', strand='.', phase='.', attrs={}):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
177 return (cname, source, type, start, end, score, strand, phase, gff_attrs(attrs))
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
178
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
179
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
180 def gff_attrs(d):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
181 if not d:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
182 return '.'
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
183 return ';'.join('%s=%s' % item for item in d.items())
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
184
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
185
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
186 def stop_err(msg):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
187 sys.stderr.write(msg)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
188 sys.exit(1)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
189
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
190
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
191 def is_int(i):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
192 try:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
193 int(i)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
194 return True
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
195 except ValueError:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
196 return False
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
197
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
198
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
199 def make_keys(data):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
200 return [read[0] for read in data]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
201
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
202
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
203 def make_peak_keys(peaks):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
204 return [peak.index for peak in peaks]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
205
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
206
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
207 def get_window(data, start, end, keys):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
208 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
209 Returns all reads from the data set with index between the two indexes
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
210 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
211 start_index = bisect.bisect_left(keys, start)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
212 end_index = bisect.bisect_right(keys, end)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
213 return data[start_index:end_index]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
214
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
215
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
216 def get_index(value, keys):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
217 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
218 Returns the index of the value in the keys using bisect
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
219 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
220 return bisect.bisect_left(keys, value)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
221
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
222
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
223 def get_range(data):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
224 lo = min([item[0] for item in data])
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
225 hi = max([item[0] for item in data])
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
226 return lo, hi
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
227
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
228
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
229 def get_chunks(lo, hi, size, overlap=500):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
230 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
231 Divides a range into chunks of maximum size size. Returns a list of
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
232 2-tuples (slice_range, process_range), each a 2-tuple (start, end).
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
233 process_range has zero overlap and should be given to process_chromosome
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
234 as-is, and slice_range is overlapped and should be used to slice the
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
235 data (using get_window) to be given to process_chromosome.
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
236 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
237 chunks = []
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
238 for start_index in range(lo, hi, size):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
239 process_start = start_index
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
240 # Don't go over upper bound
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
241 process_end = min(start_index + size, hi)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
242 # Don't go under lower bound
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
243 slice_start = max(process_start - overlap, lo)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
244 # Don't go over upper bound
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
245 slice_end = min(process_end + overlap, hi)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
246 chunks.append(((slice_start, slice_end), (process_start, process_end)))
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
247 return chunks
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
248
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
249
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
250 def allocate_array(data, width):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
251 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
252 Allocates a new array with the dimensions required to fit all reads in
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
253 the argument. The new array is totally empty. Returns the array and the
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
254 shift (number to add to a read index to get the position in the array it
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
255 should be at).
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
256 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
257 lo, hi = get_range(data)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
258 rng = hi - lo
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
259 shift = width - lo
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
260 return numpy.zeros(rng+width*2, numpy.float), shift
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
261
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
262
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
263 def normal_array(width, sigma, normalize=True):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
264 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
265 Returns an array of the normal distribution of the specified width
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
266 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
267 sigma2 = float(sigma)**2
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
268
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
269 def normal_func(x):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
270 return math.exp(-x * x / (2 * sigma2))
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
271
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
272 # width is the half of the distribution
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
273 values = map(normal_func, range(-width, width))
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
274 values = numpy.array(values, numpy.float)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
275 # normalization
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
276 if normalize:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
277 values = 1.0/math.sqrt(2 * numpy.pi * sigma2) * values
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
278 return values
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
279
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
280
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
281 def call_peaks(array, shift, data, keys, direction, down_width, up_width, exclusion):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
282 peaks = []
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
283
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
284 def find_peaks():
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
285 # Go through the array and call each peak
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
286 results = (array > numpy.roll(array, 1)) & (array > numpy.roll(array, -1))
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
287 indexes = numpy.where(results)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
288 for index in indexes[0]:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
289 pos = down_width or exclusion // 2
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
290 neg = up_width or exclusion // 2
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
291 # Reverse strand
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
292 if direction == 2:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
293 # Swap positive and negative widths
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
294 pos, neg = neg, pos
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
295 peaks.append(Peak(int(index)-shift, pos, neg))
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
296 find_peaks()
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
297
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
298 def calculate_reads():
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
299 # Calculate the number of reads in each peak
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
300 for peak in peaks:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
301 reads = get_window(data, peak.start, peak.end, keys)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
302 peak.value = sum([read[direction] for read in reads])
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
303 # Flat list of indexes with frequency
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
304 indexes = [r for read in reads for r in [read[0]] * read[direction]]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
305 peak.stddev = numpy.std(indexes)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
306 calculate_reads()
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
307
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
308 def perform_exclusion():
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
309 # Process the exclusion zone
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
310 peak_keys = make_peak_keys(peaks)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
311 peaks_by_value = peaks[:]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
312 peaks_by_value.sort(key=lambda peak: -peak.value)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
313 for peak in peaks_by_value:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
314 peak.safe = True
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
315 window = get_window(peaks,
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
316 peak.index-exclusion//2,
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
317 peak.index+exclusion//2,
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
318 peak_keys)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
319 for excluded in window:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
320 if excluded.safe:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
321 continue
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
322 i = get_index(excluded.index, peak_keys)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
323 del peak_keys[i]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
324 del peaks[i]
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
325 perform_exclusion()
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
326 return peaks
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
327
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
328
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
329 def process_chromosome(cname, data, writer, process_bounds, width, sigma, down_width, up_width, exclusion, filter):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
330 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
331 Process a chromosome. Takes the chromosome name, list of reads, a CSV
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
332 writer to write processes results to, the bounds (2-tuple) to write
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
333 results in, and options.
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
334 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
335 if not data:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
336 return
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
337 keys = make_keys(data)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
338 # Create the arrays that hold the sum of the normals
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
339 forward_array, forward_shift = allocate_array(data, width)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
340 reverse_array, reverse_shift = allocate_array(data, width)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
341 normal = normal_array(width, sigma)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
342
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
343 def populate_array():
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
344 # Add each read's normal to the array
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
345 for read in data:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
346 index, forward, reverse = read
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
347 # Add the normals to the appropriate regions
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
348 if forward:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
349 forward_array[index+forward_shift-width:index+forward_shift+width] += normal * forward
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
350 if reverse:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
351 reverse_array[index+reverse_shift-width:index+reverse_shift+width] += normal * reverse
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
352 populate_array()
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
353 forward_peaks = call_peaks(forward_array, forward_shift, data, keys, 1, down_width, up_width, exclusion)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
354 reverse_peaks = call_peaks(reverse_array, reverse_shift, data, keys, 2, down_width, up_width, exclusion)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
355 # Convert chromosome name in preparation for writing output
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
356 cname = convert_data(cname, 'zeropad', 'numeric')
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
357
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
358 def write(cname, strand, peak):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
359 start = max(peak.start, 1)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
360 end = peak.end
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
361 value = peak.value
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
362 stddev = peak.stddev
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
363 if value > filter:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
364 # This version of genetrack outputs only gff files.
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
365 writer.writerow(gff_row(cname=cname,
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
366 source='genetrack',
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
367 start=start,
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
368 end=end,
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
369 score=value,
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
370 strand=strand,
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
371 attrs={'stddev': stddev}))
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
372
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
373 for peak in forward_peaks:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
374 if process_bounds[0] < peak.index < process_bounds[1]:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
375 write(cname, '+', peak)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
376 for peak in reverse_peaks:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
377 if process_bounds[0] < peak.index < process_bounds[1]:
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
378 write(cname, '-', peak)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
379
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
380
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
381 def sort_chromosome_reads_by_index(input_path):
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
382 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
383 Return a gff file with chromosome reads sorted by index.
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
384 """
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
385 # Will this sort produce different results across platforms?
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
386 output_path = tempfile.NamedTemporaryFile(delete=False).name
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
387 command = 'sort -k 1,1 -k 4,4n "%s" > "%s"' % (input_path, output_path)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
388 p = subprocess.Popen(command, shell=True)
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
389 p.wait()
25cd59a002d9 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/genetrack commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
390 return output_path