libStatGen Software  1
SamReferenceInfo.cpp
00001 /*
00002  *  Copyright (C) 2010  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 "SamReferenceInfo.h"
00019 
00020 SamReferenceInfo::SamReferenceInfo()
00021     : myReferenceContigs(),
00022       myReferenceHash(),
00023       myReferenceLengths()
00024 {
00025     clear();
00026 }
00027 
00028 
00029 SamReferenceInfo::~SamReferenceInfo()
00030 {
00031     clear();
00032 }
00033 
00034 // Add reference sequence name and reference sequence length.
00035 void SamReferenceInfo::add(const char* referenceSequenceName, 
00036                            int32_t referenceSequenceLength)
00037 {
00038         myReferenceHash.Add(referenceSequenceName, 
00039                             myReferenceContigs.Length());
00040         myReferenceContigs.Push(referenceSequenceName);
00041         myReferenceLengths.Push(referenceSequenceLength);
00042 }
00043 
00044 
00045 int SamReferenceInfo::getReferenceID(const String & referenceName, 
00046                                      bool addID)
00047 {
00048     if (referenceName == "*")
00049         return -1;
00050     
00051     int id = myReferenceHash.Find(referenceName);
00052 
00053     if (id >= 0)
00054         return myReferenceHash.Integer(id);
00055     
00056     if(!addID)
00057     {
00058         // Don't add the id, so return NO_REF_ID
00059         return(NO_REF_ID);
00060     }
00061 
00062     id = myReferenceContigs.Length();
00063     myReferenceContigs.Push(referenceName);
00064     myReferenceLengths.Push(0);
00065     myReferenceHash.Add(referenceName, id);
00066 
00067     return id;
00068 }
00069 
00070 
00071 int SamReferenceInfo::getReferenceID(const char* referenceName, 
00072                                      bool addID)
00073 {
00074     String referenceNameString = referenceName;
00075 
00076     return(getReferenceID(referenceNameString, addID));
00077 }
00078 
00079 
00080 const String & SamReferenceInfo::getReferenceLabel(int id) const
00081 {
00082     static String noname("*");
00083 
00084     if ((id < 0) || (id >= myReferenceContigs.Length()))
00085     {
00086         return noname;
00087     }
00088 
00089     return myReferenceContigs[id];
00090 }
00091 
00092 
00093 int32_t SamReferenceInfo::getNumEntries() const
00094 {
00095     // The number of entries is the size of referenceLengths.
00096     return(myReferenceLengths.Length());
00097 }
00098 
00099 
00100 const char* SamReferenceInfo::getReferenceName(int index) const
00101 {
00102     if((index >= 0) && (index < getNumEntries()))
00103     {
00104         return(myReferenceContigs[index].c_str());
00105     }
00106     
00107     // Out of range, return blank
00108     return("");
00109 }
00110    
00111 
00112 int32_t SamReferenceInfo::getReferenceLength(int index) const
00113 {
00114     if((index >= 0) && (index < getNumEntries()))
00115     {
00116         return(myReferenceLengths[index]);
00117     }
00118     
00119     // Out of bounds, return 0
00120     return(0);
00121 }
00122 
00123 void SamReferenceInfo::clear()
00124 {
00125     myReferenceContigs.Clear();
00126     myReferenceHash.Clear();
00127     myReferenceLengths.Clear();
00128 }
00129 
00130 
00131 SamReferenceInfo& SamReferenceInfo::operator = (const SamReferenceInfo &newInfo)
00132 {
00133     clear();
00134     // Copy Reference contigs, hash, lengths.
00135     myReferenceContigs = newInfo.myReferenceContigs;
00136     myReferenceHash = newInfo.myReferenceHash;
00137     myReferenceLengths = newInfo.myReferenceLengths;
00138     return(*this);
00139 }
00140 
00141 
00142 bool SamReferenceInfo::operator== (const SamReferenceInfo& rhs) const
00143 {
00144     // Hash may be different, but if Contigs are the same, the hashes will
00145     // contain the same basic info (maybe just at different indices.
00146     return((myReferenceContigs == rhs.myReferenceContigs) &&
00147            (myReferenceLengths == rhs.myReferenceLengths));
00148 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends