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