From ffa852df3aa4c54c41e06a33400f3b42f29deda7 Mon Sep 17 00:00:00 2001
From: Aaron <aaronquinlan@gmail.com>
Date: Tue, 23 Mar 2010 16:30:03 -0400
Subject: [PATCH] slopBed now uses the GenomeFile class.

---
 src/slopBed/Makefile    |  7 +++---
 src/slopBed/slopBed.cpp | 51 ++++++++++-------------------------------
 src/slopBed/slopBed.h   | 18 ++++++++-------
 3 files changed, 26 insertions(+), 50 deletions(-)

diff --git a/src/slopBed/Makefile b/src/slopBed/Makefile
index f5c1c618..fff5a637 100755
--- a/src/slopBed/Makefile
+++ b/src/slopBed/Makefile
@@ -9,14 +9,14 @@ BIN_DIR = ../../bin/
 # -------------------
 # define our includes
 # -------------------
-INCLUDES = -I$(UTILITIES_DIR)/bedFile/ -I$(UTILITIES_DIR)/lineFileUtilities/ -I$(UTILITIES_DIR)/version/
+INCLUDES = -I$(UTILITIES_DIR)/bedFile/ -I$(UTILITIES_DIR)/genomeFile/ -I$(UTILITIES_DIR)/lineFileUtilities/ -I$(UTILITIES_DIR)/version/
 
 # ----------------------------------
 # define our source and object files
 # ----------------------------------
 SOURCES= slopBedMain.cpp slopBed.cpp
 OBJECTS= $(SOURCES:.cpp=.o)
-_EXT_OBJECTS=bedFile.o lineFileUtilities.o
+_EXT_OBJECTS=bedFile.o genomeFile.o lineFileUtilities.o
 EXT_OBJECTS=$(patsubst %,$(OBJ_DIR)/%,$(_EXT_OBJECTS))
 BUILT_OBJECTS= $(patsubst %,$(OBJ_DIR)/%,$(OBJECTS))
 PROGRAM= slopBed
@@ -37,7 +37,8 @@ $(BUILT_OBJECTS): $(SOURCES)
 $(EXT_OBJECTS):
 	@$(MAKE) --no-print-directory -C $(UTILITIES_DIR)/lineFileUtilities/
 	@$(MAKE) --no-print-directory -C $(UTILITIES_DIR)/bedFile/
-	
+	@$(MAKE) --no-print-directory -C $(UTILITIES_DIR)/genomeFile/
+		
 clean:
 	@echo "Cleaning up."
 	@rm -f $(OBJ_DIR)/* $(BIN_DIR)/*
diff --git a/src/slopBed/slopBed.cpp b/src/slopBed/slopBed.cpp
index a5054d79..590bf2bc 100755
--- a/src/slopBed/slopBed.cpp
+++ b/src/slopBed/slopBed.cpp
@@ -12,9 +12,7 @@
 #include "lineFileUtilities.h"
 #include "slopBed.h"
 
-/*
-	Constructor
-*/
+
 BedSlop::BedSlop(string &bedFile, string &genomeFile, bool &forceStrand, int &leftSlop, int &rightSlop) {
 
 	this->bedFile = bedFile;
@@ -24,14 +22,11 @@ BedSlop::BedSlop(string &bedFile, string &genomeFile, bool &forceStrand, int &le
 	this->leftSlop = leftSlop;
 	this->rightSlop = rightSlop;
 	
-	this->bed = new BedFile(bedFile);	
+	this->bed    = new BedFile(bedFile);
+	this->genome = new GenomeFile(genomeFile);	
 }
 
 
-
-/*
-	Destructor
-*/
 BedSlop::~BedSlop(void) {
 
 }
@@ -49,7 +44,6 @@ void BedSlop::SlopBed(istream &bedInput) {
 		lineNum++;
 		
 		BED bedEntry;     // used to store the current BED line from the BED file.
-
 		if (this->bed->parseLine(bedEntry, bedFields, lineNum)) {
 			AddSlop(bedEntry);
 			bed->reportBedNewLine(bedEntry);			
@@ -60,18 +54,18 @@ void BedSlop::SlopBed(istream &bedInput) {
 
 void BedSlop::AddSlop(BED &bed) {
 
-	/*
-	   special handling if the BED entry is on the negative
-	   strand and the user cares about strandedness.
-	*/
-	if ((this->forceStrand) &&  (bed.strand == "-")) {
+	// special handling if the BED entry is on the negative
+	// strand and the user cares about strandedness.
+	int chromSize = genome->getChromSize(bed.chrom);
+	
+	if ( (this->forceStrand) && (bed.strand == "-") ) {
 		// inspect the start
 		if ((bed.start - rightSlop) > 0) bed.start -= rightSlop;
 		else bed.start = 0;
 
 		// inspect the start		
-		if ((bed.end + leftSlop) <= this->chromSizes[bed.chrom]) bed.end += leftSlop;
-		else bed.end = this->chromSizes[bed.chrom];
+		if ((bed.end + leftSlop) <= chromSize) bed.end += leftSlop;
+		else bed.end = chromSize;
 	}
 	else {		
 		// inspect the start
@@ -79,35 +73,14 @@ void BedSlop::AddSlop(BED &bed) {
 		else bed.start = 0;
 		
 		// inspect the end
-		if ((bed.end + rightSlop) <= this->chromSizes[bed.chrom]) bed.end += rightSlop;
-		else bed.end = this->chromSizes[bed.chrom];
+		if ((bed.end + rightSlop) <= chromSize) bed.end += rightSlop;
+		else bed.end = chromSize;
 	}
 }
 
 
 void BedSlop::DetermineBedInput() {
 
-
-	/* open the GENOME file for reading.
-	   if successful, load each chrom and it's size into
-	   the "chromSize" map.  also compute the total size of the genome
-	   and store in "genomeSize"
-	*/
-	ifstream genome(this->genomeFile.c_str(), ios::in);
-	if ( !genome ) {
-		cerr << "Error: The requested genome file (" <<this->genomeFile << ") could not be opened. Exiting!" << endl;
-		exit (1);
-	}
-	else {
-		string chrom;
-		unsigned int size;
-		while (genome >> chrom >> size) {
-			if (chrom.size() > 0 && size > 0) {
-				this->chromSizes[chrom] = size;
-			}
-		}
-	}
-
 	if (this->bedFile != "stdin") {   // process a file
 		ifstream beds(this->bedFile.c_str(), ios::in);
 		if ( !beds ) {
diff --git a/src/slopBed/slopBed.h b/src/slopBed/slopBed.h
index 272ca797..e6dbce0e 100755
--- a/src/slopBed/slopBed.h
+++ b/src/slopBed/slopBed.h
@@ -1,9 +1,3 @@
-#include "bedFile.h"
-#include <vector>
-#include <iostream>
-#include <fstream>
-#include <map>
-#include <cstdlib>
 /*****************************************************************************
   slopBed.h
 
@@ -15,6 +9,15 @@
 
   Licenced under the GNU General Public License 2.0+ license.
 ******************************************************************************/
+
+#include "bedFile.h"
+#include "genomeFile.h"
+
+#include <vector>
+#include <iostream>
+#include <fstream>
+#include <map>
+#include <cstdlib>
 #include <ctime>
 using namespace std;
 
@@ -49,6 +52,5 @@ private:
 	int rightSlop;
 	
 	BedFile *bed;
-	
-	map<string, int, less<string> > chromSizes;
+	GenomeFile *genome;
 };
-- 
GitLab