Mercurial > repos > iuc > cwpair2
annotate cwpair2_util.py @ 6:c4b926c9831c draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
author | iuc |
---|---|
date | Thu, 02 May 2019 08:30:22 -0400 |
parents | 71188f3f4b76 |
children |
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') |
5
71188f3f4b76
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit cb490a0eee9efc2cd6c98d5bb714b75b5894c56c
iuc
parents:
3
diff
changeset
|
9 from matplotlib import pyplot # noqa: I202,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' |
6
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
18 # Statistics histograms output directory. |
0
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 """ |
6
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
49 Returns the centre of the bin in which a data point falls |
0
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): |
6
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
67 # There could be more than one mode for a frequency distribution, |
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
68 # return the median of the modes to be consistent |
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
69 max_frequency = max(self.dist.values()) |
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
70 modes = sorted(_[0] for _ in self.dist.items() if _[1] == max_frequency) |
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
71 median_index = len(modes) // 2 |
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
72 return modes[median_index] |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
73 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
74 def size(self): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
75 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
|
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 stop_err(msg): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
79 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
|
80 sys.exit(1) |
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 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
83 def distance(peak1, peak2): |
6
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
84 return (peak2[1] + peak2[2]) / 2.0 - (peak1[1] + peak1[2]) / 2.0 |
0
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 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
87 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
|
88 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
|
89 |
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 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
|
92 if not d: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
93 return '.' |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
94 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
|
95 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
96 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
97 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
|
98 # 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
|
99 chromosomes = {} |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
100 for line in reader: |
6
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
101 line = line.rstrip("\r\n") |
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
102 if not line or line.startswith('#'): |
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
103 continue |
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
104 cname, _, _, start, end, value, strand, _, _ = line.split("\t") |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
105 start = int(start) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
106 end = int(end) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
107 value = float(value) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
108 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
|
109 chromosomes[cname] = [] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
110 peaks = chromosomes[cname] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
111 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
|
112 return chromosomes |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
113 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
114 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
115 def perc95(chromosomes): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
116 """ |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
117 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
|
118 """ |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
119 values = [] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
120 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
|
121 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
|
122 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
|
123 values.sort() |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
124 # 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
|
125 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
|
126 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
127 |
6
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
128 def peak_filter(chromosomes, threshold): |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
129 """ |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
130 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
|
131 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
|
132 """ |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
133 if threshold < 1: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
134 p95 = perc95(chromosomes) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
135 threshold = p95 * threshold |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
136 # 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
|
137 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
|
138 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
|
139 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
140 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
141 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
|
142 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
|
143 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
|
144 return watson, crick |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
145 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
146 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
147 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
|
148 dist = FrequencyDistribution(-up_distance, down_distance, binsize=binsize) |
6
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
149 for data in chromosomes.values(): |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 return dist |
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 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
159 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
|
160 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
|
161 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
162 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
163 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
|
164 """ |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
165 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
|
166 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
|
167 """ |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
177 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
178 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
|
179 if not window: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
180 return None |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
181 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
|
182 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
183 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
184 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
|
185 if not window: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
186 return None |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
187 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
188 def key(cpeak): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
189 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
|
190 # 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
|
191 if d < 0: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
192 # 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
|
193 d = 10000 - d |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
194 return d |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
195 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
|
196 |
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 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
|
199 if not window: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
200 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
|
201 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
|
202 |
3
436dc65bd902
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit f924b8d7ac95d574cad6b81b6cd49a9c1f642483
iuc
parents:
2
diff
changeset
|
203 |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
204 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
|
205 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
206 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
207 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
|
208 pyplot.clf() |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 pyplot.legend(labels) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
215 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 # 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
|
221 ax = pyplot.gca() |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
222 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
|
223 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
|
224 pyplot.savefig(fname) |
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 create_directories(): |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
228 # 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
|
229 os.mkdir(HISTOGRAM) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
230 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
|
231 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
|
232 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
|
233 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
234 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
235 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
|
236 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
|
237 if method == 'all': |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
238 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
|
239 else: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
240 match_methods = [method] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
241 statistics = [] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
242 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
|
243 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
|
244 galaxy_hid, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
245 match_method, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
246 threshold, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
247 up_distance, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
248 down_distance, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
249 binsize, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
250 output_files) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
251 statistics.append(stats) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
252 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
|
253 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
|
254 statistics[0]['graph_path'], |
5
71188f3f4b76
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit cb490a0eee9efc2cd6c98d5bb714b75b5894c56c
iuc
parents:
3
diff
changeset
|
255 labels=list(METHODS.keys())) |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
256 return statistics |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
257 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
258 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
259 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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 # 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
|
265 statistics = {} |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
266 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
|
267 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
|
268 statistics['dir'] = fpath |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
269 if threshold >= 1: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
270 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
|
271 else: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
272 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
|
273 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
|
274 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
275 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
|
276 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
|
277 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
278 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
|
279 # 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
|
280 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
|
281 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
282 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
|
283 # 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
|
284 output_file_path = make_path(output_type, extension, fname) |
6
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
285 return csv.writer(open(output_file_path, 'wt'), delimiter='\t', lineterminator="\n") |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
286 |
6
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
287 with open(dataset_path, 'rt') as input: |
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
288 try: |
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
289 chromosomes = parse_chromosomes(input) |
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
290 except Exception: |
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
291 stop_err('Unable to parse file "%s".\n%s' % (dataset_path, traceback.format_exc())) |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
292 if output_details: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
293 # Details |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
294 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
|
295 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
|
296 if output_plots: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
297 # Final Plot |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
298 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
|
299 if output_orphans: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
300 # Orphans |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
301 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
|
302 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
|
303 if output_plots: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
304 # Preview Plot |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
305 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
|
306 # Matched Pairs. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
307 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
|
308 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
|
309 if output_plots: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
310 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
|
311 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
|
312 if threshold > 0: |
6
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
313 # Apply peak_filter |
c4b926c9831c
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit ca66053a0d915edba3d7f1d4ce014718e7f4f0f2
iuc
parents:
5
diff
changeset
|
314 peak_filter(chromosomes, threshold) |
0
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
315 if method == 'mode': |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
316 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
|
317 mode = freq.mode() |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
318 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
|
319 if output_plots: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
320 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
|
321 else: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
322 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
|
323 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
|
324 orphans = 0 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
325 # 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
|
326 x = [] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
327 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
|
328 # 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
|
329 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
|
330 # 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
|
331 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
|
332 # 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
|
333 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
|
334 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
|
335 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
|
336 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
|
337 if method == 'mode': |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
338 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
|
339 else: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
340 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
|
341 if match: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
342 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
|
343 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
|
344 dist.add(d) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
345 # 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
|
346 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
|
347 source='cwpair', |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
348 start=midpoint, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
349 end=midpoint + 1, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
350 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
|
351 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
|
352 if output_details: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
353 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
|
354 peak[1], |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
355 peak[2], |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
356 peak[3], |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
357 '+', |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
358 cname, |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
359 match[1], |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
360 match[2], |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
361 match[3], '-', |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
362 midpoint, |
2
abc464ca7260
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit 0e7c1b37cf73425c6637b4e196fdeb290e042bc1
iuc
parents:
0
diff
changeset
|
363 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
|
364 d)) |
2
abc464ca7260
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit 0e7c1b37cf73425c6637b4e196fdeb290e042bc1
iuc
parents:
0
diff
changeset
|
365 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
|
366 del crick[i] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
367 del keys[i] |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
368 else: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
369 if output_orphans: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
370 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
|
371 # 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
|
372 orphans += 1 |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
373 # 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
|
374 if output_orphans: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
375 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
|
376 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
|
377 # 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
|
378 orphans += len(crick) |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
379 # 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
|
380 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
|
381 # 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
|
382 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
|
383 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
|
384 # 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
|
385 # 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
|
386 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
|
387 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
|
388 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
|
389 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
|
390 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
|
391 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
|
392 else: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
393 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
|
394 # Print row_tmp. |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
395 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
|
396 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
|
397 statistics['orphans'] = orphans |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
398 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
|
399 if output_plots: |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
400 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
|
401 statistics['dist'] = dist |
8600bfe7ed52
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff
changeset
|
402 return statistics |