Commit c432a190 authored by Aaron Quinlan's avatar Aaron Quinlan
Browse files

Merge pull request #194 from jayhesselberth/reverse-windows

`-reverse` option added to `makewindows`
parents 64b95927 efda13cb
......@@ -11,10 +11,11 @@ Licenced under the GNU General Public License 2.0 license.
******************************************************************************/
#include "windowMaker.h"
WindowMaker::WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE input_file_type, uint32_t size, uint32_t step)
WindowMaker::WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE input_file_type, uint32_t size, uint32_t step, bool reverse)
: _size(size)
, _step(step)
, _count(0)
, _reverse(reverse)
, _window_method(FIXED_WINDOW_SIZE)
, _id_method(id_method)
{
......@@ -24,10 +25,11 @@ WindowMaker::WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE
MakeWindowsFromBED(fileName);
}
WindowMaker::WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE input_file_type, uint32_t count)
WindowMaker::WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE input_file_type, uint32_t count, bool reverse)
: _size(0)
, _step(0)
, _count(count)
, _reverse(reverse)
, _window_method(FIXED_WINDOW_COUNT)
, _id_method(id_method)
{
......@@ -76,10 +78,20 @@ void WindowMaker::MakeBEDWindow(const BED& interval)
MakeFixedCountWindow(interval);
}
uint32_t WindowMaker::CalculateWindows(const BED& interval) {
uint32_t num_windows = (interval.end - interval.start) / _step;
if ((interval.end - interval.start) % _step > 0) {
// add 1 to num_windows if the last window is less than _step
num_windows += 1;
}
return num_windows;
}
void WindowMaker::MakeFixedSizeWindow(const BED& interval) {
uint32_t i=1;
uint32_t num_windows = CalculateWindows(interval);
for (uint32_t start = interval.start; start <= interval.end; start += _step, ++i) {
string name = GenerateID(interval,i);
string name = GenerateID(interval, i, num_windows, _reverse);
if ((start + _size) <= interval.end) {
cout << interval.chrom << "\t" << start << "\t" << start + _size << name << endl;
}
......@@ -92,28 +104,39 @@ void WindowMaker::MakeFixedSizeWindow(const BED& interval) {
void WindowMaker::MakeFixedCountWindow(const BED& interval) {
uint32_t interval_size = interval.end - interval.start ;
uint32_t window_size = (interval_size-1)/_count + 1; // integer version of ceil(interval_size/_count)
if (window_size==0 || interval_size==0)
return;
uint32_t i=1;
for (uint32_t start = interval.start; start < interval.end; start += window_size, ++i) {
string name = GenerateID(interval,i);
string name = GenerateID(interval, i, _count, _reverse);
uint32_t end = min(start + window_size,interval.end);
cout << interval.chrom << "\t" << start << "\t" << end << name << endl;
}
}
string WindowMaker::GenerateID(const BED& interval, uint32_t window_index) const {
string WindowMaker::GenerateID(const BED& interval, uint32_t window_index, uint32_t num_windows, bool _reverse) const {
stringstream s;
// cout << "num_windows: " << num_windows << endl;
// cout << "window_index: " << window_index << endl << endl;
switch(_id_method) {
case ID_SOURCE_ID:
s << "\t" << interval.name;
break;
case ID_WINDOW_NUMBER:
s << "\t" << window_index;
if (_reverse == true && num_windows > 0) {
s << "\t" << num_windows - window_index + 1;
} else {
s << "\t" << window_index;
}
break;
case ID_SOURCE_ID_WINDOW_NUMBER:
s << "\t" << interval.name << "_" << window_index;
if (_reverse == true && num_windows > 0) {
s << "\t" << interval.name << "_" << num_windows - window_index + 1;
} else {
s << "\t" << interval.name << "_" << window_index;
}
default:
case ID_NONE:
break;
......
......@@ -37,8 +37,8 @@ public:
};
// constructor
WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE input_file_type, uint32_t count);
WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE input_file_type, uint32_t size, uint32_t step);
WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE input_file_type, uint32_t count, bool reverse);
WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE input_file_type, uint32_t size, uint32_t step, bool reverse);
// destructor
~WindowMaker(void);
......@@ -50,6 +50,7 @@ private:
uint32_t _size;
uint32_t _step;
uint32_t _count;
bool _reverse; // should window numbering be reversed?
WINDOW_METHOD _window_method;
ID_METHOD _id_method;
......@@ -57,6 +58,7 @@ private:
void MakeFixedSizeWindow(const BED& interval);
void MakeFixedCountWindow(const BED& interval);
uint32_t CalculateWindows(const BED& interval);
string GenerateID(const BED& interval, uint32_t window_index) const;
string GenerateID(const BED& interval, uint32_t window_index, uint32_t num_windows, bool _reverse) const;
};
......@@ -43,6 +43,7 @@ int windowmaker_main(int argc, char* argv[]) {
bool haveBed = false;
bool haveSize = false;
bool haveCount = false;
bool reverse = false;
for(int i = 1; i < argc; i++) {
int parameterLength = (int)strlen(argv[i]);
......@@ -112,6 +113,9 @@ int windowmaker_main(int argc, char* argv[]) {
i++;
}
}
else if(PARAMETER_CHECK("-reverse", 8, parameterLength)) {
reverse = true;
}
else {
cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl;
showHelp = true;
......@@ -142,9 +146,11 @@ int windowmaker_main(int argc, char* argv[]) {
if (!showHelp) {
WindowMaker *wm = NULL;
if (haveCount)
wm = new WindowMaker(inputFile, idMethod, inputFileType, count);
wm = new WindowMaker(inputFile, idMethod,
inputFileType, count, reverse);
if (haveSize)
wm = new WindowMaker(inputFile, idMethod, inputFileType, size, step);
wm = new WindowMaker(inputFile, idMethod,
inputFileType, size, step, reverse);
delete wm;
}
else {
......@@ -189,6 +195,10 @@ void windowmaker_help(void) {
cerr << "\t\tto fixed number of windows (i.e. same number of windows, with" << endl;
cerr << "\t\tvarying window sizes)." << endl << endl;
cerr << "\t-reverse" << endl;
cerr << "\t\t Reverse numbering of windows in the output, i.e. report " << endl;
cerr << "\t\t windows in decreasing order" << endl << endl;
cerr << "ID Naming Options: " << endl;
cerr << "\t-i src|winnum|srcwinnum" << endl;
cerr << "\t\tThe default output is 3 columns: chrom, start, end ." << endl;
......@@ -276,6 +286,25 @@ void windowmaker_help(void) {
cerr << " ..." << endl;
cerr << endl;
cerr << " # Reverse window numbers: "<< endl;
cerr << " $ cat input.bed" << endl;
cerr << " chr5 60000 70000 AAA" << endl;
cerr << " chr5 73000 90000 BBB" << endl;
cerr << " chr5 100000 101000 CCC" << endl;
cerr << " $ " << PROGRAM_NAME << " -b input.bed -n 3 -i winnum -reverse" << endl;
cerr << " chr5 60000 63334 3" << endl;
cerr << " chr5 63334 66668 2" << endl;
cerr << " chr5 66668 70000 1" << endl;
cerr << " chr5 73000 78667 3" << endl;
cerr << " chr5 78667 84334 2" << endl;
cerr << " chr5 84334 90000 1" << endl;
cerr << " chr5 100000 100334 3" << endl;
cerr << " chr5 100334 100668 2" << endl;
cerr << " chr5 100668 101000 1" << endl;
cerr << " ..." << endl;
cerr << endl;
cerr << " # Add a name column, based on the source ID + window number: "<< endl;
cerr << " $ cat input.bed" << endl;
cerr << " chr5 60000 70000 AAA" << endl;
......
chr5 60000 70000 AAA
chr5 73000 90000 BBB
chr5 100000 101000 CCC
BT=${BT-../../bin/bedtools}
check()
{
if diff $1 $2; then
echo ok
else
echo fail
fi
}
###########################################################
# Test window size / forward window numbering
############################################################
echo " makewindows.t01...\c"
echo \
"chr5 60000 65000 1
chr5 65000 70000 2
chr5 73000 78000 1
chr5 78000 83000 2
chr5 83000 88000 3
chr5 88000 90000 4
chr5 100000 101000 1" > exp
$BT makewindows -b input.bed -w 5000 -i winnum > obs
check obs exp
rm obs exp
###########################################################
# Test window size / reverse window numbering
############################################################
echo " makewindows.t02...\c"
echo \
"chr5 60000 65000 2
chr5 65000 70000 1
chr5 73000 78000 4
chr5 78000 83000 3
chr5 83000 88000 2
chr5 88000 90000 1
chr5 100000 101000 1" > exp
$BT makewindows -b input.bed -reverse -w 5000 -i winnum > obs
check obs exp
rm obs exp
###########################################################
# Test window+step size / forward window numbering
############################################################
echo " makewindows.t03...\c"
echo \
"chr5 60000 65000 1
chr5 62000 67000 2
chr5 64000 69000 3
chr5 66000 70000 4
chr5 68000 70000 5
chr5 73000 78000 1
chr5 75000 80000 2
chr5 77000 82000 3
chr5 79000 84000 4
chr5 81000 86000 5
chr5 83000 88000 6
chr5 85000 90000 7
chr5 87000 90000 8
chr5 89000 90000 9
chr5 100000 101000 1" > exp
$BT makewindows -b input.bed -w 5000 -s 2000 -i winnum > obs
check obs exp
rm obs exp
###########################################################
# Test window size / reverse window numbering
############################################################
echo " makewindows.t04...\c"
echo \
"chr5 60000 65000 5
chr5 62000 67000 4
chr5 64000 69000 3
chr5 66000 70000 2
chr5 68000 70000 1
chr5 73000 78000 9
chr5 75000 80000 8
chr5 77000 82000 7
chr5 79000 84000 6
chr5 81000 86000 5
chr5 83000 88000 4
chr5 85000 90000 3
chr5 87000 90000 2
chr5 89000 90000 1
chr5 100000 101000 1" > exp
$BT makewindows -b input.bed -reverse -w 5000 -s 2000 -i winnum > obs
check obs exp
rm obs exp
###########################################################
# Test fixed size / forward window numbering
############################################################
echo " makewindows.t05...\c"
echo \
"chr5 60000 63334 1
chr5 63334 66668 2
chr5 66668 70000 3
chr5 73000 78667 1
chr5 78667 84334 2
chr5 84334 90000 3
chr5 100000 100334 1
chr5 100334 100668 2
chr5 100668 101000 3" > exp
$BT makewindows -n 3 -b input.bed -i winnum > obs
check obs exp
rm obs exp
###########################################################
# Test fixed size / reverse window numbering
############################################################
echo " makewindows.t06...\c"
echo \
"chr5 60000 63334 3
chr5 63334 66668 2
chr5 66668 70000 1
chr5 73000 78667 3
chr5 78667 84334 2
chr5 84334 90000 1
chr5 100000 100334 3
chr5 100334 100668 2
chr5 100668 101000 1" > exp
$BT makewindows -n 3 -b input.bed -reverse -i winnum > obs
check obs exp
rm obs exp
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment