-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwinextract.cpp
234 lines (193 loc) · 8.04 KB
/
winextract.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// ======================================================================
// PureCLIP: capturing target-specific protein-RNA interaction footprints
// ======================================================================
// Copyright (C) 2017 Sabrina Krakau, Max Planck Institute for Molecular
// Genetics
//
// 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/>.
//
// =======================================================================
// Author: Sabrina Krakau <[email protected]>
// =======================================================================
#include <fstream>
#include <seqan/basic.h>
#include <seqan/sequence.h>
#include <seqan/stream.h>
#include <seqan/seq_io.h>
#include <seqan/arg_parse.h>
#include <iostream>
#include <seqan/find.h>
#include <seqan/bed_io.h>
#include <seqan/misc/interval_tree.h>
using namespace seqan;
struct AppOptions
{
CharString inRefFileName;
CharString inBamFileName;
CharString inBedFileName;
CharString outFileName;
CharString sitesFileName;
unsigned windowSize;
bool useGivenWindow;
bool addScoreToName;
// Verbosity level. 0 -- quiet, 1 -- normal, 2 -- verbose, 3 -- very verbose.
int verbosity;
AppOptions() :
windowSize(100),
useGivenWindow(false),
addScoreToName(false),
verbosity(1)
{}
};
ArgumentParser::ParseResult
parseCommandLine(AppOptions & options, int argc, char const ** argv)
{
// Setup ArgumentParser.
ArgumentParser parser("winextract");
// Set short description, version, and date.
setShortDescription(parser, "Extract sequence window");
setVersion(parser, "0.1");
setDate(parser, "Mai 2017");
// Define usage line and long description.
addUsageLine(parser, "[\\fIOPTIONS\\fP] \"\\fITEXT\\fP\"");
addDescription(parser, "Extracts sequences form reference given BED regions.");
// We require one argument.
addOption(parser, ArgParseOption("g", "genome", "Genome reference file.", ArgParseArgument::INPUT_FILE));
setValidValues(parser, "genome", ".fa .fasta");
setRequired(parser, "genome", true);
addOption(parser, ArgParseOption("c", "in-bed", "Input cand-regions.", ArgParseArgument::INPUT_FILE));
setValidValues(parser, "in-bed", ".bed");
setRequired(parser, "in-bed", true);
addOption(parser, ArgParseOption("o", "output", "Output file.", ArgParseArgument::OUTPUT_FILE));
setValidValues(parser, "output", ".fasta .fa");
setRequired(parser, "output", true);
addOption(parser, ArgParseOption("w", "window", "Window size to analyse.", ArgParseArgument::INTEGER));
setMinValue(parser, "window", "5");
setMaxValue(parser, "window", "2000"); // ?
addOption(parser, ArgParseOption("u", "uow", "Use given window."));
addOption(parser, ArgParseOption("s", "asn", "Add score to output sequence name."));
// Parse command line.
ArgumentParser::ParseResult res = parse(parser, argc, argv);
// Only extract options if the program will continue after parseCommandLine()
if (res != ArgumentParser::PARSE_OK)
return res;
// Extract option values.
getOptionValue(options.inRefFileName, parser, "genome");
getOptionValue(options.inBedFileName, parser, "in-bed");
getOptionValue(options.outFileName, parser, "output");
getOptionValue(options.windowSize, parser, "window");
if (isSet(parser, "uow"))
options.useGivenWindow = true;
if (isSet(parser, "asn"))
options.addScoreToName = true;
return ArgumentParser::PARSE_OK;
}
template<typename TOptions>
bool doIt(TOptions & options)
{
typedef FragmentStore<> TFragmentStore;
std::cout << "Load reference file ..." << std::endl;
TFragmentStore store;
loadContigs(store, toCString(options.inRefFileName));
std::cout << "Load candidate sites from BED file ..." << std::endl;
String<String<IntervalAndCargo<> > > candRegs;
resize(candRegs, length(store.contigNameStore));
BedFileIn bedIn(toCString(options.inBedFileName));
BedRecord<Bed6> bedRecord;
std::cout << "Extract sequence for windows around candidate sites ..." << std::endl;
SeqFileOut seqFileOut(toCString(options.outFileName));
while (!atEnd(bedIn))
{
try
{
readRecord(bedRecord, bedIn);
}
catch (ParseError const & e)
{
std::cerr << "ERROR: input BED record is badly formatted. " << e.what() << "\n";
}
catch (IOError const & e)
{
std::cerr << "ERROR: could not copy input BED record. " << e.what() << "\n";
}
for (unsigned i = 0; i < length(store.contigNameStore); ++i)
{
if (bedRecord.ref == store.contigNameStore[i]) // || bedRecord.ref == prefix(store.contigNameStore[i], length(bedRecord.ref)) )
{
unsigned beginPos;
unsigned endPos;
if (!options.useGivenWindow)
{
int pos = bedRecord.beginPos;
beginPos = 0;
endPos = length(store.contigStore[i].seq);
if (pos - (int)(options.windowSize/2) > 0)
beginPos = pos - (options.windowSize/2);
else
continue; // for simplicity ignore windows overlapping with contig border
if (pos + (options.windowSize/2) + 1 < length(store.contigStore[i].seq) )
endPos = pos + (options.windowSize/2) +1;
else
continue;
}
else
{
beginPos = std::max(bedRecord.beginPos, 0);
endPos = std::min(bedRecord.endPos, (int)length(store.contigStore[i].seq));
}
//std::cout << store.contigNameStore[i] << " pos: " << pos << " beginPos: " << beginPos << " endPos: " << endPos << " length contig: " << length(store.contigStore[i].seq) << std::endl;
DnaString inf = infix(store.contigStore[i].seq, beginPos, endPos);
if (bedRecord.strand == '-') reverseComplement(inf);
CharString id = "cand_";
std::stringstream ss;
ss << store.contigNameStore[i];
ss << "_";
ss << beginPos;
ss << "_";
ss << endPos;
ss << "_";
if (bedRecord.strand == '+')
ss << "F";
else
ss << "R";
if (options.addScoreToName)
{
ss << "_";
ss << bedRecord.score;
}
CharString str = ss.str();
ss.str("");
ss.clear();
append(id, str);
writeRecord(seqFileOut, id, inf);
continue;
}
}
}
return 0;
}
int main(int argc, char const ** argv)
{
// Parse the command line.
ArgumentParser parser;
AppOptions options;
ArgumentParser::ParseResult res = parseCommandLine(options, argc, argv);
// If there was an error parsing or built-in argument parser functionality
// was triggered then we exit the program. The return code is 1 if there
// were errors and 0 if there were none.
if (res != ArgumentParser::PARSE_OK)
return res == ArgumentParser::PARSE_ERROR;
doIt(options);
return 0;
}