Bio::D...TS::VCF(3pm)
π Bio::D...TS::VCF(3pm) π
okay bestie, so this is a page from the User Contributed Perl Documentation, and it's all about the Perl module Bio::DB::HTS::VCF. grab a snack, we're reading VCF/BCF files together π§¬β¨
π LICENSE π
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.
yes bestie, that's the whole legal vibe. no cap, don't skip it π
π AUTHORS π
- Rishi Nag
<rishi@ebi.ac.uk>, original author. - Alessandro Vullo
"<avullo at cpan.org>", the current developer and maintainer.
β¨ NAME β¨
Bio::DB::HTS::VCF -- Read VCF/BCF data files
π¦ DESCRIPTION π¦
This module is literally a Perl interface to the HTSlib library, and its whole job is reading variant calls stored in VCF and BCF file databases.
The functions it gives you are for opening a VCF/BCF file, reading header values, querying specific chromosome intervals, and then reading row values.
There's also a sweep set of methods that lets you run through rows one by one, either backwards or forwards through the file. iconic.
πΈ SYNOPSIS πΈ
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";
}
π METHODS π
π new π
Opens a VCF/BCF file for reading. If the file is indexed (that's tabix for VCF, csi for BCF, iykyk) then the index gets opened too, and it's used for querying arbitrary locations on the chromosomes.
$vcf = Bio::DB::HTS::VCF->new($filepath)
Returns an instance of Bio::DB::HTS::VCF.
π header π
Returns an instance of Bio::DB::HTS::VCF::Header, which represents the header of the file.
$header = $vcf->header()
π num_variants π
Returns the number of variants (that's rows, bestie) in the file.
$nv = $vcf->num_variants();
π close π
Closes the VCF/BCF file. Allocated memory gets released, and yes, that includes the index if there is one.
$vcf->close()
π next π
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 the end of file is reached.
π Querying an indexed VCF/BCF file π
If the file is indexed, you can query it 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 you've got an iterator, individual rows belonging to the result set can be accessed sequentially by iteratively invoking the iterator's next method until it returns nothing.
π query π
$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 it raises an exception in case the underlying HTSlib library cannot parse the region. π
π§ HEADER METHODS π§
Once the file has been opened, various global values can be read from the header. so many vibes.
π version π
Returns the VCF file version, as a string.
$h->version()
π num_samples π
Returns the number of samples.
$h->num_samples()
π get_sample_names π
Returns the list of sample names.
$sample_names = $h->get_sample_names()
Returns an array ref of strings representing the sample names.
π get_seqnames π
Returns the number of sequence names.
$h->num_seqnames()
π get_seqnames π
Returns the list of sequence names.
$h->get_seqnames()
Returns an array ref of strings representing the sequence names.
π fmt_text π
Get header formatted text, as a string.
$h->fmt_text()
Returns the text string representing the content of the header.
π ROW METHODS π
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 that 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()
π¦ Accessing alleles information π¦
$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
π¦ Row filters π¦
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.
π¦ Accessing info fields π¦
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.
π¦ Accessing format fields π¦
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, ... ].
π¦ Accessing genotypes π¦
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.
π VCF SWEEP OBJECTS π
Open the file and process using sweeps. Heads up bestie: 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, because 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. fr fr, take the warning seriously π
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
and that's the whole page, bestie π«Ά this one came from perl v5.40.0, dated 2024-10-31, and it's the manual page Bio::D...TS::VCF(3pm). slay, go read some variants πβ¨
π¦ where this page comes from
- debian trixie: libbio-db-hts-perl 3.01-5 Β· Apache-2.0
- ubuntu questing: libbio-db-hts-perl 3.01-5
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms