-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRefReader_istr.cpp
218 lines (179 loc) · 6.1 KB
/
RefReader_istr.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Metal - A fast methylation alignment and calling tool for WGBS data.
// Copyright (C) 2017 Jonas Fischer
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Jonas Fischer [email protected]
#include <fstream>
#include <algorithm>
#include "RefReader_istr.h"
void readReference(const std::string& filename, std::vector<struct CpG>& cpgTab, std::vector<struct CpG>& cpgStartTab, std::vector<std::vector<char> >& genSeq, std::unordered_map<uint8_t, std::string>& chrMap, const bool humanOptFlag)
{
std::string line;
// stores the sequence of each chromosome
std::vector<char> seq;
genSeq.reserve(MyConst::CHROMNUM);
seq.reserve(MyConst::CHROMMAX);
cpgTab.reserve(MyConst::CPGMAX);
// stores the chromosome index we are currently reading
uint8_t chrIndex = 0;
// flag stating if we currently read a real chromosome assembly
bool contFlag = false;
// flag stating if the last character read in the previous line was a c
bool lastC = false;
std::ifstream ifs (filename);
if(!ifs)
{
std::cerr << "Opening genome reference file " << filename << " was unsuccessful! Exiting..." << std::endl;
exit(1);
}
std::cout << "Start reading reference file " << filename << std::endl;
uint32_t unIDCount = 1;
while (getline(ifs, line)) {
// Test for id tag line
if (line.begin() != line.end() && *(line.begin()) == '>' && (line.begin() + 1) != line.end())
{
// if we read primary assembly previously, write it to vectors
if (contFlag)
{
seq.shrink_to_fit();
genSeq.emplace_back(move(seq));
// reset buffer
seq = std::vector<char>();
seq.reserve(MyConst::CHROMMAX);
// reset flag for last c
lastC = false;
}
if (humanOptFlag)
{
// check if primary assembly
// (GRCH versions)
if (*(line.begin() + 1) == 'C' || (*(line.begin() + 1) == 'N' && *(line.begin() + 2) == 'C'))
{
++chrIndex;
std::string chrHeader (line.begin() + 1, line.end());
uint32_t pos = chrHeader.find_first_of(" \t");
std::string chrID(chrHeader, 0, pos);
for (const auto chrP : chrMap)
{
if (chrP.second == chrID)
{
std::cout << "WARNING: Chromosome identifier " << chrID << " found in header\n" <<
chrHeader << "\nis not unique.";
chrID.append("_");
chrID.append(std::to_string(unIDCount++));
std::cout << "Renaming to " << chrID << "\n";
break;
}
}
chrMap.insert(std::pair<uint8_t,std::string>(chrIndex - 1, chrID));
contFlag = true;
continue;
// throw out unlocalized contigs
// (hg versions)
} else if ( *(line.begin() + 1) == 'c')
{
++chrIndex;
std::string chrHeader (line.begin() + 1, line.end());
uint32_t pos = chrHeader.find_first_of(" \t");
std::string chrID(chrHeader, 0, pos);
for (const auto chrP : chrMap)
{
if (chrP.second == chrID)
{
std::cout << "WARNING: Chromosome identifier " << chrID << " found in header\n" <<
chrHeader << "\nis not unique.";
chrID.append("_");
chrID.append(std::to_string(unIDCount++));
std::cout << "Renaming to " << chrID << "\n";
break;
}
}
// test if real primary assembly sequence
if (!isPrimaryHG(chrID))
{
--chrIndex;
contFlag = false;
continue;
}
chrMap.insert(std::pair<uint8_t,std::string>(chrIndex - 1, chrID));
contFlag = true;
continue;
} else {
contFlag = false;
}
} else {
++chrIndex;
if (chrIndex > MyConst::CHROMNUM)
{
std::cout << "\nWARNING: Number of read chromosomes and number of specified chromosomes the organism should have do not match!\n"
<< "Maybe the reference genome contains unlocalized contigs. You should consider removing them from the reference fasta.\n"
<< "For the human reference genome GRCH and HG versions, use \"--human_opt\" to mitigate this problem.\n";
}
std::string chrHeader (line.begin() + 1, line.end());
uint32_t pos = chrHeader.find_first_of(" \t");
std::string chrID(chrHeader, 0, pos);
for (const auto chrP : chrMap)
{
if (chrP.second == chrID)
{
std::cout << "WARNING: Chromosome identifier " << chrID << " found in header\n" <<
chrHeader << "\nis not unique.";
chrID.append("_");
chrID.append(std::to_string(unIDCount++));
std::cout << "Renaming to " << chrID << "\n";
break;
}
}
std::cout << chrHeader << "\n" << chrID << "\n\n";
chrMap.insert(std::pair<uint8_t,std::string>(chrIndex - 1, chrID));
contFlag = true;
continue;
}
}
// check if we are in real primary chromosome assembly
if (contFlag)
{
// parse the line
readLine(line, lastC, chrIndex, cpgTab, cpgStartTab, seq);
} else {
continue;
}
}
// if we read primary assembly previously, write it to vectors
if (contFlag)
{
seq.shrink_to_fit();
genSeq.emplace_back(move(seq));
}
cpgTab.shrink_to_fit();
genSeq.shrink_to_fit();
ifs.close();
std::cout << "Done reading reference file" << std::endl;
}
bool isPrimaryHG(std::string chrID)
{
for (unsigned int i = 1; i <= 22; ++i)
{
if (chrID == ("chr" + std::to_string(i)))
{
return true;
}
}
if (chrID == "chrX")
return true;
if (chrID == "chrY")
return true;
return false;
}