Skip to content
Snippets Groups Projects
Commit 74cb840e authored by Aaron's avatar Aaron
Browse files

fixed bug in merge when using -n and -nms. Cleaned up logic and use VectorOps

parent 4151c4d2
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ INCLUDES = -I$(UTILITIES_DIR)/bedFile/ \ ...@@ -9,6 +9,7 @@ INCLUDES = -I$(UTILITIES_DIR)/bedFile/ \
-I$(UTILITIES_DIR)/lineFileUtilities/ \ -I$(UTILITIES_DIR)/lineFileUtilities/ \
-I$(UTILITIES_DIR)/gzstream/ \ -I$(UTILITIES_DIR)/gzstream/ \
-I$(UTILITIES_DIR)/fileType/ \ -I$(UTILITIES_DIR)/fileType/ \
-I$(UTILITIES_DIR)/VectorOps/ \
-I$(UTILITIES_DIR)/version/ -I$(UTILITIES_DIR)/version/
# ---------------------------------- # ----------------------------------
......
...@@ -37,87 +37,29 @@ void BedMerge::ReportMergedNames(const vector<string> &names) { ...@@ -37,87 +37,29 @@ void BedMerge::ReportMergedNames(const vector<string> &names) {
void BedMerge::ReportMergedScores(const vector<string> &scores) { void BedMerge::ReportMergedScores(const vector<string> &scores) {
// setup a VectorOps instances for the list of scores.
// VectorOps methods used for each possible operation.
VectorOps vo(scores);
std::stringstream buffer;
if (scores.size() > 0) { if (scores.size() > 0) {
printf("\t"); if (_scoreOp == "sum")
buffer << setprecision (PRECISION) << vo.GetSum();
// convert the scores to floats else if (_scoreOp == "min")
vector<float> data; buffer << setprecision (PRECISION) << vo.GetMin();
for (size_t i = 0 ; i < scores.size() ; i++) { else if (_scoreOp == "max")
data.push_back(atof(scores[i].c_str())); buffer << setprecision (PRECISION) << vo.GetMax();
} else if (_scoreOp == "mean")
buffer << setprecision (PRECISION) << vo.GetMean();
if (_scoreOp == "sum") { else if (_scoreOp == "median")
printf("%.3f", accumulate(data.begin(), data.end(), 0.0)); buffer << setprecision (PRECISION) << vo.GetMedian();
} else if (_scoreOp == "mode")
else if (_scoreOp == "min") { buffer << setprecision (PRECISION) << vo.GetMode();
printf("%.3f", *min_element( data.begin(), data.end() )); else if (_scoreOp == "antimode")
} buffer << setprecision (PRECISION) << vo.GetAntiMode();
else if (_scoreOp == "max") { else if (_scoreOp == "collapse")
printf("%.3f", *max_element( data.begin(), data.end() )); buffer << setprecision (PRECISION) << vo.GetCollapse();
} cout << "\t" << buffer.str();
else if (_scoreOp == "mean") {
double total = accumulate(data.begin(), data.end(), 0.0);
double mean = total / data.size();
printf("%.3f", mean);
}
else if (_scoreOp == "median") {
double median = 0.0;
sort(data.begin(), data.end());
int totalLines = data.size();
if ((totalLines % 2) > 0) {
long mid;
mid = totalLines / 2;
median = data[mid];
}
else {
long midLow, midHigh;
midLow = (totalLines / 2) - 1;
midHigh = (totalLines / 2);
median = (data[midLow] + data[midHigh]) / 2.0;
}
printf("%.3f", median);
}
else if ((_scoreOp == "mode") || (_scoreOp == "antimode")) {
// compute the frequency of each unique value
map<string, int> freqs;
vector<string>::const_iterator dIt = scores.begin();
vector<string>::const_iterator dEnd = scores.end();
for (; dIt != dEnd; ++dIt) {
freqs[*dIt]++;
}
// grab the mode and the anti mode
string mode, antiMode;
int count = 0;
int minCount = INT_MAX;
for(map<string,int>::const_iterator iter = freqs.begin(); iter != freqs.end(); ++iter) {
if (iter->second > count) {
mode = iter->first;
count = iter->second;
}
if (iter->second < minCount) {
antiMode = iter->first;
minCount = iter->second;
}
}
// report
if (_scoreOp == "mode") {
printf("%s", mode.c_str());
}
else if (_scoreOp == "antimode") {
printf("%s", antiMode.c_str());
}
}
else if (_scoreOp == "collapse") {
vector<string>::const_iterator scoreItr = scores.begin();
vector<string>::const_iterator scoreEnd = scores.end();
for (; scoreItr != scoreEnd; ++scoreItr) {
if (scoreItr < (scoreEnd - 1))
cout << *scoreItr << ";";
else
cout << *scoreItr;
}
}
} }
else { else {
cerr << endl cerr << endl
...@@ -180,6 +122,23 @@ void BedMerge::Report(string chrom, int start, int end, ...@@ -180,6 +122,23 @@ void BedMerge::Report(string chrom, int start, int end,
else if (_numEntries == true && _reportNames == false && _reportScores == false) { else if (_numEntries == true && _reportNames == false && _reportScores == false) {
printf("\t%d\n", mergeCount); printf("\t%d\n", mergeCount);
} }
// merged intervals, counts, and scores
else if (_numEntries == true && _reportNames == false && _reportScores == true) {
printf("\t%d", mergeCount);
ReportMergedScores(scores);
printf("\n");
}
// merged intervals, counts, and names
else if (_numEntries == true && _reportNames == true && _reportScores == false) {
ReportMergedNames(names);
printf("\t%d\n", mergeCount);
}
// merged intervals, counts, names, and scores
else if (_numEntries == true && _reportNames == true && _reportScores == true) {
ReportMergedNames(names);
ReportMergedScores(scores);
printf("\t%d\n", mergeCount);
}
// merged intervals and names // merged intervals and names
else if (_numEntries == false && _reportNames == true && _reportScores == false) { else if (_numEntries == false && _reportNames == true && _reportScores == false) {
ReportMergedNames(names); ReportMergedNames(names);
...@@ -218,6 +177,25 @@ void BedMerge::ReportStranded(string chrom, int start, int end, ...@@ -218,6 +177,25 @@ void BedMerge::ReportStranded(string chrom, int start, int end,
else if (_numEntries == true && _reportNames == false && _reportScores == false) { else if (_numEntries == true && _reportNames == false && _reportScores == false) {
printf("\t%d\t%s\n", mergeCount, strand.c_str()); printf("\t%d\t%s\n", mergeCount, strand.c_str());
} }
// merged intervals, counts, and scores
else if (_numEntries == true && _reportNames == false && _reportScores == true) {
printf("\t%d", mergeCount);
ReportMergedScores(scores);
printf("\t%s\n", strand.c_str());
}
// merged intervals, counts, and names
else if (_numEntries == true && _reportNames == true && _reportScores == false) {
ReportMergedNames(names);
printf("\t%d\t%s", mergeCount, strand.c_str());
printf("\n");
}
// merged intervals, counts, names, and scores
else if (_numEntries == true && _reportNames == true && _reportScores == true) {
ReportMergedNames(names);
ReportMergedScores(scores);
printf("\t%s\t%d", strand.c_str(), mergeCount);
printf("\n");
}
// merged intervals and names // merged intervals and names
else if (_numEntries == false && _reportNames == true && _reportScores == false) { else if (_numEntries == false && _reportNames == true && _reportScores == false) {
ReportMergedNames(names); ReportMergedNames(names);
......
...@@ -14,12 +14,15 @@ ...@@ -14,12 +14,15 @@
#include <algorithm> #include <algorithm>
#include <numeric> #include <numeric>
#include <iostream> #include <iostream>
#include <iomanip>
#include <fstream> #include <fstream>
#include <limits.h> #include <limits.h>
#include <stdlib.h> #include <stdlib.h>
#include "VectorOps.h"
using namespace std; using namespace std;
const int PRECISION = 21;
//************************************************ //************************************************
// Class methods and elements // Class methods and elements
......
...@@ -98,10 +98,6 @@ int merge_main(int argc, char* argv[]) { ...@@ -98,10 +98,6 @@ int merge_main(int argc, char* argv[]) {
cerr << endl << "*****" << endl << "*****ERROR: Need -i BED file. " << endl << "*****" << endl; cerr << endl << "*****" << endl << "*****ERROR: Need -i BED file. " << endl << "*****" << endl;
showHelp = true; showHelp = true;
} }
if (reportNames && numEntries) {
cerr << endl << "*****" << endl << "*****ERROR: Request either -n OR -nms, not both." << endl << "*****" << endl;
showHelp = true;
}
if ((reportScores == true) && (scoreOp != "sum") && (scoreOp != "max") && (scoreOp != "min") && (scoreOp != "mean") && if ((reportScores == true) && (scoreOp != "sum") && (scoreOp != "max") && (scoreOp != "min") && (scoreOp != "mean") &&
(scoreOp != "mode") && (scoreOp != "median") && (scoreOp != "antimode") && (scoreOp != "collapse")) (scoreOp != "mode") && (scoreOp != "median") && (scoreOp != "antimode") && (scoreOp != "collapse"))
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment