Skip to content

Instantly share code, notes, and snippets.

View davetang's full-sized avatar
🦀
🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀

Dave Tang davetang

🦀
🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀
View GitHub Profile
@davetang
davetang / first.pl
Last active December 22, 2015 09:29
My very first Gist
#!/bin/env perl
use strict;
use warnings;
print "My very first Gist on GitHub!\n";
exit(0);
__END__
@davetang
davetang / read_bam.R
Last active January 19, 2023 18:18
Read in BAM file and store as a data frame using Bioconductor's Rsamtools
#install if necessary
source("http://bioconductor.org/biocLite.R")
biocLite("Rsamtools")
#load library
library(Rsamtools)
#read in entire BAM file
bam <- scanBam("wgEncodeRikenCageHchCellPapAlnRep1.bam")
@davetang
davetang / permutation_with_replacement.R
Created September 8, 2013 14:23
Calculating the number of permutations with repetition/replacement
#install if necessary
install.packages('gtools')
#load library
library(gtools)
#urn with 3 balls
x <- c('red', 'blue', 'black')
#pick 2 balls from the urn with replacement
#get all permutations
permutations(n=3,r=2,v=x,repeats.allowed=T)
# [,1] [,2]
@davetang
davetang / bam_df_to_coverage.R
Last active December 22, 2015 14:19
After storing a BAM file as a data frame (as per https://gist.github.com/davetang/6460320), we can calculate the coverage of mapped reads by using a density plot
#use chr22 as an example
#how many entries on the negative strand of chr22?
table(bam_df$rname == 'chr22' & bam_df$flag == 16)
# FALSE TRUE
#3875997 24413
#function for checking negative strand
check_neg <- function(x){
if (intToBits(x)[5] == 1){
return(T)
@davetang
davetang / permutation_without_replacement.R
Created September 8, 2013 14:28
Calculating the number of permutations without repetition/replacement
#install if necessary
install.packages('gtools')
#load library
library(gtools)
#urn with 3 balls
x <- c('red', 'blue', 'black')
#pick 2 balls from the urn with replacement
#get all permutations
permutations(n=3,r=2,v=x)
# [,1] [,2]
@davetang
davetang / perm_without_replacement.R
Created September 8, 2013 15:20
Function for calculating number of permutations without replacement
perm_without_replacement <- function(n, r){
return(factorial(n)/factorial(n - r))
}
#16 choices, choose 16
perm_without_replacement(16,16)
#[1] 2.092279e+13
#16 choices, choose 3
perm_without_replacement(16,3)
@davetang
davetang / comb_with_replacement.R
Created September 9, 2013 02:43
Calculate the number of combinations with replacement
comb_with_replacement <- function(n, r){
return( factorial(n + r - 1) / (factorial(r) * factorial(n - 1)) )
}
#have 3 elements, choosing 3
comb_with_replacement(3,3)
#[1] 10
@davetang
davetang / cmd_line_param.R
Last active December 22, 2015 17:28
An example R script that takes in 2 command line parameters, checks whether these 2 arguments are digits and then sums them.
#!/bin/env Rscript
#usage
usage <- 'Usage: cmd_line_param.R <integer_1> <integer_2>';
#store command line arguments
args <- commandArgs(trailingOnly = T)
#conditional checks
if (length(args) != 2){
@davetang
davetang / random_region.R
Last active April 17, 2018 20:56
Sampling random regions of the hg19 genome and obtaining the corresponding sequence
#how many regions to sample?
number <- 50000
#how many bp to add to start
span <- 4
#some necessary packages
#install if necessary
source("http://bioconductor.org/biocLite.R")
biocLite("BSgenome")
@davetang
davetang / biomart_refseq.R
Last active May 12, 2022 06:55
Using biomaRt to fetch all human mRNA refSeqs and their corresponding chromosome coordinates
#install if necessary
source("http://bioconductor.org/biocLite.R")
biocLite("biomaRt")
#load library
library("biomaRt")
#use ensembl mart
ensembl <- useMart("ensembl",dataset="hsapiens_gene_ensembl")