#Script to read in and plot MS Spectra from Shiadzu MALDI-TOF
#Step 1: Read files into dataframes
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
standardSpectra = read.table('A3_0001_A3_25-07-18_16-03_0001.txt', header = FALSE, skip = 8)
sampleSpectra = read.table('A4_0001_A4_25-07-18_16-04_0001.txt', header = FALSE, skip = 8)
#Step 2: Remove outlier in first row and add column names
standardSpectra = standardSpectra[-1,] #remove first row
standardSpectra = standardSpectra[standardSpectra$V1<900,] #set threshold
sampleSpectra = sampleSpectra[-1,]
sampleSpectra = sampleSpectra[sampleSpectra$V1<900,]
names(standardSpectra) = c('m_z', 'intensity') #name columns
names(sampleSpectra) = c('m_z', 'intensity')
#Step 3: Tidy data
sampleSpectra$type = 'Sample'
sampleSpectra$m_z = round(sampleSpectra$m_z) #round m/z values
standardSpectra$type = 'Standard'
standardSpectra$m_z = round(standardSpectra$m_z)
ms_data = rbind(sampleSpectra, standardSpectra)
names(ms_data) = c('m_z', 'intensity', 'type')
#Step 4: Visualize data using ggplot
ggplot(
ms_data, aes(x = m_z, y = intensity))+
geom_col(aes(colour = type , fill = type))+
scale_color_manual(values = c("#ffc868", "#e5216b"))+
scale_fill_manual(values = c("#ffc868", "#e5216b"))+
xlab("m/z Ratio") + ylab("Intensity (mV)")+
theme(legend.position="right")
#Step 5: Isolate sample type visuals using facet wrap
ggplot(
ms_data, aes(x = m_z, y = intensity))+
geom_col(aes(colour = type, fill = type))+
scale_color_manual(values = c("#ffc868", "#e5216b"))+
scale_fill_manual(values = c("#ffc868", "#e5216b"))+
xlab("m/z Ratio")+
ylab("Intensity (mV)")+
facet_wrap(~type, nrow = 2)
#Find peaks with identical m/z ratios
matchSpectra = merge(sampleSpectra, standardSpectra, by = "m_z") #18 matches