-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpopins_utils.h
370 lines (304 loc) · 9.69 KB
/
popins_utils.h
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#ifndef POPINS_UILS_H_
#define POPINS_UILS_H_
#include <seqan/bam_io.h>
#include <seqan/seq_io.h>
using namespace seqan;
// ==========================================================================
// Struct SampleInfo
// ==========================================================================
struct SampleInfo
{
CharString sample_id;
CharString bam_file;
double avg_cov;
unsigned read_len;
CharString adapter_type;
SampleInfo() {}
};
// ==========================================================================
// Function initSampleInfo()
// ==========================================================================
SampleInfo
initSampleInfo(CharString & filename, CharString sample_id, CharString & adapter_type)
{
SampleInfo info;
info.bam_file = filename;
info.sample_id = sample_id;
BamFileIn bamFile(toCString(filename));
BamHeader header;
readHeader(header, bamFile);
BamAlignmentRecord record;
readRecord(record, bamFile);
info.read_len = length(record.seq);
info.adapter_type = adapter_type;
return info;
}
// ==========================================================================
// Function readSampleInfo()
// ==========================================================================
bool
readSampleInfo(SampleInfo & info, CharString & filename)
{
std::ifstream stream(toCString(filename));
if (!stream.good())
{
std::cerr << "ERROR: Could not open sample info file \'" << filename << "\' for reading." << std::endl;
return 1;
}
std::string field, value;
while (stream >> field >> value)
{
if (field.compare("SAMPLE_ID") == 0)
info.sample_id = value;
else if (field.compare("BAM_FILE") == 0)
info.bam_file = value;
else if (field.compare("AVG_COV") == 0)
info.avg_cov = lexicalCast<double>(value);
else if (field.compare("READ_LEN") == 0)
lexicalCast<unsigned>(info.read_len, value);
else if (field.compare("ADAPTER_TYPE") == 0)
info.adapter_type = value;
else
std::cerr << "WARNING: Ignoring field \'" << field << "\' in sample info file \'" << filename << "\'." << std::endl;
}
return 0;
}
// ==========================================================================
// Function write()
// ==========================================================================
bool
writeSampleInfo(SampleInfo & info, CharString & filename)
{
// open the fileDate
std::ofstream stream(toCString(filename));
if (!stream.good())
{
std::cerr << "ERROR: Could not open sample info file \'" << filename << "\' for writing." << std::endl;
return 1;
}
stream << "SAMPLE_ID" << "\t" << info.sample_id << "\n";
stream << "BAM_FILE" << "\t" << info.bam_file << "\n";
stream << "AVG_COV" << "\t" << info.avg_cov << "\n";
stream << "READ_LEN" << "\t" << info.read_len << "\n";
stream << "ADAPTER_TYPE" << "\t" << info.adapter_type << "\n";
stream.close();
return 0;
}
// ==========================================================================
void printStatus(const char * message)
{
// Get the current date and time.
char timestamp[80];
time_t now = time(0);
struct tm tstruct;
tstruct = *localtime(&now);
strftime(timestamp, sizeof(timestamp), "[PopIns %Y-%m-%d %X] ", &tstruct);
// Print time and message.
std::cerr << timestamp << message << std::endl;
}
void printStatus(std::ostringstream & message)
{
std::string msg = message.str();
printStatus(toCString(msg));
}
// ==========================================================================
// Returns true if file exists, otherwise false.
inline bool exists(CharString const & filename)
{
struct stat buffer;
return (stat(toCString(filename), &buffer) == 0);
}
// ==========================================================================
// Lists all files <prefix>/*/<filename>.
String<Pair<CharString> >
listFiles(CharString & prefix, CharString & filename)
{
String<Pair<CharString> > paths;
DIR *dir = opendir(toCString(prefix));
struct dirent *entry = readdir(dir);
while (entry != NULL)
{
if (entry->d_type == DT_DIR)
{
CharString sampleID = entry->d_name;
if (sampleID == "." || sampleID == "..")
{
entry = readdir(dir);
continue;
}
std::stringstream path;
path << prefix << "/" << sampleID << "/" << filename;
CharString pathStr = path.str();
if (exists(pathStr))
appendValue(paths, Pair<CharString>(sampleID, pathStr));
else
std::cerr << "WARNING: \'" << pathStr << "\' does not exist." << std::endl;
}
entry = readdir(dir);
}
closedir(dir);
return paths;
}
// ==========================================================================
// List all directories <prefix>/*/ and return only the basename.
String<CharString>
listSubdirectories(CharString & prefix)
{
String<CharString> subdirs;
DIR *dir = opendir(toCString(prefix));
struct dirent *entry = readdir(dir);
while (entry != NULL)
{
if (entry->d_type == DT_DIR)
{
CharString sampleID = entry->d_name;
if (sampleID == "." || sampleID == "..")
{
entry = readdir(dir);
continue;
}
appendValue(subdirs, sampleID);
}
entry = readdir(dir);
}
closedir(dir);
return subdirs;
}
// ==========================================================================
inline CharString
getFileName(CharString const & path, CharString const & name)
{
CharString filename = path;
filename += "/";
filename += name;
return filename;
}
// ==========================================================================
inline void
removeFile(CharString const & path, const char * filename)
{
CharString file = path;
file += "/";
file += filename;
remove(toCString(file));
}
// ==========================================================================
// Function checkFileEnding()
// ==========================================================================
bool
checkFileEnding(CharString & filename, std::string ending)
{
std::string name = toCString(filename);
size_t dotPos = name.find_last_of('.');
if (dotPos == std::string::npos)
return false;
return name.substr(dotPos + 1, 3) == ending;
}
// ==========================================================================
// Function readFileNames()
// ==========================================================================
bool
readFileNames(String<CharString> & files, CharString & filenameFile)
{
if (filenameFile == "") return 0;
std::fstream stream(toCString(filenameFile), std::fstream::in);
if (!stream.is_open())
{
std::cerr << "ERROR: Could not open file listing files " << filenameFile << std::endl;
return 1;
}
std::string file;
while (stream >> file)
appendValue(files, CharString(file));
return 0;
}
// ==========================================================================
template <typename TValue>
bool readFileNames(String<CharString> & files, String<TValue> & values)
{
if (length(files) > 1) return 0;
std::cerr << "ReadFileNames " << length(files) << " " << files << std::endl;
// Open input file
CharString filenameFile = files[0];
std::cerr << filenameFile << " " << files << std::endl;
std::fstream stream(toCString(filenameFile), std::fstream::in);
if (!stream.is_open())
{
std::cerr << "ERROR: Could not open file listing files " << filenameFile << std::endl;
return 1;
}
clear(files);
clear(values);
std::string file;
TValue val;
while (stream >> file >> val)
{
appendValue(files, file);
appendValue(values, val);
}
return 0;
}
// ==========================================================================
bool
parseInterval(Triple<CharString, unsigned, unsigned> & out, CharString & in)
{
Iterator<CharString, Rooted>::Type it = begin(in, Rooted());
unsigned colonPos = 0;
while (it != end(in))
{
if (*it == ':')
{
colonPos = position(it);
break;
}
++it;
}
if (colonPos == 0)
{
out.i1 = in;
out.i2 = 0;
out.i3 = maxValue<unsigned>();
return 0;
}
unsigned dashPos = 0;
while (it != end(in))
{
if (*it == '-')
{
dashPos = position(it);
break;
}
++it;
}
if (dashPos == 0)
{
std::cerr << "ERROR: Interval is not in format CHR:BEG-END." << std::endl;
return 1;
}
out.i1 = prefix(in, colonPos);
out.i2 = lexicalCast<unsigned>(infix(in, colonPos + 1, dashPos));
out.i3 = lexicalCast<unsigned>(suffix(in, dashPos + 1));
return 0;
}
bool
readChromosomes(std::set<CharString> & chromosomes, CharString & referenceFile)
{
// Load or build and save the FASTA index.
FaiIndex faiIndex;
if (!open(faiIndex, toCString(referenceFile)))
{
if (!build(faiIndex, toCString(referenceFile)))
{
std::cerr << "ERROR: FASTA index could not be loaded or built.\n";
return 1;
}
if (!save(faiIndex)) // Name is stored from when reading.
{
std::cerr << "WARNING: FASTA index could not be written to disk.\n";
}
}
for (unsigned i = 0; i < length(faiIndex.seqNameStore); ++i)
chromosomes.insert(faiIndex.seqNameStore[i]);
return 0;
}
#endif // POPINS_UILS_H_