BaseUtilities.cpp

00001 /*
00002  *  Copyright (C) 2010-2012  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include "BaseUtilities.h"
00019 #include <ctype.h>
00020 #include "BaseAsciiMap.h"
00021 
00022 
00023 bool BaseUtilities::isAmbiguous(char base)
00024 {
00025     switch(base)
00026     {
00027         case 'N':
00028         case 'n':
00029         case '.':
00030             return(true);
00031             break;
00032         default:
00033             break;
00034     };
00035 
00036     // Not 'N', 'n', or '.', so return false.
00037     return(false);
00038 }
00039 
00040 bool BaseUtilities::areEqual(char base1, char base2)
00041 {
00042     // If they are the same, return true.
00043     if(base1 == base2)
00044     {
00045         return(true);
00046     }
00047     // If one of the bases is '=', return true.
00048     if((base1 == '=') || (base2 == '='))
00049     {
00050         return(true);
00051     }
00052 
00053     // Check both in upercase.
00054     if(toupper(base1) == toupper(base2))
00055     {
00056         // same in upper case.
00057         return(true);
00058     }
00059 
00060     // The bases are different.
00061     return(false);
00062 }
00063 
00064 
00065 // Get phred base quality from the specified ascii quality.
00066 uint8_t BaseUtilities::getPhredBaseQuality(char charQuality)
00067 {
00068     if(charQuality == UNKNOWN_QUALITY_CHAR)
00069     {
00070         return(UNKNOWN_QUALITY_INT);
00071     }
00072 
00073     return(charQuality - 33);
00074 }
00075 
00076 
00077 char BaseUtilities::getAsciiQuality(uint8_t phredQuality)
00078 {
00079     if(phredQuality == UNKNOWN_QUALITY_INT)
00080     {
00081         return(UNKNOWN_QUALITY_CHAR);
00082     }
00083     return(phredQuality + 33);
00084 }
00085 
00086 
00087 void BaseUtilities::reverseComplement(std::string& sequence)
00088 {
00089     int start = 0;
00090     int end = sequence.size() - 1;
00091     char tempChar;
00092 
00093     while(start < end)
00094     {
00095         tempChar = sequence[start];
00096         sequence[start] = BaseAsciiMap::base2complement[(int)(sequence[end])];
00097         sequence[end] = BaseAsciiMap::base2complement[(int)tempChar];
00098         // Move both pointers.
00099         ++start;
00100         --end;
00101     }
00102 
00103     // there was an odd number of entries, complement the middle one.
00104     if(start == end)
00105     {
00106         tempChar = sequence[start];
00107         sequence[start] = BaseAsciiMap::base2complement[(int)tempChar];
00108     }
00109 }
Generated on Mon Feb 11 13:45:18 2013 for libStatGen Software by  doxygen 1.6.3