0
|
1 /*****************************************************************************
|
|
2 slopBed.cpp
|
|
3
|
|
4 (c) 2009 - Aaron Quinlan
|
|
5 Hall Laboratory
|
|
6 Department of Biochemistry and Molecular Genetics
|
|
7 University of Virginia
|
|
8 aaronquinlan@gmail.com
|
|
9
|
|
10 Licensed under the GNU General Public License 2.0 license.
|
|
11 ******************************************************************************/
|
|
12 #include "lineFileUtilities.h"
|
|
13 #include "slopBed.h"
|
|
14
|
|
15
|
|
16 BedSlop::BedSlop(string &bedFile, string &genomeFile, bool forceStrand, float leftSlop, float rightSlop, bool fractional) {
|
|
17
|
|
18 _bedFile = bedFile;
|
|
19 _genomeFile = genomeFile;
|
|
20 _forceStrand = forceStrand;
|
|
21 _leftSlop = leftSlop;
|
|
22 _rightSlop = rightSlop;
|
|
23 _fractional = fractional;
|
|
24
|
|
25 _bed = new BedFile(bedFile);
|
|
26 _genome = new GenomeFile(genomeFile);
|
|
27
|
|
28 // get going, slop it up.
|
|
29 SlopBed();
|
|
30 }
|
|
31
|
|
32
|
|
33 BedSlop::~BedSlop(void) {
|
|
34
|
|
35 }
|
|
36
|
|
37
|
|
38 void BedSlop::SlopBed() {
|
|
39
|
|
40 int lineNum = 0;
|
|
41 BED bedEntry, nullBed; // used to store the current BED line from the BED file.
|
|
42 BedLineStatus bedStatus;
|
|
43
|
|
44 _bed->Open();
|
|
45 bedStatus = _bed->GetNextBed(bedEntry, lineNum);
|
|
46 while (bedStatus != BED_INVALID) {
|
|
47 if (bedStatus == BED_VALID) {
|
|
48 if (_fractional == false) {
|
|
49 AddSlop(bedEntry, (int) _leftSlop, (int) _rightSlop);
|
|
50 }
|
|
51 else {
|
|
52 int leftSlop = (int) (_leftSlop * bedEntry.size());
|
|
53 int rightSlop = (int) (_rightSlop * bedEntry.size());
|
|
54 AddSlop(bedEntry, leftSlop, rightSlop);
|
|
55 }
|
|
56 _bed->reportBedNewLine(bedEntry);
|
|
57 bedEntry = nullBed;
|
|
58 }
|
|
59 bedStatus = _bed->GetNextBed(bedEntry, lineNum);
|
|
60 }
|
|
61 _bed->Close();
|
|
62 }
|
|
63
|
|
64
|
|
65 void BedSlop::AddSlop(BED &bed, int leftSlop, int rightSlop) {
|
|
66
|
|
67 // special handling if the BED entry is on the negative
|
|
68 // strand and the user cares about strandedness.
|
|
69 CHRPOS chromSize = _genome->getChromSize(bed.chrom);
|
|
70
|
|
71 if ( (_forceStrand) && (bed.strand == "-") ) {
|
|
72 // inspect the start
|
|
73 if ( (static_cast<int>(bed.start) - rightSlop) > 0 ) bed.start -= rightSlop;
|
|
74 else bed.start = 0;
|
|
75
|
|
76 // inspect the start
|
|
77 if ( (static_cast<int>(bed.end) + leftSlop) <= static_cast<int>(chromSize)) bed.end += leftSlop;
|
|
78 else bed.end = chromSize;
|
|
79 }
|
|
80 else {
|
|
81 // inspect the start
|
|
82 if ( (static_cast<int>(bed.start) - leftSlop) > 0) bed.start -= leftSlop;
|
|
83 else bed.start = 0;
|
|
84
|
|
85 // inspect the end
|
|
86 if ( (static_cast<int>(bed.end) + rightSlop) <= static_cast<int>(chromSize)) bed.end += rightSlop;
|
|
87 else bed.end = chromSize;
|
|
88 }
|
|
89 }
|
|
90
|
|
91
|