-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplot_annotated_spectrum.R
More file actions
executable file
·68 lines (49 loc) · 2.23 KB
/
plot_annotated_spectrum.R
File metadata and controls
executable file
·68 lines (49 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
library(mzR)
library(MSnbase)
library(protViz)
library(stringr)
options(stringsAsFactors = FALSE)
#To call functions defined in Spectra_functions.R
source("./Spectra_functions.R")
#set working directory which contains the PSM table
setwd("")
#set absolute file path where raw files are located
mzml_path=""
infile=""
##input PSM table is tsv output from MS-GF plus search engine
##double check if the dataframe has column names "SpecFile" , "Peptide", "Charge", "ScanNum", "SpecEValue", "Precursor"
DF = read.table(infile,header=T,comment.char = "",quote="",sep = "\t")
DF$Sequence=gsub("[^A-Z]","",DF$Peptide)
Frag.ions.tolerance= 0.02 # unit Dalton
relative = FALSE
Spectra_list= vector(mode="list", length=length(unique(as.character(DF[,]$SpectraFile))))
names(Spectra_list)=unique(as.character(DF[,]$SpectraFile))
require(lattice)
pdf("PSM.annotated.spectra.pdf",width=12,height=7,useDingbats = FALSE)
for (i in 1:nrow(DF)){
spectra_file=as.character(DF[i,]$SpectraFile)
mzml_file=paste0(mzml_path,spectra_file)
ScanNum=as.integer(DF[i,]$ScanNum)
peptide=as.character(DF[i,]$Peptide)
seq=DF[i,]$Sequence
precMass=DF[i,]$Precursor
precCharge=DF[i,]$Charge
if (is.null(Spectra_list[[spectra_file]])){
Spectra_list[[spectra_file]]=openMSfile(mzml_file,verbose=T)
}
exp_peaks<-as.data.frame(peaks(Spectra_list[[spectra_file]],scan=ScanNum))
colnames(exp_peaks) = c("mz","intensity")
predicted_peaks = predict_MS2_spectrum(Peptide = peptide, product_ion_charge = 1)
match_ions = match_exp2predicted(exp_peaks, predicted_peaks, tolerance =Frag.ions.tolerance, relative = relative )
spectrum_info = paste("Scan Number:",as.character(ScanNum),
"precMass:",as.character(precMass),
"precCharge:",as.character(precCharge),
"Sequence:",seq)
print (ggplot(exp_peaks,aes(x=mz, ymax=intensity, ymin=0)) +geom_linerange()+
geom_point(data = match_ions, aes(x=mz, y=intensity, color=type))+
geom_text(data = match_ions, aes(x=mz, y=intensity, label= ion ),colour="black")+
annotate("text", -Inf, Inf, label = spectrum_info, hjust = 0, vjust = 1)+
ylab('Intensity')+xlab('M/Z')+ggtitle(peptide)+theme(plot.title = element_text(hjust = 0.5))
)
}
dev.off()