-
Notifications
You must be signed in to change notification settings - Fork 0
/
genehuman.cpp
179 lines (156 loc) · 4.2 KB
/
genehuman.cpp
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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// genehuman.cpp
// APOBECXP
//
// Created by Marat Kazanov on 09/12/2018.
// Copyright © 2018 Marat Kazanov. All rights reserved.
//
#include "genehuman.hpp"
#include <fstream>
#include "service.h"
#include "ghuman.hpp"
#include <iostream>
CHumanGene::CHumanGene(string startpos_, string endpos_, string strand_, string info_)
{
size_t pos1,pos2;
startpos = str2ul(startpos_);
endpos = str2ul(endpos_);
strand = strand_[0];
pos1 = info_.find("GeneID:");
if(pos1 != string::npos)
{
pos2 = info_.find(";",pos1);
geneID = str2ul(info_.substr(pos1+7, pos2-pos1-7));
}
pos1 = info_.find("gene=");
if(pos1 != string::npos)
{
pos2 = info_.find(";",pos1);
geneName = info_.substr(pos1+5, pos2-pos1-5);
}
}
CHumanGene::CHumanGene(unsigned long pos_)
{
startpos = pos_;
}
CHumanGene::CHumanGene(unsigned long startpos_, unsigned long endpos_)
{
startpos = startpos_;
endpos = endpos_;
}
CHumanGenes::CHumanGenes()
{
genes = new set<CHumanGene>[24];
genebits = new bitset<250000000>[24];
}
void CHumanGenes::LoadGenes(string path)
{
cout << "Loading genes" << '\n';
string line;
int chrNum;
ifstream f(path.c_str());
if (!f.is_open())
{
printf("File not exists\n");
return;
}
vector<string> flds;
while(getline(f, line))
{
if (line.length() != 0)
{
if(line[0] == '#')
continue;
flds = split(line);
if(flds[2] != "gene")
continue;
chrNum = CHumanGenome::GetChrFromNCBIName(flds[0]);
if(chrNum < 0 || chrNum > 23)
continue;
genes[chrNum].insert(CHumanGene(flds[3],flds[4],flds[6],flds[8]));
}
}
f.close();
}
void CHumanGenes::PrepareForSearch()
{
set<CHumanGene>::iterator it;
unsigned long maxend;
int chrNum;
// set maxpos
for(chrNum=0;chrNum<24;chrNum++)
{
maxend = 0;
for(it=genes[chrNum].begin();it!=genes[chrNum].end();++it)
{
(*it).maxendpos = (maxend < (*it).startpos) ? 0 : maxend;
maxend = (maxend > (*it).endpos) ? maxend : (*it).endpos;
}
}
// set bits
unsigned long i;
for(chrNum=0;chrNum<24;chrNum++)
{
for(it=genes[chrNum].begin();it!=genes[chrNum].end();++it)
{
for(i=it->startpos;i<=it->endpos;i++)
genebits[chrNum][i-1] = 1;
}
}
}
int CHumanGenes::GetGenesByPos(int chrNum, unsigned long pos, vector<CHumanGene>& geneList)
{
set<CHumanGene>::iterator it;
CHumanGene g(pos);
if(genebits[chrNum][pos-1] != 1)
return(0);
it = genes[chrNum].upper_bound(g);
if(it == genes[chrNum].begin())
return(0);
it--;
while(1)
{
if((*it).startpos <= pos && (*it).endpos >= pos)
geneList.push_back((*it));
if(pos > (*it).maxendpos)
break;
it--;
}
if(geneList.empty())
return(0);
else
return(1);
}
/*void CHumanGenes::MergeIntervals(vector<CHumanGene>& ret)
{
set<CHumanGene>::iterator si;
CHumanGene prevGene(-1,0);
unsigned long startpos=-1,endpos=-1;
for(si=genes.begin();si!=genes.end();si++)
{
if((*si).maxendpos == 0 || prevGene.chrNum != (*si).chrNum || prevGene.isNull())
{
if(!prevGene.isNull())
ret.push_back(CHumanGene(prevGene.chrNum,startpos,endpos));
startpos = (*si).startpos;
endpos = (*si).endpos;
}
else
{
endpos = ((*si).endpos > endpos ? (*si).endpos : endpos);
}
prevGene = (*si);
}
ret.push_back(CHumanGene(prevGene.chrNum,startpos,endpos));
}*/
void CHumanGenes::SaveToFile(string path)
{
ofstream f;
f.open(path.c_str());
set<CHumanGene>::iterator it;
int chrNum;
for(chrNum=0;chrNum<24;chrNum++)
for(it=genes[chrNum].begin();it!=genes[chrNum].end();++it)
f << chrNum << '\t' << (*it).startpos << '\t' << (*it).endpos << '\t' << (*it).maxendpos << '\t' << (*it).strand << '\t' << (*it).geneID << '\t' << (*it).geneName << '\n';
f.close();
}