@@ -184,7 +184,8 @@ impl Record {
184
184
185
185
/// Get the reference id of the record.
186
186
///
187
- /// To look up the contig name, use `bcf::header::HeaderView::rid2name`.
187
+ /// To look up the contig name,
188
+ /// use [`HeaderView::rid2name`](../header/struct.HeaderView.html#method.rid2name).
188
189
///
189
190
/// # Returns
190
191
///
@@ -197,7 +198,32 @@ impl Record {
197
198
}
198
199
}
199
200
200
- // Update the internal reference ID number.
201
+ /// Update the reference id of the record.
202
+ ///
203
+ /// To look up reference id for a contig name,
204
+ /// use [`HeaderView::name2rid`](../header/struct.HeaderView.html#method.name2rid).
205
+ ///
206
+ /// # Example
207
+ ///
208
+ /// Example assumes we have a Record `record` from a VCF with a header containing region
209
+ /// named `1`. See [module documentation](../index.html#example-writing) for how to set
210
+ /// up VCF, header, and record.
211
+ ///
212
+ /// ```
213
+ /// # use rust_htslib::bcf::{Format, Writer};
214
+ /// # use rust_htslib::bcf::header::Header;
215
+ /// # let mut header = Header::new();
216
+ /// # let header_contig_line = r#"##contig=<ID=1,length=10>"#;
217
+ /// # header.push_record(header_contig_line.as_bytes());
218
+ /// # header.push_sample("test_sample".as_bytes());
219
+ /// # let mut vcf = Writer::from_stdout(&header, true, Format::VCF).unwrap();
220
+ /// # let mut record = vcf.empty_record();
221
+ /// let rid = record.header().name2rid(b"1").ok();
222
+ /// record.set_rid(rid);
223
+ /// assert_eq!(record.rid(), rid);
224
+ /// let name = record.header().rid2name(record.rid().unwrap()).ok();
225
+ /// assert_eq!(Some("1".as_bytes()), name);
226
+ /// ```
201
227
pub fn set_rid ( & mut self , rid : Option < u32 > ) {
202
228
match rid {
203
229
Some ( rid) => self . inner_mut ( ) . rid = rid as i32 ,
@@ -424,6 +450,29 @@ impl Record {
424
450
/// # Errors
425
451
///
426
452
/// Returns error if GT tag is not present in header.
453
+ ///
454
+ /// # Example
455
+ ///
456
+ /// Example assumes we have a Record `record` from a VCF with a `GT` `FORMAT` tag.
457
+ /// See [module documentation](../index.html#example-writing) for how to set up
458
+ /// VCF, header, and record.
459
+ ///
460
+ /// ```
461
+ /// # use rust_htslib::bcf::{Format, Writer};
462
+ /// # use rust_htslib::bcf::header::Header;
463
+ /// # use rust_htslib::bcf::record::GenotypeAllele;
464
+ /// # let mut header = Header::new();
465
+ /// # let header_contig_line = r#"##contig=<ID=1,length=10>"#;
466
+ /// # header.push_record(header_contig_line.as_bytes());
467
+ /// # let header_gt_line = r#"##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">"#;
468
+ /// # header.push_record(header_gt_line.as_bytes());
469
+ /// # header.push_sample("test_sample".as_bytes());
470
+ /// # let mut vcf = Writer::from_stdout(&header, true, Format::VCF).unwrap();
471
+ /// # let mut record = vcf.empty_record();
472
+ /// let alleles = &[GenotypeAllele::Unphased(1), GenotypeAllele::Unphased(1)];
473
+ /// record.push_genotypes(alleles);
474
+ /// assert_eq!("1/1", &format!("{}", record.genotypes().unwrap().get(0)));
475
+ /// ```
427
476
pub fn push_genotypes ( & mut self , genotypes : & [ GenotypeAllele ] ) -> Result < ( ) > {
428
477
let encoded: Vec < i32 > = genotypes. iter ( ) . map ( |gt| i32:: from ( * gt) ) . collect ( ) ;
429
478
self . push_format_integer ( b"GT" , & encoded)
@@ -494,6 +543,28 @@ impl Record {
494
543
/// # Errors
495
544
///
496
545
/// Returns error if tag is not present in header.
546
+ ///
547
+ /// # Example
548
+ ///
549
+ /// Example assumes we have a Record `record` from a VCF with an `AF` `FORMAT` tag.
550
+ /// See [module documentation](../index.html#example-writing) for how to set up
551
+ /// VCF, header, and record.
552
+ ///
553
+ /// ```
554
+ /// # use rust_htslib::bcf::{Format, Writer};
555
+ /// # use rust_htslib::bcf::header::Header;
556
+ /// # use rust_htslib::bcf::record::GenotypeAllele;
557
+ /// # let mut header = Header::new();
558
+ /// # let header_contig_line = r#"##contig=<ID=1,length=10>"#;
559
+ /// # header.push_record(header_contig_line.as_bytes());
560
+ /// # let header_af_line = r#"##FORMAT=<ID=AF,Number=1,Type=Float,Description="Frequency">"#;
561
+ /// # header.push_record(header_af_line.as_bytes());
562
+ /// # header.push_sample("test_sample".as_bytes());
563
+ /// # let mut vcf = Writer::from_stdout(&header, true, Format::VCF).unwrap();
564
+ /// # let mut record = vcf.empty_record();
565
+ /// record.push_format_float(b"AF", &[0.5]);
566
+ /// assert_eq!(0.5, record.format(b"AF").float().unwrap()[0][0]);
567
+ /// ```
497
568
pub fn push_format_float ( & mut self , tag : & [ u8 ] , data : & [ f32 ] ) -> Result < ( ) > {
498
569
self . push_format ( tag, data, htslib:: BCF_HT_REAL )
499
570
}
@@ -747,6 +818,10 @@ pub enum GenotypeAllele {
747
818
748
819
impl GenotypeAllele {
749
820
/// Decode given integer according to BCF standard.
821
+ #[ deprecated(
822
+ since = "0.36.0" ,
823
+ note = "Please use the conversion trait From<i32> for GenotypeAllele instead."
824
+ ) ]
750
825
pub fn from_encoded ( encoded : i32 ) -> Self {
751
826
match ( encoded, encoded & 1 ) {
752
827
( 0 , 0 ) => GenotypeAllele :: UnphasedMissing ,
@@ -783,7 +858,19 @@ impl From<GenotypeAllele> for i32 {
783
858
GenotypeAllele :: Unphased ( a) => ( a, 0 ) ,
784
859
GenotypeAllele :: Phased ( a) => ( a, 1 ) ,
785
860
} ;
786
- allele + 1 << 1 | phased
861
+ ( allele + 1 ) << 1 | phased
862
+ }
863
+ }
864
+
865
+ impl From < i32 > for GenotypeAllele {
866
+ fn from ( encoded : i32 ) -> GenotypeAllele {
867
+ match ( encoded, encoded & 1 ) {
868
+ ( 0 , 0 ) => GenotypeAllele :: UnphasedMissing ,
869
+ ( 1 , 1 ) => GenotypeAllele :: PhasedMissing ,
870
+ ( e, 1 ) => GenotypeAllele :: Phased ( ( e >> 1 ) - 1 ) ,
871
+ ( e, 0 ) => GenotypeAllele :: Unphased ( ( e >> 1 ) - 1 ) ,
872
+ _ => panic ! ( "unexpected phasing type" ) ,
873
+ }
787
874
}
788
875
}
789
876
@@ -825,11 +912,7 @@ impl<'a, B: Borrow<Buffer> + 'a> Genotypes<'a, B> {
825
912
/// this method will return `[Unphased(1), Phased(1)]`.
826
913
pub fn get ( & self , i : usize ) -> Genotype {
827
914
let igt = self . encoded [ i] ;
828
- Genotype (
829
- igt. iter ( )
830
- . map ( |& e| GenotypeAllele :: from_encoded ( e) )
831
- . collect ( ) ,
832
- )
915
+ Genotype ( igt. iter ( ) . map ( |& e| GenotypeAllele :: from ( e) ) . collect ( ) )
833
916
}
834
917
}
835
918
0 commit comments