Debian Codegraph @

uk/ac/babraham/FastQC/Graphs/BaseGroup.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2010-17 Simon Andrews
3 *
4 * This file is part of FastQC.
5 *
6 * FastQC is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FastQC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FastQC; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 package uk.ac.babraham.FastQC.Graphs;
21
22 import java.util.Vector;
23
24 import uk.ac.babraham.FastQC.FastQCConfig;
25
26 /**
27 * The base group class is a simple way to create a set of bins into
28 * which positions within a read can be put such that early positions
29 * get a group to themselves and later positions get averaged so that
30 * general trends can still be observed.
31 *
32 */
33 public class BaseGroup {
34
35 private int lowerCount;
36 private int upperCount;
37
38 public static BaseGroup [] makeBaseGroups (int maxLength) {
39
40
41 // They might have set a fixed max length. If the observed
42 // length is longer than this then tough - they'll have to deal
43 // with it, but if not then we'll use the global value instead
44 // of theirs
45
46 if (FastQCConfig.getInstance().minLength > maxLength) {
47 maxLength = FastQCConfig.getInstance().minLength;
48 }
49
50 if (FastQCConfig.getInstance().nogroup) {
51 return(makeUngroupedGroups(maxLength));
52 }
53
54
55 if (FastQCConfig.getInstance().nogroup) {
56 return(makeUngroupedGroups(maxLength));
57 }
58 else if (FastQCConfig.getInstance().expgroup) {
59 return(makeExponentialBaseGroups(maxLength));
60 }
61 else {
62 return(makeLinearBaseGroups(maxLength));
63 }
64 }
65
66 public static BaseGroup [] makeUngroupedGroups (int maxLength) {
67
68 int startingBase = 1;
69 int interval = 1;
70
71 Vector<BaseGroup> groups = new Vector<BaseGroup>();
72
73 while (startingBase <= maxLength) {
74
75 int endBase = startingBase+(interval-1);
76 if (endBase > maxLength) endBase = maxLength;
77
78 BaseGroup bg = new BaseGroup(startingBase, endBase);
79 groups.add(bg);
80
81 startingBase += interval;
82 }
83
84 return groups.toArray(new BaseGroup[0]);
85
86 }
87
88 public static BaseGroup [] makeExponentialBaseGroups (int maxLength) {
89
90 int startingBase = 1;
91 int interval = 1;
92
93 Vector<BaseGroup> groups = new Vector<BaseGroup>();
94
95 while (startingBase <= maxLength) {
96
97 int endBase = startingBase+(interval-1);
98 if (endBase > maxLength) endBase = maxLength;
99
100 BaseGroup bg = new BaseGroup(startingBase, endBase);
101 groups.add(bg);
102
103 startingBase += interval;
104
105 // See if we need to increase the interval
106 if (startingBase == 10 && maxLength > 75) {
107 interval = 5;
108 }
109 if (startingBase == 50 && maxLength > 200) {
110 interval = 10;
111 }
112 if (startingBase == 100 && maxLength > 300) {
113 interval = 50;
114 }
115 if (startingBase == 500 && maxLength > 1000) {
116 interval = 100;
117 }
118 if (startingBase == 1000 && maxLength > 2000) {
119 interval = 500;
120 }
121
122
123 }
124
125 return groups.toArray(new BaseGroup[0]);
126
127 }
128
129 private static int getLinearInterval (int length) {
130 // The the first 9bp as individual residues since odd stuff
131 // can happen there, then we find a grouping value which gives
132 // us a total set of groups below 75. We limit the intervals
133 // we try to sensible whole numbers.
134
135 int [] baseValues = new int [] {2,5,10};
136 int multiplier = 1;
137
138 while (true) {
139 for (int b=0;b<baseValues.length;b++) {
140 int interval = baseValues[b] * multiplier;
141 int groupCount = 9 + ((length-9)/interval);
142 if ((length-9) % interval != 0) {
143 groupCount += 1;
144 }
145
146 if (groupCount < 75) return interval;
147 }
148
149 multiplier *= 10;
150
151 if (multiplier == 10000000) {
152 throw new IllegalStateException("Couldn't find a sensible interval grouping for length '"+length+"'");
153 }
154 }
155
156 }
157
158 public static BaseGroup [] makeLinearBaseGroups (int maxLength) {
159
160 // For lengths below 75bp we just return everything.
161 if (maxLength <= 75) return makeUngroupedGroups(maxLength);
162
163 // We need to work out what interval we're going to use.
164
165 int interval = getLinearInterval(maxLength);
166
167
168 int startingBase = 1;
169
170 Vector<BaseGroup> groups = new Vector<BaseGroup>();
171
172 while (startingBase <= maxLength) {
173
174 int endBase = startingBase+(interval-1);
175
176 if (startingBase < 10) endBase = startingBase;
177
178 if (startingBase == 10 && interval > 10) {
179 endBase = interval-1;
180 }
181
182 if (endBase > maxLength) endBase = maxLength;
183
184 BaseGroup bg = new BaseGroup(startingBase, endBase);
185 groups.add(bg);
186
187 if (startingBase < 10) {
188 startingBase +=1;
189 }
190 else if (startingBase == 10 && interval > 10) {
191 startingBase = interval;
192 }
193 else {
194 startingBase += interval;
195 }
196
197 }
198
199 return groups.toArray(new BaseGroup[0]);
200
201 }
202
203
204 /**
205 *
206 * @param lowerCount
207 * @param upperCount
208 */
209
210 private BaseGroup (int lowerCount, int upperCount) {
211 this.lowerCount = lowerCount;
212 this.upperCount = upperCount;
213 }
214
215 public int lowerCount () {
216 return lowerCount;
217 }
218
219 public int upperCount () {
220 return upperCount;
221 }
222
223 public boolean containsValue (int value) {
224 return value>=lowerCount && value<=upperCount;
225 }
226
227 public String toString () {
228 if (lowerCount == upperCount) {
229 return ""+lowerCount;
230 }
231 else {
232 return ""+lowerCount+"-"+upperCount;
233 }
234 }
235
236 }