Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add makewindows initial window/step feature #936

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/windowMaker/windowMaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ 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, bool reverse)
WindowMaker::WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE input_file_type, uint32_t size, uint32_t step, uint32_t initialSize, uint32_t initialStep, bool reverse)
: _size(size)
, _step(step)
, _count(0)
, _reverse(reverse)
, _window_method(FIXED_WINDOW_SIZE)
, _id_method(id_method)
, _initialSize(initialSize)
, _initialStep(initialStep)
{
if (input_file_type==GENOME_FILE)
MakeWindowsFromGenome(fileName);
Expand All @@ -32,6 +34,8 @@ WindowMaker::WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE
, _reverse(reverse)
, _window_method(FIXED_WINDOW_COUNT)
, _id_method(id_method)
, _initialSize(0)
, _initialStep(0)
{
if (input_file_type==GENOME_FILE)
MakeWindowsFromGenome(fileName);
Expand Down Expand Up @@ -79,24 +83,38 @@ void WindowMaker::MakeBEDWindow(const BED& 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;
uint32_t num_windows = 0;
if (interval.end - interval.start > 0) {
num_windows = 1;
}
CHRPOS rest_interval = interval.end - interval.start - (CHRPOS)_initialStep;
// need to avoid adding a negative number, which may result in a huge unsigned positive number
if (rest_interval > 0) {
num_windows += rest_interval / _step;
if (rest_interval % _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 current_size = _initialSize;
uint32_t current_step = _initialStep;
uint32_t num_windows = CalculateWindows(interval);
for (uint32_t start = interval.start; start <= interval.end; start += _step, ++i) {
for (uint32_t start = interval.start; start <= interval.end; ++i) {
string name = GenerateID(interval, i, num_windows, _reverse);
if ((start + _size) <= interval.end) {
cout << interval.chrom << "\t" << start << "\t" << start + _size << name << endl;
if ((start + current_size) <= interval.end) {
cout << interval.chrom << "\t" << start << "\t" << start + current_size << name << endl;
}
else if (start < interval.end) {
cout << interval.chrom << "\t" << start << "\t" << interval.end << name << endl;
}
start += current_step;
current_step = _step; // for i >= 2
current_size = _size; // for i >= 2
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/windowMaker/windowMaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class WindowMaker {

// constructor
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);
WindowMaker(string &fileName, ID_METHOD id_method, INPUT_FILE_TYPE input_file_type, uint32_t size, uint32_t step, uint32_t initialSize, uint32_t initialStep, bool reverse);

// destructor
~WindowMaker(void);
Expand All @@ -53,6 +53,8 @@ class WindowMaker {
bool _reverse; // should window numbering be reversed?
WINDOW_METHOD _window_method;
ID_METHOD _id_method;
uint32_t _initialSize;
uint32_t _initialStep;

void MakeBEDWindow(const BED& interval);

Expand Down
57 changes: 56 additions & 1 deletion src/windowMaker/windowMakerMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ int windowmaker_main(int argc, char* argv[]) {
uint32_t size = 0;
uint32_t step = 0;
uint32_t count = 0;
uint32_t initialSize = 0;
uint32_t initialStep = 0;

bool haveGenome = false;
bool haveBed = false;
bool haveStep = false;
bool haveSize = false;
bool haveCount = false;
bool reverse = false;
bool haveInitialSize = false;
bool haveInitialStep = false;

for(int i = 1; i < argc; i++) {
int parameterLength = (int)strlen(argv[i]);
Expand Down Expand Up @@ -117,6 +121,30 @@ int windowmaker_main(int argc, char* argv[]) {
else if(PARAMETER_CHECK("-reverse", 8, parameterLength)) {
reverse = true;
}
else if(PARAMETER_CHECK("-iw", 3, parameterLength)) {
if ((i+1) < argc) {
haveInitialSize = true;
int tmp = atoi(argv[i + 1]);
if (tmp <= 0) {
cerr << endl << "*****" << endl << "*****ERROR: The initial window size (-iw) option must be greater than zero." << endl << "*****" << endl;
showHelp = true;
}
initialSize = tmp;
i++;
}
}
else if(PARAMETER_CHECK("-is", 3, parameterLength)) {
if ((i+1) < argc) {
haveInitialStep = true;
int tmp = atoi(argv[i + 1]);
if (tmp <= 0) {
cerr << endl << "*****" << endl << "*****ERROR: The initial step size (-is) option must be greater than zero." << endl << "*****" << endl;
showHelp = true;
}
initialStep = tmp;
i++;
}
}
else {
cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl;
showHelp = true;
Expand Down Expand Up @@ -151,6 +179,24 @@ int windowmaker_main(int argc, char* argv[]) {
cerr << endl << "*****" << endl << "*****ERROR: The step (-s) option must be greater than zero. " << endl << "*****" << endl;
showHelp = true;
}
if (haveInitialSize && !haveSize) {
// imply that initialSize cannot be combined with count
cerr << endl << "*****" << endl << "*****ERROR: Need -w (window size) to use -iw (initial window size)." << endl << "*****" << endl;
showHelp = true;
}
if (haveInitialStep && !haveStep) {
// imply that initialStep cannot be combined with count
cerr << endl << "*****" << endl << "*****ERROR: Need -s (step size) to use -is (initial step size)." << endl << "*****" << endl;
showHelp = true;
}
if (!haveInitialSize) {
initialSize = size;
if (!haveInitialStep) {
initialStep = step;
}
} else if (!haveInitialStep) {
initialStep = initialSize;
}

if (!showHelp) {
WindowMaker *wm = NULL;
Expand All @@ -159,7 +205,7 @@ int windowmaker_main(int argc, char* argv[]) {
inputFileType, count, reverse);
if (haveSize)
wm = new WindowMaker(inputFile, idMethod,
inputFileType, size, step, reverse);
inputFileType, size, step, initialSize, initialStep, reverse);
delete wm;
}
else {
Expand Down Expand Up @@ -199,6 +245,15 @@ void windowmaker_help(void) {
cerr << "\t\tcreating a new window. Used to create \"sliding\" windows." << endl;
cerr << "\t\t- Defaults to window size (non-sliding windows)." << endl << endl;

cerr << "\t-iw <initial_window_size>" << endl;
cerr << "\t\tWindow size for the first window. Cannot be used without -w <window_size>" << endl;
cerr << "\t\t- Defaults to window size (i.e., the first window is treated equally)." << endl << endl;

cerr << "\t-is <initial_step_size>" << endl;
cerr << "\t\tStep size for the first window. Cannot be used without -s <step_size>" << endl;
cerr << "\t\t- Defaults to <initial_window_size> if -iw is present." << endl;
cerr << "\t\t Otherwise, defaults to step size." << endl << endl;

cerr << "\t-n <number_of_windows>" << endl;
cerr << "\t\tDivide each input interval (either a chromosome or a BED interval)" << endl;
cerr << "\t\tto fixed number of windows (i.e. same number of windows, with" << endl;
Expand Down
86 changes: 86 additions & 0 deletions test/makewindows/test-makewindows.sh
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,90 @@ echo \
$BT makewindows -b a.19bp.bed -n 10 -i srcwinnum 2> obs
check obs exp
rm obs exp

###########################################################
# Test window + initial window size
###########################################################
echo -e " makewindows.t10...\c"
echo \
"chr5 60000 62000 3
chr5 62000 66000 2
chr5 66000 70000 1
chr5 73000 75000 5
chr5 75000 79000 4
chr5 79000 83000 3
chr5 83000 87000 2
chr5 87000 90000 1
chr5 100000 101000 1" > exp
$BT makewindows -b input.bed -w 4000 -iw 2000 -i winnum -reverse > obs
check obs exp
rm obs exp

###########################################################
# Test window + step + initial step size
###########################################################
echo -e " makewindows.t11...\c"
echo \
"chr5 60000 64000 3
chr5 62000 66000 2
chr5 66000 70000 1
chr5 73000 77000 5
chr5 75000 79000 4
chr5 79000 83000 3
chr5 83000 87000 2
chr5 87000 90000 1
chr5 100000 101000 1" > exp
$BT makewindows -b input.bed -w 4000 -s 4000 -is 2000 -i winnum -reverse > obs
check obs exp
rm obs exp

###########################################################
# Test window + initial window + step + initial step size
###########################################################
echo -e " makewindows.t12...\c"
echo \
"chr5 60000 62000 3
chr5 63000 67000 2
chr5 68000 70000 1
chr5 73000 75000 4
chr5 76000 80000 3
chr5 81000 85000 2
chr5 86000 90000 1
chr5 100000 101000 1" > exp
$BT makewindows -b input.bed -w 4000 -iw 2000 -s 5000 -is 3000 -i winnum -reverse > obs
check obs exp
rm obs exp

###########################################################
# Test window + initial window + step + initial step (forward numbering)
###########################################################
echo -e " makewindows.t13...\c"
echo \
"chr5 60000 62000 1
chr5 63000 67000 2
chr5 68000 70000 3
chr5 73000 75000 1
chr5 76000 80000 2
chr5 81000 85000 3
chr5 86000 90000 4
chr5 100000 101000 1" > exp
$BT makewindows -b input.bed -w 4000 -iw 2000 -s 5000 -is 3000 -i winnum > obs
check obs exp
rm obs exp

###########################################################
# Test window + initial window + step + initial step (large initial sizes)
###########################################################
echo -e " makewindows.t14...\c"
echo \
"chr5 60000 68000 1
chr5 73000 81000 4
chr5 83000 85000 3
chr5 86000 88000 2
chr5 89000 90000 1
chr5 100000 101000 1" > exp
$BT makewindows -b input.bed -w 2000 -iw 8000 -s 3000 -is 10000 -i winnum -reverse > obs
check obs exp
rm obs exp

[[ $FAILURES -eq 0 ]] || exit 1;