From bad67893b4f73eef555c162b77005adca3a3687c Mon Sep 17 00:00:00 2001 From: Aaron Quinlan <aaronquinlan@gmail.com> Date: Sun, 26 Apr 2009 16:07:34 -0400 Subject: [PATCH] Added linksBed and updated Makefile --- src/linksBed/Makefile | 44 ++++++++++++ src/linksBed/linksBed.cpp | 134 +++++++++++++++++++++++++++++++++++++ src/linksBed/linksBed.h | 43 ++++++++++++ src/linksBed/linksMain.cpp | 110 ++++++++++++++++++++++++++++++ 4 files changed, 331 insertions(+) create mode 100755 src/linksBed/Makefile create mode 100755 src/linksBed/linksBed.cpp create mode 100755 src/linksBed/linksBed.h create mode 100755 src/linksBed/linksMain.cpp diff --git a/src/linksBed/Makefile b/src/linksBed/Makefile new file mode 100755 index 00000000..74856d00 --- /dev/null +++ b/src/linksBed/Makefile @@ -0,0 +1,44 @@ +CXX = g++ +CXXFLAGS = -O3 -Wall +LDFLAGS = + +UTILITIES_DIR = ../utils/ +OBJ_DIR = ../../obj/ +BIN_DIR = ../../bin/ + +# ------------------- +# define our includes +# ------------------- +INCLUDES = -I$(UTILITIES_DIR)/bedFile/ + +# ---------------------------------- +# define our source and object files +# ---------------------------------- +SOURCES= linksMain.cpp linksBed.cpp +OBJECTS= $(SOURCES:.cpp=.o) +_EXT_OBJECTS=bedFile.o +EXT_OBJECTS=$(patsubst %,$(OBJ_DIR)/%,$(_EXT_OBJECTS)) +BUILT_OBJECTS= $(patsubst %,$(OBJ_DIR)/%,$(OBJECTS)) +PROGRAM= linksBed +LIBS= + +all: $(PROGRAM) + +.PHONY: all + +$(PROGRAM): $(BUILT_OBJECTS) $(EXT_OBJECTS) + @echo " * linking $(PROGRAM)" + @$(CXX) $(LDFLAGS) $(CXXFLAGS) -o $(BIN_DIR)/$@ $^ $(LIBS) + +$(BUILT_OBJECTS): $(SOURCES) + @echo " * compiling" $(*F).cpp + @$(CXX) -c -o $@ $(*F).cpp $(LDFLAGS) $(CXXFLAGS) $(INCLUDES) + +$(EXT_OBJECTS): + @$(MAKE) --no-print-directory -C $(UTILITIES_DIR)/bedFile/ + +clean: + @echo "Cleaning up." + @rm -f $(OBJ_DIR)/* $(BIN_DIR)/* + +.PHONY: clean \ No newline at end of file diff --git a/src/linksBed/linksBed.cpp b/src/linksBed/linksBed.cpp new file mode 100755 index 00000000..ad637bb8 --- /dev/null +++ b/src/linksBed/linksBed.cpp @@ -0,0 +1,134 @@ +// +// linksBed.cpp +// BEDTools +// +// Created by Aaron Quinlan Spring 2009. +// Copyright 2009 Aaron Quinlan. All rights reserved. +// +// Summary: Creates HTML or UCSC browser coordinates from a BED file. +// + +#include "linksBed.h" + +// +// Constructor +// +BedLinks::BedLinks(string &bedFile, string &base, string &org, string &db) { + this->bedFile = bedFile; + this->bed = new BedFile(bedFile); + + this->base = base; + this->org = org; + this->db = db; +} + +// +// Destructor +// +BedLinks::~BedLinks(void) { +} + + +void BedLinks::WriteURL(BED &bed, string &base) { + + string position = bed.chrom; + std::stringstream posStream; + posStream << ":" << bed.start << "-" << bed.end; + position.append(posStream.str()); + + cout << "<tr>" << endl; + cout << "\t<td>" << endl; + cout << "\t\t<a href=" << base << position << ">"; + cout << bed.chrom << ":" << bed.start << "-" << bed.end; + cout << "</a>" << endl; + cout << "\t</td>" << endl; + + if (this->bed->bedType >= 4) { + cout << "\t<td>" << endl; + cout << bed.name << endl; + cout << "\t</td>" << endl; + } + cout << "</tr>" << endl; +} + + +void BedLinks::LinksBed() { + + + // construct the html base. + string org = this->org; + string db = this->db; + string base = this->base; + base.append("/cgi-bin/hgTracks?org="); + base.append(org); + base.append("&db="); + base.append(db); + base.append("&position="); + + // create the HTML header + cout << "<html>" << endl <<"\t<body>" << endl; + cout << "<title>" << this->bedFile << "</title>" << endl; + // start the table of entries + cout << "<br>Firefox users: Press and hold the \"apple\" or \"alt\" key and click link to open in new tab." << endl; + cout << "<p style=\"font-family:courier\">" << endl; + cout << "<table border=\"0\"" << endl; + + + // Are we dealing with a BED file or a BED passed via stdin? + + // Case 1: Proper BED File. + if ( (this->bedFile != "") && (this->bedFile != "stdin") ) { + + // open the BED file for reading + ifstream bed(bedFile.c_str(), ios::in); + if ( !bed ) { + cerr << "Error: The requested bed file (" <<bedFile << ") could not be opened. Exiting!" << endl; + exit (1); + } + + string bedLine; + BED bedEntry; + int lineNum = 0; + + cout << "<h3>BED Entries from: " << this->bedFile << "</h3>" << endl; + + while (getline(bed, bedLine)) { + + vector<string> bedFields; + Tokenize(bedLine,bedFields); + + lineNum++; + + if (this->bed->parseBedLine(bedEntry, bedFields, lineNum)) { + bedEntry.count = 0; + WriteURL(bedEntry, base); + } + } + cout << "</table>" << endl; + cout << "</p>" << endl; + cout << "\t</body>" << endl <<"</html>" << endl; + } + // Case 2: STDIN. + else { + string bedLine; + BED bedEntry; + int lineNum = 0; + + cout << "<h3>BED Entries from: stdin </h3>" << endl; + + while (getline(cin, bedLine)) { + + vector<string> bedFields; + Tokenize(bedLine,bedFields); + + lineNum++; + if (this->bed->parseBedLine(bedEntry, bedFields, lineNum)) { + bedEntry.count = 0; + WriteURL(bedEntry, base); + } + } + cout << "</table>" << endl; + cout << "</p>" << endl; + cout << "\t</body>" << endl <<"</html>" << endl; + } +} diff --git a/src/linksBed/linksBed.h b/src/linksBed/linksBed.h new file mode 100755 index 00000000..f104a999 --- /dev/null +++ b/src/linksBed/linksBed.h @@ -0,0 +1,43 @@ +#include "bedFile.h" +#include <vector> +#include <algorithm> +#include <map> +#include <list> +#include <iostream> +#include <fstream> + +using namespace std; + + +//*********************************************** +// Typedefs +//*********************************************** +typedef list<BED> bedList; + + +//************************************************ +// Class methods and elements +//************************************************ +class BedLinks { + +public: + + // constructor + BedLinks(string &, string &, string &, string &); + + // destructor + ~BedLinks(void); + + void WriteURL(BED &, string &); + void LinksBed(); // the default. sorts by chrom (asc.) then by start (asc.) + +private: + string bedFile; + string base; + string org; + string db; + + // instance of a bed file class. + BedFile *bed; + +}; diff --git a/src/linksBed/linksMain.cpp b/src/linksBed/linksMain.cpp new file mode 100755 index 00000000..ce116329 --- /dev/null +++ b/src/linksBed/linksMain.cpp @@ -0,0 +1,110 @@ +#include <iostream> +#include "linksBed.h" + +using namespace std; + +// define our program name +#define PROGRAM_NAME "linksBed" + +// define the version +#define VERSION "1.1.0" + +// define our parameter checking macro +#define PARAMETER_CHECK(param, paramLen, actualLen) (strncmp(argv[i], param, min(actualLen, paramLen))== 0) && (actualLen == paramLen) + +// function declarations +void ShowHelp(void); + +int main(int argc, char* argv[]) { + + // our configuration variables + bool showHelp = false; + + // input files + string bedFile; + bool haveBed = false; + + string org = "human"; + string db = "hg18"; + string base = "http://genome.ucsc.edu"; + + for(int i = 1; i < argc; i++) { + int parameterLength = (int)strlen(argv[i]); + + if(PARAMETER_CHECK("-h", 2, parameterLength) || + PARAMETER_CHECK("--help", 5, parameterLength)) { + showHelp = true; + } + } + + if(showHelp) ShowHelp(); + + // do some parsing (all of these parameters require 2 strings) + for(int i = 1; i < argc; i++) { + + int parameterLength = (int)strlen(argv[i]); + + if(PARAMETER_CHECK("-i", 2, parameterLength)) { + haveBed = true; + bedFile = argv[i + 1]; + i++; + } + else if(PARAMETER_CHECK("-base", 5, parameterLength)) { + base = argv[i + 1]; + i++; + } + else if(PARAMETER_CHECK("-org", 4, parameterLength)) { + org = argv[i + 1]; + i++; + } + else if(PARAMETER_CHECK("-db", 3, parameterLength)) { + db = argv[i + 1]; + i++; + } + else { + cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl; + showHelp = true; + } + } + + // make sure we have both input files + if (!haveBed) { + cerr << endl << "*****" << endl << "*****ERROR: Need -i BED file. " << endl << "*****" << endl; + showHelp = true; + } + + if (!showHelp) { + BedLinks *bl = new BedLinks(bedFile, base, org, db); + bl->LinksBed(); + return 0; + } + else { + ShowHelp(); + } +} + +void ShowHelp(void) { + + cerr << "===============================================" << endl; + cerr << " " <<PROGRAM_NAME << " v" << VERSION << endl ; + cerr << " Aaron Quinlan, Ph.D. (aaronquinlan@gmail.com) " << endl ; + cerr << " Hall Laboratory, University of Virginia" << endl; + cerr << "===============================================" << endl << endl; + cerr << "Description: Creates HTML links from a BED file." << endl << endl; + cerr << "Usage: " << PROGRAM_NAME << " [OPTIONS] -i <input.bed>" << endl << endl; + + // cerr << "OPTIONS: " << endl; + // cerr << "\t" << "-sizeA\t\t" << "Sort the BED file by feature size in ascending order. Sorts across all chromosomes." << endl << endl; + // cerr << "\t" << "-sizeD\t\t" << "Sort the BED file by feature size in descending order. Sorts across all chromosomes." << endl << endl; + // cerr << "\t" << "-chrThenSizeA\t" << "Sort the BED file by chrom (ascending), then feature size in ascending order." << endl << endl; + // cerr << "\t" << "-chrThenSizeD\t" << "Sort the BED file by chrom (ascending), then feature size in descending order." << endl << endl; + // cerr << "\t" << "-chrThenScoreA\t" << "Sort the BED file by chrom (ascending), then score in ascending order." << endl << endl; + // cerr << "\t" << "-chrThenScoreD\t" << "Sort the BED file by chrom (ascending), then scor size in descending order." << endl << endl; + // + cerr << "NOTES: " << endl; + cerr << "\t" << "-i stdin\t\t" << "Allows BED file A to be read from stdin. E.g.: cat a.bed | sortBed -i stdin" << endl << endl; + cerr << "\t***Only BED3 - BED6 formats allowed.***"<< endl << endl; + // end the program here + exit(1); + +} -- GitLab