Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.ext.htsjdk

File VCFReader.java

 

Coverage histogram

../../../img/srcFileCovDistChart8.png
19% of files have more coverage

Code metrics

26
41
12
1
214
129
27
0.66
3.42
12
2.25

Classes

Class Line # Actions
VCFReader 16 41 27 20
0.746835574.7%
 

Contributing tests

This file is covered by 6 tests. .

Source view

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    * A thin wrapper for htsjdk classes to read either plain, or compressed, or
14    * compressed and indexed VCF files
15    */
 
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    * Constructor given a raw or compressed VCF file or a (tabix) index file
28    * <p>
29    * For now, file type is inferred from its suffix: .gz or .bgz for compressed
30    * data, .tbi for an index file, anything else is assumed to be plain text
31    * VCF.
32    *
33    * @param f
34    * @throws IOException
35    */
 
36  6 toggle 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   
 
54  5 toggle @Override
55    public void close() throws IOException
56    {
57  5 if (reader != null)
58    {
59  5 reader.close();
60    }
61    }
62   
63    /**
64    * Returns an iterator over VCF variants in the file. The client should call
65    * close() on the iterator when finished with it.
66    */
 
67  1 toggle @Override
68    public CloseableIterator<VariantContext> iterator()
69    {
70  1 return reader == null ? null : reader.iterator();
71    }
72   
73    /**
74    * Queries for records overlapping the region specified. Note that this method
75    * is performant if the VCF file is indexed, and may be very slow if it is
76    * not.
77    * <p>
78    * Client code should call close() on the iterator when finished with it.
79    *
80    * @param chrom
81    * the chromosome to query
82    * @param start
83    * query interval start
84    * @param end
85    * query interval end
86    * @return
87    */
 
88  40 toggle 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    * Returns an iterator over variant records read from a flat file which
106    * overlap the specified chromosomal positions. Call close() on the iterator
107    * when finished with it!
108    *
109    * @param chrom
110    * @param start
111    * @param end
112    * @return
113    */
 
114  40 toggle 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    // prime look-ahead buffer with next matching record
124    private VariantContext next = findNext();
125   
 
126  69 toggle 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    // todo what is the undeprecated way to get
147    // the chromosome for the variant?
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   
 
157  68 toggle @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   
 
168  29 toggle @Override
169    public VariantContext next()
170    {
171    /*
172    * return the next match, and then re-prime
173    * it with the following one (if any)
174    */
175  29 VariantContext temp = next;
176  29 next = findNext();
177  29 return temp;
178    }
179   
 
180  0 toggle @Override
181    public void remove()
182    {
183    // not implemented
184    }
185   
 
186  86 toggle @Override
187    public void close()
188    {
189  86 it.close();
190    }
191    };
192    }
193   
194    /**
195    * Returns an object that models the VCF file headers
196    *
197    * @return
198    */
 
199  4 toggle public VCFHeader getFileHeader()
200    {
201  4 return reader == null ? null : reader.getFileHeader();
202    }
203   
204    /**
205    * Answers true if we are processing a tab-indexed VCF file, false if it is a
206    * plain text (uncompressed) file.
207    *
208    * @return
209    */
 
210  0 toggle public boolean isIndex()
211    {
212  0 return indexed;
213    }
214    }