1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.ext.htsjdk; |
22 |
|
|
23 |
|
import java.io.Closeable; |
24 |
|
import java.io.File; |
25 |
|
import java.io.IOException; |
26 |
|
|
27 |
|
import htsjdk.samtools.util.CloseableIterator; |
28 |
|
import htsjdk.variant.variantcontext.VariantContext; |
29 |
|
import htsjdk.variant.vcf.VCFFileReader; |
30 |
|
import htsjdk.variant.vcf.VCFHeader; |
31 |
|
import jalview.bin.Console; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
|
|
| 66.3% |
Uncovered Elements: 29 (86) |
Complexity: 29 |
Complexity Density: 0.63 |
|
37 |
|
public class VCFReader implements Closeable, Iterable<VariantContext> |
38 |
|
{ |
39 |
|
private static final String GZ = "gz"; |
40 |
|
|
41 |
|
private static final String TBI_EXTENSION = ".tbi"; |
42 |
|
|
43 |
|
private static final String CSI_EXTENSION = ".csi"; |
44 |
|
|
45 |
|
private boolean indexed; |
46 |
|
|
47 |
|
private VCFFileReader reader; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@param |
58 |
|
@throws |
59 |
|
|
|
|
| 55% |
Uncovered Elements: 9 (20) |
Complexity: 6 |
Complexity Density: 0.5 |
|
60 |
2 |
public VCFReader(String filePath) throws IOException... |
61 |
|
{ |
62 |
2 |
indexed = false; |
63 |
2 |
if (filePath.endsWith(TBI_EXTENSION) |
64 |
|
|| filePath.endsWith(CSI_EXTENSION)) |
65 |
|
{ |
66 |
0 |
indexed = true; |
67 |
0 |
filePath = filePath.substring(0, filePath.length() - 4); |
68 |
|
} |
69 |
2 |
else if (new File(filePath + TBI_EXTENSION).exists()) |
70 |
|
{ |
71 |
0 |
indexed = true; |
72 |
|
} |
73 |
2 |
else if (new File(filePath + CSI_EXTENSION).exists()) |
74 |
|
{ |
75 |
0 |
indexed = true; |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
2 |
File file = new File(filePath); |
83 |
2 |
if (file.exists()) |
84 |
|
{ |
85 |
2 |
reader = new VCFFileReader(file, indexed); |
86 |
|
} |
87 |
|
else |
88 |
|
{ |
89 |
0 |
Console.error("File not found: " + filePath); |
90 |
|
} |
91 |
|
} |
92 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
93 |
2 |
@Override... |
94 |
|
public void close() throws IOException |
95 |
|
{ |
96 |
2 |
if (reader != null) |
97 |
|
{ |
98 |
2 |
reader.close(); |
99 |
|
} |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
106 |
1 |
@Override... |
107 |
|
public CloseableIterator<VariantContext> iterator() |
108 |
|
{ |
109 |
1 |
return reader == null ? null : reader.iterator(); |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
@param |
120 |
|
|
121 |
|
@param |
122 |
|
|
123 |
|
@param |
124 |
|
|
125 |
|
@return |
126 |
|
|
|
|
| 55.6% |
Uncovered Elements: 4 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
127 |
1 |
public CloseableIterator<VariantContext> query(final String chrom,... |
128 |
|
final int start, final int end) |
129 |
|
{ |
130 |
1 |
if (reader == null) |
131 |
|
{ |
132 |
0 |
return null; |
133 |
|
} |
134 |
1 |
if (indexed) |
135 |
|
{ |
136 |
0 |
return reader.query(chrom, start, end); |
137 |
|
} |
138 |
|
else |
139 |
|
{ |
140 |
1 |
return queryUnindexed(chrom, start, end); |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
@param |
150 |
|
@param |
151 |
|
@param |
152 |
|
@return |
153 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
154 |
1 |
protected CloseableIterator<VariantContext> queryUnindexed(... |
155 |
|
final String chrom, final int start, final int end) |
156 |
|
{ |
157 |
1 |
final CloseableIterator<VariantContext> it = reader.iterator(); |
158 |
|
|
159 |
1 |
return new CloseableIterator<VariantContext>() |
160 |
|
{ |
161 |
|
boolean atEnd = false; |
162 |
|
|
163 |
|
|
164 |
|
private VariantContext next = findNext(); |
165 |
|
|
|
|
| 72.7% |
Uncovered Elements: 6 (22) |
Complexity: 7 |
Complexity Density: 0.5 |
|
166 |
2 |
private VariantContext findNext()... |
167 |
|
{ |
168 |
2 |
if (atEnd) |
169 |
|
{ |
170 |
0 |
return null; |
171 |
|
} |
172 |
2 |
VariantContext variant = null; |
173 |
4 |
while (it.hasNext()) |
174 |
|
{ |
175 |
3 |
variant = it.next(); |
176 |
3 |
int vstart = variant.getStart(); |
177 |
|
|
178 |
3 |
if (vstart > end) |
179 |
|
{ |
180 |
0 |
atEnd = true; |
181 |
0 |
close(); |
182 |
0 |
return null; |
183 |
|
} |
184 |
|
|
185 |
3 |
int vend = variant.getEnd(); |
186 |
|
|
187 |
|
|
188 |
3 |
if (chrom.equals(variant.getContig()) && (vstart <= end) |
189 |
|
&& (vend >= start)) |
190 |
|
{ |
191 |
1 |
return variant; |
192 |
|
} |
193 |
|
} |
194 |
1 |
return null; |
195 |
|
} |
196 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
197 |
1 |
@Override... |
198 |
|
public boolean hasNext() |
199 |
|
{ |
200 |
1 |
boolean hasNext = !atEnd && (next != null); |
201 |
1 |
if (!hasNext) |
202 |
|
{ |
203 |
1 |
close(); |
204 |
|
} |
205 |
1 |
return hasNext; |
206 |
|
} |
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
208 |
1 |
@Override... |
209 |
|
public VariantContext next() |
210 |
|
{ |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
1 |
VariantContext temp = next; |
216 |
1 |
next = findNext(); |
217 |
1 |
return temp; |
218 |
|
} |
219 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
220 |
0 |
@Override... |
221 |
|
public void remove() |
222 |
|
{ |
223 |
|
|
224 |
|
} |
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
226 |
2 |
@Override... |
227 |
|
public void close() |
228 |
|
{ |
229 |
2 |
it.close(); |
230 |
|
} |
231 |
|
}; |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
@return |
238 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
239 |
0 |
public VCFHeader getFileHeader()... |
240 |
|
{ |
241 |
0 |
return reader == null ? null : reader.getFileHeader(); |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
@return |
249 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
250 |
0 |
public boolean isIndex()... |
251 |
|
{ |
252 |
0 |
return indexed; |
253 |
|
} |
254 |
|
} |