windowMaker.cpp 3.54 KB
Newer Older
Aaron's avatar
Aaron committed
1
/*****************************************************************************
Aaron's avatar
Aaron committed
2
windowMaker.cpp
Aaron's avatar
Aaron committed
3

Aaron's avatar
Aaron committed
4
5
6
7
8
(c) 2009 - Aaron Quinlan
Hall Laboratory
Department of Biochemistry and Molecular Genetics
University of Virginia
aaronquinlan@gmail.com
Aaron's avatar
Aaron committed
9

Aaron's avatar
Aaron committed
10
Licenced under the GNU General Public License 2.0 license.
Aaron's avatar
Aaron committed
11
12
13
******************************************************************************/
#include "windowMaker.h"

14
WindowMaker::WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE input_file_type, uint32_t size, uint32_t step)
Aaron's avatar
Aaron committed
15
16
17
18
: _size(size)
, _step(step)
, _count(0)
, _window_method(FIXED_WINDOW_SIZE)
19
, _id_method(id_method)
Aaron's avatar
Aaron committed
20
{
Aaron's avatar
Aaron committed
21
22
23
24
25
26
    if (input_file_type==GENOME_FILE)
        MakeWindowsFromGenome(fileName);
    else
        MakeWindowsFromBED(fileName);
}

27
WindowMaker::WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE input_file_type, uint32_t count)
Aaron's avatar
Aaron committed
28
29
30
31
    : _size(0)
    , _step(0)
    , _count(count)
    , _window_method(FIXED_WINDOW_COUNT)
32
    , _id_method(id_method)
Aaron's avatar
Aaron committed
33
34
35
36
37
{
    if (input_file_type==GENOME_FILE)
        MakeWindowsFromGenome(fileName);
    else
        MakeWindowsFromBED(fileName);
Aaron's avatar
Aaron committed
38
39
40
41
42
}

WindowMaker::~WindowMaker(void) {}


Aaron's avatar
Aaron committed
43
44
45
void WindowMaker::MakeWindowsFromGenome(const string& genomeFileName) {

    GenomeFile *_genome = new GenomeFile(genomeFileName);
Aaron's avatar
Aaron committed
46
47

    // get a list of the chroms in the user's genome
Aaron's avatar
Aaron committed
48
    vector<string> chromList = _genome->getChromList();
Aaron's avatar
Aaron committed
49
50
51
52

    // process each chrom in the genome
    for (size_t c = 0; c < chromList.size(); ++c) {
        string chrom = chromList[c];
Aaron's avatar
Aaron committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66

        BED bed(chrom,0,_genome->getChromSize(chrom));
        MakeBEDWindow(bed);
    }
}

void WindowMaker::MakeWindowsFromBED(string& bedFileName) {
    BedFile bf(bedFileName);
    bf.Open();

    BED bed;
    while (bf.GetNextBed(bed)) {
        if (bf._status == BED_VALID)
            MakeBEDWindow(bed);
Aaron's avatar
Aaron committed
67
    }
Aaron's avatar
Aaron committed
68
    bf.Close();
Aaron's avatar
Aaron committed
69
70
}

Aaron's avatar
Aaron committed
71
72
73
74
75
76
77
78
79
void WindowMaker::MakeBEDWindow(const BED& interval)
{
    if (_window_method==FIXED_WINDOW_SIZE)
        MakeFixedSizeWindow(interval);
    else
        MakeFixedCountWindow(interval);
}

void WindowMaker::MakeFixedSizeWindow(const BED& interval) {
80
81
82
    uint32_t i=1;
    for (uint32_t start = interval.start; start <= interval.end; start += _step, ++i) {
        string name = GenerateID(interval,i);
Aaron's avatar
Aaron committed
83
        if ((start + _size) <= interval.end) {
84
            cout << interval.chrom << "\t" << start << "\t" << start + _size << name << endl;
Aaron's avatar
Aaron committed
85
86
        }
        else if (start < interval.end) {
87
            cout << interval.chrom << "\t" << start << "\t" << interval.end << name << endl;
Aaron's avatar
Aaron committed
88
89
90
91
92
93
94
95
96
97
        }
    }
}

void WindowMaker::MakeFixedCountWindow(const BED& interval) {
    uint32_t interval_size = interval.end - interval.start ;
    uint32_t window_size = (interval_size-1)/_count + 1; // integer version of ceil(interval_size/_count)
    if (window_size==0 || interval_size==0)
        return;

98
    uint32_t i=1;
99
    for (uint32_t start = interval.start; start < interval.end; start += window_size, ++i) {
100
        string name = GenerateID(interval,i);
Aaron's avatar
Aaron committed
101
        uint32_t end = min(start + window_size,interval.end);
102
        cout << interval.chrom << "\t" << start << "\t" << end << name << endl;
Aaron's avatar
Aaron committed
103
    }
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
}

string WindowMaker::GenerateID(const BED& interval, uint32_t window_index) const {
    stringstream s;
    switch(_id_method) {
    case ID_SOURCE_ID:
         s << "\t" << interval.name;
         break;
    case ID_WINDOW_NUMBER:
         s << "\t" << window_index;
         break;
    case ID_SOURCE_ID_WINDOW_NUMBER:
         s << "\t" << interval.name << "_" << window_index;
    default:
    case ID_NONE:
         break;
    }
    return s.str();
}