Mercurial > repos > iuc > cwpair2
annotate cwpair2_util.py @ 2:abc464ca7260 draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit 0e7c1b37cf73425c6637b4e196fdeb290e042bc1
author | iuc |
---|---|
date | Tue, 26 Jul 2016 06:06:17 -0400 |
parents | 8600bfe7ed52 |
children | 436dc65bd902 |
rev | line source |
---|---|
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
1 import bisect |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
2 import csv |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
3 import os |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
4 import sys |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
5 import traceback |
2
abc464ca7260
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit 0e7c1b37cf73425c6637b4e196fdeb290e042bc1
iuc
parents:
0
diff
changeset
|
6 |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
7 import matplotlib |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
8 matplotlib.use('Agg') |
2
abc464ca7260
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit 0e7c1b37cf73425c6637b4e196fdeb290e042bc1
iuc
parents:
0
diff
changeset
|
9 from matplotlib import pyplot # noqa: E402 |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
10 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
11 # Data outputs |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
12 DETAILS = 'D' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
13 MATCHED_PAIRS = 'MP' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
14 ORPHANS = 'O' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
15 # Data output formats |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
16 GFF_EXT = 'gff' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
17 TABULAR_EXT = 'tabular' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
18 # Statistics historgrams output directory. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
19 HISTOGRAM = 'H' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
20 # Statistics outputs |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
21 FINAL_PLOTS = 'F' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
22 PREVIEW_PLOTS = 'P' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
23 STATS_GRAPH = 'C' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
24 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
25 # Graph settings. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
26 COLORS = 'krg' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
27 Y_LABEL = 'Peak-pair counts' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
28 X_LABEL = 'Peak-pair distance (bp)' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
29 TICK_WIDTH = 3 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
30 ADJUST = [0.140, 0.9, 0.9, 0.1] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
31 PLOT_FORMAT = 'pdf' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
32 pyplot.rc('xtick.major', size=10.00) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
33 pyplot.rc('ytick.major', size=10.00) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
34 pyplot.rc('lines', linewidth=4.00) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
35 pyplot.rc('axes', linewidth=3.00) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
36 pyplot.rc('font', family='Bitstream Vera Sans', size=32.0) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
37 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
38 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
39 class FrequencyDistribution(object): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
40 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
41 def __init__(self, start, end, binsize=10, d=None): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
42 self.start = start |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
43 self.end = end |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
44 self.dist = d or {} |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
45 self.binsize = binsize |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
46 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
47 def get_bin(self, x): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
48 """ |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
49 Returns the bin in which a data point falls |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
50 """ |
2
abc464ca7260
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit 0e7c1b37cf73425c6637b4e196fdeb290e042bc1
iuc
parents:
0
diff
changeset
|
51 return self.start + (x - self.start) // self.binsize * self.binsize + self.binsize / 2.0 |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
52 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
53 def add(self, x): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
54 x = self.get_bin(x) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
55 self.dist[x] = self.dist.get(x, 0) + 1 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
56 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
57 def graph_series(self): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
58 x = [] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
59 y = [] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
60 for i in range(self.start, self.end, self.binsize): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
61 center = self.get_bin(i) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
62 x.append(center) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
63 y.append(self.dist.get(center, 0)) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
64 return x, y |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
65 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
66 def mode(self): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
67 return max(self.dist.items(), key=lambda data: data[1])[0] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
68 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
69 def size(self): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
70 return sum(self.dist.values()) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
71 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
72 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
73 def stop_err(msg): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
74 sys.stderr.write(msg) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
75 sys.exit(1) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
76 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
77 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
78 def distance(peak1, peak2): |
2
abc464ca7260
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit 0e7c1b37cf73425c6637b4e196fdeb290e042bc1
iuc
parents:
0
diff
changeset
|
79 return (peak2[1] + peak2[2]) / 2 - (peak1[1] + peak1[2]) / 2 |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
80 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
81 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
82 def gff_row(cname, start, end, score, source, type='.', strand='.', phase='.', attrs={}): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
83 return (cname, source, type, start, end, score, strand, phase, gff_attrs(attrs)) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
84 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
85 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
86 def gff_attrs(d): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
87 if not d: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
88 return '.' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
89 return ';'.join('%s=%s' % item for item in d.items()) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
90 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
91 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
92 def parse_chromosomes(reader): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
93 # This version of cwpair2 accepts only gff format as input. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
94 chromosomes = {} |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
95 reader.next() |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
96 for line in reader: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
97 cname, junk, junk, start, end, value, strand, junk, junk = line |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
98 start = int(start) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
99 end = int(end) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
100 value = float(value) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
101 if cname not in chromosomes: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
102 chromosomes[cname] = [] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
103 peaks = chromosomes[cname] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
104 peaks.append((strand, start, end, value)) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
105 return chromosomes |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
106 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
107 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
108 def perc95(chromosomes): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
109 """ |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
110 Returns the 95th percentile value of the given chromosomes. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
111 """ |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
112 values = [] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
113 for peaks in chromosomes.values(): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
114 for peak in peaks: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
115 values.append(peak[3]) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
116 values.sort() |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
117 # Get 95% value |
2
abc464ca7260
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit 0e7c1b37cf73425c6637b4e196fdeb290e042bc1
iuc
parents:
0
diff
changeset
|
118 return values[int(len(values) * 0.95)] |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
119 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
120 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
121 def filter(chromosomes, threshold=0.05): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
122 """ |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
123 Filters the peaks to those above a threshold. Threshold < 1.0 is interpreted |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
124 as a proportion of the maximum, >=1.0 as an absolute value. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
125 """ |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
126 if threshold < 1: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
127 p95 = perc95(chromosomes) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
128 threshold = p95 * threshold |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
129 # Make the threshold a proportion of the |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
130 for cname, peaks in chromosomes.items(): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
131 chromosomes[cname] = [peak for peak in peaks if peak[3] > threshold] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
132 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
133 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
134 def split_strands(chromosome): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
135 watson = [peak for peak in chromosome if peak[0] == '+'] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
136 crick = [peak for peak in chromosome if peak[0] == '-'] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
137 return watson, crick |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
138 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
139 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
140 def all_pair_distribution(chromosomes, up_distance, down_distance, binsize): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
141 dist = FrequencyDistribution(-up_distance, down_distance, binsize=binsize) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
142 for cname, data in chromosomes.items(): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
143 watson, crick = split_strands(data) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
144 crick.sort(key=lambda data: float(data[1])) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
145 keys = make_keys(crick) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
146 for peak in watson: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
147 for cpeak in get_window(crick, peak, up_distance, down_distance, keys): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
148 dist.add(distance(peak, cpeak)) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
149 return dist |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
150 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
151 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
152 def make_keys(crick): |
2
abc464ca7260
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit 0e7c1b37cf73425c6637b4e196fdeb290e042bc1
iuc
parents:
0
diff
changeset
|
153 return [(data[1] + data[2]) // 2 for data in crick] |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
154 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
155 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
156 def get_window(crick, peak, up_distance, down_distance, keys=None): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
157 """ |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
158 Returns a window of all crick peaks within a distance of a watson peak. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
159 crick strand MUST be sorted by distance |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
160 """ |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
161 strand, start, end, value = peak |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
162 midpoint = (start + end) // 2 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
163 lower = midpoint - up_distance |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
164 upper = midpoint + down_distance |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
165 keys = keys or make_keys(crick) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
166 start_index = bisect.bisect_left(keys, lower) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
167 end_index = bisect.bisect_right(keys, upper) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
168 return [cpeak for cpeak in crick[start_index:end_index]] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
169 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
170 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
171 def match_largest(window, peak): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
172 if not window: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
173 return None |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
174 return max(window, key=lambda cpeak: cpeak[3]) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
175 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
176 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
177 def match_closest(window, peak): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
178 if not window: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
179 return None |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
180 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
181 def key(cpeak): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
182 d = distance(peak, cpeak) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
183 # Search negative distances last |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
184 if d < 0: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
185 # And then prefer less negative distances |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
186 d = 10000 - d |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
187 return d |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
188 return min(window, key=key) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
189 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
190 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
191 def match_mode(window, peak, mode): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
192 if not window: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
193 return None |
2
abc464ca7260
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit 0e7c1b37cf73425c6637b4e196fdeb290e042bc1
iuc
parents:
0
diff
changeset
|
194 return min(window, key=lambda cpeak: abs(distance(peak, cpeak) - mode)) |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
195 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
196 METHODS = {'mode': match_mode, 'closest': match_closest, 'largest': match_largest} |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
197 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
198 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
199 def frequency_plot(freqs, fname, labels=[], title=''): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
200 pyplot.clf() |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
201 pyplot.figure(figsize=(10, 10)) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
202 for i, freq in enumerate(freqs): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
203 x, y = freq.graph_series() |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
204 pyplot.plot(x, y, '%s-' % COLORS[i]) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
205 if len(freqs) > 1: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
206 pyplot.legend(labels) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
207 pyplot.xlim(freq.start, freq.end) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
208 pyplot.ylim(ymin=0) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
209 pyplot.ylabel(Y_LABEL) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
210 pyplot.xlabel(X_LABEL) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
211 pyplot.subplots_adjust(left=ADJUST[0], right=ADJUST[1], top=ADJUST[2], bottom=ADJUST[3]) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
212 # Get the current axes |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
213 ax = pyplot.gca() |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
214 for l in ax.get_xticklines() + ax.get_yticklines(): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
215 l.set_markeredgewidth(TICK_WIDTH) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
216 pyplot.savefig(fname) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
217 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
218 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
219 def create_directories(): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
220 # Output histograms in pdf. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
221 os.mkdir(HISTOGRAM) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
222 os.mkdir('data_%s' % DETAILS) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
223 os.mkdir('data_%s' % ORPHANS) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
224 os.mkdir('data_%s' % MATCHED_PAIRS) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
225 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
226 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
227 def process_file(dataset_path, galaxy_hid, method, threshold, up_distance, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
228 down_distance, binsize, output_files): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
229 if method == 'all': |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
230 match_methods = METHODS.keys() |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
231 else: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
232 match_methods = [method] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
233 statistics = [] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
234 for match_method in match_methods: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
235 stats = perform_process(dataset_path, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
236 galaxy_hid, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
237 match_method, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
238 threshold, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
239 up_distance, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
240 down_distance, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
241 binsize, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
242 output_files) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
243 statistics.append(stats) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
244 if output_files == 'all' and method == 'all': |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
245 frequency_plot([s['dist'] for s in statistics], |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
246 statistics[0]['graph_path'], |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
247 labels=METHODS.keys()) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
248 return statistics |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
249 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
250 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
251 def perform_process(dataset_path, galaxy_hid, method, threshold, up_distance, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
252 down_distance, binsize, output_files): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
253 output_details = output_files in ["all", "matched_pair_orphan_detail"] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
254 output_plots = output_files in ["all"] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
255 output_orphans = output_files in ["all", "matched_pair_orphan", "matched_pair_orphan_detail"] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
256 # Keep track of statistics for the output file |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
257 statistics = {} |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
258 input = csv.reader(open(dataset_path, 'rt'), delimiter='\t') |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
259 fpath, fname = os.path.split(dataset_path) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
260 statistics['fname'] = '%s: data %s' % (method, str(galaxy_hid)) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
261 statistics['dir'] = fpath |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
262 if threshold >= 1: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
263 filter_string = 'fa%d' % threshold |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
264 else: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
265 filter_string = 'f%d' % (threshold * 100) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
266 fname = '%s_%su%dd%d_on_data_%s' % (method, filter_string, up_distance, down_distance, galaxy_hid) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
267 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
268 def make_histogram_path(output_type, fname): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
269 return os.path.join(HISTOGRAM, 'histogram_%s_%s.%s' % (output_type, fname, PLOT_FORMAT)) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
270 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
271 def make_path(output_type, extension, fname): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
272 # Returns the full path for an output. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
273 return os.path.join(output_type, '%s_%s.%s' % (output_type, fname, extension)) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
274 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
275 def td_writer(output_type, extension, fname): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
276 # Returns a tab-delimited writer for a specified output. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
277 output_file_path = make_path(output_type, extension, fname) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
278 return csv.writer(open(output_file_path, 'wt'), delimiter='\t') |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
279 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
280 try: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
281 chromosomes = parse_chromosomes(input) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
282 except Exception: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
283 stop_err('Unable to parse file "%s".\n%s' % (dataset_path, traceback.format_exc())) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
284 if output_details: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
285 # Details |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
286 detailed_output = td_writer('data_%s' % DETAILS, TABULAR_EXT, fname) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
287 detailed_output.writerow(('chrom', 'start', 'end', 'value', 'strand') * 2 + ('midpoint', 'c-w reads sum', 'c-w distance (bp)')) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
288 if output_plots: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
289 # Final Plot |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
290 final_plot_path = make_histogram_path(FINAL_PLOTS, fname) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
291 if output_orphans: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
292 # Orphans |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
293 orphan_output = td_writer('data_%s' % ORPHANS, TABULAR_EXT, fname) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
294 orphan_output.writerow(('chrom', 'strand', 'start', 'end', 'value')) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
295 if output_plots: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
296 # Preview Plot |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
297 preview_plot_path = make_histogram_path(PREVIEW_PLOTS, fname) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
298 # Matched Pairs. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
299 matched_pairs_output = td_writer('data_%s' % MATCHED_PAIRS, GFF_EXT, fname) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
300 statistics['stats_path'] = 'statistics.%s' % TABULAR_EXT |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
301 if output_plots: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
302 statistics['graph_path'] = make_histogram_path(STATS_GRAPH, fname) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
303 statistics['perc95'] = perc95(chromosomes) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
304 if threshold > 0: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
305 # Apply filter |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
306 filter(chromosomes, threshold) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
307 if method == 'mode': |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
308 freq = all_pair_distribution(chromosomes, up_distance, down_distance, binsize) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
309 mode = freq.mode() |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
310 statistics['preview_mode'] = mode |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
311 if output_plots: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
312 frequency_plot([freq], preview_plot_path, title='Preview frequency plot') |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
313 else: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
314 statistics['preview_mode'] = 'NA' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
315 dist = FrequencyDistribution(-up_distance, down_distance, binsize=binsize) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
316 orphans = 0 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
317 # x will be used to archive the summary dataset |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
318 x = [] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
319 for cname, chromosome in chromosomes.items(): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
320 # Each peak is (strand, start, end, value) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
321 watson, crick = split_strands(chromosome) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
322 # Sort by value of each peak |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
323 watson.sort(key=lambda data: -float(data[3])) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
324 # Sort by position to facilitate binary search |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
325 crick.sort(key=lambda data: float(data[1])) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
326 keys = make_keys(crick) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
327 for peak in watson: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
328 window = get_window(crick, peak, up_distance, down_distance, keys) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
329 if method == 'mode': |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
330 match = match_mode(window, peak, mode) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
331 else: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
332 match = METHODS[method](window, peak) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
333 if match: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
334 midpoint = (match[1] + match[2] + peak[1] + peak[2]) // 4 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
335 d = distance(peak, match) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
336 dist.add(d) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
337 # Simple output in gff format. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
338 x.append(gff_row(cname, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
339 source='cwpair', |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
340 start=midpoint, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
341 end=midpoint + 1, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
342 score=peak[3] + match[3], |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
343 attrs={'cw_distance': d})) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
344 if output_details: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
345 detailed_output.writerow((cname, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
346 peak[1], |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
347 peak[2], |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
348 peak[3], |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
349 '+', |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
350 cname, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
351 match[1], |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
352 match[2], |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
353 match[3], '-', |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
354 midpoint, |
2
abc464ca7260
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit 0e7c1b37cf73425c6637b4e196fdeb290e042bc1
iuc
parents:
0
diff
changeset
|
355 peak[3] + match[3], |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
356 d)) |
2
abc464ca7260
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit 0e7c1b37cf73425c6637b4e196fdeb290e042bc1
iuc
parents:
0
diff
changeset
|
357 i = bisect.bisect_left(keys, (match[1] + match[2]) / 2) |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
358 del crick[i] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
359 del keys[i] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
360 else: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
361 if output_orphans: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
362 orphan_output.writerow((cname, peak[0], peak[1], peak[2], peak[3])) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
363 # Keep track of orphans for statistics. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
364 orphans += 1 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
365 # Remaining crick peaks are orphans |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
366 if output_orphans: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
367 for cpeak in crick: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
368 orphan_output.writerow((cname, cpeak[0], cpeak[1], cpeak[2], cpeak[3])) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
369 # Keep track of orphans for statistics. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
370 orphans += len(crick) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
371 # Sort output descending by score. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
372 x.sort(key=lambda data: float(data[5]), reverse=True) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
373 # Writing a summary to gff format file |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
374 for row in x: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
375 row_tmp = list(row) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
376 # Dataset in tuple cannot be modified in Python, so row will |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
377 # be converted to list format to add 'chr'. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
378 if row_tmp[0] == "999": |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
379 row_tmp[0] = 'chrM' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
380 elif row_tmp[0] == "998": |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
381 row_tmp[0] = 'chrY' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
382 elif row_tmp[0] == "997": |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
383 row_tmp[0] = 'chrX' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
384 else: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
385 row_tmp[0] = row_tmp[0] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
386 # Print row_tmp. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
387 matched_pairs_output.writerow(row_tmp) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
388 statistics['paired'] = dist.size() * 2 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
389 statistics['orphans'] = orphans |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
390 statistics['final_mode'] = dist.mode() |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
391 if output_plots: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
392 frequency_plot([dist], final_plot_path, title='Frequency distribution') |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
393 statistics['dist'] = dist |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
394 return statistics |