libStatGen Software  1
BaseCount.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 <iostream>
00019 #include <iomanip>
00020 #include "BaseCount.h"
00021 
00022 // Constructor.  Initializes the array to be all 0s.
00023 BaseCount::BaseCount()
00024 {
00025    // Init each element of the array to 0.
00026    for(int i = 0; i < myBaseSize; i++)
00027    {
00028       myBaseCount[i] = 0;
00029    }
00030 }
00031 
00032 
00033 // Update the count for the specified index as well as the overall count 
00034 // (The last index).
00035 // Returns false if the specified index is < 0 or >= myBaseSize-1.  The
00036 // reason returns false if it is equal to the size-1 is because the last
00037 // index is used to track an overall count.
00038 bool BaseCount::incrementCount(int baseIndex)
00039 {
00040    // Check to see if the index is within range (>=0 & < myBaseSize-1)
00041    // The last entry of the array is invalid since it is used to track
00042    // total occurrence of all other entries.
00043    if((baseIndex < myBaseSize-1) && (baseIndex >= 0))
00044    {
00045       // Valid index, so increment that index as well as the overall
00046       // count (index myBaseSize-1) and return true.
00047       myBaseCount[baseIndex]++;
00048       myBaseCount[myBaseSize-1]++;
00049       return true;
00050    }
00051    else
00052    {
00053       // Invalid index, return false
00054       return false;
00055    }
00056 }
00057 
00058 
00059 
00060 // Prints the percentage for each index 0 to myBaseSize-2.  Also prints
00061 // the total number of entries (index myBaseSize-1).
00062 void BaseCount::printPercent()
00063 {
00064    // Do not divide by 0, so check to see if there are any bases by checking
00065    // the last index of the array.
00066    if(myBaseCount[myBaseSize-1] == 0)
00067    {
00068       // No entries for any index.
00069       std::cout << "No Valid Bases found.";
00070    }
00071    else
00072    {
00073       // Print the percentage for each index.
00074       for(int i = 0; i < myBaseSize -1; i++)
00075       {
00076          double percentage = 
00077             (myBaseCount[i]/(double)myBaseCount[myBaseSize-1]) * 100;
00078          std::cout << " " << std::setw(7) << percentage;
00079       }
00080       // Print the total number of bases.
00081       std::cout << "\t" << myBaseCount[myBaseSize-1];
00082    }
00083    std::cout << std::endl;
00084 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends