|
| 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 | + |
0 commit comments