If you are tired of remembering (googling) how to extract different kinds of archives like me, use this gem I found.
The extract function can deal with all kinds of archives. I found it is very handy.
see it from the original author on github.
A wet-dry hybrid biologist's take on genetics and genomics. Mostly is about Linux, R, python, reproducible research, open science and NGS. Grab my book to transform yourself to a computational biologist https://divingintogeneticsandgenomics.ck.page/

This blog by Tommy Tang is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Monday, April 25, 2016
Wednesday, April 20, 2016
library size normalization for ChIP-seq
I have discussed how to use DESeq2 to do differential binding for ChIP-seq at here.
I am experimenting
DiffBind to do the same thing, which internally uses EdgR, DESeq and DESeq2. The author Rory Stark is very responsive on the bioconductor support site and has answered several of my questions.
Today, I am going to keep a note here for normalizing the ChIP-seq data. If one compares ChIP-seq versus RNA-seq data, they are in the end all counts data. For RNA-seq, we usually get a read count table for the counts in the exons (union of them is for a gene); for ChIP-seq, we get a read count table for counts within the peaks. The peaks have to be identified by other tools such as MACS first. The counts data follow a (negative) binomial distribution. That's why tools such as DESeq2, which was developed for RNAseq is used for ChIP-seq.
After we get a count table, it comes to the normalization problem. If you are interested, read this paper Beyond library size: a field guide to NGS normalization. In the
DiffBind package, the counts table is obtained by a function ?dba.count.
There are several ways to specify how the counts are normalized for the binding affinity matrix:
score
which score to use in the binding affinity matrix. Note that all raw read counts are maintained for use by dba.analyze, regardless of how this is set. One of:
DBA_SCORE_READS raw read count for interval using only reads from ChIP
DBA_SCORE_READS_FOLD raw read count for interval from ChIP divided by read count for interval from control
DBA_SCORE_READS_MINUS raw read count for interval from ChIP minus read count for interval from control
DBA_SCORE_RPKM RPKM for interval using only reads from ChIP
DBA_SCORE_RPKM_FOLD RPKM for interval from ChIP divided by RPKM for interval from control
DBA_SCORE_TMM_READS_FULL TMM normalized (using edgeR), using ChIP read counts and Full Library size
DBA_SCORE_TMM_READS_EFFECTIVE TMM normalized (using edgeR), using ChIP read counts and Effective Library size
DBA_SCORE_TMM_MINUS_FULL TMM normalized (using edgeR), using ChIP read counts minus Control read counts and Full Library size
DBA_SCORE_TMM_MINUS_EFFECTIVE TMM normalized (using edgeR), using ChIP read counts minus Control read counts and Effective Library size
DBA_SCORE_TMM_READS_FULL_CPM same as DBA_SCORE_TMM_READS_FULL, but reporrted in counts-per-million.
DBA_SCORE_TMM_READS_EFFECTIVE_CPM same as DBA_SCORE_TMM_READS_EFFECTIVE, but reporrted in counts-per-million.
DBA_SCORE_TMM_MINUS_FULL_CPM same as DBA_SCORE_TMM_MINUS_FULL, but reporrted in counts-per-million.
DBA_SCORE_TMM_MINUS_EFFECTIVE_CPM Tsame as DBA_SCORE_TMM_MINUS_EFFECTIVE, but reporrted in counts-per-million.
DBA_SCORE_TMM_READS_FULL vs DBA_SCORE_TMM_READS_EFFECTIVE:Diffbind let's you to choose use full library size or effective library size for trimmed mean of M values(TMM) normalization which was proposed by Mark D Robinson for RNAseq.
Full library size is the number of reads in the bam files.
Effective library size is the number of reads mapped in the exons or within the peaks. It is the column sums for the matrix.
Effective library size is the number of reads mapped in the exons or within the peaks. It is the column sums for the matrix.
Note that effective library size (bFullLibrarySize =FALSE) may be more appropriate for situations when the overall signal (binding rate) is expected to be directly comparable between the samples.
If one wants to subtract the input reads, one can use
DBA_SCORE_TMM_MINUS_FULL and DBA_SCORE_TMM_MINUS_EFFECTIVE
No matter what score you choose, for differential binding analysis in
Diffbind, it is always the raw counts is used for the binding matrix. Diffbind (by default) subtract the input raw reads for subsequent analysis. Whether or not this is good was discussed here.
For example, if one uses
DESeq2, the details are as follows:For each contrast, a separate analysis is performed. First, a matrix of counts is constructed for the contrast, with columns for all the samples in the first group, followed by columns for all the samples in the second group. The raw read count is used for this matrix; if the bSubControl parameter is set to TRUE (as it is by default), the raw number of reads in the control sample (if available) will be subtracted. Next the library size is computed for each sample for use in subsequent normalization. By default, this is the total number of reads in peaks (the sum of each column). Alternatively, if the bFullLibrarySize parameter is set to TRUE, the total number of reads in the library (calculated from the source BAM/BED file) is used. The first step concludes with a call to DESeq2’s DESeqDataSetFromMatrix function, which returns a DESeqDataSet object. If bFullLibrarySize is set to TRUE, then sizeFactors is called with the number of reads in the BAM/BED files for each ChIP sample, divided by the minimum of these; otherwise,estimateSizeFactorsis invoked.estimateDispersionsis then called with theDESeqDataSetobject andfitTypeset to local. Next the model is fitted and tested using nbinomWaldTest
estimateSizeFactors in DESeq2:Given a matrix or data frame of count data, this function estimates the size factors as follows: Each column is divided by the geometric means of the rows. The median (or, ir requested, another location estimator) of these ratios (skipping the genes with a geometric mean of zero) is used as the size factor for this column.
Friday, March 25, 2016
The most powerful unix commands I have learned so far: find + parallel
I have been using unix since 2013, yet I learn unix tricks almost everyday. The most powerful commands I have learned so far is the find, xargs and parallel commands.
please check
parallel GNU page for documentations.parallel has changed my way to do repetitive works. Now, I use fewer and fewer for loops.
Use case 1: I have 100 folders with names starting with H3K4me3, inside each folder, I have 5
.gz files that I want to cat together. The usual way to do it:# !/bin/bash
for dir in H3K4me3*/
do
cd $dir && cat *H3K4me3.bed.gz > ${dir}_merged.gz
cd ..
done
Note that cat works well with *gz files.
The parallel way:
ls -d H3K4me3* | parallel 'find {} -name "*H3K4me3*bed.gz" | xargs cat > {}_H3K4me3.bed.gz'
Using
parallel, I can take full advantage of the multi-core nodes on the computing cluster, so it is much faster.
Use case 2: I have 100 folders (50 folder names start with H3K4me3, 50 start with H3K4me), each folder has multiple levels of sub-folders. I want to delete some bam files in 50 of them with name starting with H3K4me3, but I do not know which sub-folder the bam files may exist.
I do not really know a way to do it without using
find. My solution would be:ls -d H3K4me3* | parallel 'find {} -name "*bam"' | parallel rm {}
piping to two
Edit on 04/04/2016:
With greater power comes greater responsibility. When you have too many files to process,
it is good to restrict parallel to only use certain number of CPUs with -j and not use swap-memory --noswap.
parallel is the magic of this solution. Unix commands are elegant and efficient!!Edit on 04/04/2016:
With greater power comes greater responsibility. When you have too many files to process,
it is good to restrict parallel to only use certain number of CPUs with -j and not use swap-memory --noswap.
Thursday, March 3, 2016
Breakpoints clustering for structural variants
Ming Tang
March 2, 2016
When structural variants are represented in
bedpe format, each structural variant is represented as two linked breakpoints. Each breakpoint is represented as a genomic interval (GRanges). It is very similar to representations of Genomic interactions (data from Hi-C, ChIA-PET):
e.g.
chr1 100 200 chr2 300 400
one of the common task is to merge the breakpoints when they are overlapping.
———|———|———————–|———-|—–
—–|———|————————-|—————|—
merged to
—–|————-|———————|—————|—
It is not a trivial problem as stated by Aaron Qunlan in this post
It is the so called “breakpoint clustering” problem. I am going to use
InteractionSet bioconductor package to solve this problem.
Please also check
clusterPairs function in the diffHic package.
see answer from the author of
InteractionSet. Note that InteractionSet is still under development. Please refer to the tutorial for more usages. Install it using devtools:install_github("LTLA/InteractionSet").
Make some dummy GRanges.
library(InteractionSet)
all.regions <- GRanges(rep("chrA",8),
IRanges(c(1,6,2,9,5,2,15,20), c(3,10,4,12,7,4,18,23)))
index.1 <- c(1,3,5,7)
index.2 <- c(2,4,6,8)
Using
mode=strict when constructing the GInteraction object or using the swapAnchors method is to ensure that the first anchor index is always less than the second anchor index for each interaction. This eliminates redundant permutations of anchor regions and ensures that an interaction between regions #1 and #2 is treated the same as an interaction between regions #2 and #1. Obviously, this assumes that redundant permutations are uninteresting.gi <- GInteractions(index.1, index.2, all.regions, mode ="strict")
gi
## StrictGInteractions object with 4 interactions and 0 metadata columns:
## seqnames1 ranges1 seqnames2 ranges2
## <Rle> <IRanges> <Rle> <IRanges>
## [1] chrA [ 1, 3] --- chrA [ 6, 10]
## [2] chrA [ 2, 4] --- chrA [ 9, 12]
## [3] chrA [ 2, 4] --- chrA [ 5, 7]
## [4] chrA [15, 18] --- chrA [20, 23]
## -------
## regions: 8 ranges and 0 metadata columns
## seqinfo: 1 sequence from an unspecified genome; no seqlengths
The first three pairs of interactions can be merged. Note that two anchors of each pair are overlapping. First, use
findOverlaps for GInteraction object to find out which pairs with two anchors are overlapping. (2D overlapping)out<- findOverlaps(gi)
out
## Hits object with 8 hits and 0 metadata columns:
## queryHits subjectHits
## <integer> <integer>
## [1] 1 1
## [2] 1 2
## [3] 1 3
## [4] 2 1
## [5] 2 2
## [6] 3 1
## [7] 3 3
## [8] 4 4
## -------
## queryLength: 4 / subjectLength: 4
This will identify all pairs of interactions in gi that have two-dimensional overlaps with each other, i.e., both anchor regions in one interaction overlap with corresponding anchor regions in the other interaction. Using graph algorithm to cluster the anchors (I myself is by no means an algorithm person :)). We then do:
library(RBGL)
## need to trick the function by specifying edgemode to be directional
g <- ftM2graphNEL(as.matrix(out), W=NULL, V=NULL, edgemode="directed")
## if you want to see what's going on
library(Rgraphviz)
plot(g)
## change it back to undirected
edgemode(g) <- "undirected"
plot(g)
connections <- connectedComp(g)
To identify groups of interactions that overlap at least one other interaction in the same group (i.e., “single-linkage clusters” of interactions that have overlapping areas in the two-dimensional interaction space). The clusters can be explicitly identified using:
cluster <- integer(length(gi))
for (i in seq_along(connections)) {
cluster[as.integer(connections[[i]])] <- i
}
We can then identify the bounding box (merge the overlapping interactions) for each cluster of interactions with:
boundingBox(gi, cluster)
## GInteractions object with 2 interactions and 0 metadata columns:
## seqnames1 ranges1 seqnames2 ranges2
## <Rle> <IRanges> <Rle> <IRanges>
## 1 chrA [ 1, 4] --- chrA [ 5, 12]
## 2 chrA [15, 18] --- chrA [20, 23]
## -------
## regions: 4 ranges and 0 metadata columns
## seqinfo: 1 sequence from an unspecified genome; no seqlengths
overlap with one anchor and get the other anchor
I have
of.interest overlaps with the second anchor, and I want to return the first anchor. how can I do it?
e.g.
first (promoters) second
——|————-|———————-|————|——– gi
|------------| of.interest (enhancers)
of.interest <- GRanges(c("chrA","chrA"), IRanges(start=c(2, 5),end=c(8,8)))
# get overlaps between of.interest and first anchor
hits1 <- findOverlaps(gi, of.interest, use.region="first")
anchors(gi[queryHits(hits1),], type="first") # overlapping
## GRanges object with 3 ranges and 0 metadata columns:
## seqnames ranges strand
## <Rle> <IRanges> <Rle>
## [1] chrA [1, 3] *
## [2] chrA [2, 4] *
## [3] chrA [2, 4] *
## -------
## seqinfo: 1 sequence from an unspecified genome; no seqlengths
anchors(gi[queryHits(hits1),], type="second") # "other"
## GRanges object with 3 ranges and 0 metadata columns:
## seqnames ranges strand
## <Rle> <IRanges> <Rle>
## [1] chrA [6, 10] *
## [2] chrA [9, 12] *
## [3] chrA [5, 7] *
## -------
## seqinfo: 1 sequence from an unspecified genome; no seqlengths
of.interest[subjectHits(hits1)]
## GRanges object with 3 ranges and 0 metadata columns:
## seqnames ranges strand
## <Rle> <IRanges> <Rle>
## [1] chrA [2, 8] *
## [2] chrA [2, 8] *
## [3] chrA [2, 8] *
## -------
## seqinfo: 1 sequence from an unspecified genome; no seqlengths
# get overlaps # between of.interest and second anchor
hits2 <- findOverlaps(gi, of.interest, use.region="second")
anchors(gi[queryHits(hits2),], type="second") # overlapping
## GRanges object with 4 ranges and 0 metadata columns:
## seqnames ranges strand
## <Rle> <IRanges> <Rle>
## [1] chrA [6, 10] *
## [2] chrA [6, 10] *
## [3] chrA [5, 7] *
## [4] chrA [5, 7] *
## -------
## seqinfo: 1 sequence from an unspecified genome; no seqlengths
anchors(gi[queryHits(hits2),], type="first") # "other"
## GRanges object with 4 ranges and 0 metadata columns:
## seqnames ranges strand
## <Rle> <IRanges> <Rle>
## [1] chrA [1, 3] *
## [2] chrA [1, 3] *
## [3] chrA [2, 4] *
## [4] chrA [2, 4] *
## -------
## seqinfo: 1 sequence from an unspecified genome; no seqlengths
of.interest[subjectHits(hits2)]
## GRanges object with 4 ranges and 0 metadata columns:
## seqnames ranges strand
## <Rle> <IRanges> <Rle>
## [1] chrA [2, 8] *
## [2] chrA [5, 8] *
## [3] chrA [2, 8] *
## [4] chrA [5, 8] *
## -------
## seqinfo: 1 sequence from an unspecified genome; no seqlengths
If you’re willing to sacrifice some information, you might consider using the linearize method:
gi$index <- seq_along(gi)
linearize(gi, of.interest[1])
## GRanges object with 3 ranges and 1 metadata column:
## seqnames ranges strand | index
## <Rle> <IRanges> <Rle> | <integer>
## [1] chrA [1, 10] * | 1
## [2] chrA [9, 12] * | 2
## [3] chrA [2, 7] * | 3
## -------
## seqinfo: 1 sequence from an unspecified genome; no seqlengths
linearize(gi, of.interest[2])
## GRanges object with 2 ranges and 1 metadata column:
## seqnames ranges strand | index
## <Rle> <IRanges> <Rle> | <integer>
## [1] chrA [1, 3] * | 1
## [2] chrA [2, 4] * | 3
## -------
## seqinfo: 1 sequence from an unspecified genome; no seqlengths
This will return a GRanges containing the “other” anchor region for all interactions that overlap each entry in of.interest (you can figure out what those interactions were by looking at index in the returned object). linearize also provides options for handling “internal” interactions where both anchor regions overlap the specified entry of of.interest; by default, the union of the two anchor regions is returned for such interactions, but they can be removed entirely by setting internal=FALSE.
Alternatively, if you already have two sets of regions(promoters, enhancers) you want to know which enhancer links to which promoter
all.enhancers<- GRanges(c("chrA","chrA"), IRanges(c(2,17), c(5,19)))
all.genes<- GRanges(c("chrA","chrA"), IRanges(c(4,6), c(7,16)))
linkOverlaps(gi, all.genes, all.enhancers)
## query subject1 subject2
## 1 1 1 1
## 2 1 2 1
## 3 2 2 1
## 4 3 1 1
## 5 3 2 1
which two genes are linked?
linkOverlaps(gi, all.genes)
## query subject1 subject2
## 1 2 2 1
## 2 3 1 1
## 3 3 2 1
which two enhancers are linked?
linkOverlaps(gi, all.enhancers)
## query subject1 subject2
## 1 3 1 1
Subscribe to:
Posts (Atom)