-
Notifications
You must be signed in to change notification settings - Fork 0
/
Smoothing.py
82 lines (72 loc) · 3.07 KB
/
Smoothing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from bs.operations import base
from bbcflib.bFlatMajor import stream as gm_stream
from bbcflib import btrack as track
from bbcflib import genrep
size_def = 11
step_def = 1
import tw2.forms as twf
import tw2.core as twc
class SmoothingForm(base.BaseForm):
track = twf.FileField(label_text='Signal: ',
help_text='Select signal file (e.g. bedgraph)',
validator=twf.FileValidator(required=True))
assembly = twf.SingleSelectField(label_text='Assembly: ',
options=genrep.GenRep().assemblies_available(),
help_text='Reference genome')
window_size = twf.TextField(label_text='Window size: ',
validator=twc.IntValidator(required=True),
value=size_def,
help_text='Size of window')
window_step = twf.TextField(label_text='Window step: ',
validator=twc.IntValidator(required=True),
value=step_def,
help_text='Size of steps between windows')
by_feature = twf.CheckBox(label_text='Window size in features (not basepairs): ',
value=False,
help_text='Will count size and step parameters in number of features, not in basepairs')
submit = twf.SubmitButton(id="submit", value="Smooth")
meta = {'version': "1.0.0",
'author': "BBCF",
'contact': "[email protected]"}
in_parameters = [{'id': 'track', 'type': 'track', 'required': True},
{'id': 'assembly', 'type': 'assembly'},
{'id': 'window_size', 'type': 'int', 'required': True},
{'id': 'window_step', 'type': 'int', 'required': True},
{'id': 'by_feature', 'type': 'boolean'}
]
out_parameters = [{'id': 'smoothed_track', 'type': 'track'}]
class SmoothingPlugin(base.OperationPlugin):
info = {
'title': 'Window smoothing',
'description': 'Window smoothing',
'path': ['Signal', 'Smoothing'],
'output': SmoothingForm,
'in': in_parameters,
'out': out_parameters,
'meta': meta,
}
def __call__(self, **kw):
tinput = track.track(kw.get('track'), chrmeta=kw.get('assembly') or None)
wsize = int(kw.get('window_size', size_def))
wstep = int(kw.get('window_step', step_def))
featurewise = kw.get('by_feature', False)
if isinstance(featurewise, basestring):
featurewise = (featurewise.lower() in ['1', 'true', 't'])
output = self.temporary_path(fname='smoothed_track', ext='sql')
if featurewise:
outfields = tinput.fields
datatype = "qualitative"
else:
outfields = ["start", "end", "score"]
datatype = "quantitative"
tout = track.track(output, fields=outfields,
chrmeta=tinput.chrmeta,
info={'datatype': datatype})
for chrom in tout.chrmeta.keys():
tout.write(gm_stream.window_smoothing(
tinput.read(selection=chrom, fields=outfields),
window_size=wsize, step_size=wstep,
featurewise=featurewise), chrom=chrom)
tout.close()
self.new_file(output, 'smoothed_track')
return 1