comparison calculate_contrast_threshold.py @ 0:7371bb087d86 draft default tip

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/calculate_contrast_threshold commit 6ba8e678f8cedabaf9b4759cddb81b8b3cd9ec31"
author iuc
date Wed, 11 Sep 2019 09:28:55 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:7371bb087d86
1 #!/usr/bin/python
2
3 import getopt
4 import math
5 import sys
6
7 import numpy as np
8
9 """
10 Program to calculate the contrast thresholds for heatmap from tagPileUp CDT
11 """
12
13
14 def rebin(a, new_shape):
15 M, N = a.shape
16 m, n = new_shape
17 if m >= M:
18 # repeat rows in data matrix
19 a = np.repeat(a, math.ceil(float(m) / M), axis=0)
20
21 M, N = a.shape
22 m, n = new_shape
23
24 row_delete_num = M % m
25 col_delete_num = N % n
26
27 np.random.seed(seed=0)
28
29 if row_delete_num > 0:
30 # select deleted rows with equal intervals
31 row_delete = np.linspace(0, M - 1, num=row_delete_num, dtype=int)
32 # sort the random selected deleted row ids
33 row_delete = np.sort(row_delete)
34 row_delete_plus1 = row_delete[1:-1] + \
35 1 # get deleted rows plus position
36 # get deleted rows plus position (top +1; end -1)
37 row_delete_plus1 = np.append(
38 np.append(row_delete[0] + 1, row_delete_plus1), row_delete[-1] - 1)
39 # put the info of deleted rows into the next rows by mean
40 a[row_delete_plus1, :] = (
41 a[row_delete, :] + a[row_delete_plus1, :]) / 2
42 a = np.delete(a, row_delete, axis=0) # random remove rows
43
44 if col_delete_num > 0:
45 # select deleted cols with equal intervals
46 col_delete = np.linspace(0, N - 1, num=col_delete_num, dtype=int)
47 # sort the random selected deleted col ids
48 col_delete = np.sort(col_delete)
49 col_delete_plus1 = col_delete[1:-1] + \
50 1 # get deleted cols plus position
51 # get deleted cols plus position (top +1; end -1)
52 col_delete_plus1 = np.append(
53 np.append(col_delete[0] + 1, col_delete_plus1), col_delete[-1] - 1)
54 # put the info of deleted cols into the next cols by mean
55 a[:, col_delete_plus1] = (
56 a[:, col_delete] + a[:, col_delete_plus1]) / 2
57 a = np.delete(a, col_delete, axis=1) # random remove columns
58
59 M, N = a.shape
60
61 # compare the heatmap matrix
62 a_compress = a.reshape((m, int(M / m), n, int(N / n))).mean(3).mean(1)
63 return np.array(a_compress)
64
65
66 def load_Data(input_file, quantile, absolute, header, start_col, row_num, col_num, min_upper_lim):
67 data0 = []
68 with open(input_file, 'r') as data:
69 if header == 'T':
70 data.readline()
71
72 for rec in data:
73 tmp = [(x.strip()) for x in rec.split('\t')]
74 data0.append(tmp[start_col:])
75 data0 = np.array(data0, dtype=float)
76
77 if row_num == -999:
78 row_num = data0.shape[0]
79 if col_num == -999:
80 col_num = data0.shape[1]
81
82 # rebin data0
83 if row_num < data0.shape[0] and col_num < data0.shape[1]:
84 data0 = rebin(data0, (row_num, col_num))
85 elif row_num < data0.shape[0]:
86 data0 = rebin(data0, (row_num, data0.shape[1]))
87 elif col_num < data0.shape[1]:
88 data0 = rebin(data0, (data0.shape[0], col_num))
89
90 # Calculate contrast limits here
91 rows, cols = np.nonzero(data0)
92 upper_lim = np.percentile(data0[rows, cols], quantile)
93 lower_lim = 0
94 if absolute != -999:
95 upper_lim = absolute
96
97 # Setting an absolute threshold to a minimum,
98 # in cases the 95th percentile contrast is <= user defined min_upper_lim
99 if quantile > 0.0:
100 print("\nQUANTILE: {}".format(quantile))
101 print("Quantile calculated UPPER LIM: {}".format(upper_lim))
102 print("LOWER LIM: {}".format(lower_lim))
103 if upper_lim <= min_upper_lim:
104 print("setting heatmap upper_threshold to min_upper_lim\n")
105 upper_lim = min_upper_lim
106
107 outfile = open('calcThreshold.txt', 'w')
108 outfile.write("upper_threshold:{}\nlower_threshold:{}\nrow_num:{}\ncol_num:{}\nheader:{}\nstart_col:{}".format(
109 upper_lim, lower_lim, row_num, col_num, header, start_col))
110 print('heatmap_upper_threshold:' + str(upper_lim))
111 print('heatmap_lower_threshold:' + str(lower_lim))
112 outfile.flush()
113 outfile.close()
114
115
116 ############################################################################
117 # python cdt_to_heatmap.py -i test.tabular.split_line -o test.tabular.split_line.png -q 0.9 -c black -d T -s 2 -r 500 -l 300 -b test.colorsplit
118 ############################################################################
119 usage = """
120 Usage:
121 This script calculates the contrast thresholds from Tag pile up heatmap data. Outputs a text file that contains the parameters for the heatmap script.
122
123 python calculateThreshold.py -i <input file> -q <quantile> -m <min upper thresold after quantile calculation> -t <absolute tag threshold> -d <header T/F> -s <start column> -r <row num after compress> -l <col num after compress>'
124
125 Example:
126 python calculateThreshold.py -i test.tabular.split_line -q 90 -m 5 -d T -s 2 -r 600 -l 300
127 """
128
129 if __name__ == '__main__':
130
131 # check for command line arguments
132 if len(sys.argv) < 2 or not sys.argv[1].startswith("-"):
133 sys.exit(usage)
134 # get arguments
135 try:
136 optlist, alist = getopt.getopt(sys.argv[1:], 'hi:o:q:t:c:d:s:r:l:m:')
137 except getopt.GetoptError:
138 sys.exit(usage)
139
140 # default quantile contrast saturation = 0.9
141 quantile = 90.0
142 min_upper_lim = 5.0
143 # absolute contrast saturation overrides quantile
144 absolute = -999
145
146 # default figure width/height is defined by matrix size
147 # if user-defined size is smaller than matrix, activate rebin function
148 row_num = -999
149 col_num = -999
150
151 for opt in optlist:
152 if opt[0] == "-h":
153 sys.exit(usage)
154 elif opt[0] == "-i":
155 input_file = opt[1]
156 elif opt[0] == "-q":
157 quantile = float(opt[1])
158 elif opt[0] == '-t':
159 absolute = float(opt[1])
160 elif opt[0] == "-d":
161 header = opt[1]
162 elif opt[0] == "-s":
163 start_col = int(opt[1])
164 elif opt[0] == "-r":
165 row_num = int(opt[1])
166 elif opt[0] == "-l":
167 col_num = int(opt[1])
168 elif opt[0] == "-m":
169 min_upper_lim = float(opt[1])
170
171 print("Header present:", header)
172 print("Start column:", start_col)
173 print("Row number (pixels):", row_num)
174 print("Col number (pixels):", col_num)
175 print("Min Upper Limit while using Quantile:", min_upper_lim)
176 if absolute != -999:
177 print("Absolute tag contrast threshold:", absolute)
178 else:
179 print("Percentile tag contrast threshold:", quantile)
180
181 if absolute == -999 and quantile <= 0:
182 print("\nInvalid threshold!!!")
183 sys.exit(usage)
184
185 load_Data(input_file, quantile, absolute,
186 header, start_col, row_num, col_num, min_upper_lim)