File: SamStatistics.cpp

package info (click to toggle)
libstatgen 1.0.15-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,588 kB
  • sloc: cpp: 49,624; ansic: 1,408; makefile: 320; sh: 60
file content (133 lines) | stat: -rw-r--r-- 3,962 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 *  Copyright (C) 2010  Regents of the University of Michigan
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "SamStatistics.h"
#include <iomanip>
#include "SamFlag.h"

SamStatistics::SamStatistics()
{
    reset();
}

SamStatistics::~SamStatistics()
{
    reset();
}

void SamStatistics::reset()
{
    myReadCount = 0;
    myMappedReadCount = 0;
    myPairedReadCount = 0;
    myProperPairedReadCount = 0;
    myBaseCount = 0;
    myMappedReadBases = 0;
    myDupReadCount = 0;
    myQCFailureReadCount = 0;
}


bool SamStatistics::updateStatistics(SamRecord& samRecord)
{
    // Each record has one read, so update the read count.
    ++myReadCount;

    int32_t readLen = samRecord.getReadLength();

    // Get the flag to determine the type or 
    // read (mapped, paired, proper paired).
    uint16_t flag = samRecord.getFlag();

    // If the read is mapped, update the mapped c
    if(SamFlag::isMapped(flag))
    {
        ++myMappedReadCount;
        myMappedReadBases += readLen;
    }
    if(SamFlag::isPaired(flag))
    {
        ++myPairedReadCount;
        if(SamFlag::isProperPair(flag))
        {
            ++myProperPairedReadCount;
        }
    }
    if(SamFlag::isDuplicate(flag))
    {
        ++myDupReadCount;
    }
    if(SamFlag::isQCFailure(flag))
    {
        ++myQCFailureReadCount;
    }
    
    // Increment the total number of bases.
    myBaseCount += readLen;

    return(true);
}


void SamStatistics::print()
{
    double DIVIDE_UNITS = 1000000;
    std::string units = "(e6)";

    std::cerr << std::fixed << std::setprecision(2);

    // If total reads is less than DIVIDE_UNITS, just show the straight number.
    if(myReadCount < DIVIDE_UNITS)
    {
        DIVIDE_UNITS = 1;
        units.clear();
    }
    
    // Read Counts
    std::cerr << "TotalReads" << units << "\t"
              << myReadCount/DIVIDE_UNITS << std::endl;
    std::cerr << "MappedReads" << units << "\t" 
              << myMappedReadCount/DIVIDE_UNITS << std::endl;
    std::cerr << "PairedReads" << units << "\t" 
              << myPairedReadCount/DIVIDE_UNITS << std::endl;
    std::cerr << "ProperPair" << units << "\t" 
              << myProperPairedReadCount/DIVIDE_UNITS << std::endl;
    std::cerr << "DuplicateReads" << units << "\t" 
              << myDupReadCount/DIVIDE_UNITS << std::endl;
    std::cerr << "QCFailureReads" << units << "\t" 
              << myQCFailureReadCount/DIVIDE_UNITS << std::endl;
    std::cerr << std::endl;

    // Read Percentages
    std::cerr << "MappingRate(%)\t" 
              << 100 * myMappedReadCount/(double)myReadCount << std::endl;
    std::cerr << "PairedReads(%)\t" 
              << 100 * myPairedReadCount/(double)myReadCount << std::endl;
    std::cerr << "ProperPair(%)\t" 
              << 100 * myProperPairedReadCount/(double)myReadCount << std::endl;
    std::cerr << "DupRate(%)\t" 
              << 100 * myDupReadCount/(double)myReadCount << std::endl;
    std::cerr << "QCFailRate(%)\t" 
              << 100 * myQCFailureReadCount/(double)myReadCount << std::endl;
    std::cerr << std::endl;

    // Base Counts
    std::cerr << "TotalBases" << units << "\t"
              << myBaseCount/DIVIDE_UNITS << std::endl;
    std::cerr << "BasesInMappedReads" << units << "\t"
              << myMappedReadBases/DIVIDE_UNITS << std::endl;
}