File: Modify.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 (156 lines) | stat: -rw-r--r-- 3,795 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 *  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 "SamFile.h"
#include "SamFlag.h"
#include "Modify.h"

void testModify()
{
    modify modTest;
    modTest.testModify("testFiles/testSam.sam");
#ifdef __ZLIB_AVAILABLE__
    modTest.testModify("testFiles/testBam.bam");
#endif
}


void modify::testModify(const char* filename)
{
    myFilename = filename;

    modifyPosition();
    modifyCigar();
    
    modifyFlag();

    modifyTags();
}

void modify::modifyPosition()
{
    openAndRead1Rec();
   
    // Verify the initial bin.
    assert(samRecord.getBin() == 4681);

    // Change the position and verify that the bin is updated.
    assert(samRecord.set0BasedPosition(33768));

    // Verify the bin was updated.
    assert(samRecord.getBin() == 4683);
    assert(samRecord.get0BasedPosition() == 33768);
}


void modify::modifyCigar()
{
    openAndRead1Rec();
   
    // Verify the initial bin.
    assert(samRecord.getBin() == 4681);

    // Change the Cigar such that it modifies the bin.
    assert(samRecord.setCigar("33768M"));

    // Verify the bin was updated.
    assert(samRecord.getBin() == 585);
}


void modify::modifyFlag()
{
    openAndRead1Rec();
   
    // Verify the initial bin.
    uint16_t flag = 73;
    assert(samRecord.getFlag() == flag);

    SamFlag::setDuplicate(flag);
    assert(flag == 1097);
    assert(samRecord.setFlag(flag));
    assert(samRecord.getFlag() == 1097);

    SamFlag::setNotDuplicate(flag);
    assert(flag == 73);
    assert(samRecord.setFlag(flag));
    assert(samRecord.getFlag() == 73);
}


void modify::openAndRead1Rec()
{
    // Open the file for reading.   
    assert(samIn.OpenForRead(myFilename.c_str()));

    // Read the sam header.
    assert(samIn.ReadHeader(samHeader));
   
    // Read the first record.   
    assert(samIn.ReadRecord(samHeader, samRecord));
}


void modify::modifyTags()
{
    assert(samIn.OpenForRead(myFilename.c_str()));
    // Read the sam header.
    assert(samIn.ReadHeader(samHeader));
   
    SamFile samOut;
    SamFile bamOut;

    std::string inputType = myFilename.substr(myFilename.find_last_of('.'));
    std::string outFileBase = "results/updateTagFrom";
    if(inputType == ".bam")
    {
        outFileBase += "Bam";
    }
    else
    {
        outFileBase += "Sam";
    }

    std::string outFile = outFileBase + ".sam";
    assert(samOut.OpenForWrite(outFile.c_str()));
    outFile = outFileBase + ".bam";
    assert(bamOut.OpenForWrite(outFile.c_str()));
    assert(samOut.WriteHeader(samHeader));
    assert(bamOut.WriteHeader(samHeader));

    int count = 0;
    // Read the records.
    while(samIn.ReadRecord(samHeader, samRecord))
    {
        if(count == 0)
        {
            assert(samRecord.rmTag("MD", 'Z'));
        }
        else if(count == 2)
        {
            assert(samRecord.rmTags("XT:A;MD:Z;AB:c;NM:i"));
        }
        else if(count == 4)
        {
            assert(samRecord.rmTags("MD:Z,AB:c,NM:i"));
        }

        assert(bamOut.WriteRecord(samHeader, samRecord));
        assert(samOut.WriteRecord(samHeader, samRecord));
        ++count;
    }
}