yat  0.21pre
BamRead.h
1 #ifndef theplu_yat_omic_bam_read
2 #define theplu_yat_omic_bam_read
3 
4 // $Id: BamRead.h 4207 2022-08-26 04:36:28Z peter $
5 
6 /*
7  Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2022 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 "yat/utility/Aligner.h"
26 // This file has to be included to keep compatibility with yat 0.11
27 #include "yat/utility/Cigar.h"
28 #include "yat/utility/deprecate.h"
29 
30 #include <htslib/sam.h>
31 
32 #include <functional>
33 #include <string>
34 #include <vector>
35 
36 namespace theplu {
37 namespace yat {
38 namespace omic {
39 
51  class BamRead
52  {
53  public:
60  BamRead(void);
61 
65  BamRead(const BamRead& other);
66 
72  BamRead(BamRead&& other) noexcept;
73 
77  virtual ~BamRead(void);
78 
82  const BamRead& operator=(const BamRead& rhs);
83 
89  BamRead& operator=(BamRead&& rhs);
90 
96  const uint8_t* aux(void) const;
97 
109  const uint8_t* aux(const char tag[2]) const;
110 
123  void aux_append(const char tag[2], char type, int len, uint8_t* data);
124 
132  void aux_del(const char tag[2]);
133 
139  int aux_size(void) const;
140 
146  const bam1_core_t& core(void) const;
147 
153  bam1_core_t& core(void);
154 
163  const uint32_t* cigar(void) const;
164 
168  uint32_t cigar(size_t i) const;
169 
173  uint32_t cigar_op(size_t i) const;
174 
178  uint32_t cigar_oplen(size_t i) const;
179 
183  std::string cigar_str(void) const;
184 
193  void cigar(const std::vector<uint32_t>& c) YAT_DEPRECATE;
194 
202  void cigar(const utility::Aligner::Cigar& cigar);
203 
212  int32_t end(void) const;
213 
221  uint16_t flag(void) const;
222 
228  bool flag_bit_is_set(uint16_t x) const;
229 
235  uint16_t get_flag_bit(uint16_t x) const;
236 
244  void set_flag_bit(uint16_t x);
245 
253  void unset_flag_bit(uint16_t x);
254 
262  void flip_flag_bit(uint16_t x);
263 
267  int32_t mpos(void) const;
268 
272  int32_t mtid(void) const;
273 
279  const char* name(void) const;
280 
286  void name(const std::string& n);
287 
291  int32_t pos(void) const;
292 
296  uint8_t qual(size_t i) const;
297 
306  void qual(size_t i, uint8_t q);
307 
313  std::string sequence(void) const;
314 
320  uint8_t sequence(size_t index) const;
321 
327  char sequence_str(size_t index) const;
328 
338  void sequence(size_t i, uint8_t x);
339 
345  void sequence(const std::string& seq, const std::vector<uint8_t>& qual);
346 
350  uint32_t sequence_length(void) const;
351 
357  void swap(BamRead& other);
358 
365  bool strand(void) const;
366 
373  bool mstrand(void) const;
374 
378  int32_t tid(void) const;
379 
380  private:
381  // ensure capacity of data pointer is (at least) n
382  void reserve(uint32_t n);
383 
393  void sequence(size_t k, uint8_t x, uint8_t y);
394 
395  bam1_t* bam_;
396 
397  friend class InBamFile;
398  friend class OutBamFile;
399  friend class BamReadIterator;
400  // access data length, current length of data
401  int& data_size(void);
402  const int& data_size(void) const;
403  uint32_t data_capacity(void) const;
404  };
405 
417  const unsigned char nt16_table(char);
418 
429  const char nt16_str(uint8_t);
430 
439  void swap(BamRead& lhs, BamRead& rhs);
440 
449  bool soft_clipped(const BamRead& bam);
450 
459  uint32_t left_soft_clipped(const BamRead& bam);
460 
469  uint32_t right_soft_clipped(const BamRead& bam);
470 
480  bool same_query_name(const BamRead& lhs, const BamRead& rhs);
481 
491  bool less_query_name(const BamRead& lhs, const BamRead& rhs);
492 
501  struct BamLessPos
502  : public std::binary_function<const BamRead&, const BamRead&, bool>
503  {
510  bool operator()(const BamRead& lhs, const BamRead& rhs) const;
511  };
512 
513 
522  struct BamLessEnd
523  : public std::binary_function<const BamRead&, const BamRead&, bool>
524  {
531  bool operator()(const BamRead& lhs, const BamRead& rhs) const;
532  };
533 
542  struct BamLessName
543  : public std::binary_function<const BamRead&, const BamRead&, bool>
544  {
548  bool operator()(const BamRead& lhs, const BamRead& rhs) const;
549  };
550 
551 
570  template<typename RandomAccessIterator>
571  double alignment_score(const BamRead& bam, RandomAccessIterator ref,
572  double mismatch, double gap=0.0, double open_gap=0.0)
573  {
574  BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<RandomAccessIterator>));
575  BOOST_CONCEPT_ASSERT((boost_concepts::RandomAccessTraversal<RandomAccessIterator>));
576  double res = 0;
577  size_t q = 0;
578 
579  for (uint32_t i = 0; i<bam.core().n_cigar; ++i) {
580  switch (bam.cigar_op(i)) {
581  case (BAM_CMATCH): {
582  RandomAccessIterator end = ref + bam.cigar_oplen(i);
583  for (; ref!=end; ++ref) {
584  if (bam.sequence_str(q) == *ref)
585  res += 1.0;
586  else
587  res -= mismatch;
588  ++q;
589  }
590  break;
591  }
592  case (BAM_CEQUAL): {
593  q += bam.cigar_oplen(i);
594  ref += bam.cigar_oplen(i);
595  res += bam.cigar_oplen(i);
596  break;
597  }
598  case (BAM_CDIFF): {
599  q += bam.cigar_oplen(i);
600  ref += bam.cigar_oplen(i);
601  res -= bam.cigar_oplen(i)*mismatch;
602  break;
603  }
604  case (BAM_CSOFT_CLIP): {
605  q += bam.cigar_oplen(i);
606  break;
607  }
608  case (BAM_CINS): {
609  q += bam.cigar_oplen(i);
610  res -= open_gap + bam.cigar_oplen(i) * gap;
611  break;
612  }
613  case (BAM_CDEL): {
614  ref += bam.cigar_oplen(i);
615  res -= open_gap + bam.cigar_oplen(i) * gap;
616  break;
617  }
618  } // end switch
619  }
620  return res;
621  }
622 
623 }}}
624 #endif
int32_t end(void) const
rightmost coordinate
void aux_del(const char tag[2])
remove a tag in aux field
virtual ~BamRead(void)
Destructor.
int32_t mpos(void) const
leftmost position for mate
bool operator()(const BamRead &lhs, const BamRead &rhs) const
const uint8_t * aux(void) const
BamRead(void)
default constructor
The Department of Theoretical Physics namespace as we define it.
const char nt16_str(uint8_t)
int32_t pos(void) const
0-based leftmost coordinate
bool operator()(const BamRead &lhs, const BamRead &rhs) const
void unset_flag_bit(uint16_t x)
std::string sequence(void) const
Class holding a bam query.
Definition: BamRead.h:51
uint8_t qual(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:571
uint32_t cigar_oplen(size_t i) const
const BamRead & operator=(const BamRead &rhs)
assignment operator
uint32_t cigar_op(size_t i) const
Definition: BamRead.h:501
uint16_t flag(void) const
bitwise flag
uint16_t get_flag_bit(uint16_t x) const
void flip_flag_bit(uint16_t x)
bool strand(void) const
int32_t mtid(void) const
Chromosome ID for mate.
void aux_append(const char tag[2], char type, int len, uint8_t *data)
append a new tag to aux field
std::string cigar_str(void) const
bool operator()(const BamRead &lhs, const BamRead &rhs) const
int aux_size(void) const
Compact Idiosyncratic Gapped Alignment Report.
Definition: Aligner.h:184
Definition: BamRead.h:542
const bam1_core_t & core(void) const
access core data struct
Definition: BamFile.h:245
bool mstrand(void) const
Definition: BamRead.h:522
void set_flag_bit(uint16_t x)
Definition: BamFile.h:134
uint32_t sequence_length(void) const
void swap(BamRead &other)
const char * name(void) const
class to iterate through a InBamFile
Definition: BamReadIterator.h:61
int32_t tid(void) const
chromosome ID
const unsigned char nt16_table(char)
bool flag_bit_is_set(uint16_t x) const
char sequence_str(size_t index) const
const uint32_t * cigar(void) const
access CIGAR array

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14