libStatGen Software  1
SamReferenceInfo.cpp
1 /*
2  * Copyright (C) 2010 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 "SamReferenceInfo.h"
19 
21  : myReferenceContigs(),
22  myReferenceHash(),
23  myReferenceLengths()
24 {
25  clear();
26 }
27 
28 
30 {
31  clear();
32 }
33 
34 // Add reference sequence name and reference sequence length.
35 void SamReferenceInfo::add(const char* referenceSequenceName,
36  int32_t referenceSequenceLength)
37 {
38  myReferenceHash.Add(referenceSequenceName,
39  myReferenceContigs.Length());
40  myReferenceContigs.Push(referenceSequenceName);
41  myReferenceLengths.Push(referenceSequenceLength);
42 }
43 
44 
45 int SamReferenceInfo::getReferenceID(const String & referenceName,
46  bool addID)
47 {
48  if (referenceName == "*")
49  return -1;
50 
51  int id = myReferenceHash.Find(referenceName);
52 
53  if (id >= 0)
54  return myReferenceHash.Integer(id);
55 
56  if(!addID)
57  {
58  // Don't add the id, so return NO_REF_ID
59  return(NO_REF_ID);
60  }
61 
62  id = myReferenceContigs.Length();
63  myReferenceContigs.Push(referenceName);
64  myReferenceLengths.Push(0);
65  myReferenceHash.Add(referenceName, id);
66 
67  return id;
68 }
69 
70 
71 int SamReferenceInfo::getReferenceID(const char* referenceName,
72  bool addID)
73 {
74  String referenceNameString = referenceName;
75 
76  return(getReferenceID(referenceNameString, addID));
77 }
78 
79 
81 {
82  static String noname("*");
83 
84  if ((id < 0) || (id >= myReferenceContigs.Length()))
85  {
86  return noname;
87  }
88 
89  return myReferenceContigs[id];
90 }
91 
92 
94 {
95  // The number of entries is the size of referenceLengths.
96  return(myReferenceLengths.Length());
97 }
98 
99 
100 const char* SamReferenceInfo::getReferenceName(int index) const
101 {
102  if((index >= 0) && (index < getNumEntries()))
103  {
104  return(myReferenceContigs[index].c_str());
105  }
106 
107  // Out of range, return blank
108  return("");
109 }
110 
111 
112 int32_t SamReferenceInfo::getReferenceLength(int index) const
113 {
114  if((index >= 0) && (index < getNumEntries()))
115  {
116  return(myReferenceLengths[index]);
117  }
118 
119  // Out of bounds, return 0
120  return(0);
121 }
122 
124 {
125  myReferenceContigs.Clear();
126  myReferenceHash.Clear();
127  myReferenceLengths.Clear();
128 }
129 
130 
132 {
133  clear();
134  // Copy Reference contigs, hash, lengths.
135  myReferenceContigs = newInfo.myReferenceContigs;
136  myReferenceHash = newInfo.myReferenceHash;
137  myReferenceLengths = newInfo.myReferenceLengths;
138  return(*this);
139 }
140 
141 
142 bool SamReferenceInfo::operator== (const SamReferenceInfo& rhs) const
143 {
144  // Hash may be different, but if Contigs are the same, the hashes will
145  // contain the same basic info (maybe just at different indices.
146  return((myReferenceContigs == rhs.myReferenceContigs) &&
147  (myReferenceLengths == rhs.myReferenceLengths));
148 }
SamReferenceInfo::clear
void clear()
Reset this reference info.
Definition: SamReferenceInfo.cpp:123
SamReferenceInfo::getNumEntries
int32_t getNumEntries() const
Get the number of entries contained here.
Definition: SamReferenceInfo.cpp:93
String
Definition: StringBasics.h:39
SamReferenceInfo::getReferenceLength
int32_t getReferenceLength(int index) const
Return the reference length at the specified index, returning 0 if the index is out of bounds.
Definition: SamReferenceInfo.cpp:112
SamReferenceInfo
Class for tracking the reference information mapping between the reference ids and the reference name...
Definition: SamReferenceInfo.h:28
SamReferenceInfo::NO_REF_ID
static const int NO_REF_ID
Constant for the value returned if a reference id does not exist for a queried reference name.
Definition: SamReferenceInfo.h:77
SamReferenceInfo::getReferenceLabel
const String & getReferenceLabel(int id) const
Get the reference name for the specified id, if the id is not found, return "*".
Definition: SamReferenceInfo.cpp:80
SamReferenceInfo::getReferenceID
int getReferenceID(const String &referenceName, bool addID=false)
Get the reference ID for the specified name, if addID is set to true, a reference id will be created ...
Definition: SamReferenceInfo.cpp:45
SamReferenceInfo::~SamReferenceInfo
~SamReferenceInfo()
Destructor.
Definition: SamReferenceInfo.cpp:29
SamReferenceInfo::getReferenceName
const char * getReferenceName(int index) const
Return the reference name at the specified index, returning "" if the index is out of bounds.
Definition: SamReferenceInfo.cpp:100
SamReferenceInfo::SamReferenceInfo
SamReferenceInfo()
Constructor.
Definition: SamReferenceInfo.cpp:20
SamReferenceInfo::operator=
SamReferenceInfo & operator=(const SamReferenceInfo &rhs)
Copy the reference information.
Definition: SamReferenceInfo.cpp:131
SamReferenceInfo::add
void add(const char *referenceSequenceName, int32_t referenceSequenceLength)
Add reference sequence name and reference sequence length.
Definition: SamReferenceInfo.cpp:35