libStatGen Software  1
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         default:
00032             break;
00033     };
00034 
00035     // Not 'N', 'n', or '.', so return false.
00036     return(false);
00037 }
00038 
00039 bool BaseUtilities::areEqual(char base1, char base2)
00040 {
00041     // If they are the same, return true.
00042     if(base1 == base2)
00043     {
00044         return(true);
00045     }
00046     // If one of the bases is '=', return true.
00047     if((base1 == '=') || (base2 == '='))
00048     {
00049         return(true);
00050     }
00051 
00052     // Check both in upercase.
00053     if(toupper(base1) == toupper(base2))
00054     {
00055         // same in upper case.
00056         return(true);
00057     }
00058 
00059     // The bases are different.
00060     return(false);
00061 }
00062 
00063 
00064 // Get phred base quality from the specified ascii quality.
00065 uint8_t BaseUtilities::getPhredBaseQuality(char charQuality)
00066 {
00067     if(charQuality == UNKNOWN_QUALITY_CHAR)
00068     {
00069         return(UNKNOWN_QUALITY_INT);
00070     }
00071 
00072     return(charQuality - 33);
00073 }
00074 
00075 
00076 char BaseUtilities::getAsciiQuality(uint8_t phredQuality)
00077 {
00078     if(phredQuality == UNKNOWN_QUALITY_INT)
00079     {
00080         return(UNKNOWN_QUALITY_CHAR);
00081     }
00082     return(phredQuality + 33);
00083 }
00084 
00085 
00086 void BaseUtilities::reverseComplement(std::string& sequence)
00087 {
00088     int start = 0;
00089     int end = sequence.size() - 1;
00090     char tempChar;
00091 
00092     while(start < end)
00093     {
00094         tempChar = sequence[start];
00095         sequence[start] = BaseAsciiMap::base2complement[(int)(sequence[end])];
00096         sequence[end] = BaseAsciiMap::base2complement[(int)tempChar];
00097         // Move both pointers.
00098         ++start;
00099         --end;
00100     }
00101 
00102     // there was an odd number of entries, complement the middle one.
00103     if(start == end)
00104     {
00105         tempChar = sequence[start];
00106         sequence[start] = BaseAsciiMap::base2complement[(int)tempChar];
00107     }
00108 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends