annotate cwpair2_util.py @ 0:8600bfe7ed52 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
author iuc
date Tue, 22 Dec 2015 17:03:46 -0500
parents
children abc464ca7260
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
6 import matplotlib
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
7 matplotlib.use('Agg')
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
8 from matplotlib import pyplot
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
9
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
10 # Data outputs
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
11 DETAILS = 'D'
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
12 MATCHED_PAIRS = 'MP'
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
13 ORPHANS = 'O'
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
14 # Data output formats
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
15 GFF_EXT = 'gff'
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
16 TABULAR_EXT = 'tabular'
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
17 # 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
18 HISTOGRAM = 'H'
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
19 # Statistics outputs
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
20 FINAL_PLOTS = 'F'
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
21 PREVIEW_PLOTS = 'P'
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
22 STATS_GRAPH = 'C'
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
23
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
24 # Graph settings.
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
25 COLORS = 'krg'
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
26 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
27 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
28 TICK_WIDTH = 3
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
29 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
30 PLOT_FORMAT = 'pdf'
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
31 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
32 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
33 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
34 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
35 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
36
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 class FrequencyDistribution(object):
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
39
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
40 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
41 self.start = start
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
42 self.end = end
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
43 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
44 self.binsize = binsize
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
45
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
46 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
47 """
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
48 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
49 """
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
50 return self.start + (x-self.start) // self.binsize * self.binsize + self.binsize/2.0
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
51
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
52 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
53 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
54 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
55
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
56 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
57 x = []
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
58 y = []
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
59 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
60 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
61 x.append(center)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
62 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
63 return x, y
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
64
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
65 def mode(self):
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
66 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
67
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
68 def size(self):
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
69 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
70
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 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
73 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
74 sys.exit(1)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
75
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 def distance(peak1, peak2):
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
78 return (peak2[1]+peak2[2])/2 - (peak1[1]+peak1[2])/2
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
79
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 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
82 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
83
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 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
86 if not d:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
87 return '.'
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
88 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
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 parse_chromosomes(reader):
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
92 # 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
93 chromosomes = {}
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
94 reader.next()
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
95 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
96 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
97 start = int(start)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
98 end = int(end)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
99 value = float(value)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
100 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
101 chromosomes[cname] = []
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
102 peaks = 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.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
104 return chromosomes
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
105
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 def perc95(chromosomes):
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
108 """
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
109 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
110 """
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
111 values = []
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
112 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
113 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
114 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
115 values.sort()
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
116 # Get 95% value
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
117 return values[int(len(values)*0.95)]
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
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
120 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
121 """
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
122 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
123 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
124 """
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
125 if threshold < 1:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
126 p95 = perc95(chromosomes)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
127 threshold = p95 * threshold
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
128 # 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
129 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
130 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
131
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 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
134 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
135 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
136 return watson, crick
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
137
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 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
140 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
141 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
142 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
143 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
144 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
145 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
146 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
147 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
148 return dist
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
149
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 def make_keys(crick):
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
152 return [(data[1] + data[2])//2 for data in crick]
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
153
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 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
156 """
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
157 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
158 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
159 """
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
160 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
161 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
162 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
163 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
164 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
165 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
166 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
167 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
168
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 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
171 if not window:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
172 return None
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
173 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
174
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 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
177 if not window:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
178 return None
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
179
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
180 def key(cpeak):
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
181 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
182 # 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
183 if d < 0:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
184 # 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
185 d = 10000 - d
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
186 return d
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
187 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
188
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 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
191 if not window:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
192 return None
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
193 return min(window, key=lambda cpeak: abs(distance(peak, cpeak)-mode))
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
194
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
195 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
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 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
199 pyplot.clf()
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
200 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
201 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
202 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
203 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
204 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
205 pyplot.legend(labels)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
206 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
207 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
208 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
209 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
210 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
211 # 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
212 ax = pyplot.gca()
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
213 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
214 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
215 pyplot.savefig(fname)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
216
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 def create_directories():
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
219 # 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
220 os.mkdir(HISTOGRAM)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
221 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
222 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
223 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
224
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 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
227 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
228 if method == 'all':
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
229 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
230 else:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
231 match_methods = [method]
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
232 statistics = []
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
233 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
234 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
235 galaxy_hid,
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
236 match_method,
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
237 threshold,
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
238 up_distance,
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
239 down_distance,
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
240 binsize,
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
241 output_files)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
242 statistics.append(stats)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
243 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
244 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
245 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
246 labels=METHODS.keys())
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
247 return statistics
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
248
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 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
251 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
252 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
253 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
254 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
255 # 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
256 statistics = {}
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
257 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
258 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
259 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
260 statistics['dir'] = fpath
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
261 if threshold >= 1:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
262 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
263 else:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
264 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
265 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
266
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
267 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
268 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
269
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
270 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
271 # 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
272 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
273
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
274 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
275 # 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
276 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
277 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
278
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
279 try:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
280 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
281 except Exception:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
282 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
283 if output_details:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
284 # Details
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
285 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
286 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
287 if output_plots:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
288 # Final Plot
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
289 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
290 if output_orphans:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
291 # Orphans
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
292 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
293 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
294 if output_plots:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
295 # Preview Plot
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
296 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
297 # Matched Pairs.
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
298 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
299 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
300 if output_plots:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
301 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
302 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
303 if threshold > 0:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
304 # Apply filter
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
305 filter(chromosomes, threshold)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
306 if method == 'mode':
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
307 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
308 mode = freq.mode()
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
309 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
310 if output_plots:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
311 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
312 else:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
313 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
314 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
315 orphans = 0
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
316 # 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
317 x = []
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
318 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
319 # 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
320 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
321 # 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
322 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
323 # 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
324 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
325 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
326 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
327 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
328 if method == 'mode':
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
329 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
330 else:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
331 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
332 if match:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
333 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
334 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
335 dist.add(d)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
336 # 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
337 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
338 source='cwpair',
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
339 start=midpoint,
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
340 end=midpoint + 1,
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
341 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
342 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
343 if output_details:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
344 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
345 peak[1],
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
346 peak[2],
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
347 peak[3],
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
348 '+',
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
349 cname,
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
350 match[1],
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
351 match[2],
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
352 match[3], '-',
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
353 midpoint,
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
354 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
355 d))
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
356 i = bisect.bisect_left(keys, (match[1]+match[2])/2)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
357 del crick[i]
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
358 del keys[i]
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
359 else:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
360 if output_orphans:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
361 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
362 # 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
363 orphans += 1
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
364 # 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
365 if output_orphans:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
366 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
367 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
368 # 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
369 orphans += len(crick)
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
370 # 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
371 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
372 # 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
373 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
374 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
375 # 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
376 # 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
377 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
378 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
379 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
380 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
381 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
382 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
383 else:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
384 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
385 # Print row_tmp.
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
386 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
387 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
388 statistics['orphans'] = orphans
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
389 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
390 if output_plots:
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
391 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
392 statistics['dist'] = dist
8600bfe7ed52 planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/cwpair2 commit e96df94dba60050fa28aaf55b5bb095717a5f260
iuc
parents:
diff changeset
393 return statistics