Exporting Data While Maintaining Clusters

61 views
Skip to first unread message

jennifer.n...@gmail.com

unread,
May 25, 2016, 3:17:08 PM5/25/16
to Cardinal MSI Help
Hi, I was wondering if there was a way in which I could export my data that would maintain the clusters generated by the package.

Thanks!

kbemis

unread,
May 25, 2016, 3:35:35 PM5/25/16
to Cardi...@googlegroups.com
Currently, the only export (other than saving data as a .RData file) is text export using any of R's functions for writing data to text files, e.g. write.table() or write.csv().

For example, you could do something like

library(CardinalWorkflows)
data
(pig206, pig206_analyses)
pData
(pig206)$myclusters <-pig206.sscg[[r=2,s=6,k=20]]$classes
write
.csv(pData(pig206),file="pixeldata.csv")

to write the pixel metadata with the clusters.

Likewise, you could use write.table or write.csv on iData(yourdataset) to write the intensities as a (# of m/z values) x (# of pixels) matrix to a text file.

If you have a particular text format you need to export as, I could help you figure out how to do it. However, exporting to other file formats isn't currently supported.

I hope this helps,
Kylie

kbemis

unread,
May 26, 2016, 7:57:57 PM5/26/16
to Cardi...@googlegroups.com
Line-by-line:

library(CardinalWorkflows)
data
(pig206, pig206_analyses)

Here I am just loading the CardinalWorkflows package with example datasets, and then loading the example datasets.

pData(pig206)$myclusters <- pig206.sscg[[r=2,s=6,k=20]]$classes

pData(pig206) accesses the data.frame with the dataset's pixel metadata. pData(pig206)$myclusters accesses the "myclusters" column of the pixel metadata. This does not exist yet, but it is created when I assign into it with <-.

pig206.sscg is a ResultSet holding segmentation results. Since it holds results for more than one parameter set, I'm indexing into it by [[r=2,s=6,k=20]], to select the results for those specific parameters.

(It can also be indexed like an ordinary list, with [[1]], [[2]], etc. In this case pig206.sscg[[15]] is the equivalent of pig206.sscg[[r=2,s=6,k=20]], because those parameters are the fifteenth parameter set in the results, which you can confirm by examining summary(pig206.sscg).

Then pig206.sscg[[r=2,s=6,k=20]]$classes just accesses the classes (clusters/segments) from the segmentation for those parameters. I'm assigning them to pData(pig206)$myclusters.

write.csv(pData(pig206), file="pixeldata.csv")

Finally, I'm using write.csv to write the pData(pig206) data.frame with the pixel metadata to a comma-separated-variable file called "pixeldata.csv".

-Kylie

jennifer.n...@gmail.com

unread,
Jun 7, 2016, 2:13:38 PM6/7/16
to Cardinal MSI Help
Thank you, that was very helpful. I am looking for a way in which I can extract the data so that I can compare the spectra of pixels from one cluster to another. I'm sure there is a way in which I can do that, but I am having some trouble finding it in the Cardinal documentation.
Message has been deleted

nheathp...@gmail.com

unread,
Jun 9, 2016, 11:00:34 AM6/9/16
to Cardinal MSI Help
Hi Jennifer,

This will plot the mean of all the pixels in a segmentation in R, you can vary the plot arguments for color / overlay, etc.:
##kidney_sa: original data
##kidney_sa.peaks : peakdata

##segmentation:
sscg_sa <- spatialShrunkenCentroids(kidney_sa.peaks,r=1,k=10,s=1)

##plot spectra by SSCG class:
plot(kidney_sa,pixel=1:ncol(kidney_sa),pixel.groups=sscg_sa$classes[[1]],fun=mean)


To get the numerical values use:

featureApply(kidney_sa,.pixel.groups=sscg_sa$classes[[1]],.fun=mean)

which will return a matrix of cluster vs m/z bin with intensity values.

Cardinal doesn't currently do any exporting, but you can coerce these mean spectra into  a MALDIquant spectrum object and export MzML files for them as shown below:

##need the MALDIquant package:

require(MALDIquant)
require(MALDIquantForeign)

##get mean cluster data as data.frame:

mean_df <- data.frame(featureApply(kidney_sa,.pixel.groups=sscg_sa$classes[[1]],.fun=mean))

##coerce mean specs to MALDIquant:

SegmentsMzMls <- lapply(c(1:sscg_sa$k[[1]]),function(x){
  spec <-createMassSpectrum(mass=mz(kidney_sa.peaks), intensity=as.numeric(mean_df[x,]))
  return(spec)
})

##write MzMl..

exportMzMl(SegmentsMzMls[[1]], file="segment_1_meanspec.mzml")

##write all MzMl via for loop..

for (i in 1:sscg_sa$k[[1]]){
  exportMzMl(SegmentsMzMls[[i]], file=paste0("segment_",i,"_meanspec.mzml"))
}


Hope that helps.

Heath

kbemis

unread,
Jun 9, 2016, 4:35:43 PM6/9/16
to Cardinal MSI Help
Heath gives excellent advice above. Thanks!

-Kylie

Marco Fidaleo

unread,
Jul 29, 2024, 9:58:40 AM7/29/24
to Cardinal MSI Help

Hello everyone,

After performing segmentation using Spatial Shrunken Centroids (SSC), I would like to extract the value of a specific m/z for each pixel within the clusters just to compare different clusters and make statements such as "this m/z signal is 10 times more higher in cluster 1 than in cluster 2." I believe the lines of code above worked in previous versions of Cardinal, but they no longer seem to be compatible with the current version. Can anyone provide assistance on how to achieve this with the latest version of Cardinal?

Thank you!

kbemis

unread,
Jul 29, 2024, 10:19:25 AM7/29/24
to Cardinal MSI Help
Here is an example using CardinalWorkflows data:

library(Cardinal)
pig206 <- CardinalWorkflows::exampleMSIData("pig206")

# preprocess
pig206_peaks <- normalize(pig206) |>
peakProcess(SNR=6, sampleSize=0.1)

# segmentation
set.seed(1)
pig206_ssc <- spatialShrunkenCentroids(pig206_peaks,
r=2, k=6, s=12)

# plot segmentation
image(pig206_ssc)

# plot shrunken centroids
plot(pig206_ssc, type="centers")

# rename segments so they don't start with a number
segments <- pig206_ssc$class
levels(segments) <- paste0("C", levels(segments))

# summarize true means
pig206_means <- summarizeFeatures(pig206_peaks,
stat="mean", groups=segments)

# means are now columns in featureData
featureData(pig206_means)

# plot true means
cols <- paste0(levels(segments), ".mean")
plot(pig206_means, formula=cols, superpose=TRUE)

# writing imzML will save fData columns in 3.6
writeMSIData(pig206_means, file="~/Desktop/pig206_means.imzML")

# or just write the fData alone
write.csv(featureData(pig206_means), file="~/Desktop/pig206_means.csv")

-Kylie

Marco Fidaleo

unread,
Jul 29, 2024, 12:51:54 PM7/29/24
to Cardinal MSI Help
Thank you very much. It works perfectly. There is one thing I don't understand. For example, I have an mz that describes a cluster; if I find this mz in the Excel file after following the procedure you described, the average value is of the same order of magnitude as the center derived from TopFeatures. Even though using Spatial Shrunken Centroids, the centers do not describe the average of a specific mz in the cluster, the fact that is a similar value does not surprise me. The problem is when I graph my specific mz, the scale values are completely different by thousands of times from the mean value. Why this difference?
M

kbemis

unread,
Jul 29, 2024, 12:59:12 PM7/29/24
to Cardinal MSI Help
Do you mean plotting the ion image shows a different scale of intensities compared to the mean spectra of a segment/cluster?

That is also to be expected because (1) the image will include all pixels rather than a single cluster and (2) the range of intensities across the whole sample will be much wider (and include many outliers/hotspots) than even the spread of average intensities for the clusters (because they are averaged).

-Kylie

Reply all
Reply to author
Forward
0 new messages