Sandbox
From Metabolomics.JP
(Difference between revisions)
m |
m |
||
Line 1: | Line 1: | ||
+ | {| class ="wikitable" | ||
{{#replace: | {{#replace: | ||
{{#lua: | {{#lua: | ||
+ | function atomicNum(atom, str) | ||
+ | if (str == "") then return 0; end | ||
+ | if (str == atom) then return 1; end | ||
+ | return tonumber(string.sub(str,2)); | ||
+ | end | ||
+ | |||
+ | function mass(str) | ||
+ | local c,h,n,o=string.match(str,"(C?[1-9]?[0-9]?)(H?[1-9]?[0-9]?)(N?[1-9]?[0-9]?)(O?[1-9]?[0-9]?)"); | ||
+ | c = atomicNum("C", c) * 12; | ||
+ | h = atomicNum("H", h); | ||
+ | n = atomicNum("N", n) * 14; | ||
+ | o = atomicNum("O", o) * 16; | ||
+ | return c + h + n + o; | ||
+ | end | ||
+ | |||
+ | function diff(str1, str2) | ||
+ | ---computes str1 - str2. If negative, returns nil.--- | ||
+ | local c1,h1,n1,o1=string.match(str1,"(C?[1-9]?[0-9]?)(H?[1-9]?[0-9]?)(N?[1-9]?[0-9]?)(O?[1-9]?[0-9]?)"); | ||
+ | local c2,h2,n2,o2=string.match(str2,"(C?[1-9]?[0-9]?)(H?[1-9]?[0-9]?)(N?[1-9]?[0-9]?)(O?[1-9]?[0-9]?)"); | ||
+ | c1 = atomicNum("C", c1); | ||
+ | h1 = atomicNum("H", h1); | ||
+ | n1 = atomicNum("N", n1); | ||
+ | o1 = atomicNum("O", o1); | ||
+ | c2 = atomicNum("C", c2); | ||
+ | h2 = atomicNum("H", h2); | ||
+ | n2 = atomicNum("N", n2); | ||
+ | o2 = atomicNum("O", o2); | ||
+ | if (c2 > c1) then return nil; else c1 = c1 - c2; end | ||
+ | if (h2 > h1) then return nil; else h1 = h1 - h2; end | ||
+ | if (n2 > n1) then return nil; else n1 = n1 - n2; end | ||
+ | if (o2 > o1) then return nil; else o1 = o1 - o2; end | ||
+ | local ret = ""; | ||
+ | if (c1 > 1) then ret = ret .. "C" .. c1; | ||
+ | else if (c1 == 1) then ret = ret .. "C"; end end | ||
+ | if (h1 > 1) then ret = ret .. "H" .. h1; | ||
+ | else if (h1 == 1) then ret = ret .. "H"; end end | ||
+ | if (n1 > 1) then ret = ret .. "N" .. n1; | ||
+ | else if (n1 == 1) then ret = ret .. "N"; end end | ||
+ | if (o1 > 1) then ret = ret .. "O" .. o1; | ||
+ | else if (o1 == 1) then ret = ret .. "O"; end end | ||
+ | return ret; | ||
+ | end | ||
+ | |||
local ruler = stdin:match("[%S ,]+"); | local ruler = stdin:match("[%S ,]+"); | ||
local axis = {}; | local axis = {}; | ||
Line 9: | Line 53: | ||
end | end | ||
print("#-\n"); | print("#-\n"); | ||
− | print("## sample"); | + | print("## sample \n"); |
for i=1, table.getn(axis) do | for i=1, table.getn(axis) do | ||
− | print("## " .. axis[i]); | + | print("#style='text-align:right'# " .. mass(axis[i]) .. "<br/>" .. axis[i]); |
end | end | ||
+ | local s; | ||
for i=1, table.getn(axis) do | for i=1, table.getn(axis) do | ||
print("#-\n"); | print("#-\n"); | ||
− | print("##" .. axis[i]); | + | print("#style='text-align:right'# " .. mass(axis[i]) .. "<br/>" .. axis[i]); |
for j=1, table.getn(axis) do | for j=1, table.getn(axis) do | ||
− | |||
if (j < i) then | if (j < i) then | ||
− | + | s = diff(axis[j],axis[i]); | |
+ | if (s == nil) then print('#style="background-color:gray"# '); | ||
+ | else print("## " .. s); | ||
+ | end | ||
+ | else print('#style="background-color:white"# '); | ||
end | end | ||
end | end | ||
Line 27: | Line 75: | ||
}} | }} | ||
|#|{{#bar:}}}} | |#|{{#bar:}}}} | ||
+ | |} | ||
<!---- | <!---- |
Revision as of 11:25, 12 May 2009