1 /*
2  * spacingFile.cpp
3  *
4  *  Created on: Apr 30, 2015
5  *      Author: nek3d
6  */
7 #include "spacingFile.h"
8 
SpacingFile(ContextSpacing * context)9 SpacingFile::SpacingFile(ContextSpacing *context)
10 : ToolBase(context),
11   _prevRec(NULL),
12   _currRec(NULL)
13 {
14 
15 }
16 
~SpacingFile()17 SpacingFile::~SpacingFile()
18 {
19 	_inputFile->deleteRecord(_prevRec);
20 	_prevRec = NULL;
21 }
22 
init()23 bool SpacingFile::init()
24 {
25 	//we're only operating on one file, so the idx is zero.
26 	_inputFile =  _context->getFile(0);
27 	return true;
28 }
29 
30 
findNext(RecordKeyVector & hits)31 bool SpacingFile::findNext(RecordKeyVector &hits)
32 {
33 	while (!_inputFile->eof()) {
34 
35 		_currRec = _inputFile->getNextRecord();
36 
37 		// no more records
38 		if (_currRec == NULL) {
39 			continue;
40 		}
41 		// first record in file.
42 		if (_prevRec == NULL) {
43 			_distance.append(".");
44 		}
45 		// the meat of the file
46 		else {
47 			// _currRecent and _prevRecious records are on the same chromosome.
48 			if (_currRec->getChrName() == _prevRec->getChrName())
49 			{
50 				// do _currRec and _prevRec overlap?
51 				if (_currRec->sameChromIntersects(_prevRec, false, false, 1E-9, 1E-9, false, false, false))
52 					_distance.append("-1");
53 				else
54 				{
55 					CHRPOS distance = _currRec->getStartPos() - _prevRec->getEndPos();
56 					ostringstream s;
57 					s << distance;
58 					_distance.append(s.str());
59 				}
60 			}
61 			// we have changed chromosomes
62 			else if (_currRec->getChrName() != _prevRec->getChrName())
63 			{
64 				_distance.append(".");
65 			}
66 		}
67 		hits.setKey(_currRec);
68 		return true;
69 	}
70 	return false;
71 }
72 
processHits(RecordOutputMgr * outputMgr,RecordKeyVector & hits)73 void SpacingFile::processHits(RecordOutputMgr *outputMgr, RecordKeyVector &hits)
74 {
75 	outputMgr->printRecord(hits.getKey(), _distance);
76 }
77 
cleanupHits(RecordKeyVector & hits)78 void SpacingFile::cleanupHits(RecordKeyVector &hits)
79 {
80 	_inputFile->deleteRecord(_prevRec);
81 	_prevRec = _currRec;
82 	_distance.clear();
83 	hits.clearAll();
84 }
85