yat  0.16.4pre
BamRead.h
1 #ifndef theplu_yat_omic_bam_read
2 #define theplu_yat_omic_bam_read
3 
4 // $Id: BamRead.h 3752 2018-10-17 01:53:42Z peter $
5 
6 /*
7  Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, 2018 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include "config_bam.h"
26 
27 #include "yat/utility/Aligner.h"
28 // This file has to be included to keep compatibility with yat 0.11
29 #include "yat/utility/Cigar.h"
30 #include "yat/utility/deprecate.h"
31 
32 #include YAT_SAM_HEADER
33 
34 #include <functional>
35 #include <string>
36 #include <vector>
37 
38 namespace theplu {
39 namespace yat {
40 namespace omic {
41 
53  class BamRead
54  {
55  public:
62  BamRead(void);
63 
67  BamRead(const BamRead& other);
68 
69 #ifdef YAT_HAVE_RVALUE
70 
75  BamRead(BamRead&& other) noexcept;
76 #endif
77 
81  virtual ~BamRead(void);
82 
86  const BamRead& operator=(const BamRead& rhs);
87 
88 #ifdef YAT_HAVE_RVALUE
89 
94  BamRead& operator=(BamRead&& rhs);
95 #endif
96 
102  const uint8_t* aux(void) const;
103 
115  const uint8_t* aux(const char tag[2]) const;
116 
129  void aux_append(const char tag[2], char type, int len, uint8_t* data);
130 
138  void aux_del(const char tag[2]);
139 
145  int aux_size(void) const;
146 
152  const bam1_core_t& core(void) const;
153 
159  bam1_core_t& core(void);
160 
169  const uint32_t* cigar(void) const;
170 
174  uint32_t cigar(size_t i) const;
175 
179  uint32_t cigar_op(size_t i) const;
180 
184  uint32_t cigar_oplen(size_t i) const;
185 
189  std::string cigar_str(void) const;
190 
199  void cigar(const std::vector<uint32_t>& c) YAT_DEPRECATE;
200 
208  void cigar(const utility::Aligner::Cigar& cigar);
209 
218  int32_t end(void) const;
219 
227  uint16_t flag(void) const;
228 
234  bool flag_bit_is_set(uint16_t x) const;
235 
241  uint16_t get_flag_bit(uint16_t x) const;
242 
250  void set_flag_bit(uint16_t x);
251 
259  void unset_flag_bit(uint16_t x);
260 
268  void flip_flag_bit(uint16_t x);
269 
273  int32_t mpos(void) const;
274 
278  int32_t mtid(void) const;
279 
285  const char* name(void) const;
286 
292  void name(const std::string& n);
293 
297  int32_t pos(void) const;
298 
302  uint8_t qual(size_t i) const;
303 
312  void qual(size_t i, uint8_t q);
313 
319  std::string sequence(void) const;
320 
326  uint8_t sequence(size_t index) const;
327 
333  char sequence_str(size_t index) const;
334 
344  void sequence(size_t i, uint8_t x);
345 
351  void sequence(const std::string& seq, const std::vector<uint8_t>& qual);
352 
356  uint32_t sequence_length(void) const;
357 
363  void swap(BamRead& other);
364 
371  bool strand(void) const;
372 
379  bool mstrand(void) const;
380 
384  int32_t tid(void) const;
385 
386  private:
387  // ensure capacity of data pointer is (at least) n
388  void reserve(uint32_t n);
389 
390  bam1_t* bam_;
391 
392  friend class InBamFile;
393  friend class OutBamFile;
394  friend class BamReadIterator;
395 #ifndef YAT_HAVE_HTSLIB
396  uint32_t calend(const bam1_core_t *c, const uint32_t *cigar) const;
397 #endif
398  // access data length, current length of data
399  int& data_size(void);
400  const int& data_size(void) const;
401  uint32_t data_capacity(void) const;
402  };
403 
417  const unsigned char nt16_table(char);
418 
431  const char nt16_str(uint8_t);
432 
441  void swap(BamRead& lhs, BamRead& rhs);
442 
451  bool soft_clipped(const BamRead& bam);
452 
461  uint32_t left_soft_clipped(const BamRead& bam);
462 
471  uint32_t right_soft_clipped(const BamRead& bam);
472 
482  bool same_query_name(const BamRead& lhs, const BamRead& rhs);
483 
493  bool less_query_name(const BamRead& lhs, const BamRead& rhs);
494 
503  struct BamLessPos
504  : public std::binary_function<const BamRead&, const BamRead&, bool>
505  {
512  bool operator()(const BamRead& lhs, const BamRead& rhs) const;
513  };
514 
515 
524  struct BamLessEnd
525  : public std::binary_function<const BamRead&, const BamRead&, bool>
526  {
533  bool operator()(const BamRead& lhs, const BamRead& rhs) const;
534  };
535 
544  struct BamLessName
545  : public std::binary_function<const BamRead&, const BamRead&, bool>
546  {
550  bool operator()(const BamRead& lhs, const BamRead& rhs) const;
551  };
552 
553 
572  template<typename RandomAccessIterator>
573  double alignment_score(const BamRead& bam, RandomAccessIterator ref,
574  double mismatch, double gap=0.0, double open_gap=0.0)
575  {
576  BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<RandomAccessIterator>));
577  BOOST_CONCEPT_ASSERT((boost_concepts::RandomAccessTraversal<RandomAccessIterator>));
578  double res = 0;
579  size_t q = 0;
580 
581  for (uint32_t i = 0; i<bam.core().n_cigar; ++i) {
582  switch (bam.cigar_op(i)) {
583  case (BAM_CMATCH): {
584  RandomAccessIterator end = ref + bam.cigar_oplen(i);
585  for (; ref!=end; ++ref) {
586  if (bam.sequence_str(q) == *ref)
587  res += 1.0;
588  else
589  res -= mismatch;
590  ++q;
591  }
592  break;
593  }
594  case (BAM_CEQUAL): {
595  q += bam.cigar_oplen(i);
596  ref += bam.cigar_oplen(i);
597  res += bam.cigar_oplen(i);
598  break;
599  }
600  case (BAM_CDIFF): {
601  q += bam.cigar_oplen(i);
602  ref += bam.cigar_oplen(i);
603  res -= bam.cigar_oplen(i)*mismatch;
604  break;
605  }
606  case (BAM_CSOFT_CLIP): {
607  q += bam.cigar_oplen(i);
608  break;
609  }
610  case (BAM_CINS): {
611  q += bam.cigar_oplen(i);
612  res -= open_gap + bam.cigar_oplen(i) * gap;
613  break;
614  }
615  case (BAM_CDEL): {
616  ref += bam.cigar_oplen(i);
617  res -= open_gap + bam.cigar_oplen(i) * gap;
618  break;
619  }
620  } // end switch
621  }
622  return res;
623  }
624 
625 }}}
626 #endif
void aux_del(const char tag[2])
remove a tag in aux field
virtual ~BamRead(void)
Destructor.
uint32_t sequence_length(void) const
int32_t tid(void) const
chromosome ID
BamRead(void)
default constructor
The Department of Theoretical Physics namespace as we define it.
const char nt16_str(uint8_t)
void unset_flag_bit(uint16_t x)
uint32_t right_soft_clipped(const BamRead &bam)
int32_t end(void) const
rightmost coordinate
const char * name(void) const
bool soft_clipped(const BamRead &bam)
const bam1_core_t & core(void) const
access core data struct
Class holding a bam query.
Definition: BamRead.h:53
uint32_t cigar_op(size_t i) const
double alignment_score(const BamRead &bam, RandomAccessIterator ref, double mismatch, double gap=0.0, double open_gap=0.0)
Definition: BamRead.h:573
const BamRead & operator=(const BamRead &rhs)
assignment operator
uint16_t get_flag_bit(uint16_t x) const
char sequence_str(size_t index) const
Definition: BamRead.h:503
bool less_query_name(const BamRead &lhs, const BamRead &rhs)
bool same_query_name(const BamRead &lhs, const BamRead &rhs)
int aux_size(void) const
int32_t mtid(void) const
Chromosome ID for mate.
void flip_flag_bit(uint16_t x)
bool strand(void) const
std::string sequence(void) const
void aux_append(const char tag[2], char type, int len, uint8_t *data)
append a new tag to aux field
uint32_t left_soft_clipped(const BamRead &bam)
const uint8_t * aux(void) const
bool flag_bit_is_set(uint16_t x) const
bool mstrand(void) const
Compact Idiosyncratic Gapped Alignment Report.
Definition: Aligner.h:179
Definition: BamRead.h:544
Definition: BamFile.h:252
int32_t mpos(void) const
leftmost position for mate
Definition: BamRead.h:524
void set_flag_bit(uint16_t x)
Definition: BamFile.h:132
void swap(BamRead &other)
uint32_t cigar_oplen(size_t i) const
class to iterate through a InBamFile
Definition: BamReadIterator.h:60
uint16_t flag(void) const
bitwise flag
uint8_t qual(size_t i) const
const unsigned char nt16_table(char)
int32_t pos(void) const
0-based laftmost coordinate
const uint32_t * cigar(void) const
access CIGAR array
std::string cigar_str(void) const

Generated on Thu Dec 12 2019 03:12:08 for yat by  doxygen 1.8.11