Commit 9cb00210 authored by Aaron's avatar Aaron
Browse files

Added bedToIgv.

parent 184656c1
......@@ -12,7 +12,7 @@ export CXXFLAGS = -Wall -O2
export LIBS = -lz
# define our source subdirectories
SUBDIRS = $(SRC_DIR)/bamToBed $(SRC_DIR)/bedToBam $(SRC_DIR)/bed12ToBed6 $(SRC_DIR)/closestBed $(SRC_DIR)/complementBed $(SRC_DIR)/coverageBed $(SRC_DIR)/fastaFromBed $(SRC_DIR)/genomeCoverageBed $(SRC_DIR)/intersectBed $(SRC_DIR)/linksBed $(SRC_DIR)/maskFastaFromBed $(SRC_DIR)/mergeBed $(SRC_DIR)/overlap $(SRC_DIR)/pairToBed $(SRC_DIR)/pairToPair $(SRC_DIR)/shuffleBed $(SRC_DIR)/slopBed $(SRC_DIR)/sortBed $(SRC_DIR)/subtractBed $(SRC_DIR)/windowBed
SUBDIRS = $(SRC_DIR)/bamToBed $(SRC_DIR)/bedToBam $(SRC_DIR)/bedToIgv $(SRC_DIR)/bed12ToBed6 $(SRC_DIR)/closestBed $(SRC_DIR)/complementBed $(SRC_DIR)/coverageBed $(SRC_DIR)/fastaFromBed $(SRC_DIR)/genomeCoverageBed $(SRC_DIR)/intersectBed $(SRC_DIR)/linksBed $(SRC_DIR)/maskFastaFromBed $(SRC_DIR)/mergeBed $(SRC_DIR)/overlap $(SRC_DIR)/pairToBed $(SRC_DIR)/pairToPair $(SRC_DIR)/shuffleBed $(SRC_DIR)/slopBed $(SRC_DIR)/sortBed $(SRC_DIR)/subtractBed $(SRC_DIR)/windowBed
UTIL_SUBDIRS = $(SRC_DIR)/utils/lineFileUtilities $(SRC_DIR)/utils/bedFile $(SRC_DIR)/utils/genomeFile $(SRC_DIR)/utils/gzstream $(SRC_DIR)/utils/bedFilePE $(SRC_DIR)/utils/sequenceUtilities $(SRC_DIR)/utils/BamTools
......
UTILITIES_DIR = ../utils/
OBJ_DIR = ../../obj/
BIN_DIR = ../../bin/
# -------------------
# define our includes
# -------------------
INCLUDES = -I$(UTILITIES_DIR)/bedFile/ -I$(UTILITIES_DIR)/genomeFile/ -I$(UTILITIES_DIR)/lineFileUtilities/ -I$(UTILITIES_DIR)/version/ -I$(UTILITIES_DIR)/gzstream/
# ----------------------------------
# define our source and object files
# ----------------------------------
SOURCES= bedToIgv.cpp
OBJECTS= $(SOURCES:.cpp=.o)
_EXT_OBJECTS=bedFile.o genomeFile.o lineFileUtilities.o gzstream.o
EXT_OBJECTS=$(patsubst %,$(OBJ_DIR)/%,$(_EXT_OBJECTS))
BUILT_OBJECTS= $(patsubst %,$(OBJ_DIR)/%,$(OBJECTS))
PROGRAM= bedToIgv
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)/lineFileUtilities/
@$(MAKE) --no-print-directory -C $(UTILITIES_DIR)/bedFile/
@$(MAKE) --no-print-directory -C $(UTILITIES_DIR)/genomeFile/
@$(MAKE) --no-print-directory -C $(UTILITIES_DIR)/gzstream/
clean:
@echo "Cleaning up."
@rm -f $(OBJ_DIR)/* $(BIN_DIR)/*
.PHONY: clean
/*****************************************************************************
bedToIgv.cpp
(c) 2009 - Aaron Quinlan
Hall Laboratory
Department of Biochemistry and Molecular Genetics
University of Virginia
aaronquinlan@gmail.com
Licenced under the GNU General Public License 2.0+ license.
******************************************************************************/
#include "lineFileUtilities.h"
#include "bedFile.h"
#include "genomeFile.h"
#include "version.h"
#include <vector>
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
// define our program name
#define PROGRAM_NAME "bedToIgv"
// 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);
void DetermineBedInput(BedFile *bed, string path, string sortType, string session,
bool collapse, bool useNames, string imageType, int slop);
void ProcessBed(istream &bedInput, BedFile *bed, string path, string sortType, string sortType,
bool collapse, bool useNames, string imageType, int slop);
int main(int argc, char* argv[]) {
// our configuration variables
bool showHelp = false;
// input files
string bedFile;
string imagePath = "./";
string sortType = "none";
string session = "none";
int slop = 0;
string imageType = "png";
bool haveBed = false;
bool collapse = false;
bool useNames = false;
// check to see if we should print out some help
if(argc <= 1) showHelp = true;
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)) {
if ((i+1) < argc) {
haveBed = true;
bedFile = argv[i + 1];
i++;
}
}
else if(PARAMETER_CHECK("-path", 5, parameterLength)) {
if ((i+1) < argc) {
imagePath = argv[i + 1];
i++;
}
}
else if(PARAMETER_CHECK("-sort", 5, parameterLength)) {
if ((i+1) < argc) {
sortType = argv[i + 1];
i++;
}
}
else if(PARAMETER_CHECK("-sess", 5, parameterLength)) {
if ((i+1) < argc) {
session = argv[i + 1];
i++;
}
}
else if(PARAMETER_CHECK("-clps", 5, parameterLength)) {
collapse = true;
}
else if(PARAMETER_CHECK("-name", 5, parameterLength)) {
useNames = true;
}
else if(PARAMETER_CHECK("-slop", 5, parameterLength)) {
if ((i+1) < argc) {
slop = atoi(argv[i + 1]);
i++;
}
}
else if(PARAMETER_CHECK("-img", 4, parameterLength)) {
if ((i+1) < argc) {
imageType = argv[i + 1];
i++;
}
}
else {
cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl;
showHelp = true;
}
}
// make sure we have an input files
if (!haveBed ) {
cerr << endl << "*****" << endl << "*****ERROR: Need -i (BED) file. " << endl << "*****" << endl;
showHelp = true;
}
if (sortType != "none") {
if ((sortType != "base") && (sortType != "position") && (sortType != "strand") &&
(sortType != "quality") && (sortType != "sample") && (sortType != "readGroup")) {
cerr << endl << "*****" << endl << "*****ERROR: Invalid sort option. " << endl << "*****" << endl;
showHelp = true;
}
}
if (slop < 0) {
cerr << endl << "*****" << endl << "*****ERROR: Slop must be >= 0. " << endl << "*****" << endl;
showHelp = true;
}
if (!showHelp) {
BedFile *bed = new BedFile(bedFile);
DetermineBedInput(bed, imagePath, sortType, session, collapse, useNames, imageType, slop);
}
else {
ShowHelp();
}
}
void ShowHelp(void) {
cerr << endl << "Program: " << PROGRAM_NAME << " (v" << VERSION << ")" << endl;
cerr << "Author: Aaron Quinlan (aaronquinlan@gmail.com)" << endl;
cerr << "Summary: Creates a batch script to create IGV images " << endl;
cerr << " at each interval defined in a BED/GFF/VCF file." << endl << endl;
cerr << "Usage: " << PROGRAM_NAME << " [OPTIONS] -i <bed/gff/vcf> -g <genome>" << endl << endl;
cerr << "Options: " << endl;
cerr << "\t-path\t" << "The full path to which the IGV snapshots should be written." << endl;
cerr << "\t\t(STRING) Default: ./" << endl << endl;
cerr << "\t-sess\t" << "The full path to an existing IGV session file to be " << endl;
cerr << "\t\tloaded prior to taking snapshots." << endl << endl;
cerr << "\t\t(STRING) Default is for no session to be loaded." << endl << endl;
cerr << "\t-sort\t" << "The type of BAM sorting you would like to apply to each image. " << endl;
cerr << "\t\tOptions: base, position, strand, quality, sample, and readGroup" << endl;
cerr << "\t\tDefault is to apply no sorting at all." << endl << endl;
cerr << "\t-clps\t" << "Collapse the aligned reads prior to taking a snapshot. " << endl;
cerr << "\t\tDefault is to no collapse." << endl << endl;
cerr << "\t-name\t" << "Use the \"name\" field (column 4) for each image's filename. " << endl;
cerr << "\t\tDefault is to use the \"chr:start-pos.ext\"." << endl << endl;
cerr << "\t-slop\t" << "Number of flanking base pairs on the left & right of the image." << endl;
cerr << "\t\t- (INT) Default = 0." << endl << endl;
cerr << "\t-img\t" << "The type of image to be created. " << endl;
cerr << "\t\tOptions: png, eps, svg" << endl;
cerr << "\t\tDefault is png." << endl << endl;
cerr << "Notes: " << endl;
cerr << "\t(1) The resulting script is meant to be run from within the IGV GUI version 1.5 or later." << endl;
cerr << "\t(2) Unless you use the -sess option, it is assumed that prior to running the script, " << endl;
cerr << "\t\tyou have loaded the proper genome, tracks and data files." << endl << endl;
// end the program here
exit(1);
}
void DetermineBedInput(BedFile *bed, string path, string sortType, string session,
bool collapse, bool useNames, string imageType, int slop) {
// dealing with a proper file
if (bed->bedFile != "stdin") {
ifstream bedStream(bed->bedFile.c_str(), ios::in);
if ( !bedStream ) {
cerr << "Error: The requested bed file (" << bed->bedFile << ") could not be opened. Exiting!" << endl;
exit (1);
}
ProcessBed(bedStream, bed, path, sortType, session, collapse, useNames, imageType, slop);
}
// reading from stdin
else {
ProcessBed(cin, bed, path, sortType, session, collapse, useNames, imageType, slop);
}
}
void ProcessBed(istream &bedInput, BedFile *bed, string path, string sortType, string session,
bool collapse, bool useNames, string imageType, int slop) {
// set the image path
cout << "snapshotDirectory " << path << endl;
// should we load a session
if (session != "none")
cout << "load " << session << endl;
// process each BED entry and convert to an IGV request
BED bedEntry, nullBed;
int lineNum = 0;
BedLineStatus bedStatus;
bed->Open();
while ((bedStatus = bed->GetNextBed(bedEntry, lineNum)) != BED_INVALID) {
if (bedStatus == BED_VALID) {
string locus, filename;
if (useNames == false) {
locus = bedEntry.chrom + ":" + ToString(bedEntry.start - slop) + "-" + ToString(bedEntry.end + slop);
filename = locus;
}
else {
locus = bedEntry.chrom + ":" + ToString(bedEntry.start - slop) + "-" + ToString(bedEntry.end + slop);
if (bedEntry.name.empty() == false)
filename = bedEntry.name;
else {
cerr << "Error: You requested that filenames be based upon the name field. However, it appears to be empty. Exiting!" << endl;
exit (1);
}
}
// goto
cout << "goto " << locus << endl;
// sort
if (sortType != "none")
cout << "sort " << sortType << endl;
// collapse
if (collapse == true)
cout << "collapse" << endl;
// snapshot
cout << "snapshot " << filename << "." << imageType << endl;
bedEntry = nullBed;
}
}
// close up
bed->Close();
}
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