/
commandes_clean.Rmd
159 lines (119 loc) · 4.59 KB
/
commandes_clean.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
output:
html_document:
keep_md: yes
---
```{r echo=FALSE}
options(width=120)
knitr::opts_knit$set(verbose = TRUE)
knitr::opts_chunk$set(cache=TRUE)
```
Analyze of the first experiment: NCms10058
==========================================
Cluster and annotate in the shell (not in R)
--------------------------------------------
```{r engine="bash"}
LIBRARY=NCms10058_1
BAMFILES=../Moirai/NCms10058_1.CAGEscan_short-reads.20150625154711/properly_paired_rmdup/*bam
level1.py -o $LIBRARY.l1.gz -f 66 -F 516 $BAMFILES
level2.py -t 0 -o $LIBRARY.l2.gz $LIBRARY.l1.gz
function osc2bed {
zcat $1 |
grep -v \# |
sed 1d |
awk '{OFS="\t"}{print $2, $3, $4, "l1", "1000", $5}'
}
function bed2annot {
bedtools intersect -a $1 -b ../annotation/annot.bed -s -loj |
awk '{OFS="\t"}{print $1":"$2"-"$3$6,$10}' |
bedtools groupby -g 1 -c 2 -o collapse
}
function bed2symbols {
bedtools intersect -a $1 -b ../annotation/gencode.v14.annotation.genes.bed -s -loj |
awk '{OFS="\t"}{print $1":"$2"-"$3$6,$10}' |
bedtools groupby -g 1 -c 2 -o distinct
}
osc2bed $LIBRARY.l2.gz | tee $LIBRARY.l2.bed | bed2annot - > $LIBRARY.l2.annot
bed2symbols $LIBRARY.l2.bed > $LIBRARY.l2.genes
```
Analysis with R
---------------
### Configuration
```{r}
library(oscR) # See https://github.com/charles-plessy/oscR for oscR.
library(smallCAGEqc) # See https://github.com/charles-plessy/smallCAGEqc for smallCAGEqc.
library(vegan)
library(ggplot2)
stopifnot(
packageVersion("oscR") >= "0.1.1"
, packageVersion("smallCAGEqc") > "0.10.0"
)
LIBRARY <- "NCms10058_1"
```
### Load data
```{r}
l2_NCki <- read.osc(paste(LIBRARY,'l2','gz',sep='.'), drop.coord=T, drop.norm=T)
colnames(l2_NCki) <- sub('raw.NCms10058_1.','NCki_',colnames(l2_NCki))
colSums(l2_NCki)
```
### Normalization number of read per sample : l2.sub ; libs$genes.sub
In all the 3 libraries used, one contain only few reads tags. The smallest one has 8,708 counts. In order to make meaningful comparisons, all of them are subsapled to 8700 counts.
```{r}
set.seed(1)
l2.sub1 <- t(rrarefy(t(l2_NCki),min(8700)))
colSums(l2.sub1)
```
### Moirai statistics
Load the QC data produced by the Moirai workflow with which the libraries were processed. Sort in the same way as the l1 and l2 tables, to allow for easy addition of columns.
```{r}
libs <- loadMoiraiStats(multiplex = "NCms10058_1.multiplex.txt", summary = "../Moirai/NCms10058_1.CAGEscan_short-reads.20150625154711/text/summary.txt", pipeline = "CAGEscan_short-reads")
```
### Number of clusters
Count the number of unique L2 clusters per libraries after subsampling, and add
this to the QC table. Each subsampling will give a different result, but the
mean result can be calculated by using the `rarefy` function at the same scale
as the subsampling.
```{r}
libs["l2.sub1"] <- colSums(l2.sub1 > 0)
libs["l2.sub1.exp"] <- rarefy(t(l2_NCki), min(colSums(l2_NCki)))
```
### Richness
Richness should also be calculated on the whole data.
```{r NCki_boxplot, dev=c('png', 'svg')}
libs["r100.l2"] <- rarefy(t(l2_NCki),100)
boxplot(data=libs, r100.l2 ~ group, ylim=c(80,100), las=1)
```
### Hierarchical annotation
Differences of sampling will not bias distort the distribution of reads between annotations, so the non-subsampled library is used here.
```{r}
annot.l2 <- read.table(paste(LIBRARY,'l2','annot',sep='.'), head=F, col.names=c('id', 'feature'), row.names=1)
annot.l2 <- hierarchAnnot(annot.l2)
rownames(libs) <- sub("HeLa", "NCki_HeLa", rownames(libs))
rownames(libs) <- sub("THP1", "NCki_THP1", rownames(libs))
libs <- cbind(libs, t(rowsum(l2_NCki, annot.l2[,'class'])))
libs$samplename <- sub('HeLa', 'NCki_HeLa', libs$samplename)
libs$samplename <- sub('THP1', 'NCki_THP1', libs$samplename)
```
### Gene symbols used normalisation data
```{r}
genesymbols <- read.table(paste(LIBRARY,'l2','genes',sep='.'), col.names=c("cluster","symbol"), stringsAsFactors=FALSE)
rownames(genesymbols) <- genesymbols$cluster
g2 <- rowsum(l2_NCki, genesymbols$symbol)
countSymbols <- countSymbols(g2)
libs[colnames(l2_NCki),"genes"] <- (countSymbols)
```
Number of genes detected in sub-sample
```{r}
l2.sub1 <- data.frame(l2.sub1)
g2.sub1 <- rowsum(l2.sub1, genesymbols$symbol)
countSymbols.sub1 <- countSymbols(g2.sub1)
libs[colnames(l2.sub1),"genes.sub1"] <- (countSymbols.sub1)
```
### Table record
save the different tables produced for later analysis
```{r}
write.table(l2_NCki, "l2_NCki_1.txt", sep = "\t", quote=FALSE)
write.table(l2.sub1, "l2.sub1_NCki_1.txt", sep = "\t", quote=FALSE)
write.table(g2.sub1, 'g2.sub1_NCki_1.txt', sep="\t", quote=F)
write.table(libs, 'libs_NCki_1.txt', sep="\t", quote=F)
```