Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. #source("https://bioconductor.org/biocLite.R")
  2. #biocLite("limma")
  3. #biocLite("edgeR")
  4. #biocLite("Glimma")
  5. #biocLite("Mus.musculus")
  6. files <- c("GSM1545535_10_6_5_11.txt",
  7. "GSM1545536_9_6_5_11.txt",
  8. "GSM1545538_purep53.txt",
  9. "GSM1545539_JMS8-2.txt",
  10. "GSM1545540_JMS8-3.txt",
  11. "GSM1545541_JMS8-4.txt",
  12. "GSM1545542_JMS8-5.txt",
  13. "GSM1545544_JMS9-P7c.txt",
  14. "GSM1545545_JMS9-P8c.txt")
  15. read.delim(files[1], nrow=5)
  16. library(limma)
  17. library(edgeR)
  18. x<- readDGE(files,columns=c(1,3))
  19. class(x)
  20. dim(x)
  21. samplenames<- substring(colnames(x),12,nchar(colnames(x)))
  22. samplenames
  23. colnames(x)<-samplenames
  24. group<- as.factor(c("LP","ML","Basal","Basal","ML","LP","Basal","ML","LP"))
  25. x$samples$group<-group
  26. lane<- as.factor(rep(c("L004","L006","L008"),c(3,4,2)))
  27. x$samples$lane<-lane
  28. x$samples
  29. library(Mus.musculus)
  30. geneid <- rownames(x)
  31. genes <- select(Mus.musculus, keys=geneid, columns=c("SYMBOL", "TXCHROM"), keytype="ENTREZID")
  32. dim(genes)
  33. head(genes)
  34. genes <- genes[!duplicated(genes$ENTREZID),]
  35. x$genes <- genes
  36. x
  37. cpm <- cpm(x)
  38. lcpm <- cpm(x, log=TRUE)
  39. table(rowSums(x$counts==0)==9)
  40. keep.exprs <- rowSums(cpm>1)>=3
  41. x <- x[keep.exprs,,keep.lib.sizes=FALSE]
  42. dim(x)
  43. library(RColorBrewer)
  44. nsamples <- ncol(x)
  45. col <- brewer.pal(nsamples, "Paired")
  46. par(mfrow=c(1,2))
  47. plot(density(lcpm[,1]), col=col[1], lwd=2, ylim=c(0,0.21), las=2, main = "", xlab="")
  48. title(main="A. Raw data", xlab = "log=cpm")
  49. abline(v=0, lty=3)
  50. for(i in 2:nsamples){
  51. den <- density(lcpm[,i])
  52. lines(den$x, den$y, col=col[i], lwd=2)
  53. }
  54. legend("topright", samplenames, text.col = col, bty="n")
  55. lcpm <- cpm(x, log=TRUE)
  56. plot(density(lcpm[,1]), col=col[1], lwd=2, ylim = c(0,0.21), las=2, main="", xlab="")
  57. title(main="B. Filtered data", xlab = "Log-cpm")
  58. abline(v=0, lty=3)
  59. for(i in 2:nsamples){
  60. den <- density(lcpm[,1])
  61. lines(den$x, den$y, col=col[i], lwd=2)
  62. }
  63. legend("topright", samplenames, text.col = col, bty="n")
  64. x <- calcNormFactors(x, method = "TMM")
  65. x$samples$norm.factors
  66. x2 <- x
  67. x2$samples$norm.factors <- 1
  68. x2$counts[,1] <- ceiling(x2$counts[,1]*0.05)
  69. x2$counts[,2] <- x2$counts[,2]*5
  70.  
  71. par(mfrow=c(1,2))
  72. lcpm <- cpm(x2, log=TRUE)
  73. boxplot(lcpm, las=2, col=col, main="")
  74. title(main="A. Example: Unnormalised data",ylab="Log-cpm")
  75. x2 <- calcNormFactors(x2)
  76. x2$samples$norm.factors
  77. lcpm <- cpm(x2, log=TRUE)
  78. boxplot(lcpm, las=2, col=col, main="")
  79. title(main="B. Example: Normalised data",ylab="Log-cpm")
  80.  
  81. lcpm <- cpm(x, log=TRUE)
  82. par(mfrow=c(1,2))
  83. col.group <- group
  84. levels(col.group) <- brewer.pal(nlevels(col.group), "Set1")
  85. col.group <- as.character(col.group)
  86. col.lane <- lane
  87. levels(col.lane) <- brewer.pal(nlevels(col.lane), "Set2")
  88. col.lane <- as.character(col.lane)
  89. plotMDS(lcpm, labels=group, col=col.group)
  90. title(main="A. Sample groups")
  91.  
  92. plotMDS(lcpm, labels=lane, col=col.lane, dim=c(3,4))
  93. title(main="B. Sequencing lanes")
  94.  
  95. library(Glimma)
  96. glMDSPlot(lcpm, labels=paste(group, lane, sep="_"), groups=x$samples[,c(2,5)],
  97. launch=FALSE)
  98. design <- model.matrix(~0+group+lane)
  99. colnames(design) <- gsub("group", "", colnames(design))
  100. design
  101.  
  102. contr.matrix <- makeContrasts(
  103. BasalvsLP = Basal-LP,
  104. BasalvsML = Basal - ML,
  105. LPvsML = LP - ML,
  106. levels = colnames(design))
  107. contr.matrix
  108.  
  109. v <- voom(x, design, plot=TRUE)
  110. v
  111.  
  112. vfit <- lmFit(v, design)
  113. vfit <- contrasts.fit(vfit, contrasts=contr.matrix)
  114. efit <- eBayes(vfit)
  115. plotSA(efit)
  116.  
  117. summary(decideTests(efit))
  118.  
  119. tfit <- treat(vfit, lfc=1)
  120. dt <- decideTests(tfit)
  121. summary(dt)
  122.  
  123. de.common <- which(dt[,1]!=0 & dt[,2]!=0)
  124. length(de.common)
  125.  
  126. head(tfit$genes$SYMBOL[de.common], n=20)
  127.  
  128. vennDiagram(dt[,1:2], circle.col=c("turquoise", "salmon"))
  129. write.fit(tfit, dt, file="results.txt")
  130.  
  131. basal.vs.lp <- topTreat(tfit, coef=1, n=Inf)
  132. basal.vs.ml <- topTreat(tfit, coef=2, n=Inf)
  133. head(basal.vs.lp)
  134.  
  135. head(basal.vs.ml)
  136.  
  137. plotMD(tfit, column=1, status=dt[,1], main=colnames(tfit)[1], xlim=c(-8,13))
  138.  
  139. glMDPlot(tfit, coef=1, status=dt, main=colnames(tfit)[1],
  140. id.column="ENTREZID", counts=x$counts, groups=group, launch=FALSE)
  141.  
  142. library(gplots)
  143. basal.vs.lp.topgenes <- basal.vs.lp$ENTREZID[1:100]
  144. i <- which(v$genes$ENTREZID %in% basal.vs.lp.topgenes)
  145. mycol <- colorpanel(1000,"blue","white","red")
  146. heatmap.2(v$E[i,], scale="row",
  147. labRow=v$genes$SYMBOL[i], labCol=group,
  148. col=mycol, trace="none", density.info="none",
  149. margin=c(8,6), lhei=c(2,10), dendrogram="column")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement