libStatGen Software  1
BaseCount.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 <iostream>
19 #include <iomanip>
20 #include "BaseCount.h"
21 
22 // Constructor. Initializes the array to be all 0s.
24 {
25  // Init each element of the array to 0.
26  for(int i = 0; i < myBaseSize; i++)
27  {
28  myBaseCount[i] = 0;
29  }
30 }
31 
32 
33 // Update the count for the specified index as well as the overall count
34 // (The last index).
35 // Returns false if the specified index is < 0 or >= myBaseSize-1. The
36 // reason returns false if it is equal to the size-1 is because the last
37 // index is used to track an overall count.
38 bool BaseCount::incrementCount(int baseIndex)
39 {
40  // Check to see if the index is within range (>=0 & < myBaseSize-1)
41  // The last entry of the array is invalid since it is used to track
42  // total occurrence of all other entries.
43  if((baseIndex < myBaseSize-1) && (baseIndex >= 0))
44  {
45  // Valid index, so increment that index as well as the overall
46  // count (index myBaseSize-1) and return true.
47  myBaseCount[baseIndex]++;
48  myBaseCount[myBaseSize-1]++;
49  return true;
50  }
51  else
52  {
53  // Invalid index, return false
54  return false;
55  }
56 }
57 
58 
59 
60 // Prints the percentage for each index 0 to myBaseSize-2. Also prints
61 // the total number of entries (index myBaseSize-1).
62 void BaseCount::printPercent()
63 {
64  // Do not divide by 0, so check to see if there are any bases by checking
65  // the last index of the array.
66  if(myBaseCount[myBaseSize-1] == 0)
67  {
68  // No entries for any index.
69  std::cout << "No Valid Bases found.";
70  }
71  else
72  {
73  // Print the percentage for each index.
74  for(int i = 0; i < myBaseSize -1; i++)
75  {
76  double percentage =
77  (myBaseCount[i]/(double)myBaseCount[myBaseSize-1]) * 100;
78  std::cout << " " << std::setw(7) << percentage;
79  }
80  // Print the total number of bases.
81  std::cout << "\t" << myBaseCount[myBaseSize-1];
82  }
83  std::cout << std::endl;
84 }
BaseCount::BaseCount
BaseCount()
Constructor, initializes the array to be all 0s.
Definition: BaseCount.cpp:23
BaseCount::incrementCount
bool incrementCount(int baseIndex)
Update the count for the specified index as well as the overall count (The last index).
Definition: BaseCount.cpp:38