-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRefGeneTable.cpp
More file actions
110 lines (97 loc) · 3.2 KB
/
CRefGeneTable.cpp
File metadata and controls
110 lines (97 loc) · 3.2 KB
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
/*
* File: CRefGeneTable.cpp
* Author: mwittig
*
* Created on May 22, 2017, 3:41 PM
*/
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <set>
#include "CMyException.h"
#include "MyTools.h"
#include "CTwoBit.h"
#include "CRefGeneEntry.h"
#include "CRefGeneTable.h"
CRefGeneTable::CRefGeneTable(const char* filename)
: m_header_line("#bin\tname\tchrom\tstrand\ttxStart\ttxEnd\tcdsStart\tcdsEnd\texonCount\texonStarts\texonEnds\tscore\tname2\tcdsStartStat\tcdsEndStat\texonFrames")
{
loadData(filename);
}
CRefGeneTable::CRefGeneTable(const CRefGeneTable& orig) : m_header_line(orig.m_header_line)
{
m_entries = orig.m_entries;
}
CRefGeneTable::~CRefGeneTable() {
}
bool CRefGeneTable::loadData(const char* filename)
{
string strLine;
ifstream* pFile = new ifstream(filename);
if(!pFile || !pFile->is_open())
throw CMyException(string("Error reading file in CRefGeneTable!")+"failed to open 2bit file \'" + filename + "\'");
getline(*pFile,strLine);
if(strLine.compare(m_header_line)!=0)
throw CMyException(string("Error reading file in CRefGeneTable! Invalid header. Got:\"")+strLine+ "\", expected: \""+m_header_line+'\"');
size_t expected_cell_count = CMyTools::GetParsedLine(strLine).size();
int line_count = 1;
while(getline(*pFile,strLine))
{
if(strLine.size() == 0) // skip empty lines
continue;
line_count++;
vector<string> parsed = CMyTools::GetParsedLine(strLine);
if(parsed.size() != expected_cell_count)
throw CMyException(string("invalid number of cells in line ")+to_string(line_count)+ " of file \""+filename+'\"');
m_entries.push_back(CRefGeneEntry(parsed));
//cout << m_entries[m_entries.size()-1].name() << endl;
}
pFile->close();
delete pFile;
return true;
}
const bool CRefGeneTable::contains(const std::string& id)const
{
try{
this->operator [](id);
return true;
}
catch(CMyException& err)
{
return false;
}
}
std::string CRefGeneTable::transcriptAt(const std::string& chrom, long pos, long pos2, long flanks)
{
set<string> setRet;
for(vector<CRefGeneEntry>::const_iterator i = m_entries.begin(); i != m_entries.end(); i++)
{
long txStart = i->txStart()-flanks;
long txEnd = i->txEnd()+flanks;
if( i->chrom().compare(chrom) == 0 &&
(
(txStart <= pos && txEnd >= pos) ||
(txStart <= pos2 && txEnd >= pos2) ||
(pos < txStart && pos2 > txEnd)
)
)
setRet.insert(i->name2());
}
ostringstream strRet("");
std::copy(setRet.begin(), setRet.end(), std::ostream_iterator<std::string>(strRet, ","));
return strRet.str().substr(0,strRet.str().size()-1);
}
const CRefGeneEntry& CRefGeneTable::operator[](const std::string& id)const
{
for(vector<CRefGeneEntry>::const_iterator i = m_entries.begin(); i != m_entries.end(); i++)
{
if(i->name().compare(id) == 0 || i->name2().compare(id) == 0)
return *i;
}
throw CMyException("Id not found");
}