From 037805622f8535d7715a99928c7a5dcdc00bf165 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Todor=20Kondi=C4=87?= <kontrapunkt@uclmail.net>
Date: Sat, 11 Mar 2023 13:06:37 +0100
Subject: [PATCH] Start adding structure for MS2 extraction.

---
 R/api.R        | 11 +++++++----
 R/data-model.R | 10 +++++++++-
 R/extraction.R | 18 +++++++++++++-----
 3 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/R/api.R b/R/api.R
index 5f541b5..f56b600 100644
--- a/R/api.R
+++ b/R/api.R
@@ -337,7 +337,8 @@ extr_data <-function(m) {
 
     ## Big ms1 chromatogram table.
     cg1 = new_ms1_cgm_table() 
-    
+
+    cg2 = new_ms2_cgm_table()
     for (fn in fine[,unique(file)]) {
 
         
@@ -356,21 +357,23 @@ extr_data <-function(m) {
 
         ## Extract MS1 chromatograms.
         fncg1 = new_ms1_cgm_table()
-
         fncg1 = extr_ms1_cgm(ms=ms,
                              isotab=isotab1,
                              qrt=F,
                              fncg1)
-        
         fncg1 = extr_ms1_cgm(ms=ms,
                              isotab=isotab2,
                              qrt=T,
                              fncg1)
-        
         cg1 = cg1[fncg1,.(precid,
                           rt,
                           intensity=i.intensity,
                           scan=i.scan)]
+
+        ## Extract MS2 chromatograms.
+        fnfd = as.data.table(fData(ms),keep.rownames="scan",key="scan")
+        fncg2 = extr_ms2_cgm(fnfd,fncg1)
+        1+1
         
     }
     
diff --git a/R/data-model.R b/R/data-model.R
index 4e136f0..3befa40 100644
--- a/R/data-model.R
+++ b/R/data-model.R
@@ -124,10 +124,18 @@ new_ms2_cgm_cat <- function() {
                an = integer(0))
 }
 
+new_ms2_cgm_table <- function() {
+    ## Varios rt-related MS2 data.
+    data.table(precid = integer(0),
+               ce = numeric(0),
+               an = integer(0),
+               rt = numeric(0))
+}
+
 new_ms2_cgm_table <- function() {
     ## Creates a chromatogram.
     data.table(precid = integer(0),
-               an = intege r(0),
+               an = integer(0),
                rt = numeric(0),
                intensity = numeric(0),
                scan = character(0),
diff --git a/R/extraction.R b/R/extraction.R
index 0410eec..08ba53d 100644
--- a/R/extraction.R
+++ b/R/extraction.R
@@ -620,7 +620,7 @@ create_fine_table <- function(m) {
                                  "rt_min",
                                  "rt_max",
                                  "file"),
-          keyby=c("file","isofine")]
+          keyby=c("file","precid")]
 
 }
 
@@ -660,14 +660,14 @@ extr_ms1_cgm <- function(ms,isotab,qrt,res) {
         ## fill the data table.
         for (i in 1L:nrow(mzrng)) {
             rt = rtime(x[i,1])
-            isofine = isotab[(i),isofine]
-            chunk = data.table(isofine=isofine,
+            precid = isotab[(i),precid]
+            chunk = data.table(precid=precid,
                                rt=rt,
                                intensity=intensity(x[i,1]),
                                scan = names(rt),
-                               key = c("isofine","rt"))
+                               key = c("precid","rt"))
 
-            res = res[chunk,.(isofine,
+            res = res[chunk,.(precid,
                               rt,
                               intensity=i.intensity,
                               scan=i.scan)]
@@ -678,3 +678,11 @@ extr_ms1_cgm <- function(ms,isotab,qrt,res) {
 }
 
 
+extr_ms2_cgm <- function(feat_table,one_file_cg1) {
+    ## Get scanIdx.
+
+    browser()
+    precs = feat_table[one_file_cg1,.(scan,scanIdx),on="scan"]
+    
+    
+}
-- 
GitLab