libStatGen Software  1
BaseUtilities.cpp
1 /*
2  * Copyright (C) 2010-2012 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "BaseUtilities.h"
19 #include <ctype.h>
20 #include "BaseAsciiMap.h"
21 
22 
24 {
25  switch(base)
26  {
27  case 'N':
28  case 'n':
29  case '.':
30  return(true);
31  default:
32  break;
33  };
34 
35  // Not 'N', 'n', or '.', so return false.
36  return(false);
37 }
38 
39 bool BaseUtilities::areEqual(char base1, char base2)
40 {
41  // If they are the same, return true.
42  if(base1 == base2)
43  {
44  return(true);
45  }
46  // If one of the bases is '=', return true.
47  if((base1 == '=') || (base2 == '='))
48  {
49  return(true);
50  }
51 
52  // Check both in upercase.
53  if(toupper(base1) == toupper(base2))
54  {
55  // same in upper case.
56  return(true);
57  }
58 
59  // The bases are different.
60  return(false);
61 }
62 
63 
64 // Get phred base quality from the specified ascii quality.
65 uint8_t BaseUtilities::getPhredBaseQuality(char charQuality)
66 {
67  if(charQuality == UNKNOWN_QUALITY_CHAR)
68  {
69  return(UNKNOWN_QUALITY_INT);
70  }
71 
72  return(charQuality - 33);
73 }
74 
75 
76 char BaseUtilities::getAsciiQuality(uint8_t phredQuality)
77 {
78  if(phredQuality == UNKNOWN_QUALITY_INT)
79  {
80  return(UNKNOWN_QUALITY_CHAR);
81  }
82  return(phredQuality + 33);
83 }
84 
85 
86 void BaseUtilities::reverseComplement(std::string& sequence)
87 {
88  int start = 0;
89  int end = sequence.size() - 1;
90  char tempChar;
91 
92  while(start < end)
93  {
94  tempChar = sequence[start];
95  sequence[start] = BaseAsciiMap::base2complement[(int)(sequence[end])];
96  sequence[end] = BaseAsciiMap::base2complement[(int)tempChar];
97  // Move both pointers.
98  ++start;
99  --end;
100  }
101 
102  // there was an odd number of entries, complement the middle one.
103  if(start == end)
104  {
105  tempChar = sequence[start];
106  sequence[start] = BaseAsciiMap::base2complement[(int)tempChar];
107  }
108 }
static unsigned char base2complement[]
This table maps 5' base space to the 3' complement base space values, as well as 5' color space value...
Definition: BaseAsciiMap.h:41
static bool isAmbiguous(char base)
Returns whether or not the specified bases is an indicator for ambiguity.
static const char UNKNOWN_QUALITY_CHAR
Character used when the quality is unknown.
Definition: BaseUtilities.h:49
static bool areEqual(char base1, char base2)
Returns whether or not two bases are equal (case insensitive), if one of the bases is '=',...
static char getAsciiQuality(uint8_t phredQuality)
Get ascii quality from the specified phred quality.
static const uint8_t UNKNOWN_QUALITY_INT
Int value used when the quality is unknown.
Definition: BaseUtilities.h:51
static uint8_t getPhredBaseQuality(char charQuality)
Get phred base quality from the specified ascii quality.