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 |
|
|
|
|
| 0% |
Uncovered Elements: 86 (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 |
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 6 |
Complexity Density: 0.5 |
|
60 |
0 |
public VCFReader(String filePath) throws IOException... |
61 |
|
{ |
62 |
0 |
indexed = false; |
63 |
0 |
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 |
0 |
else if (new File(filePath + TBI_EXTENSION).exists()) |
70 |
|
{ |
71 |
0 |
indexed = true; |
72 |
|
} |
73 |
0 |
else if (new File(filePath + CSI_EXTENSION).exists()) |
74 |
|
{ |
75 |
0 |
indexed = true; |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
0 |
File file = new File(filePath); |
83 |
0 |
if (file.exists()) |
84 |
|
{ |
85 |
0 |
reader = new VCFFileReader(file, indexed); |
86 |
|
} |
87 |
|
else |
88 |
|
{ |
89 |
0 |
Console.error("File not found: " + filePath); |
90 |
|
} |
91 |
|
} |
92 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
93 |
0 |
@Override... |
94 |
|
public void close() throws IOException |
95 |
|
{ |
96 |
0 |
if (reader != null) |
97 |
|
{ |
98 |
0 |
reader.close(); |
99 |
|
} |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
106 |
0 |
@Override... |
107 |
|
public CloseableIterator<VariantContext> iterator() |
108 |
|
{ |
109 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
127 |
0 |
public CloseableIterator<VariantContext> query(final String chrom,... |
128 |
|
final int start, final int end) |
129 |
|
{ |
130 |
0 |
if (reader == null) |
131 |
|
{ |
132 |
0 |
return null; |
133 |
|
} |
134 |
0 |
if (indexed) |
135 |
|
{ |
136 |
0 |
return reader.query(chrom, start, end); |
137 |
|
} |
138 |
|
else |
139 |
|
{ |
140 |
0 |
return queryUnindexed(chrom, start, end); |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
@param |
150 |
|
@param |
151 |
|
@param |
152 |
|
@return |
153 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
154 |
0 |
protected CloseableIterator<VariantContext> queryUnindexed(... |
155 |
|
final String chrom, final int start, final int end) |
156 |
|
{ |
157 |
0 |
final CloseableIterator<VariantContext> it = reader.iterator(); |
158 |
|
|
159 |
0 |
return new CloseableIterator<VariantContext>() |
160 |
|
{ |
161 |
|
boolean atEnd = false; |
162 |
|
|
163 |
|
|
164 |
|
private VariantContext next = findNext(); |
165 |
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 7 |
Complexity Density: 0.5 |
|
166 |
0 |
private VariantContext findNext()... |
167 |
|
{ |
168 |
0 |
if (atEnd) |
169 |
|
{ |
170 |
0 |
return null; |
171 |
|
} |
172 |
0 |
VariantContext variant = null; |
173 |
0 |
while (it.hasNext()) |
174 |
|
{ |
175 |
0 |
variant = it.next(); |
176 |
0 |
int vstart = variant.getStart(); |
177 |
|
|
178 |
0 |
if (vstart > end) |
179 |
|
{ |
180 |
0 |
atEnd = true; |
181 |
0 |
close(); |
182 |
0 |
return null; |
183 |
|
} |
184 |
|
|
185 |
0 |
int vend = variant.getEnd(); |
186 |
|
|
187 |
|
|
188 |
0 |
if (chrom.equals(variant.getContig()) && (vstart <= end) |
189 |
|
&& (vend >= start)) |
190 |
|
{ |
191 |
0 |
return variant; |
192 |
|
} |
193 |
|
} |
194 |
0 |
return null; |
195 |
|
} |
196 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
197 |
0 |
@Override... |
198 |
|
public boolean hasNext() |
199 |
|
{ |
200 |
0 |
boolean hasNext = !atEnd && (next != null); |
201 |
0 |
if (!hasNext) |
202 |
|
{ |
203 |
0 |
close(); |
204 |
|
} |
205 |
0 |
return hasNext; |
206 |
|
} |
207 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
208 |
0 |
@Override... |
209 |
|
public VariantContext next() |
210 |
|
{ |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
0 |
VariantContext temp = next; |
216 |
0 |
next = findNext(); |
217 |
0 |
return temp; |
218 |
|
} |
219 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
220 |
0 |
@Override... |
221 |
|
public void remove() |
222 |
|
{ |
223 |
|
|
224 |
|
} |
225 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
226 |
0 |
@Override... |
227 |
|
public void close() |
228 |
|
{ |
229 |
0 |
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 |
|
} |