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 |
|
|
|
|
| 75.6% |
Uncovered Elements: 21 (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 |
6 |
public VCFReader(String filePath) throws IOException... |
61 |
|
{ |
62 |
6 |
indexed = false; |
63 |
6 |
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 |
6 |
else if (new File(filePath + TBI_EXTENSION).exists()) |
70 |
|
{ |
71 |
0 |
indexed = true; |
72 |
|
} |
73 |
6 |
else if (new File(filePath + CSI_EXTENSION).exists()) |
74 |
|
{ |
75 |
0 |
indexed = true; |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
6 |
File file = new File(filePath); |
83 |
6 |
if (file.exists()) |
84 |
|
{ |
85 |
6 |
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 |
5 |
@Override... |
94 |
|
public void close() throws IOException |
95 |
|
{ |
96 |
5 |
if (reader != null) |
97 |
|
{ |
98 |
5 |
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 |
40 |
public CloseableIterator<VariantContext> query(final String chrom,... |
128 |
|
final int start, final int end) |
129 |
|
{ |
130 |
40 |
if (reader == null) |
131 |
|
{ |
132 |
0 |
return null; |
133 |
|
} |
134 |
40 |
if (indexed) |
135 |
|
{ |
136 |
0 |
return reader.query(chrom, start, end); |
137 |
|
} |
138 |
|
else |
139 |
|
{ |
140 |
40 |
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 |
40 |
protected CloseableIterator<VariantContext> queryUnindexed(... |
155 |
|
final String chrom, final int start, final int end) |
156 |
|
{ |
157 |
40 |
final CloseableIterator<VariantContext> it = reader.iterator(); |
158 |
|
|
159 |
40 |
return new CloseableIterator<VariantContext>() |
160 |
|
{ |
161 |
|
boolean atEnd = false; |
162 |
|
|
163 |
|
|
164 |
|
private VariantContext next = findNext(); |
165 |
|
|
|
|
| 90.9% |
Uncovered Elements: 2 (22) |
Complexity: 7 |
Complexity Density: 0.5 |
|
166 |
77 |
private VariantContext findNext()... |
167 |
|
{ |
168 |
77 |
if (atEnd) |
169 |
|
{ |
170 |
0 |
return null; |
171 |
|
} |
172 |
77 |
VariantContext variant = null; |
173 |
179 |
while (it.hasNext()) |
174 |
|
{ |
175 |
145 |
variant = it.next(); |
176 |
145 |
int vstart = variant.getStart(); |
177 |
|
|
178 |
145 |
if (vstart > end) |
179 |
|
{ |
180 |
6 |
atEnd = true; |
181 |
6 |
close(); |
182 |
6 |
return null; |
183 |
|
} |
184 |
|
|
185 |
139 |
int vend = variant.getEnd(); |
186 |
|
|
187 |
|
|
188 |
139 |
if (chrom.equals(variant.getContig()) && (vstart <= end) |
189 |
|
&& (vend >= start)) |
190 |
|
{ |
191 |
37 |
return variant; |
192 |
|
} |
193 |
|
} |
194 |
34 |
return null; |
195 |
|
} |
196 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
197 |
76 |
@Override... |
198 |
|
public boolean hasNext() |
199 |
|
{ |
200 |
76 |
boolean hasNext = !atEnd && (next != null); |
201 |
76 |
if (!hasNext) |
202 |
|
{ |
203 |
40 |
close(); |
204 |
|
} |
205 |
76 |
return hasNext; |
206 |
|
} |
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
208 |
37 |
@Override... |
209 |
|
public VariantContext next() |
210 |
|
{ |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
37 |
VariantContext temp = next; |
216 |
37 |
next = findNext(); |
217 |
37 |
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 |
86 |
@Override... |
227 |
|
public void close() |
228 |
|
{ |
229 |
86 |
it.close(); |
230 |
|
} |
231 |
|
}; |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
@return |
238 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
239 |
4 |
public VCFHeader getFileHeader()... |
240 |
|
{ |
241 |
4 |
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 |
|
} |