-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.cpp
182 lines (157 loc) · 4.3 KB
/
class.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
180
181
182
#include "class.hpp"
namespace PEAK{
/**************************************************************/
/* NAME : Class Peak constructor */
/* DESCRIPTION : */
/**************************************************************/
Peak::Peak(const unsigned int lower, const unsigned int upper, const unsigned int th,const char* input,const char* output, const char* Chr) {
this->lower = lower;
this->upper = upper;
this->dim = upper - lower+1;
this->th = th;
try
{
count = new unsigned int [dim];
}
catch (...)
{
cerr << "Error: It is not possible to allocate "<<dim*4<<" Bites of memory"<<endl;
throw;
}
for (unsigned int i=0; i<dim; i++)
count[i]=0;
inputName =string(input);
outputName=string(output);
this->Chr=string(Chr);
}
/**************************************************************/
/* NAME : Class Peak destructor */
/* DESCRIPTION : */
/**************************************************************/
Peak::~Peak(){
if (count!=NULL) {
delete[] count;
}
}
/**************************************************************/
/* NAME : Class Peak Print */
/* DESCRIPTION : it prints the class values*/
/**************************************************************/
void Peak::Print()const {
cout<<"Chr.: "<< Chr <<endl;
cout<<"Lower: "<< lower <<endl;
cout<<"Upper: "<< upper <<endl;
cout<<"Input file: "<< inputName<<endl;
cout<<"Output file: "<< outputName<<endl;
for (unsigned int i=0; i<dim; i++)
if (count[i]!=0)
cout<<"count["<<i+lower<<"] = "<<count[i]<< endl;
}
/**************************************************************/
/* NAME : Class Peak Read */
/* DESCRIPTION : it reads the input file*/
/**************************************************************/
void Peak::Read(){
ifstream in;
char buffer[MAXSIZE];
Parser parser;
char delim[] = "\t ";
in.open(inputName.c_str(),ifstream::in);
if(!in)
{
cerr << "\n*****Error opening input file "<<inputName<<" *****" << endl;
throw;
}
unsigned int npeaks=0;
while (!in.eof())
{
buffer[0]='\0';
in.getline(buffer,MAXSIZE);
npeaks++;
if (npeaks%100000==0)
{
cout<<"Processed peaks: "<<npeaks<<endl;
}
#if DEBUG
cout<<buffer<<endl;
#endif
parser.update(delim,buffer);
if ((parser.size()==3) && (parser.get(0)==Chr))
{
if (atoi(parser.get(1).c_str())>atoi(parser.get(2).c_str()))
{
cout<<"\nError in peak: "<< npeaks<< "\n\n";
}
else
{
unsigned int l,u;
if ((unsigned int) atoi(parser.get(1).c_str())<lower)
l=lower;
else
l=(unsigned int)atoi(parser.get(1).c_str());
if ((unsigned int)atoi(parser.get(2).c_str()) > upper)
u=upper;
else
u=(unsigned int)atoi(parser.get(2).c_str());
for (unsigned int i=l;i<=u && i>=lower && i<= upper; i++)
{
count[i-lower]++;
}
}
}
}
}
/**************************************************************/
/* NAME : Class Peak CheckPeak */
/* DESCRIPTION : it identifies the peak according to the input threshold*/
/**************************************************************/
void Peak::CheckPeak(){
unsigned int l=0;
bool inPeak=false;
ofstream out;
out.open(outputName.c_str(),ofstream::out);
if(!out)
{
cerr << "\n*****Error opening output file "<<outputName<<" *****" << endl;
throw;
}
for (unsigned int i=0; i<dim; i++)
{
if ((!inPeak)&&(count[i]>=th))
{
inPeak=true;
l=i;
}
else
if ((count[i]<th) && (inPeak))
{
inPeak=false;
printPeak(l,i-1,out);
}
}
if (inPeak)
printPeak(l,dim-1,out);
}
/**************************************************************/
/* NAME : Class Peak printPeak */
/* DESCRIPTION : it prints the peak position*/
/**************************************************************/
void Peak::printPeak(const unsigned int l, const unsigned int u, ofstream& out ){
#if DEBUG
cout<<Chr<<"\t"<<l+lower<<"\t"<<u+lower<<endl;
#endif
out<<Chr<<"\t"<<l+lower<<"\t"<<u+lower<<endl;
if (DIST){
for (unsigned int i=l; i<=u; i++){
#if DEBUG
cout << count[i] << " ";
#endif
out << count[i] << " ";
}
#if DEBUG
cout<<endl;
#endif
out<<endl;
}
}
}