Skip to content

Commit 43019e4

Browse files
authored
Merge pull request #3 from MJS-708/v0.1.1
Release v0.1.1
2 parents b7eed07 + b3513a1 commit 43019e4

28 files changed

+1633
-0
lines changed

.gitignore

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# History files
2+
.Rhistory
3+
.Rapp.history
4+
5+
# Session Data files
6+
.RData
7+
8+
# User-specific files
9+
.Ruserdata
10+
11+
# Example code in package build process
12+
*-Ex.R
13+
14+
# Output files from R CMD build
15+
/*.tar.gz
16+
17+
# Output files from R CMD check
18+
/*.Rcheck/
19+
20+
# RStudio files
21+
.Rproj.user/
22+
23+
# produced vignettes
24+
vignettes/*.html
25+
vignettes/*.pdf
26+
27+
# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3
28+
.httr-oauth
29+
30+
# knitr and R markdown default cache directories
31+
*_cache/
32+
/cache/
33+
34+
# Temporary files created by R markdown
35+
*.utf8.md
36+
*.knit.md
37+
38+
# R Environment Variables
39+
.Renviron
40+
41+
#Folders
42+
data/
43+
tests/
44+
vignettes/

DESCRIPTION

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Package: MSIprocessing
2+
Title: R package for processing MSI data - specifically extends Cardinal functionality
3+
Version: 0.1.0
4+
Author: Matthew Smith <[email protected]>
5+
Maintainer: Matthew Smith <[email protected]>
6+
Description: Offers MSI functionality such as normalisation
7+
License: MIT + file LICENSE
8+
Encoding: UTF-8
9+
LazyData: true
10+
Roxygen: list(markdown = TRUE)
11+
RoxygenNote: 7.1.1
12+
Imports:
13+
Cardinal,
14+
dplyr,
15+
ggplot2,
16+
ggthemes,
17+
grid,
18+
impute,
19+
scales,
20+
tibble
21+
Depends:
22+
R (>= 2.10)
23+
Suggests:
24+
rmarkdown,
25+
knitr,
26+
testthat (>= 3.0.0)
27+
Config/testthat/edition: 3
28+
VignetteBuilder: knitr

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

NAMESPACE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(calc_svds)
4+
export(checkAdductPair)
5+
export(createDatamatrix)
6+
export(create_xyMatrix)
7+
export(error)
8+
export(impute_scale)
9+
export(med)
10+
export(medianNorm)
11+
export(missing_values_df)
12+
export(myMean)
13+
export(myMed)
14+
export(normPixelsDF_anatomies)
15+
export(pixelDF_NormCompare)
16+
export(pixelsDF_NormCompare)
17+
export(ppm_limits)
18+
export(scale_colour_Publication)
19+
export(scale_fill_Publication)
20+
export(theme_Publication)
21+
import(ggplot2)
22+
import(ggthemes)
23+
import(scales)

R/cardinalExtensionFunctions.R

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
################################################################
2+
########## General Cardinal extention Functions ################
3+
################################################################
4+
5+
#' Function to create data matrix from MSI object
6+
#'
7+
#' @param MSIobject MSI object from Cardinal
8+
#' @param inputNA Whether to convert 0's in matrix to NA (default = T)
9+
#' @return dataframe with rows = ? cols = ?
10+
#'
11+
#' @export
12+
13+
createDatamatrix <- function(MSIobject, inputNA = T){
14+
15+
MSIobject = as(MSIobject, "MSContinuousImagingExperiment")
16+
17+
df <- data.frame(spectra(MSIobject))
18+
19+
if(rownames(df)[1] == "1"){ # No row names
20+
df <- df %>%
21+
add_column(mz=as.numeric(mz(MSIobject))) %>%
22+
column_to_rownames(var = "mz") %>%
23+
rename_all(~sprintf("pixel_%s", 1:ncol(MSIobject)))
24+
}
25+
26+
if(inputNA){
27+
df <- replace(df, df==0, NA)
28+
}
29+
30+
return(df)
31+
}
32+
33+
34+
35+
#' Function for median normalisation
36+
#'
37+
#' @param x vector (relating to pixel spectrum)
38+
#'
39+
#' @return normalised spectrum vector
40+
#'
41+
#' @export
42+
medianNorm <- function(x){
43+
44+
pixelInt <- as.vector(x) %>%
45+
ifelse(.==0, NA, .)
46+
47+
#print(pixelInt)
48+
49+
med <- median(pixelInt, na.rm=T)
50+
51+
#print(med)
52+
53+
newInt <- x / med
54+
#print(newInt)
55+
56+
x[1:length(pixelInt)] <- newInt
57+
}
58+
#test <- daphProc[,1 ]
59+
60+
61+
62+
#' Imputes Missing Values and scales MSI object
63+
#' @param MSIobject MSI object from Cardinal
64+
#'
65+
#' @return MSIobject with missing values imputed and datamatrix scaled.
66+
#'
67+
#' @export
68+
impute_scale <- function(MSIobject){
69+
70+
dataMatrix <- createDatamatrix(MSIobject)
71+
print(nrow(dataMatrix)) # features
72+
print(ncol(dataMatrix)) # pixels / samples
73+
MVimputed <- missing_values_df(dataMatrix, rowmax = 0.99, colmax = 0.99)
74+
#View(MVimputed[1:11, 1:11])
75+
print(nrow(MVimputed))
76+
print(ncol(MVimputed))
77+
78+
# Pareto scale (variance stabilisation)
79+
scaled <- t(scale(t(MVimputed), center = T, scale = sqrt(apply(t(MVimputed), 2, sd, na.rm=T))))
80+
# mean center (to mean of all features in sample)
81+
# DIvide each feature by sd of feature - to stop high intense features skewing data (SD of intense features higher)
82+
83+
#View(scaled[1:11, 1:11])
84+
85+
#Create new object with intensity etc
86+
MSIobject <- MSImagingExperiment(imageData=as.matrix(scaled), featureData=fData(MSIobject), pixelData=pData(MSIobject))
87+
88+
return(as(MSIobject, "MSProcessedImagingExperiment"))
89+
}
90+
91+
92+
93+
#' Function to check colocalisation of suspected adduct pair
94+
#'
95+
#' @param MSIobject MSI object from Cardinal
96+
#' @param mz1 First mz value to run colocalisation against
97+
#' @param mz2 Second mz value to check colocalisation score above given value
98+
#' @param threshold Threshold score to define whether features colocalise
99+
#' @param method Method to use for scoring colocalisation (Default = c("M1"))
100+
#' @param topn Number of top colocalised features to check (default = 20)
101+
#'
102+
#' @return Boolean whether mz feature colocalise
103+
#' @export
104+
105+
checkAdductPair <- function(MSIobject, mz1, mz2, threshold, method = "M1", topn=20){
106+
107+
coloc <- data.frame( colocalized(MSIobject, mz = mz1, tolerance=20,
108+
units="ppm", n= topn, sort.by = c(method) ) ) %>%
109+
subset(correlation > threshold)
110+
111+
#View(coloc)
112+
113+
return(mz2 %in% coloc$mz)
114+
}
115+
116+
117+
#' Function to create x,y matrix for each feature - for feature selection.
118+
#' @param MSIobject MSI object from Cardinal
119+
#' @param mz mz channel to generate matrix for
120+
#'
121+
#' @return Data matrix for given mz; rows = y coordinate, cols = x-coordinate in the image
122+
#' @export
123+
124+
create_xyMatrix <- function(MSIobject, mz){
125+
matrix = slice(MSIobject, mz=mz) %>%
126+
ifelse(. == 0, NA, .) %>%
127+
ifelse(is.na(.), median(., na.rm=T), .) # impute MVs - using median
128+
return(matrix)
129+
}
130+
#matrix = create_xyMatrix(MSIobject, mz=782.57)
131+
132+
133+
#' Function to create dataframe with information about specific pixel in specific anatomical region of MSI object
134+
#' @param MSIobject MSI object from Cardinal
135+
#' @param pixel pixel to select spectrum from
136+
#' @param anatomy label for the anatomical region pixel belongs to
137+
#' @param normalisation Which normalisation step has been applied to 'MSIobject'
138+
#' @param medianDF median spectrum to compare to
139+
#'
140+
#' @return Dataframe with headers: pixel, anatomy, mz, intensity (of mz feature), median, FC (to the median spectrum), normalisation
141+
#' @export
142+
143+
pixelDF_NormCompare <- function(MSIobject, pixel, anatomy, normalisation, medianDF){
144+
145+
subsetObject <- MSIobject[, pixel] # Subset data, to only include sample
146+
147+
pixelDF <- tibble(pixel=pixel, Anatomy = anatomy, mz= fData(subsetObject)@mz,
148+
intensity = createDatamatrix(subsetObject)[,1]) %>%
149+
subset(mz %in% medianDF$mz) %>%
150+
mutate(median = medianDF$median,
151+
FC = intensity / median,
152+
normalisation = normalisation)
153+
154+
return(pixelDF)
155+
}
156+
157+
#' Function to create dataframe with information about all pixels in defined anatomical region of MSI object
158+
#'
159+
#' @param MSIobject MSI object from Cardinal
160+
#' @param pixels pixels to select spectra from
161+
#' @param anatomy label for the anatomical region pixels belongs to
162+
#' @param normalisation Which normalisation step has been applied to 'MSIobject'
163+
#' @param medianDF median spectrum to compare to
164+
#'
165+
#' @return Dataframe with headers: pixel, anatomy, mz, intensity (of mz feature), median, FC (to the median spectrum)
166+
#' @export
167+
168+
pixelsDF_NormCompare <- function(MSIobject, pixels, anatomy, normalisation, medianDF){
169+
170+
pixelsDF <- tibble(pixel=numeric(), Anatomy = character(), mz= numeric(),
171+
intensity = numeric(), median = numeric(), FC = numeric())
172+
173+
for(pixel in pixels){
174+
print(pixel)
175+
176+
pixelDF <- pixelDF_NormCompare(MSIobject, pixel, anatomy, normalisation, medianDF)
177+
pixelsDF <- rbind(pixelsDF, pixelDF)
178+
#print(head(pixelsDF))
179+
}
180+
181+
return(pixelsDF)
182+
}
183+
184+
185+
#' Function to create dataframe with information about all pixels in several defined anatomical regions of MSI object
186+
#' Specifically used to compare distribution of feature distributions in different anatomical regions after normalisation
187+
#'
188+
#' @param MSIobject MSI object from Cardinal
189+
#' @param pixel_list list of pixel vectors from different anatomical regions
190+
#' @param anatomies vector of labels for the different anatomical regions pixels belongs to (same order as 'pixel_list')
191+
#' @param normalisation Which normalisation step has been applied to 'MSIobject'
192+
#' @param medianDF median spectrum to compare to
193+
#'
194+
#' @return Dataframe with headers: pixel, anatomy, mz, intensity (of mz feature), median, FC (to the median spectrum)
195+
#' @export
196+
normPixelsDF_anatomies <- function(MSIobject, pixel_list, anatomies, normalisation, medianDF){
197+
198+
pixelsDF <- tibble(pixel=numeric(), Anatomy = character(), mz= numeric(),
199+
intensity = numeric(), median = numeric(), FC = numeric())
200+
201+
for(i in 1:length(anatomies)){
202+
203+
pixels <- pixel_list[[i]]
204+
anatomy <- anatomies[i]
205+
206+
print(anatomy)
207+
208+
tempDF <- pixelsDF_NormCompare(MSIobject = MSIobject, pixels = pixels, anatomy=anatomy,
209+
normalisation=normalisation, medianDF= medianDF)
210+
211+
pixelsDF <- rbind(pixelsDF, tempDF)
212+
}
213+
return(pixelsDF)
214+
}
215+
216+

R/data.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#' Small MSI object pen ink
2+
#'
3+
#' A small MSI object from a sample of pen ink.
4+
#'
5+
#' @format MSI object with 265 features and 25 pixels
6+
"exampleMSI_object"

0 commit comments

Comments
 (0)