# Supporting Information for:
# Development of a Scoring Parameter to  Characterize Data Quality of Centroids in High-Resolution Mass Spectra.
###By: Max Reuschenbach, Lotta Hohrenk-Danzouma, Torsten C. Schmidt & Gerrit Renner (2022)




# Before being able to run the code the following packages must be installed:
#install.packages("Matrix")
#install.packages("mzR")
#install.packages("R.utils")
#install.packages("data.table")
#install.packages("pracma")
#install.packages("tidyverse")

library(Matrix)
library(mzR)
library(R.utils)
library(data.table)
library(pracma)
library(tidyverse)


### The following function contains the Centroiding algorithm.
### To use the algorithm, only the file path for a profile *.mzXML file must be inserted into "path_mzXML". It is encouraged to perform the transformation of the vendor-specific profile format (e.g., *.raw in case of Orbitrap-MS, Thermo) with the msConvert tool of Proteowizard. The centroided dataset (Centroided_Profile_DQS.csv) is then saved to the global R environment and the folder in which this code is located. 
### The first function (DQS_centroiding_Orbitrap) is used for Orbitrap-MS, while the second function (DQS_centroiding_TOF) can be used for TOF-MS. The second function additionally states the asymmetry of each peak, as a model (Bi-Gaussian) is implemented that estimates that.

DQS_centroiding_Orbitrap <- function(path_mzXML){

##Import of file and import info

profile_mz <- mzR::openMSfile(path_mzXML)
profile_mz_1 <-mzR::spectra(profile_mz)
header_profile_mz <- mzR::header(profile_mz)
RT <- header_profile_mz$retentionTime[header_profile_mz$msLevel==1]
profile_ms_level <- profile_mz_1[header_profile_mz$msLevel==1]
rm(header_profile_mz,profile_mz,profile_mz_1)
profile_ms_level <- do.call(rbind,profile_ms_level)
profile_ms_level <- as.matrix(profile_ms_level)


### Peak detection algorithm for Orbitrap-MS
id_1 <- sign(c(profile_ms_level[-1,2],0)-profile_ms_level[,2])
id_2 <- c(0,id_1==1)
id_3 <- c(0,id_2)[-length(id_2)]
id_4 <- ((id_2 -id_3 )==1)
id_2 <- c(0,id_1==-1)
id_3 <- c(id_1==-1,0)
id_5 <- ((id_2-id_3  )==1)*(-1)
rm(id_1,id_2,id_3)
id_6 <- id_4 +id_5
rm(id_4,id_5)
id_6[which(id_6==1)] <- cumsum(id_6[which(id_6==1)])
id_6[which(id_6==-1)] <- cumsum(id_6[which(id_6==-1)])
id_6 <- cumsum(id_6)
profile_ms_level <- cbind(profile_ms_level,id_6[-length(id_6)])
rm(id_6)
Intensity_position_1 <-which((profile_ms_level[,2]>0 & profile_ms_level[,3]==0)==TRUE)
I_new <- profile_ms_level[,2]
I_new <-insert(x=I_new,ats= c(Intensity_position_1),values=c(profile_ms_level[,2][Intensity_position_1]))
I_new2 <-insert(x=I_new,ats= c(which(diff(I_new)==0 & I_new>0))+1,values=c(rep(0,length(which(diff(I_new)==0 & I_new>0)))))
mz_new <- profile_ms_level[,1]
mz_new <-insert(x=mz_new,ats= c(Intensity_position_1),values=c(profile_ms_level[,1][Intensity_position_1]))
mz_new <-insert(x=mz_new,ats= c(which(diff(I_new)==0 & I_new>0))+1,values=c(rep(0,length(which(diff(I_new)==0 & I_new>0)))))
profile_ms_level <- cbind(mz_new,I_new2)
rm(I_new2,mz_new,Intensity_position_1,I_new)
id_1 <- sign(c(profile_ms_level[-1,2],0)-profile_ms_level[,2])
id_2 <- c(0,id_1==1)
id_3 <- c(0,id_2)[-length(id_2)]
id_4 <- ((id_2 -id_3 )==1)
id_2 <- c(0,id_1==-1)
id_3 <- c(id_1==-1,0)
id_5 <- ((id_2-id_3  )==1)*(-1)
rm(id_1,id_2,id_3)
id_6 <- id_4 +id_5
rm(id_4,id_5)
id_6[which(id_6==1)] <- cumsum(id_6[which(id_6==1)])
id_6[which(id_6==-1)] <- cumsum(id_6[which(id_6==-1)])
id_6 <- cumsum(id_6)
profile_ms_level <- cbind(profile_ms_level,id_6)
rm(id_6)
profile_ms_level2 <-profile_ms_level
rm(profile_ms_level)
profile_ms_level2 <- profile_ms_level2[-which(profile_ms_level2[,3]==0),]


## Least-Squares Gaussian Peak fit


Sparse_matrix_y <- sparseMatrix(x=c(profile_ms_level2[,2]),                                i = c(1:dim(profile_ms_level2)[1]),                                 j = c(c(profile_ms_level2[,3])))
Sparse_matrix_x <- sparseMatrix(x=c(profile_ms_level2[,1]),                                i = c(1:dim(profile_ms_level2)[1]),                                 j = c(c(profile_ms_level2[,3])))
centroids <- Matrix::colSums((Sparse_matrix_y*Sparse_matrix_x))/Matrix::colSums(Sparse_matrix_y)

rm(Sparse_matrix_y,Sparse_matrix_x)

Sparse_matrix_x <- sparseMatrix(x=c(rep(1,length(profile_ms_level2[,1]))),i = c(1:dim(profile_ms_level2)[1]),j = c(c(profile_ms_level2[,3])))
Sparse_matrix_x2 <- sparseMatrix(x=centroids,i = c(1:length(centroids)), j = c(1:length(centroids)))
cent_matrix <- Sparse_matrix_x %*% Sparse_matrix_x2
rm(Sparse_matrix_x,Sparse_matrix_x2)

profile_ms_level2 <- cbind(profile_ms_level2,c(profile_ms_level2[,1]-cent_matrix@x))
Sparse_matrix_y <- sparseMatrix(x=c(profile_ms_level2[,2]),i = c(1:dim(profile_ms_level2)[1]),j = c(c(profile_ms_level2[,3])))
Sparse_matrix_y2 <- sparseMatrix(x=c(rep(1,length(profile_ms_level2[,2]))),i = c(1:dim(profile_ms_level2)[1]),j = c(c(profile_ms_level2[,3])))



Sparse_matrix_y3 <- sparseMatrix(x=c(Matrix::colSums(Sparse_matrix_y)),                                i = c(1:length(Matrix::colSums(Sparse_matrix_y))),                                j = c(1:length(Matrix::colSums(Sparse_matrix_y))))



Sparse_matrix_y4 <- sparseMatrix(x=c(1/Matrix::colSums(Sparse_matrix_y2)),i = c(1:length(Matrix::colSums(Sparse_matrix_y))),j = c(1:length(Matrix::colSums(Sparse_matrix_y))))

Sparse_matrix_y3 <- (Sparse_matrix_y2 %*% Sparse_matrix_y3) %*% Sparse_matrix_y4
rm(Sparse_matrix_y2,Sparse_matrix_y4)


profile_ms_level2 <- cbind(profile_ms_level2,c((Sparse_matrix_y@x/Sparse_matrix_y3@x)^2))
rm(Sparse_matrix_y,Sparse_matrix_y3)

profile_ms_level2_test_matrix <- profile_ms_level2[which(is.na(profile_ms_level2[,3])==FALSE),]
rm(profile_ms_level2)
reg_matrix_x <- sparseMatrix(x=c(rep(1,length(profile_ms_level2_test_matrix[,4])),profile_ms_level2_test_matrix[,4],(profile_ms_level2_test_matrix[,4])^2),  i = c(1:dim(profile_ms_level2_test_matrix)[1],1:dim(profile_ms_level2_test_matrix)[1],1:dim(profile_ms_level2_test_matrix)[1]),  j = c(profile_ms_level2_test_matrix[,3]*3-2,profile_ms_level2_test_matrix[,3]*3-1,profile_ms_level2_test_matrix[,3]*3-0))
d_o_f <- Matrix::colSums(reg_matrix_x)
d_o_f <- (d_o_f[seq(1,dim(reg_matrix_x)[2],by=3)])
d_o_f <- d_o_f - 3
reg_matrix_y <- sparseMatrix(x=c(log(profile_ms_level2_test_matrix[,2])),i = c(1:dim(profile_ms_level2_test_matrix)[1]),j = c(c(profile_ms_level2_test_matrix[,3])))
### Weighting of the Regression with W~ I^2
n_w = 2
reg_matrix_w <- sparseMatrix(x=c(profile_ms_level2_test_matrix[,5]^n_w),i = c(1:dim(profile_ms_level2_test_matrix)[1]),j = c(1:dim(profile_ms_level2_test_matrix)[1]))
norm_x_v <- Matrix::t(reg_matrix_x)%*% reg_matrix_w%*%reg_matrix_x

### Determination of the inverse matrix
a <- Matrix::diag(norm_x_v)
a <- a[seq(from=1,length(a), 3)]
b_l <- length(Matrix::diag(norm_x_v))
b <- norm_x_v[seq(1, b_l, 3),seq(2, b_l, 3)]
b <- Matrix::diag(b)
c <- norm_x_v[seq(1, b_l, 3),seq(3, b_l, 3)]
c <- Matrix::diag(c)
f <- norm_x_v[seq(2, b_l, 3),seq(3, b_l, 3)]
f <- Matrix::diag(f)
i <- norm_x_v[seq(3, b_l, 3),seq(3, b_l, 3)]
i <- Matrix::diag(i)
det <- a*(c*i-f*f) - b*(b*i-f*c) + c*(b*f-c*c)
a_a <- c*i - f*f
a_a <- a_a/det
b_a <- c*f - b*i
b_a  <- b_a /det
c_a <- b*f - c*c
c_a  <-c_a /det
e_a <- a*i - c*c
e_a  <-e_a /det
f_a <- c*b - a*f
f_a  <-f_a /det
i_a <- a*c - b*b
i_a  <-i_a /det
row_a_a <- seq(1,(dim(norm_x_v)[1]),by=3)
row_d_a <- seq(2,(dim(norm_x_v)[1]),by=3)
row_g_a <- seq(3,(dim(norm_x_v)[1]),by=3)
adjunct_x_inv <- sparseMatrix(x = c(a_a,b_a,c_a,b_a,e_a,f_a,c_a,f_a,i_a),i=c(row_a_a,row_a_a,row_a_a,row_d_a,row_d_a,row_d_a,row_g_a,row_g_a,row_g_a ),j=c(row_a_a,row_d_a,row_g_a,row_a_a,row_d_a,row_g_a,row_a_a,row_d_a,row_g_a))
rm(a_a,b_a,c_a,e_a,f_a,i_a,row_a_a,row_d_a,row_g_a,a,b,c,f,i,norm_x_v)
### Calculation of the matrix with the regression coefficients
reg <- adjunct_x_inv %*% (Matrix::t(reg_matrix_x) %*% reg_matrix_w %*% reg_matrix_y)
beta_0 <- Matrix::diag(reg[seq(1,dim(reg)[1],by=3),1:dim(reg)[2]])
beta_1 <- Matrix::diag(reg[seq(2,dim(reg)[1],by=3),1:dim(reg)[2]])
beta_2 <- Matrix::diag(reg[seq(3,dim(reg)[1],by=3),1:dim(reg)[2]])
rm(reg)
y_hat <-(reg_matrix_x %*% adjunct_x_inv %*% Matrix::t(reg_matrix_x) %*% reg_matrix_w  %*% reg_matrix_y)
e_r <- reg_matrix_y - y_hat
e_r <- e_r^2

e_r <- e_r*Matrix::diag(reg_matrix_w)
e_r <-Matrix::colSums(e_r)
rm(y_hat,reg_matrix_w,reg_matrix_x)
MSE <- e_r/(d_o_f)
SE_b0 <- Matrix::diag(adjunct_x_inv)[seq(1,dim(adjunct_x_inv)[1],by=3)]*MSE
SE_b1 <- Matrix::diag(adjunct_x_inv)[seq(2,dim(adjunct_x_inv)[1],by=3)]*MSE
SE_b2 <- Matrix::diag(adjunct_x_inv)[seq(3,dim(adjunct_x_inv)[1],by=3)]*MSE
sigma_parabola <- sqrt(-1/(2*beta_2))
mu_parabola <-  (beta_1/(2*abs(beta_2)))
height_parabola <- exp(beta_0 - (beta_1*beta_1)/(4*beta_2))
Area <- sigma_parabola*height_parabola *sqrt(2*pi)
k_1 = exp(2*beta_0 - ((beta_1*beta_1)/(2*beta_2)))
k_2 = sqrt(2*pi)*exp(beta_0 - (beta_1*beta_1)/(4*beta_2))
k_3 = sqrt(1/(2*abs(beta_2)))
d_0 = abs((pi*k_1*SE_b0)/beta_2)
d_1 = abs((pi*beta_1*beta_1*k_1)/(4*beta_2*beta_2*beta_2))*SE_b1
d_2 = abs((k_2/(4*beta_2*beta_2*k_3)) - ((k_2*beta_1*beta_1*k_3)/(4*beta_2*beta_2)))^2 * SE_b2
d_area <- sqrt(d_0 + d_1 + d_2)
d_area_rel <- (d_area/Area)
rm(k_1,k_2,k_3,d_0,d_1,d_2,d_area)
DQS_centroid <- 1-erf(d_area_rel)
RT_identifier<-c(1,as.integer(factor(cumsum(diff(centroids)<0)*NA^(diff(centroids)<0))))
RT_identifier[!is.na(RT_identifier)] <- 0
RT_identifier[is.na(RT_identifier)] <- 1
RT_identifier <- cumsum(RT_identifier)+1
RT_identifier <- as.data.frame(RT_identifier)
RT_identifier2 <- as.data.frame(cbind(unique(RT_identifier),RT))
RT_identifier2 <- merge(RT_identifier2,RT_identifier,by="RT_identifier")[,2]
Centroids_df <- cbind(c(mu_parabola+centroids)[which(beta_2<0)],sigma_parabola[which(beta_2<0)],height_parabola[which(beta_2<0)], Area[which(beta_2<0)],DQS_centroid[which(beta_2<0)],d_o_f[which(beta_2<0)],RT_identifier2[which(beta_2<0)],RT_identifier$RT_identifier[which(beta_2<0)])
Centroids_df <- Centroids_df[-which(Centroids_df[,6]<1),]
Centroids_df <- Centroids_df[,-6]


colnames(Centroids_df) <- c("Centroid","Peak width [sigma]", "Peak height","Peak area","DQS","RT [s]","Scans")
data.table::fwrite(x = Centroids_df,file = "Centroided_Profile_DQS.csv")
return(Centroids_df)
}








DQS_centroiding_TOF <- function(path_mzXML){

  ##Import of file and import info



profile_mz <- mzR::openMSfile(path_mzXML)

profile_mz_1 <-mzR::spectra(profile_mz)

header_profile_mz <- mzR::header(profile_mz)

RT <- header_profile_mz$retentionTime[header_profile_mz$msLevel==1]
RT_df <- cbind(rep(1,length(RT)),RT)
profile_ms_level <- profile_mz_1[header_profile_mz$msLevel==1]
rm(header_profile_mz,profile_mz,profile_mz_1)
profile_ms_level <- do.call(rbind,profile_ms_level)
profile_ms_level <- as.matrix(profile_ms_level)
#profile_ms_level <- profile_ms_level[1:1299513,]

### Peak detection algorithm for TOF-MS

id_1 <- sign(c(profile_ms_level[-1,2],0)-profile_ms_level[,2])
id_2 <- c(0,id_1==1)
id_3 <- c(0,id_2)[-length(id_2)]
id_4 <- ((id_2 -id_3 )==1)
id_2 <- c(0,id_1==-1)
id_3 <- c(id_1==-1,0)
id_5 <- ((id_2-id_3  )==1)*(-1)
rm(id_1,id_2,id_3)
id_6 <- id_4 +id_5
rm(id_4,id_5)
id_6[which(id_6==1)] <- cumsum(id_6[which(id_6==1)])
id_6[which(id_6==-1)] <- cumsum(id_6[which(id_6==-1)])
id_6 <- cumsum(id_6)
profile_ms_level <- cbind(profile_ms_level,id_6[-length(id_6)])
rm(id_6)
Intensity_position_1 <-which((profile_ms_level[,2]>0 & profile_ms_level[,3]==0)==TRUE)
I_new <- profile_ms_level[,2]
I_new <-insert(x=I_new,ats= c(Intensity_position_1),values=c(profile_ms_level[,2][Intensity_position_1]))
I_new2 <-insert(x=I_new,ats= c(which(diff(I_new)==0 & I_new>0))+1,values=c(rep(0,length(which(diff(I_new)==0 & I_new>0)))))
mz_new <- profile_ms_level[,1]
mz_new <-insert(x=mz_new,ats= c(Intensity_position_1),values=c(profile_ms_level[,1][Intensity_position_1]))
mz_new <-insert(x=mz_new,ats= c(which(diff(I_new)==0 & I_new>0))+1,values=c(rep(0,length(which(diff(I_new)==0 & I_new>0)))))
profile_ms_level <- cbind(mz_new,I_new2)
rm(I_new2,mz_new,Intensity_position_1,I_new)
id_1 <- sign(c(profile_ms_level[-1,2],0)-profile_ms_level[,2])
id_2 <- c(0,id_1==1)
id_3 <- c(0,id_2)[-length(id_2)]
id_4 <- ((id_2 -id_3 )==1)
id_2 <- c(0,id_1==-1)
id_3 <- c(id_1==-1,0)
id_5 <- ((id_2-id_3  )==1)*(-1)
rm(id_1,id_2,id_3)
id_6 <- id_4 +id_5
rm(id_4,id_5)
id_6[which(id_6==1)] <- cumsum(id_6[which(id_6==1)])
id_6[which(id_6==-1)] <- cumsum(id_6[which(id_6==-1)])
id_6 <- cumsum(id_6)
profile_ms_level <- cbind(profile_ms_level,id_6)
rm(id_6)
profile_ms_level2 <-profile_ms_level
rm(profile_ms_level)
profile_ms_level2 <- profile_ms_level2[-which(profile_ms_level2[,3]==0),]


## Least-Squares Bi-Gaussian Peak fit


output <- profile_ms_level2
output <- as.data.frame(output)
colnames(output) <- c("Centroid","Peak height","bin")




output <- output %>% group_by(bin) %>% mutate(I_scaled = `Peak height`/max(`Peak height`))

pos <- which(output$I_scaled==1)


output_C_new <- R.utils::insert(x=output$Centroid ,ats=c(pos),values=c(output$Centroid[pos]))
output_I_new <- R.utils::insert(x=output$`Peak height` ,ats=c(pos),values=c(output$`Peak height`[pos]))
output_bin_new <- R.utils::insert(x=c(0,diff(output$bin)),ats=c(pos+1),values=1)
output_bin_old <- R.utils::insert(x=output$bin,ats=c(pos),values=output$bin[pos])
output_I_scaled <- R.utils::insert(x=output$I_scaled,ats=c(pos),values=1)

output2 <-cbind(output_C_new,output_I_new,output_bin_new,output_bin_old,output_I_scaled)

output2[,3] <- cumsum(output2[,3])+1

colnames(output2) <- c("Centroid","Peak height","bin","bin_old","I_scaled")
output2 <- as.data.frame(output2)


output2$rt <- c(0)
output2$rt[which(diff(output2$Centroid)<0)+1] <- 1
output2$rt <- cumsum(output2$rt)+1
xt <- output2

RT_bin <- output2 %>% group_by(bin_old) %>% summarize(rt = max(rt))
RT_bin <- RT_bin$rt

reg_matrix_y2 <- Matrix::sparseMatrix(x=c(xt$`Peak height`),i = c(1:dim(xt)[1]),j = c(xt$bin))

reg_matrix_x2 <- Matrix::sparseMatrix(x=c(xt$Centroid),i = c(1:dim(xt)[1]),  j = c(xt$bin))

centroids <- Matrix::colSums((reg_matrix_y2*reg_matrix_x2))/Matrix::colSums(reg_matrix_y2)


peak_pos <- xt$Centroid[which(xt$I_scaled==1)]
rm(reg_matrix_y2,reg_matrix_x2)


reg_matrix_x2 <- Matrix::sparseMatrix(x=c(rep(1,length(xt$Centroid))), i = c(1:dim(xt)[1]),  j = c(c(xt$bin)))

reg_matrix_x3 <- Matrix::sparseMatrix(x=peak_pos,i = c(1:length(centroids)),  j = c(1:length(centroids)))

cent_matrix <- reg_matrix_x2 %*% reg_matrix_x3
rm(reg_matrix_x2,reg_matrix_x3)



xt <- cbind(xt,c(xt$Centroid-cent_matrix@x))


reg_matrix_y2 <- Matrix::sparseMatrix(x=c(xt$`Peak height`),i = c(1:dim(xt)[1]), j = c(xt$bin))


reg_matrix_y22 <- Matrix::sparseMatrix(x=c(rep(1,length(xt$`Peak height`))),i = c(1:dim(xt)[1]), j = c(xt$bin))



reg_matrix_y3 <- Matrix::sparseMatrix(x=c(Matrix::colSums(reg_matrix_y2)), i = c(1:length(Matrix::colSums(reg_matrix_y2))), j = c(1:length(Matrix::colSums(reg_matrix_y2))))




reg_matrix_y33 <- Matrix::sparseMatrix(x=c(1/Matrix::colSums(reg_matrix_y22)),i = c(1:length(Matrix::colSums(reg_matrix_y2))), j = c(1:length(Matrix::colSums(reg_matrix_y2))))

reg_matrix_y3 <- (reg_matrix_y22 %*% reg_matrix_y3)%*%reg_matrix_y33
rm(reg_matrix_y22,reg_matrix_y33)
output2_test_matrix <- cbind(xt$Centroid,xt$`Peak height`,xt$bin,c(xt$`c(xt$Centroid - cent_matrix@x)`),c((reg_matrix_y2@x/reg_matrix_y3@x)^2))

rm(reg_matrix_y2,reg_matrix_y3)






reg_matrix_x <- Matrix::sparseMatrix(x=c(rep(1,length(output2_test_matrix[,4])),(output2_test_matrix[,4])^2),  i = c(1:dim(output2_test_matrix)[1],1:dim(output2_test_matrix)[1]),  j = c(output2_test_matrix[,3]*2-1,output2_test_matrix[,3]*2-0))



d_o_f <- Matrix::colSums(reg_matrix_x)
d_o_f <- (d_o_f[seq(1,dim(reg_matrix_x)[2],by=2)])

reg_matrix_y <- Matrix::sparseMatrix(x=c(log(output2_test_matrix[,2])),i = c(1:dim(output2_test_matrix)[1]),  j = c(c(output2_test_matrix[,3])))


n_w = 2
reg_matrix_w <- Matrix::sparseMatrix(x=c(output2_test_matrix[,5]^n_w), i = c(1:dim(output2_test_matrix)[1]),j = c(1:dim(output2_test_matrix)[1]))
norm_x_v <- Matrix::t(reg_matrix_x)%*% reg_matrix_w%*%reg_matrix_x
a <- diag(norm_x_v)
a <- a[seq(1, length(a), 2)]
b <- norm_x_v[seq(1, length(diag(norm_x_v)), 2),seq(2,length(diag(norm_x_v)), 2)]
b <- diag(b)
c <- norm_x_v[seq(2, length(diag(norm_x_v)), 2),seq(1,length(diag(norm_x_v)), 2)]
c <- diag(c)
d <-  diag(norm_x_v)
d <- d[seq(2,length(d),2)]

det <- a*d - b*c

a2 <- a/det
b2 <- -b/det
c2 <- -c/det
d2 <- d/det


pos_a <- seq(2,(dim(norm_x_v)[1]),by=2)
pos_d <- seq(1,(dim(norm_x_v)[1]),by=2)

adjunct_x_inv <- Matrix::sparseMatrix(x = c(a2,d2,b2,c2),i=c(pos_a,pos_d,pos_a,pos_d),j=c(pos_a,pos_d,pos_d,pos_a))
rm(a_a,b_a,c_a,e_a,f_a,i_a,row_a_a,row_d_a,row_g_a,a,b,c,f,i,norm_x_v)

reg <- adjunct_x_inv %*% (Matrix::t(reg_matrix_x) %*% reg_matrix_w %*% reg_matrix_y)
beta_0 <- diag(reg[seq(1,dim(reg)[1],by=2),1:dim(reg)[2]])

beta_2 <- diag(reg[seq(2,dim(reg)[1],by=2),1:dim(reg)[2]])

beta_01 <- beta_0[seq(1,length(beta_0),2)]
beta_02 <- beta_0[seq(2,length(beta_0),2)]
beta_21 <- beta_2[seq(1,length(beta_0),2)]
beta_22 <- beta_2[seq(2,length(beta_0),2)]

rm(reg)
y_hat <-adjunct_x_inv %*% Matrix::t(reg_matrix_x)
y_hat2 <-y_hat %*% reg_matrix_w  %*% reg_matrix_y
y_hat3 <-(reg_matrix_x %*% y_hat2)

e_r <- reg_matrix_y - y_hat3
e_r <- e_r^2


e_r <- e_r*diag(reg_matrix_w)
e_r <-Matrix::colSums(e_r)

e_r1 <- e_r[seq(1,length(e_r),by=2)]
e_r2 <- e_r[seq(2,length(e_r),by=2)]

e_r <- e_r2+e_r1

dof1 <- d_o_f[seq(1,length(d_o_f),by=2)]
dof2 <- d_o_f[seq(2,length(d_o_f),by=2)]
d_o_f <- dof1+dof1-4-1
dof1 <- dof1-2
dof2 <- dof2-2



rm(y_hat,y_hat2,reg_matrix_w,reg_matrix_x)



MSE1 <- e_r1/(dof1)
MSE2 <- e_r2/(dof2)


MSE <- ((MSE1*dof1)/d_o_f) + ((MSE2*dof2)/d_o_f)

SE_b01 <- diag(adjunct_x_inv)[seq(1,dim(adjunct_x_inv)[1],by=4)]*MSE1
SE_b02 <- diag(adjunct_x_inv)[seq(3,dim(adjunct_x_inv)[1],by=4)]*MSE2

SE_b01 <- sqrt(SE_b01)
SE_b02 <- sqrt(SE_b02)


SE_b21 <- diag(adjunct_x_inv)[seq(2,dim(adjunct_x_inv)[1],by=4)]*MSE2
SE_b22 <- diag(adjunct_x_inv)[seq(4,dim(adjunct_x_inv)[1],by=4)]*MSE2
SE_b21 <- sqrt(SE_b21)
SE_b22 <- sqrt(SE_b22)


sigma_parabola1 <- sqrt(-1/(2*beta_21))

sigma_parabola2 <- sqrt(-1/(2*beta_22))

height_parabola1 <- exp(beta_01)
height_parabola2 <- exp(beta_02)
Area1 <- height_parabola1*sigma_parabola1*sqrt(2*pi)
Area2 <- height_parabola2*sigma_parabola2*sqrt(2*pi)


mu_parabola <- peak_pos[seq(1,length(peak_pos),2)]






d_area1 = sqrt(pi)*sqrt(((-beta_21^2*SE_b01^2 - 0.25*SE_b21^2)*exp(2*beta_01))/(beta_21^3))
  d_area2 = sqrt(pi)*sqrt(((-beta_22^2*SE_b02^2 - 0.25*SE_b22^2)*exp(2*beta_02))/(beta_22^3))



sigma21_ratio <- sigma_parabola2/sigma_parabola1
  peak_id <- c(1:length(mu_parabola))


df <- cbind(mu_parabola,sigma_parabola1,sigma_parabola2,sigma21_ratio,height_parabola1,height_parabola2,Area1,Area2,d_area1,d_area2,d_o_f ,dof1,dof2,RT_bin)
  df <- as.data.frame(df)

df$area_tot <- df$Area1+df$Area2


df$d_area_rel <- (df$d_area1+df$d_area2)/df$area_tot

df$asymm_DQS_tot <- 1-pracma::erf(df$d_area_rel)

df$mean_sigma <- (df$sigma_parabola2+df$sigma_parabola1)/2
df$h_tot <- (df$height_parabola2+df$height_parabola1)/2
df$peak_id <- peak_id
df <- df[df$dof1>0,]
df <- df[df$dof2>0,]

df <- df[df$d_o_f>0,]
#
df <- df[!is.na(df$sigma_parabola1),]
df <- df[!is.na(df$sigma_parabola2),]
df <- df[df$sigma_parabola1>0,]
df <- df[df$sigma_parabola2>0,]
#
#

#
 df2 <- cbind(df$mu_parabola,df$mean_sigma,df$h_tot,df$area_tot,df$asymm_DQS_tot,df$RT_bin,df$sigma21_ratio,df$peak_id)#[,c(1,18,19,15,17,14,4)]
#
df2 <- as.data.frame(df2)
RT_identifier <- as.data.frame(df$RT_bin)
RT_identifier2 <- as.data.frame(cbind(unique(RT_identifier),RT))
RT_identifier2 <- merge(RT_identifier2,RT_identifier,by="df$RT_bin")[,2]
df2$RT <- RT_identifier2

colnames(df2) <- c("Centroid","Peak width [sigma]", "Peak height", "Peak area", "DQS", "Scans", "Asymmetry","Peak_id","RT [s]")





data.table::fwrite(x = df2,file = "Centroided_Profile_DQS_TOF.csv")
  return(df2)
}




