-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.R
106 lines (86 loc) · 3.2 KB
/
global.R
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
library(stringr)
library(tidyverse)
library(readxl)
library(data.table)
ROUND_TO = 5
#Load References
ion_types_norm = read_excel("resources.xlsx", sheet = "ion_type_norm")
ion_types_deut = read_excel("resources.xlsx", sheet = "ion_type_deut")
atoms_ref = read_excel("resources.xlsx", sheet = "atoms") #Atomic masses from https://physics.nist.gov/cgi-bin/Compositions/stand_alone.pl
amino_acids_norm = read_excel("resources.xlsx", sheet = "amino_acids_norm")
amino_acids_deut = read_excel("resources.xlsx", sheet = "amino_acids_deut")
losses_ref = read_excel("resources.xlsx", sheet = "losses")
term_norm = read_excel("resources.xlsx", sheet = "term_norm")
term_deut = read_excel("resources.xlsx", sheet = "term_deut")
#Global tables
ion_types_ref = ion_types_norm
amino_acids_ref = amino_acids_norm
term_ref = term_norm
#Atom table
atom_table_wide_mono = atoms_ref %>%
select(Abbrev1, MonoisotopicMass) %>%
pivot_wider(names_from = "Abbrev1", values_from = "MonoisotopicMass")
atom_table_wide_avg = atoms_ref %>%
select(Abbrev1, AverageMass) %>%
pivot_wider(names_from = "Abbrev1", values_from = "AverageMass")
#Amino acids norm
amino_acids_norm = amino_acids_norm %>%
arrange(Abbrev1)
amino_acids_norm = as.data.table(amino_acids_norm, key = "Abbrev1")
#Amino acids deut
amino_acids_deut = amino_acids_deut %>%
arrange(Abbrev1)
amino_acids_deut = as.data.table(amino_acids_deut, key = "Abbrev1")
#Sequence to atoms
sequence_to_atoms = function(sequenceString){
aar = amino_acids_ref
atom_table = data.table(Abbrev1 = unlist(strsplit(sequenceString, split = "", fixed = TRUE)))
atom_table = aar[match(atom_table$Abbrev1, aar$Abbrev1)]
atom_table = atom_table[,!1:5]
atom_table = colSums(atom_table)
atom_table
}
#Atoms to mass
atoms_to_mass = function(atom_table, mono){
if(mono == "Monoisotopic"){
for(i in 1:length(atoms_ref$Abbrev1)){
atom_table[1,i] = atom_table[1,i] * atoms_ref$MonoisotopicMass[i]
}
}
else{
for(i in 1:length(atoms_ref$Abbrev1)){
atom_table[1,i] = atom_table[1,i] * atoms_ref$AverageMass[i]
}
}
sum(atom_table)
}
#Naming for losses
losses_ref = losses_ref %>%
rowwise() %>%
mutate(loss_mass = atoms_to_mass(across(C:D), TRUE)) %>%
mutate(loss_display = if_else(!is.na(sidechain), paste0(round(loss_mass), Abbrev1), loss))
# #Calculate amino acid masses
# amino_acids_ref = amino_acids_ref %>%
# rowwise() %>%
# mutate(MonoisotopicMass = atoms_to_mass(across(C:`e-`), TRUE)) %>%
# mutate(AverageMass = atoms_to_mass(across(C:`e-`), FALSE))
#
# #Calculate ion type masses
# ion_types_ref = ion_types_ref %>%
# rowwise() %>%
# mutate(MonoisotopicMass = atoms_to_mass(across(C:`e-`), TRUE)) %>%
# mutate(AverageMass = atoms_to_mass(across(C:`e-`), FALSE))
#
# #Atoms to mass
# atoms_to_mass = function(atom_table){
# mass_table = atom_table %>%
# pivot_longer(cols = everything(), names_to = "Abbrev1", values_to = "number")
# mass_table = mass_table %>%
# left_join(atoms_ref, by = "Abbrev1")
# mass_table = mass_table %>%
# mutate(MonoisotopicMass = number * MonoisotopicMass) %>%
# mutate(AverageMass = number * AverageMass) %>%
# summarise(across(MonoisotopicMass:AverageMass, sum))
#
# mass_table
# }