forked from applevir/STEAK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputfile.cpp
More file actions
242 lines (217 loc) · 6.28 KB
/
inputfile.cpp
File metadata and controls
242 lines (217 loc) · 6.28 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// STEAK Specific Transposable Element Aligner (HERV-K)
// Copyright (C) 2015 Cindy Santander, Philippe Gambron
// Distributed under the GNU General Public License version 3 (GPLv3.txt or https://www.gnu.org/licenses/gpl-3.0.html)
#include "inputfile.hpp"
#include "parallelenvironment.hpp"
#include <math.h>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include "data.hpp"
class file_exception: public std::exception{
virtual const char* what() const throw(){
return "Unable to open file";
}
} file_exception;
fasta_file::fasta_file(const std::string filename, bool sequential){
unsigned long long size;
in.open(filename, std::ios::in);
if(!in.is_open()){
throw file_exception;
}
int pos=filename.rfind(".");
if(parallel_environment::get_process_num()==0 && (pos==std::string::npos || pos==filename.length()-1 || !(filename.substr(pos+1)=="fasta" || filename.substr(pos+1)=="fa" || filename.substr(pos+1)=="fastq" || filename.substr(pos+1)=="fq"))){
std::cout << "The TE reference file must be a fasta file.\n";
}
in.seekg(0, in.end);
size=in.tellg();
int num_processes, process_num;
if(sequential){
num_processes=1;
process_num=0;
}else{
num_processes=parallel_environment::get_num_processes();
process_num=parallel_environment::get_process_num();
}
local_size=ceil(((double)size)/num_processes);
local_beginning=local_size*process_num;
local_end=local_beginning+local_size;
in.seekg(local_beginning);
local_position=local_beginning;
}
fasta_file::~fasta_file(){
in.close();
}
bool fasta_file::get_read(Read& read){
bool keep=false, within_sequence;
std::string line;
std::string sequence="", name;
while((local_position<local_end || keep) && std::getline(in, line)){
boost::trim(line);
if(keep && line.length()>0 && (line.substr(0,1)==">" || line.substr(0,1)=="@")){
in.seekg(local_position);
read.set(name, sequence);
return true;
}
if(line.length()>0 && line.substr(0,1)=="+"){
within_sequence=false;
}
if(keep && within_sequence){
sequence=sequence+line;
}
if(!keep && line.length()>0 && (line.substr(0,1)==">" || line.substr(0,1)=="@")){
keep=true;
within_sequence=true;
name=line.substr(1);
boost::trim(name);
}
local_position=in.tellg();
}
if(in.eof() && keep){
read.set(name, sequence);
return true;
}
return false;
}
fastq_file::fastq_file(const std::string& filename){
unsigned long long size;
in.open(filename, std::ios::in);
if(!in.is_open()){
throw file_exception;
}
int pos=filename.rfind(".");
if(parallel_environment::get_process_num()==0 && (pos==std::string::npos || pos==filename.length()-1 || !(filename.substr(pos+1)=="fastq" || filename.substr(pos+1)=="fq"))){
std::cout << "The input file must be a fastq file.\n";
}
in.seekg(0, in.end);
size=in.tellg();
local_size=ceil(((double)size)/parallel_environment::get_num_processes());
local_beginning=local_size*parallel_environment::get_process_num();
local_end=local_beginning+local_size;
in.seekg(local_beginning);
local_position=local_beginning;
std::string line;
}
fastq_file::~fastq_file(){
in.close();
}
bool fastq_file::get_start_position(bool recursive){
if(parallel_environment::get_process_num()>0){
std::string line;
std::vector<unsigned long long> positions;
std::vector<std::string> lines;
do{
positions.push_back(in.tellg());
if(!std::getline(in, line)){
return false;
}
}while(line!="+");
if(positions.size()>3){
in.seekg(positions[positions.size()-3]);
}else{
if(recursive){
get_start_position(false);
}
}
}
return true;
}
bool fastq_file::get_pair_beginning(){
unsigned long long pos1, pos2;
Read read1, read2;
pos1=in.tellg();
if(!get_read(read1)){
return false;
}
pos2=in.tellg();
if(!get_read(read2)){
return false;
}
if(read1.name.substr(0, read1.name.rfind("/"))==read2.name.substr(0, read2.name.rfind("/"))){
in.seekg(pos1);
}else{
in.seekg(pos2);
}
return true;
}
bool fastq_file::get_read(Read& read, bool second_read){
if(local_position>=local_end+1 &!second_read){
return false;
}
int i_line=0;
std::string name, sequence, quality, line;
while(std::getline(in, line)){
switch(i_line){
case 0:
name=line.substr(1);
break;
case 1:
sequence=line;
break;
case 3:
quality=line;
read.set(name, sequence, quality);
local_position=in.tellg();
return true;
default:
break;
}
++i_line;
}
return false;
}
sam_file::sam_file(const std::string& filename){
unsigned long long size;
in.open(filename, std::ios::in);
if(!in.is_open()){
throw file_exception;
}
int pos=filename.rfind(".");
if(parallel_environment::get_process_num()==0 && (pos==std::string::npos || pos==filename.length()-1 || !(filename.substr(pos+1)=="sam"))){
std::cout << "The input file must be a SAM file. BAM files can be fed through samtools view and a pipe\n";
}
in.seekg(0, in.end);
size=in.tellg();
local_size=ceil(((double)size)/parallel_environment::get_num_processes());
local_beginning=local_size*parallel_environment::get_process_num();
local_end=local_beginning+local_size;
in.seekg(local_beginning);
local_position=local_beginning;
std::string line;
if(parallel_environment::get_process_num()>0){
getline(in, line);
}else{
unsigned long long current_position;
do{
current_position=in.tellg();
getline(in, line);
}while(line.substr(0, 1)=="@");
in.seekg(current_position);
}
}
sam_file::~sam_file(){
in.close();
}
bool sam_file::get_read(Read& read){
std::string line;
if(!std::getline(in, line) || local_position>=local_end){
return false;
}
SAMData samData;
samData.set(line);
read.set(samData);
local_position=in.tellg(); //local_position+line.length()+1;
return true;
}
bam_pipe::bam_pipe(){
input=&std::cin;
}
int bam_pipe::get_read(Read& read){
std::string line;
if(!std::getline(*input, line)){
return false;
}
SAMData samData;
samData.set(line);
read.set(samData);
return true;
}