NewChromsweep.cpp 6.19 KB
Newer Older
nkindlon's avatar
nkindlon committed
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
/*****************************************************************************
  NewChromsweep.cpp

  (c) 2009 - Aaron Quinlan
  Hall Laboratory
  Department of Biochemistry and Molecular Genetics
  University of Virginia
  aaronquinlan@gmail.com

  Licenced under the GNU General Public License 2.0 license.
******************************************************************************/

#include "NewChromsweep.h"
#include "Context.h"
#include "FileRecordMgr.h"

NewChromSweep::NewChromSweep(Context *context,
                       bool useMergedIntervals)
:	_context(context),
 	_queryFRM(NULL),
 	_databaseFRM(NULL),
 	_useMergedIntervals(false),
 	_queryRecordsTotalLength(0),
 	_databaseRecordsTotalLength(0),
 	_wasInitialized(false),
 	_currQueryRec(NULL),
27
28
 	_currDatabaseRec(NULL),
 	_runToQueryEnd(false)
nkindlon's avatar
nkindlon committed
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

{
}


bool NewChromSweep::init() {
    
	//Create new FileRecordMgrs for the two input files.
	//Open them, and get the first record from each.
	//if any of that goes wrong, return false;
	//otherwise, return true.
    _queryFRM = new FileRecordMgr(_context->getQueryFileIdx(), _context);
    _databaseFRM = new FileRecordMgr(_context->getDatabaseFileIdx(), _context);
    
    if (!_queryFRM->open()) {
    	return false;
    }
    if (!_databaseFRM->open()) {
    	return false;
    }

    _context->determineOutputType();

    nextRecord(false);
    if (_currDatabaseRec == NULL) {
    	return false;
    }
56
57
58
59
60
61
62

    //determine whether to stop when the database end is hit, or keep going until the
    //end of the query file is hit as well.

    if (_context->getNoHit() || _context->getWriteCount() || _context->getWriteOverlap() || _context->getWriteAllOverlap() || _context->getLeftJoin()) {
    	_runToQueryEnd = true;
    }
nkindlon's avatar
nkindlon committed
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
157
158
159
160
161
162
163
164
165
    _wasInitialized = true;
    return true;
 }

void NewChromSweep::closeOut() {
	while (!_queryFRM->eof()) {
		nextRecord(true);
	}
	while (!_databaseFRM->eof()) {
		nextRecord(false);
	}
}

NewChromSweep::~NewChromSweep(void) {
	if (!_wasInitialized) {
		return;
	}
	if (_currQueryRec != NULL) {
		_queryFRM->deleteRecord(_currQueryRec);
	}
	if (_currDatabaseRec != NULL) {
		_databaseFRM->deleteRecord(_currDatabaseRec);
	}
	_queryFRM->close();
	_databaseFRM->close();

	delete _queryFRM;
	delete _databaseFRM;

}


void NewChromSweep::scanCache() {
	recListIterType cacheIter = _cache.begin();
    while (cacheIter != _cache.end())
    {
    	const Record *cacheRec = cacheIter->value();
        if (_currQueryRec->sameChrom(cacheRec) && !(_currQueryRec->after(cacheRec))) {
            if (intersects(_currQueryRec, cacheRec)) {
                _hits.push_back(cacheRec);
            }
            cacheIter = _cache.next();
        }
        else {
            cacheIter = _cache.deleteCurrent();
    		_databaseFRM->deleteRecord(cacheRec);
        }
    }
}

void NewChromSweep::clearCache()
{
	//delete all objects pointed to by cache
	for (recListIterType iter = _cache.begin(); iter != _cache.end(); iter = _cache.next()) {
		_databaseFRM->deleteRecord(iter->value());
	}
	_cache.clear();
}

bool NewChromSweep::chromChange()
{
    // the files are on the same chrom
	if (_currDatabaseRec == NULL || _currQueryRec->sameChrom(_currDatabaseRec)) {
		return false;
	}
	// the query is ahead of the database. fast-forward the database to catch-up.
	if (_currQueryRec->chromAfter(_currDatabaseRec)) {

		while (_currDatabaseRec != NULL &&
				_currQueryRec->chromAfter(_currDatabaseRec)) {
			_databaseFRM->deleteRecord(_currDatabaseRec);
			nextRecord(false);
		}
		clearCache();
        return false;
    }
    // the database is ahead of the query.
    else {
        // 1. scan the cache for remaining hits on the query's current chrom.
        if (_currQueryRec->getChrName() == _currChromName)
        {
            scanCache();
        }
        // 2. fast-forward until we catch up and report 0 hits until we do.
        else if (_currQueryRec->chromBefore(_currDatabaseRec))
        {
            clearCache();
        }

        return true;
    }
}


bool NewChromSweep::next(RecordKeyList &next) {
	if (_currQueryRec != NULL) {
		_queryFRM->deleteRecord(_currQueryRec);
	}
	nextRecord(true);
	if (_currQueryRec == NULL) { //eof hit!
		return false;
	}

166
	if (_currDatabaseRec == NULL && _cache.empty() && !_runToQueryEnd) {
nkindlon's avatar
nkindlon committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
		return false;
	}
	_hits.clear();
	_currChromName = _currQueryRec->getChrName();
	// have we changed chromosomes?
	if (!chromChange()) {
		// scan the database cache for hits
		scanCache();
		//skip if we hit the end of the DB
		// advance the db until we are ahead of the query. update hits and cache as necessary
		while (_currDatabaseRec != NULL &&
				_currQueryRec->sameChrom(_currDatabaseRec) &&
				!(_currDatabaseRec->after(_currQueryRec))) {
			if (intersects(_currQueryRec, _currDatabaseRec)) {
				_hits.push_back(_currDatabaseRec);
			}
			if (_currQueryRec->after(_currDatabaseRec)) {
				_databaseFRM->deleteRecord(_currDatabaseRec);
				_currDatabaseRec = NULL;
			} else {
				_cache.push_back(_currDatabaseRec);
				_currDatabaseRec = NULL;
			}
			nextRecord(false);
		}
	}
	next.setKey(_currQueryRec);
	next.setListNoCopy(_hits);
	return true;
}

void NewChromSweep::nextRecord(bool query) {
	if (query) {
200
//		if (!_context->getUseMergedIntervals()) {
nkindlon's avatar
nkindlon committed
201
			_currQueryRec = _queryFRM->allocateAndGetNextRecord();
202
203
204
//		} else {
//			_currQueryRec = _queryFRM->allocateAndGetNextMergedRecord(_context->getSameStrand() ? FileRecordMgr::SAME_STRAND_EITHER : FileRecordMgr::ANY_STRAND);
//		}
nkindlon's avatar
nkindlon committed
205
206
207
208
		if (_currQueryRec != NULL) {
			_queryRecordsTotalLength += (unsigned long)(_currQueryRec->getEndPos() - _currQueryRec->getStartPos());
		}
	} else { //database
209
//		if (!_context->getUseMergedIntervals()) {
nkindlon's avatar
nkindlon committed
210
			_currDatabaseRec = _databaseFRM->allocateAndGetNextRecord();
211
212
213
//		} else {
//			_currDatabaseRec = _databaseFRM->allocateAndGetNextMergedRecord(_context->getSameStrand() ? FileRecordMgr::SAME_STRAND_EITHER : FileRecordMgr::ANY_STRAND);
//		}
nkindlon's avatar
nkindlon committed
214
215
216
217
218
219
220
221
222
223
224
		if (_currDatabaseRec != NULL) {
			_databaseRecordsTotalLength += (unsigned long)(_currDatabaseRec->getEndPos() - _currDatabaseRec->getStartPos());
		}
	}
}

bool NewChromSweep::intersects(const Record *rec1, const Record *rec2) const
{
	return rec1->sameChromIntersects(rec2, _context->getSameStrand(), _context->getDiffStrand(),
			_context->getOverlapFraction(), _context->getReciprocal());
}