Skip to content

Commit

Permalink
function imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jasenfinch committed Nov 17, 2016
1 parent 9df0dd2 commit cacd40d
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 331 deletions.
12 changes: 0 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
language: r

services:
- docker

sudo: required
bioc_required: true

Expand All @@ -18,15 +15,6 @@ r_binary_packages:
bioc_packages:
- mzR

r_github_packages:
- jasenfinch/binneR

after_success:
- docker login -e="$DOCKER_EMAIL" -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"
- git clone https://github.com/jasenfinch/HRM-docker.git
- docker build -t jasenfinch/hrm-docker ./HRM-docker
- docker push jasenfinch/hrm-docker

notifications:
email:
on_success: change
Expand Down
6 changes: 1 addition & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,5 @@ Maintainer: Jasen Finch <[email protected]>
Description: A spectral binning approach for high resolution flow infusion mass
spectrometry data.
License: GPL (>= 2)
Depends: mzR, ggplot2, plyr
Imports:
parallel,
shiny,
ggplot2
Imports: parallel,shiny,ggplot2,plyr,mzR
RoxygenNote: 5.0.1
47 changes: 37 additions & 10 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
# Generated by roxygen2: do not edit by hand

export(addMasses)
export(colMasses)
export(combScans)
export(massMat)
export(readFiles)
export(sampProcess)
export(viewSpectrum)
import(ggplot2)
import(mzR)
import(parallel)
import(plyr)
import(shiny)
importFrom(ggplot2,aes_string)
importFrom(ggplot2,geom_line)
importFrom(ggplot2,geom_segment)
importFrom(ggplot2,geom_text)
importFrom(ggplot2,geom_vline)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,ggtitle)
importFrom(ggplot2,theme_bw)
importFrom(ggplot2,xlab)
importFrom(ggplot2,ylab)
importFrom(mzR,header)
importFrom(mzR,openMSfile)
importFrom(mzR,peaks)
importFrom(mzR,runInfo)
importFrom(parallel,makeCluster)
importFrom(parallel,parLapplyLB)
importFrom(parallel,stopCluster)
importFrom(plyr,ldply)
importFrom(shiny,fileInput)
importFrom(shiny,fluidPage)
importFrom(shiny,fluidRow)
importFrom(shiny,mainPanel)
importFrom(shiny,numericInput)
importFrom(shiny,plotOutput)
importFrom(shiny,radioButtons)
importFrom(shiny,reactive)
importFrom(shiny,renderPlot)
importFrom(shiny,renderUI)
importFrom(shiny,shinyApp)
importFrom(shiny,sidebarLayout)
importFrom(shiny,sidebarPanel)
importFrom(shiny,sliderInput)
importFrom(shiny,tags)
importFrom(shiny,textInput)
importFrom(shiny,titlePanel)
importFrom(shiny,uiOutput)
importFrom(stats,aggregate)
17 changes: 8 additions & 9 deletions R/addMasses.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#' Add in masses with zero intensity if not present in a peak matrix in a peak list
#' @name addMasses
#' @description This function will add rows to a peak matrix of missing m/z identified within the supplied peak list to ensure equal matrix row lengths for further processing
#' @param x A list containing peak matrices for each scan
#' @return A list containing peak matrices with added 0 values
#' @author Jasen Finch
#' @export
# Add in masses with zero intensity if not present in a peak matrix in a peak list
# @name addMasses
# @description This function will add rows to a peak matrix of missing m/z identified within the supplied peak list to ensure equal matrix row lengths for further processing
# @param x A list containing peak matrices for each scan
# @return A list containing peak matrices with added 0 values
# @author Jasen Finch

addMasses <- function(x){
masses <- colMasses(x)
y.1 <- NULL
for (i in 1:length(x)){
for (i in 1:length(x)) {
y <- data.frame(x[[i]])
mat <- matrix(0,nrow=length(masses),ncol=2)
mat <- matrix(0,nrow = length(masses),ncol = 2)
names(mat) <- c("mz","intensity")
z <- masses %in% y[,1]
mat[,1] <- masses
Expand Down
15 changes: 7 additions & 8 deletions R/colMasses.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#' Collate all masses present in the supplied list
#' @name colMasses
#' @description Collate all masses present across all scans in the supplied peak list, into a single reference vector.
#' @param x A peak list containing peak matrices for each scan
#' @return A vector containing all the masses present in the supplied peak list
#' @author Jasen Finch
#' @export
# Collate all masses present in the supplied list
# @name colMasses
# @description Collate all masses present across all scans in the supplied peak list, into a single reference vector.
# @param x A peak list containing peak matrices for each scan
# @return A vector containing all the masses present in the supplied peak list
# @author Jasen Finch

colMasses <- function(x){
z <- NULL
for (i in 1:length(x)){
for (i in 1:length(x)) {
y <- data.frame(x[[i]])
y.1 <- as.numeric(y[,1])
z <- c(z,y.1)
Expand Down
43 changes: 21 additions & 22 deletions R/combScans.R
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
#' Combine scan ranges and retrieve given scans
#' @name combScans
#' @description A function that will split a peak list into appropriate modes, retrieve peak matrices of the supplied relevant scans and combine scan ranges.
#' @param x A peak list containing peak matrices for each scan
#' @param scans A vector of scan numbers that should be retrieved
#' @param sranges A list of vectors containing the scan events present
#' @param modes A vector of strings denoting mode names
#' @return A list containing peak lists for the relevant scans with combined scan ranges for each present mode.
#' @author Jasen Finch
#' @export
#' @import plyr
# Combine scan ranges and retrieve given scans
# @name combScans
# @description A function that will split a peak list into appropriate modes, retrieve peak matrices of the supplied relevant scans and combine scan ranges.
# @param x A peak list containing peak matrices for each scan
# @param scans A vector of scan numbers that should be retrieved
# @param sranges A list of vectors containing the scan events present
# @param modes A vector of strings denoting mode names
# @return A list containing peak lists for the relevant scans with combined scan ranges for each present mode.
# @author Jasen Finch
#' @importFrom plyr ldply

combScans <- function(x,scans,sranges,modes){
# separate scans based on modes
pat <- unlist(lapply(modes,function(x,sranges){
rep(x,length(sranges))
},sranges=sranges))
},sranges = sranges))
pat <- rep(pat,length(x)/length(pat))
pos.neg <- lapply(modes,function(y,x,pat){
x <- x[which(pat==y)]
x <- x[which(pat == y)]
return(x)
},pat=pat,x=x)
},pat = pat,x = x)
# separate scan ranges and keep only relevant scans
pat.1 <- unlist(lapply(seq(1,length(pos.neg[[1]])/length(sranges)),rep,times=length(sranges)))
pat.1 <- unlist(lapply(seq(1,length(pos.neg[[1]])/length(sranges)),rep,times = length(sranges)))
pos.neg <- lapply(pos.neg,function(x,pattern){
y <- list()
for (i in unique(pattern)){
y[[i]] <- x[which(pattern==i)]
for (i in unique(pattern)) {
y[[i]] <- x[which(pattern == i)]
}
return(y)
},pattern=pat.1)
},pattern = pat.1)
pos.neg <- lapply(pos.neg,function(x,scans){
return(x[scans])
},scans=scans)
},scans = scans)
# combine scan ranges into single lists, boundary always equal to upper limit
pos.neg <- lapply(pos.neg,function(x,sranges){
return(lapply(x,function(y,sranges){
for (i in 1:length(y)){
for (i in 1:length(y)) {
z <- y[[i]]
y[[i]] <- z[which(z[,1]>sranges[[i]][1] & z[,1] <= sranges[[i]][2]),]
y[[i]] <- z[which(z[,1] > sranges[[i]][1] & z[,1] <= sranges[[i]][2]),]
}
y <- ldply(y,data.frame)
colnames(y) <- c("mz","intensity")
return(y)
},sranges=sranges))},sranges=sranges)
},sranges = sranges))},sranges = sranges)
names(pos.neg) <- names(mode)
return(pos.neg)
}
Expand Down
19 changes: 9 additions & 10 deletions R/massMat.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#' build an intensity matrix for supplied peak list
#' @name massMat
#' @description build and intensity matrix for a supplied peak list. Can be used to build intensity matrices for combining either scans within a sample of multiple samples.
#' @param x A peak list containing peak matrices for each scan of equal row length
#' @return An intensity matrix with as many rows as peak matrices in peak list
#' @author Jasen Finch
#' @export
# build an intensity matrix for supplied peak list
# @name massMat
# @description build and intensity matrix for a supplied peak list. Can be used to build intensity matrices for combining either scans within a sample of multiple samples.
# @param x A peak list containing peak matrices for each scan of equal row length
# @return An intensity matrix with as many rows as peak matrices in peak list
# @author Jasen Finch

massMat <- function(x){
z <- data.frame(x[1])
mat <- matrix(0,nrow=length(x),ncol=length(z[,1]))
for (i in 1:length(x)){
mat <- matrix(0,nrow = length(x),ncol = length(z[,1]))
for (i in 1:length(x)) {
y <- data.frame(x[i])
y <- t(y)
y <- y[2,]
mat[i,] <- y
}
colnames(mat) <- z[,1]
return (mat)
return(mat)
}
71 changes: 27 additions & 44 deletions R/readFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,31 @@
#' @return A list containing peak lists for the relevant scans with combined scan ranges for each present mode in the data file.
#' @author Jasen Finch
#' @export
#' @import parallel
readFiles <- function(files,dp,scans,sranges=list(c(50,1000)),modes=c("p","n"),nCores=2){ # for data collected in both modes
if(nCores<2){
pl <- lapply(files,sampProcess,scans=scans,dp=dp,sranges=sranges,modes=modes)
# split modes
pos.neg <- list()
for (i in 1:length(modes)){
pos.neg[[i]] <- lapply(pl,function(x,mode){return(x[mode])},mode=modes[i])
gc()
}
# add in masses to get equal lengths for each sample
for (i in 1:length(pos.neg)){
pos.neg[[i]] <- addMasses(pos.neg[[i]])
gc()
}
# build intensity matrix
for (i in 1:length(pos.neg)){
pos.neg[[i]] <- massMat(pos.neg[[i]])
gc()
}
}else{
clust = makeCluster(nCores, type="PSOCK")
clusterExport(clust,c(ls("package:binneR"),ls("package:mzR"),ls("package:Rcpp"),ls("package:plyr")))
pl <- parLapplyLB(clust,files ,fun= sampProcess,scans=scans,dp=dp,sranges=sranges,modes=modes)
# split modes
pos.neg <- list()
for (i in 1:length(modes)){
pos.neg[[i]] <- lapply(pl,function(x,mode){return(x[mode])},mode=modes[i])
}
pl <- NULL
gc()
# add in masses to get equal lengths for each sample
pos.neg <- parLapplyLB(clust,pos.neg,fun=addMasses)
gc()
# build intensity matrix
pos.neg <- parLapplyLB(clust,pos.neg,fun=massMat)
stopCluster(clust)
}
for (i in 1:length(modes)){
colnames(pos.neg[[i]]) <- paste(modes[i],colnames(pos.neg[[i]]),sep="")
}
names(pos.neg) <- modes
gc()
return(pos.neg)
#' @importFrom parallel makeCluster parLapplyLB stopCluster
#' @examples
#' res <- readFiles(list.files(system.file('mzXML',package = 'binneR'),
#' full.names=TRUE),dp = 2,scans = 6:17)

readFiles <- function(files,dp, scans, sranges = list(c(50,1000)), modes = c("n","p"), nCores = 1){ # for data collected in both modes
clust = makeCluster(nCores, type = "PSOCK")
pl <- parLapplyLB(clust,files ,fun = sampProcess,scans = scans,dp = dp,sranges = sranges,modes = modes)
# split modes
pos.neg <- list()
for (i in 1:length(modes)) {
pos.neg[[i]] <- lapply(pl,function(x,mode){return(x[mode])},mode = modes[i])
}
pl <- NULL
gc()
# add in masses to get equal lengths for each sample
pos.neg <- parLapplyLB(clust,pos.neg,fun = addMasses)
gc()
# build intensity matrix
pos.neg <- parLapplyLB(clust,pos.neg,fun = massMat)
stopCluster(clust)
for (i in 1:length(modes)) {
colnames(pos.neg[[i]]) <- paste(modes[i],colnames(pos.neg[[i]]),sep = "")
}
names(pos.neg) <- modes
gc()
return(pos.neg)
}
30 changes: 15 additions & 15 deletions R/sampProcess.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#' process a single data file
#' @name sampProcess
#' @description Apply spectral binning on a single data file.
#' @param file File path of converted data file
#' @param scans A vector of scan numbers that should be retrieved
#' @param dp An integer denoting the number of decimal places for spectral binning
#' @param sranges A list of vectors containing the scan events present
#' @param modes A vector of strings denoting mode names
#' @return A list containing peak lists for the relevant scans with combined scan ranges for each present mode in the data file.
#' @author Jasen Finch
#' @export
#' @import mzR
# process a single data file
# @name sampProcess
# @description Apply spectral binning on a single data file.
# @param file File path of converted data file
# @param scans A vector of scan numbers that should be retrieved
# @param dp An integer denoting the number of decimal places for spectral binning
# @param sranges A list of vectors containing the scan events present
# @param modes A vector of strings denoting mode names
# @return A list containing peak lists for the relevant scans with combined scan ranges for each present mode in the data file.
# @author Jasen Finch
#' @importFrom mzR openMSfile peaks
#' @importFrom stats aggregate

sampProcess <- function(file,scans,dp,sranges,modes){
aa <- openMSfile(file)
Expand All @@ -23,8 +23,8 @@ sampProcess <- function(file,scans,dp,sranges,modes){
# aggregate bins to give ion totals
y <- aggregate(y[,"intensity"],list(y[,"mz"]),sum)
return(y)
},dp=dp))
},dp=dp)
},dp = dp))
},dp = dp)
# add zeros for missing bins in each scan
pl <- lapply(pl,addMasses)
pl <- lapply(pl,massMat)
Expand All @@ -34,7 +34,7 @@ sampProcess <- function(file,scans,dp,sranges,modes){
return(x)
})
pl <- lapply(pl,function(x){
x <- data.frame(mz=as.numeric(as.character(names(x))),intensity=x,row.names=NULL)
x <- data.frame(mz = as.numeric(as.character(names(x))),intensity = x,row.names = NULL)
return(x)
})
names(pl) <- modes
Expand Down
Loading

0 comments on commit cacd40d

Please sign in to comment.