Struct rust_htslib::bcf::record::Record [−][src]
Expand description
A VCF/BCF record.
New records can be created by the empty_record
methods of bcf::Reader
and bcf::Writer
.
Example
use rust_htslib::bcf::{Format, Writer}; use rust_htslib::bcf::header::Header; // Create minimal VCF header with a single sample let mut header = Header::new(); header.push_sample("sample".as_bytes()); // Write uncompressed VCF to stdout with above header and get an empty record let mut vcf = Writer::from_stdout(&header, true, Format::Vcf).unwrap(); let mut record = vcf.empty_record();
Fields
inner: *mut bcf1_t
Implementations
Return associated header.
Return reference to the inner C struct.
Remarks
Note that this function is only required as long as Rust-Htslib does not provide full access to all aspects of Htslib.
Return mutable reference to inner C struct.
Remarks
Note that this function is only required as long as Rust-Htslib does not provide full access to all aspects of Htslib.
Get the reference id of the record.
To look up the contig name,
use HeaderView::rid2name
.
Returns
Some(rid)
if the internalrid
is set to a value that is not-1
None
if the internalrid
is set to-1
Update the reference id of the record.
To look up reference id for a contig name,
use HeaderView::name2rid
.
Example
Example assumes we have a Record record
from a VCF with a header containing region
named 1
. See module documentation for how to set
up VCF, header, and record.
let rid = record.header().name2rid(b"1").ok(); record.set_rid(rid); assert_eq!(record.rid(), rid); let name = record.header().rid2name(record.rid().unwrap()).ok(); assert_eq!(Some("1".as_bytes()), name);
Return the 0-based, exclusive end position
Example
let alleles: &[&[u8]] = &[b"AGG", b"TG"]; record.set_alleles(alleles).expect("Failed to set alleles"); record.set_pos(5); assert_eq!(record.end(), 8)
Return the value of the ID column.
When empty, returns b".".to_vec()
.
Add the ID string (the ID field is semicolon-separated), checking for duplicates.
Return Filters
iterator for enumerating all filters that have been set.
A record having the PASS
filter will return an empty Filter
here.
Query whether the filter with the given ID has been set.
Arguments
flt_id
- The filter ID to query for.
Set the given filters IDs to the FILTER column.
Setting an empty slice removes all filters.
Arguments
flt_ids
- The identifiers of the filter values to set.
Add the given filter to the FILTER column.
If val
corresponds to "PASS"
then all existing filters are removed first. If other than
"PASS"
, then existing "PASS"
is removed.
Arguments
flt_id
- The corresponding filter ID value to add.
Remove the given filter from the FILTER column.
Arguments
val
- The corresponding filter ID to remove.pass_on_empty
- Set to “PASS” when removing the last value.
Get alleles strings.
The first allele is the reference allele.
Set alleles. The first allele is the reference allele.
Example
assert_eq!(record.allele_count(), 0); let alleles: &[&[u8]] = &[b"A", b"TG"]; record.set_alleles(alleles).expect("Failed to set alleles"); assert_eq!(record.allele_count(), 2)
Get the value of the given info tag.
Get the number of samples.
Get the number of alleles, including reference allele.
Add/replace genotypes in FORMAT GT tag.
Arguments
genotypes
- a flattened, two-dimensional array of GenotypeAllele, the first dimension contains one array for each sample.
Errors
Returns error if GT tag is not present in header.
Example
Example assumes we have a Record record
from a VCF with a GT
FORMAT
tag.
See module documentation for how to set up
VCF, header, and record.
let alleles = &[GenotypeAllele::Unphased(1), GenotypeAllele::Unphased(1)]; record.push_genotypes(alleles); assert_eq!("1/1", &format!("{}", record.genotypes().unwrap().get(0)));
Get genotypes as vector of one Genotype
per sample.
Example
Parsing genotype field (GT
tag) from a VCF record:
use crate::rust_htslib::bcf::{Reader, Read}; let mut vcf = Reader::from_path(&"test/test_string.vcf").expect("Error opening file."); let expected = ["./1", "1|1", "0/1", "0|1", "1|.", "1/1"]; for (rec, exp_gt) in vcf.records().zip(expected.iter()) { let mut rec = rec.expect("Error reading record."); let genotypes = rec.genotypes().expect("Error reading genotypes"); assert_eq!(&format!("{}", genotypes.get(0)), exp_gt); }
Retrieve data for a FORMAT
field
Example
Note: some boilerplate for the example is hidden for clarity. See module documentation for an example of the setup used here.
header.push_sample(b"sample1").push_sample(b"sample2").push_record(br#"##FORMAT=<ID=DP,Number=1,Type=Integer,Description="Read Depth">"#); record.push_format_integer(b"DP", &[20, 12]).expect("Failed to set DP format field"); let read_depths = record.format(b"DP").integer().expect("Couldn't retrieve DP field"); let sample1_depth = read_depths[0]; assert_eq!(sample1_depth, &[20]); let sample2_depth = read_depths[1]; assert_eq!(sample2_depth, &[12])
Errors
Attention: the returned BufferBacked
from integer()
(read_depths
), which holds the data, has to be kept in scope as long as the data is
accessed. If parts of the data are accessed after the BufferBacked
object is been
dropped, you will access unallocated memory.
Get the value of the given format tag for each sample.
Add/replace a float-typed FORMAT tag.
Arguments
tag
- The tag’s string.data
- a flattened, two-dimensional array, the first dimension contains one array for each sample.
Errors
Returns error if tag is not present in header.
Example
Example assumes we have a Record record
from a VCF with an AF
FORMAT
tag.
See module documentation for how to set up
VCF, header, and record.
record.push_format_float(b"AF", &[0.5]); assert_eq!(0.5, record.format(b"AF").float().unwrap()[0][0]);
Add a string-typed FORMAT tag. Note that genotypes are treated as a special case and cannot be added with this method. See instead push_genotypes.
Arguments
tag
- The tag’s string.data
- a two-dimensional array, the first dimension contains one array for each sample. Must be non-empty.
Errors
Returns error if tag is not present in header.
Add/replace an integer-typed INFO entry.
Remove the integer-typed INFO entry.
Add/replace a float-typed INFO entry.
Remove the flag from the INFO column.
Add/replace a string-typed INFO entry.
Remove the string field from the INFO column.
Remove unused alleles.
Get the length of the reference allele. If the record has no reference allele, then the
result will be 0
.
Example
let alleles: &[&[u8]] = &[b"AGG", b"TG"]; record.set_alleles(alleles).expect("Failed to set alleles"); assert_eq!(record.rlen(), 3)
Clear all parts of the record. Useful if you plan to reuse a record object multiple times.
Example
let alleles: &[&[u8]] = &[b"AGG", b"TG"]; record.set_alleles(alleles).expect("Failed to set alleles"); record.set_pos(6); record.clear(); assert_eq!(record.rlen(), 0); assert_eq!(record.pos(), 0)