libStatGen Software  1
SamRecordHelper.cpp
00001 /*
00002  *  Copyright (C) 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 "SamRecordHelper.h"
00019 #include <stdexcept>
00020 
00021 int SamRecordHelper::checkSequence(SamRecord& record, int32_t pos0Based, 
00022                                     const char* sequence)
00023 {
00024     const char* readSeq = record.getSequence();
00025 
00026     // Get the cigar.
00027     Cigar* cigar = record.getCigarInfo();
00028 
00029     if(cigar == NULL)
00030     {
00031         throw std::runtime_error("Failed to get Cigar.");
00032     }
00033 
00034     int32_t readStartIndex = 
00035         cigar->getQueryIndex(pos0Based, record.get0BasedPosition());
00036 
00037     // if the read start is negative, this position was deleted, so 
00038     // return false, it doesn't match.
00039     if(readStartIndex == Cigar::INDEX_NA)
00040     {
00041         return(false);
00042     }
00043 
00044     // Increment the readSeq start to where this position is found.
00045     readSeq += readStartIndex;
00046     if(strncmp(readSeq, sequence, strlen(sequence)) == 0)
00047     {
00048         // Match, so return the readStartIndex (cycle).
00049         return(readStartIndex);
00050     }
00051     // Did not match.
00052     return(-1);
00053 }
00054 
00055 
00056 bool SamRecordHelper::genSamTagsString(SamRecord& record,
00057                                        String& returnString,
00058                                        char delim)
00059 {
00060     char tag[3];
00061     char vtype;
00062     void* value;
00063 
00064     // Reset the tag iterator to ensure that all the tags are written.
00065     record.resetTagIter();
00066 
00067     // While there are more tags, write them to the recordString.
00068     bool firstEntry = true;
00069     bool returnStatus = true;
00070     while(record.getNextSamTag(tag, vtype, &value) != false)
00071     {
00072         if(!firstEntry)
00073         {
00074             returnString += delim;
00075         }
00076         else
00077         {
00078             firstEntry = false;
00079         }
00080         returnStatus &= genSamTagString(tag, vtype, value, returnString);
00081     }
00082     return(returnStatus);
00083 }
00084 
00085 
00086 bool SamRecordHelper::genSamTagString(const char* tag, char vtype, 
00087                                       void* value, String& returnString)
00088 {
00089     returnString += tag;
00090     returnString += ":"; 
00091     returnString += vtype;
00092     returnString += ":";
00093     if(SamRecord::isIntegerType(vtype))
00094     {
00095         returnString += (int)*(int*)value;
00096     }
00097     else if(SamRecord::isFloatType(vtype))
00098     {
00099         returnString.appendFullFloat(*(float*)value);
00100     }
00101     else if(SamRecord::isCharType(vtype))
00102     {
00103         returnString += (char)*(char*)value;
00104     }
00105     else if(SamRecord::isStringType(vtype))
00106     {
00107         // String type.
00108         returnString += (String)*(String*)value;
00109     }
00110     else
00111     {
00112         // Could not determine the type.
00113         return(false);
00114     }
00115     return(true);
00116 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends