Skip to content

Instantly share code, notes, and snippets.

View SamBuckberry's full-sized avatar

Sam Buckberry SamBuckberry

View GitHub Profile
#' Get the signal from bigwig file for defined regions.
#'
#' @param bigwig Path to a bigwig file. Also accpets http paths.
#' @param gr A genomic ranges object of regions to obtain signal. All ranges should be the same width.
#' @param range The distance from the centre of gr to flank. Default=100.
#' @param log Logical. Is the signal in the bigwig file in log space? Default=FALSE.
#' If log = TRUE, the scores in the bigwig file are transformed by e^scores.
#' @return A numeric vector of aggregate signal if aggregate = TRUE, which is the default.
#' If aggregate = FALSE a data.frame of signal of all regions is returned.
#' @export
library(magrittr)
library(GenomicRanges)
library(BSgenome.Mmusculus.UCSC.mm10)
library(rtracklayer)
library(reshape2)
library(ggplot2)
library(dplyr)
library(JASPAR2016)
library(TFBSTools)
library(stringr)
library(magrittr)
library(GenomicRanges)
library(BSgenome.Mmusculus.UCSC.mm10)
library(rtracklayer)
library(reshape2)
library(ggplot2)
library(dplyr)
library(JASPAR2016)
library(TFBSTools)
library(stringr)
### Get Tn5 bias track for regions
bias_bigwig <- "~/Desktop/mm10_bias_chr19.Scores.bigwig"
atac_regions <- "~/polo_iPSC/ATACseq/processed_data/atac_peaks/atac_all_peaks_union.bed"
atac_bam <- "~/Desktop/atac_iPSC_combined_replicates_chr19.bam"
ctcf <- read.table("~/R_packages/RunATAC/inst/exdata/ctcf.pwm") %>% as.matrix()
#' Read BED formatted file to GRanges object
#'
#' @param bed_file Path Path to BED formatted file.
library(data.table)
library(R.utils)
aggregateStrandsCG <- function(CGmap){
# Do not output scientific notation
options(scipen=999)
# Create the temp directory for uncompressed CGmap file
tmpFile <- tempfile(tmpdir = tempdir(), fileext = ".CGmap.tmp")
@SamBuckberry
SamBuckberry / plotAnnotatedScatter.R
Last active August 29, 2015 14:23
Quick plotting of correlations with P-values
# See examples at http://sambuckberry.github.io/2015/07/15/quickCorPlots/
plotAnnotatedScatter <- function(x, y, pointCol=rgb(0,0,0,0.7),
legendPos="topleft", legendCex=1, ... ){
# Generate a linear model summary
fit <- lm(y ~ x)
fitSum <- summary(fit)
r2 <- fitSum$r.squared
pVal <- fitSum$coefficients[2,4]
@SamBuckberry
SamBuckberry / countOverlaps.R
Last active September 30, 2015 06:27
count the number of reads in a BAM file that overlap genomic features defined in GTF file
#source("http://bioconductor.org/biocLite.R")
#biocLite(c("rtracklayer", "Rsamtools", "GenomicRanges"))
library(rtracklayer)
library(Rsamtools)
library(GenomicRanges)
library(GenomicAlignments)
#' SummarizeOverlapsByGene
#'
@SamBuckberry
SamBuckberry / indexBamFiles.txt
Created May 15, 2014 06:17
Create .bam file indexes for all bam files in a directory using samtools and parallel
## Write a bash script for looping through the bam files
#! /bin/bash
for i in *accepted_hits.bam
do
samtools index ${i}
done
## Save this files as indexBamFiles.sh
@SamBuckberry
SamBuckberry / readBAM.R
Last active January 3, 2019 09:13
Import a bam file into R
# install the Rsamtools package if necessary
source("http://bioconductor.org/biocLite.R")
biocLite("Rsamtools")
# load the library
library(Rsamtools)
# specify the bam file you want to import
bamFile <- "test.bam"