Copyright [2015-2018] EMBL-European Bioinformatics Institute
Licensed under the Apache License, Version 2.0 (the "License"); you
may not use this file except in compliance with the License. You may obtain a
copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
Rishi Nag <
[email protected]>, original author.
Alessandro Vullo "<avullo at cpan.org>", the current developer
and maintainer.
Bio::DB::HTS::VCF -- Read VCF/BCF data files
This module provides a Perl interface to the HTSlib library for reading variant
calls stored in VCF and BCF file databases.
The functions provided are for opening a VCF/BCF file, reading header values,
querying specific chromosome intervals and then reading row values.
A sweep set of methods allows running through rows one by one, either backwards
or forwards through the file.
use Bio::DB::HTS::VCF ;
### File Open ###
my $v = Bio::DB::HTS::VCF->new( filename => "/path/to/myfile.vcf.gz" );
# once the file has been opened, various global values can be read from the header
my $h = $v->header();
$h->get_seqnames() ;
$h->version() ; # read the VCF file version
$h->num_samples() ;
$h->get_sample_names() ; #return an array of sample names
$h->num_seqnames() ;
$h->get_seqnames() ; # return an array of sequence names
### Individual rows can be read in and fields accessed ###
my $row = $v->next() ;
# row functions
$row->chromosome($h) ;
$row->position() ;
$row->id() ;
$row->num_filters() ;
$row->quality() ;
# retrieve alleles
my $num_alleles = $row->num_alleles();
my $alleles = $row->get_alleles();
my $allele_index = 1;
for my $a (@$alleles) {
printf( "(%s, %s)\n", $a, $row->get_variant_type($allele_index++) ) ;
}
# query filters
$row->has_filter($h,"DP50");
$row->has_filter($h,"."); # PASS filter check
$row->get_info_type($h, "AF"); # one of "String", "Integer", "Float" or "Flag".
$info_result = $row->get_info($h, "NS"); # [3]
$row->get_format_type($h, "GT") ; # "String"
$row->get_format($h, "DP") ; # [ 1, 8, 5 ]
### free memory associated with the row
Bio::DB::HTS::VCF::Row->destroy($row);
### query specific locations
my $iter = $v->query("20:1000000-1231000");
while (my $result = $iter->next) {
print $result->chromosome($h), $result->position(), $result->id(), $result->num_filters(), $result->quality(), "\n";
}
Opens a VCF/BCF file for reading. If the file is indexed (i.e. tabix for VCF,
csi for BCF) the index is opened and used for querying arbitrary locations on
the chromosomes.
- $vcf = Bio::DB::HTS::VCF->new($filepath)
- Returns an instance of Bio::DB::HTS::VCF.
Returns instance of Bio::DB::HTS::VCF::Header, representing the header of the
file.
- $header = $vcf->header()
Returns the number of variants (i.e. rows) of the file.
- $nv = $vcf->num_variants();
Close the VCF/BCF file, allocated memory will be released, included the index,
if present.
- $vcf->close()
Returns the next row (starting from the first one) read from the file.
- $row = $vcf->next()
- Returns an instance of Bio::DB::HTS::VCF::Row or undef if
end of file is reached.
If the file is indexed, the file can be queried for variants on a specified
region. Regions can be specified using either the "chr",
"chr:start" or "chr:start-end" format, with start <=
end.
Once an iterator is obtained, individual rows belonging to the result set can be
sequentially accessed by iteratively invoking the iterator next method until
it returns nothing.
- $iterator = $vcf->query($region);
- Returns an instance of Bio::DB::HTS::VCF::Iterator or undef
if the chromosome is not found in the index or raises an exception in case
the underlying HTSlib library cannot parse the region.
Once the file has been opened, various global values can be read from the
header.
Returns the VCF file version, as a string
- $h->version()
Returns the number of samples
- $h->num_samples()
Returns the list of sample names
- $sample_names = $h->get_sample_names()
- Returns an array ref of strings representing the sample
names
Returns the number of sequence names
- $h->num_seqnames()
Returns the list of sequence names
- $h->get_seqnames()
- Returns an array ref of strings representing the sequence
names
Get header formatted text, as a string
- $h->fmt_text()
- Returns the text string representing the content of the
header
Individual rows can be read in and fields accessed. To read a row use the next
function, which returns a Bio::DB::HTS::VCF::Row instance.
Various fields can then be read from the row object. Some of the functions to
read these fields will need the header object supplied.
- $row->print($header)
- Returns a formatted textual representation of the row.
- $row->chromosome($header)
- $row->position()
- $row->id()
- $row->quality()
- $row->reference()
- $row->num_alleles()
- Returns the number of alleles
- $row->get_alleles()
- Returns the alleles as strings in an array ref
The variant type of an allele can be determined using the index of the allele.
The index starts from 1 for the first allele:
- $row->is_snp()
- Returns a true value if the row refers to a SNP.
- $row->get_variant_type($allele_index)
- This will return one of the values as defined in htslib. As
of v1.3.1 these are as follows.
- VCF_REF 0
- VCF_SNP 1
- VCF_MNP 2
- VCF_INDEL 4
- VCF_OTHER 8
Each row object has filters that may or may not have been applied to it.
- $row->num_filters()
- Returns the number of filters of the row.
- $row->has_filter($header, $filter)
- Returns 0 if the filter is not present, 1 if it is present.
The PASS filter is represented by a dot.
Each row may have additional info fields associated with each allele in the row.
- $row->get_info_type($header, $info_id)
- Returns the type of the info ID as specified in the VCF
file header, one of "String", "Integer",
"Float" or "Flag".
- $row->get_info($header, $info_id) or
$row->get_info($header)
- If an info_id string is passed, returns an array ref of
values for that particular info field, one for each allele in the row. If
the row does not have an item of that info, or it does not exist in the
file, a string "ID_NOT_FOUND" will be returned.
Alternatively, the get_info() method can be invoked by just passing
the header. In this case, the whole info field is returned organised as a
hash ref where keys are the info IDs and values are the info fields for
the corresponding ID.
Formats are dealt with similarly to info fields.
- $row->get_format_type($header, $format_id)
- Returns the type of the format ID as specified in the VCF
file header, one of "String", "Integer",
"Float" or "Flag".
- $row->get_format($header, $format_id) or
$row->get_format($header)
- If a format_id string is passed, returns an array ref of
values for that particular format ID. If the row does not have an item of
that format, or it does not exist in the file, a string
"ID_NOT_FOUND" will be returned.
Alternatively, the get_format() method can be invoked by just passing
the header. In this case, it returns the complete format specification as
a hash ref of FORMAT_ID => [ FORMAT_ID_VALUE, ... ].
Genotype records are currently returned as a series of integers, across all the
samples for the row.
- $row->get_genotypes($header)
- Returns an array reference of integers representing
genotype records.
Open the file and process using sweeps. Note that the two methods maintain
pointers that are independant of one another. Using the
next_row() will
start at the first row in the file and go on to the next row in subsequent
reads. This is independant of
previous_row() calls. Similarly
previous_row() will start at the last row and read backwards. However a
call to
next_row() is needed beforehand as the read fails otherwise.
At the time of writing there are issues which seem to be due to the underlying
HTSlib API calls, so using the
next() function is preferable to using
sweeps.
use Bio::DB::HTS::VCF ;
my $sweep = Bio::DB::HTS::VCFSweep->new(filename => "data/test.vcf.gz");
$sweep->header;
my $row_forwards = $sweep->next_row(); #returns first row in file
my $row_backwards = $sweep->previous_row(); #returns last row in file
my $row_forwards = $sweep->next_row(); # returns second row in file
my $row_backwards = $sweep->previous_row(); #returns penultimate row in file