Template:MassBank/Matrix

From Metabolomics.JP
(Difference between revisions)
Jump to: navigation, search
m
Line 3: Line 3:
 
   FORMULA_PAT = "(C?[1-9]?[0-9]?)(H?[1-9]?[0-9]?)(C?l?[2-9]?)(N?[1-9]?[0-9]?)(O?[1-9]?[0-9]?)(P?[2-9]?)(S?[2-9]?)";
 
   FORMULA_PAT = "(C?[1-9]?[0-9]?)(H?[1-9]?[0-9]?)(C?l?[2-9]?)(N?[1-9]?[0-9]?)(O?[1-9]?[0-9]?)(P?[2-9]?)(S?[2-9]?)";
 
   FORMULA_CHAR = "CHNOPSl0-9";
 
   FORMULA_CHAR = "CHNOPSl0-9";
   ATOMS = {C=12, H=1, Cl=35, N=14, O=16, P=31, S=32};
+
   ATOMS = {"C", "H", "Cl", "N", "O", "P", "S"};
 +
  MASS = {C=12, H=1, Cl=35, N=14, O=16, P=31, S=32};
  
 
   function map(f, list)
 
   function map(f, list)
     local ret = {};
+
     local ret = {}
 
     for _, v in pairs(list) do
 
     for _, v in pairs(list) do
         table.insert(ret, f(v));
+
         table.insert(ret, f(v))
 +
    end
 +
    return ret
 +
  end
 +
 
 +
  function pairlis(x,y)
 +
    local ret = {}
 +
    for i,v in pairs(x) do
 +
      table.insert(ret, {v, y[i]})
 +
    end
 +
    return ret
 +
  end
 +
 
 +
  function toFormula(t)
 +
    for i=1,table.getn(t) do
 +
      if (t[i] == "")
 +
      then t[i] = 0
 +
      else if (t[i] == ATOMS[i])
 +
        then t[i] = 1
 +
        else t[i]=tonumber(string.sub(t[i],1+string.len(ATOMS[i])))
 +
        end
 +
      end
 +
    end
 +
  end
 +
 
 +
  function toMass(str)
 +
    local t = {toFormula(string.match(str,FORMULA_PAT))}
 +
    ret = 0;
 +
    for i=1, table.getn(t) do
 +
      ret = ret + t[i] * MASS[i];
 
     end
 
     end
 
     return ret;
 
     return ret;
Line 15: Line 45:
 
   function atomicNum(atom, str)
 
   function atomicNum(atom, str)
 
     if (str == "") then return 0 end
 
     if (str == "") then return 0 end
     if (ATOMS[str] ~= nil) then return 1 end
+
     if (MASS[str] ~= nil) then return 1 end
 
     if (str == atom) then return 1 end
 
     if (str == atom) then return 1 end
     if (string.len(str) > 2 and ATOMS[string.sub(str,1,2)] ~= nil)
+
     if (string.len(str) > 2 and MASS[string.sub(str,1,2)] ~= nil)
 
       then return tonumber(string.sub(str,3));
 
       then return tonumber(string.sub(str,3));
 
     else return tonumber(string.sub(str,2));
 
     else return tonumber(string.sub(str,2));
Line 122: Line 152:
 
   print("## {{PAGENAME}} \n");
 
   print("## {{PAGENAME}} \n");
 
   for i=1, table.getn(axis) do
 
   for i=1, table.getn(axis) do
     print("#style='text-align:right'# " .. mass(axis[i]) .. "<br/>" .. axis[i]);
+
     print("#style='text-align:right'# " .. toMass(axis[i]) .. "<br/>" .. axis[i]);
 
   end
 
   end
 
   ---Rows---
 
   ---Rows---

Revision as of 23:03, 13 May 2009

./lua: /tmp/mw_ex_lua_XJFWOp:23: bad argument |1 to 'getn' (table expected, got string) No ion information in the form &&(formula)&&(formula)&&...&&

MassBank/Matrix

Personal tools
Namespaces

Variants
Actions