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

Added bamToBed.

parent f7286189
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,7 @@ export CXX ?= g++
#include includes/$(BLD_PLATFORM).inc
# define our source subdirectories
SUBDIRS = $(SRC_DIR)/closestBed $(SRC_DIR)/complementBed $(SRC_DIR)/coverageBed $(SRC_DIR)/intersectBed $(SRC_DIR)/mergeBed $(SRC_DIR)/genomeCoverageBed $(SRC_DIR)/fastaFromBed $(SRC_DIR)/shuffleBed $(SRC_DIR)/slopBed $(SRC_DIR)/sortBed $(SRC_DIR)/windowBed $(SRC_DIR)/subtractBed $(SRC_DIR)/linksBed $(SRC_DIR)/pairToBed $(SRC_DIR)/pairToPair $(SRC_DIR)/maskFastaFromBed
SUBDIRS = $(SRC_DIR)/closestBed $(SRC_DIR)/complementBed $(SRC_DIR)/coverageBed $(SRC_DIR)/intersectBed $(SRC_DIR)/mergeBed $(SRC_DIR)/genomeCoverageBed $(SRC_DIR)/fastaFromBed $(SRC_DIR)/shuffleBed $(SRC_DIR)/slopBed $(SRC_DIR)/sortBed $(SRC_DIR)/windowBed $(SRC_DIR)/subtractBed $(SRC_DIR)/linksBed $(SRC_DIR)/pairToBed $(SRC_DIR)/pairToPair $(SRC_DIR)/maskFastaFromBed $(SRC_DIR)/bamToBed
UTIL_SUBDIRS = $(SRC_DIR)/utils/lineFileUtilities $(SRC_DIR)/utils/bedFile $(SRC_DIR)/utils/bedFilePE $(SRC_DIR)/utils/sequenceUtilities
......
File added
No preview for this file type
File added
No preview for this file type
No preview for this file type
CXX= g++
CXXFLAGS= -Wall -O3 -m64 -g
LIBS= -lz
UTILITIES_DIR = ../utils/
OBJ_DIR = ../../obj/
BIN_DIR = ../../bin/
# -------------------
# define our includes
# -------------------
INCLUDES = -I$(UTILITIES_DIR)/BamTools/ -I$(UTILITIES_DIR)/version/
# ----------------------------------
# define our source and object files
# ----------------------------------
SOURCES= bamToBed.cpp
OBJECTS= $(SOURCES:.cpp=.o)
_EXT_OBJECTS=BamReader.o BGZF.o
EXT_OBJECTS=$(patsubst %,$(OBJ_DIR)/%,$(_EXT_OBJECTS))
BUILT_OBJECTS= $(patsubst %,$(OBJ_DIR)/%,$(OBJECTS))
PROGRAM= bamToBed
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)/BamTools/
clean:
@echo "Cleaning up."
@rm -f $(OBJ_DIR)/* $(BIN_DIR)/*
.PHONY: clean
/*****************************************************************************
bamToBed.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 "version.h"
#include "BamReader.h"
#include "BamWriter.h"
#include "BamAux.h"
using namespace BamTools;
#include <vector>
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
// define our program name
#define PROGRAM_NAME "bamToBed"
// 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 PrintBed(const BamAlignment &bam, const RefVector &refs, bool useEditDistance);
void PrintBedPE(const BamAlignment &bam, const RefVector &refs, bool useEditDistance);
int main(int argc, char* argv[]) {
// our configuration variables
bool showHelp = false;
// input files
string bamFile;
bool haveBam = false;
bool writeBedPE = false;
bool useEditDistance = 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) {
haveBam = true;
bamFile = argv[i + 1];
}
i++;
}
else if(PARAMETER_CHECK("-bedpe", 6, parameterLength)) {
writeBedPE = true;
}
else if(PARAMETER_CHECK("-ed", 3, parameterLength)) {
useEditDistance = true;
}
else {
cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl;
showHelp = true;
}
}
// make sure we have an input files
if (!haveBam ) {
cerr << endl << "*****" << endl << "*****ERROR: Need -i (BAM) file. " << endl << "*****" << endl;
showHelp = true;
}
// make sure we have an input files
if (writeBedPE && useEditDistance) {
cerr << endl << "*****" << endl << "*****ERROR: Cannot use edit distance with BEDPE. " << endl << "*****" << endl;
showHelp = true;
}
if (!showHelp) {
// open the BAM file
BamReader reader;
reader.Open(bamFile);
// get header & reference information
string header = reader.GetHeaderText();
RefVector refs = reader.GetReferenceData();
BamAlignment bam;
while (reader.GetNextAlignment(bam)) {
if (!writeBedPE) {
PrintBed(bam, refs, useEditDistance);
}
else {
PrintBedPE(bam, refs, useEditDistance);
}
}
reader.Close();
}
else {
ShowHelp();
}
}
void ShowHelp(void) {
cerr << endl << "Program: " << PROGRAM_NAME << " (v" << VERSION << ")" << endl;
cerr << "Author: Aaron Quinlan (aaronquinlan@gmail.com)" << endl;
cerr << "Summary: Converts BAM alignments to BED6 or BEDPE format." << endl << endl;
cerr << "Usage: " << PROGRAM_NAME << " [OPTIONS] -i <bam> " << endl << endl;
cerr << "Options: " << endl;
cerr << "\t-bedpe\t" << "Write BEDPE format." << endl << endl;
cerr << "\t-ed\t" << "Use BAM edit distance (NM tag) for score." << endl;
cerr << "\t\tDefault is to use mapping quality." << endl;
cerr << "\t\tNot available for BEDPE format." << endl << endl;
// end the program here
exit(1);
}
void PrintBed(const BamAlignment &bam, const RefVector &refs, bool useEditDistance) {
string strand = "+";
if (bam.IsReverseStrand()) strand = "-";
if (useEditDistance == false) {
printf("%s\t%d\t%d\t\%s\t%d\t%s\n", refs.at(bam.RefID).RefName.c_str(), bam.Position,
bam.Position + bam.Length, bam.Name.c_str(),
bam.MapQuality, strand.c_str());
}
else {
uint8_t editDistance;
if (bam.GetEditDistance(editDistance)) {
printf("%s\t%d\t%d\t\%s\t%u\t%s\n", refs.at(bam.RefID).RefName.c_str(), bam.Position,
bam.Position + bam.Length, bam.Name.c_str(),
editDistance, strand.c_str());
}
}
}
void PrintBedPE(const BamAlignment &bam, const RefVector &refs, bool useEditDistance) {
if (bam.InsertSize > 0) {
string strand1 = "+";
string strand2 = "+";
if (bam.IsReverseStrand()) strand1 = "-";
if (bam.IsMateReverseStrand()) strand2 = "-";
printf("%s\t%d\t%d\t\%s\t%d\t%d\t%s\t%d\t%s\t%s\n",
refs.at(bam.RefID).RefName.c_str(), bam.Position, bam.Position + bam.Length,
refs.at(bam.MateRefID).RefName.c_str(), bam.MatePosition, bam.MatePosition + bam.Length,
bam.Name.c_str(), bam.MapQuality, strand1.c_str(), strand2.c_str());
}
}
\ No newline at end of file
../intersectBed/hmec.50.txt.pairs.conc.bam
\ No newline at end of file
chr1 50 60
chr1 100 200
chr1 400 500
chr1 500 800
chr1 100
chr1 1000
......@@ -248,14 +248,10 @@ void BedIntersect::IntersectBam(string bamFile) {
if (this->bamOutput == true) {
overlapsFound = FindOneOrMoreOverlap(a, hits);
if (overlapsFound == true) {
if (!this->noHit) {
writer.SaveAlignment(bamAlignment);
}
if (!this->noHit) writer.SaveAlignment(bamAlignment)
}
else {
if (this->noHit) {
writer.SaveAlignment(bamAlignment);
}
if (this->noHit) writer.SaveAlignment(bamAlignment);
}
}
else {
......
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