Code
-- autogen structure code V1 -- print out current structure as a ScriptV2 program
--
-- autogenerates a LUA program in the recipe log file to restore structures
-- to the current state at some future time. Grabbing this code is a way you
-- can, for example, save a session of manual Structure Mode edits to a puzzle.
--
-- By default Win7 users will find the autogenerated code in folder
-- C:\ProgramData\foldit with a filename
-- like scriptlog.default or scriptlog.trackname
--
-- default data folder varies by operating system, e.g. XP has approximately
-- C:\Documents and Settings\All User\Application Data\Foldit
--
-- Copy the autogenerated LUA code (the lines between
-- the "ScripOutput" statements) -- into the foldit ScriptV2 editor to run.
--
-- I don't recall where or from whom I first learned the well known foldit fact
-- that secondary structures can be saved using a LUA code fragment.
-- Marie Suchard has suggested printing out model information
-- and using this as input to other
-- programs such as spreadsheets: http://fold.it/portal/recipe/30483
--
-- Created under the old v1 LUA, hopefully conversion to v2 will not be hard
--
-- by gramps (John McLeod)
-- v0.01 -- September 1, 2011
-- v0.02 -- September 5, 2011 -- new Restore_One_Stuct function
-- this allows tighter autogenerated code
-- also fixed a bug in logic which was incorrectly rendering
-- when the last segment was a singleton structure element
-- v0.03 -- October 1, 2011 -- loops also created separately
-- this is so the output of the autogenerated code more
-- conveniently documents the structure
-- V1 -- July 21, 2012 -- conversion to ScriptV2
-- in the new version of Lua print() arguments come out separated
-- by Tab characters which resulted in some pain for this program!
local Struct_Types = {
["L"] = "Loop",
["E"] = "Sheet",
["H"] = "Helix",
["M"] = "Ligand" }
-- Funky variable with fragile calculation
-- This will work as long as puzzle ligands are
-- of structure type "M" and occur at the end
-- of the puzzle. If this changes then the following will
-- require manual setting
--
local puzzle_num_ligands = 0
do -- start constructor for variable puzzle_num_ligands
local seg_no = structure.GetCount()
while structure.GetSecondaryStructure(seg_no) == "M" do -- M ligands happen at end
puzzle_num_ligands = puzzle_num_ligands + 1
seg_no = seg_no - 1
end
end -- end of constructor for variable puzzle_num_ligands
local puzzle_last_seg = structure.GetCount() - puzzle_num_ligands
-- generate program name and header comment in the autogenerated code
print("newprog = \"changeme\"")
print("--")
print("-- autogenerated code")
print("") -- generate the type table
print("local Struct_Types = {")
print("[\"L\"] = \"Loop\",")
print("[\"E\"] = \"Sheet\",")
print("[\"H\"] = \"Helix\",")
print("[\"M\"] = \"Ligand\" }")
print("") -- generate function that turns everything into a loop
print("function Make_All_Loop()")
print(" selection.SelectAll()")
print(" structure.SetSecondaryStructureSelected(\"L\")")
print(" print(\"everything has been changed to a loop\")")
print(" selection.DeselectAll()")
print("end -- function Make_All_Loop")
print("") -- generate function that restores one struct element
print("function Restore_One_Stuct(s_start,s_end,s_type)")
print("selection.SelectRange(s_start,s_end)")
print("print(Struct_Types[s_type]..\"(\"..s_start..\",\"..s_end..\")\")")
print("structure.SetSecondaryStructureSelected(s_type)")
print("selection.DeselectAll()")
print("end -- function Restore_One_Stuct")
print("") -- generate function that restores old structures
print("function Restore_Structs()")
current_struct_begin = 1
current_struct = structure.GetSecondaryStructure(1)
for x=2,puzzle_last_seg do
if structure.GetSecondaryStructure(x) ~= current_struct then
if (Struct_Types[current_struct] == "Sheet") or
(Struct_Types[current_struct] == "Loop") or
(Struct_Types[current_struct] == "Helix") then
-- generate code to restore structure just past
detail_line = " Restore_One_Stuct("
detail_line = detail_line .. current_struct_begin
detail_line = detail_line .. ","
detail_line = detail_line .. (x-1)
detail_line = detail_line .. ","
detail_line = detail_line .. "\""
detail_line = detail_line .. current_struct
detail_line = detail_line .. "\")"
print(detail_line)
-- print(" Restore_One_Stuct(", current_struct_begin, ",",
-- x - 1, ",", "\"", current_struct, "\")")
end
current_struct_begin = x
current_struct = structure.GetSecondaryStructure(x)
end
end
if (Struct_Types[current_struct] == "Sheet") or
(Struct_Types[current_struct] == "Loop") or
(Struct_Types[current_struct] == "Helix") then
-- generate code to restore last structure
detail_line = " Restore_One_Stuct("
detail_line = detail_line .. current_struct_begin
detail_line = detail_line .. ","
detail_line = detail_line .. (puzzle_last_seg)
detail_line = detail_line .. ","
detail_line = detail_line .. "\""
detail_line = detail_line .. current_struct
detail_line = detail_line .. "\")"
print(detail_line)
-- print(" Restore_One_Stuct(", current_struct_begin, ",",
-- puzzle_last_seg, ",", "\"", current_struct,"\")")
end
print("end -- function Restore_Structs")
print("") -- generate MAIN stuff
print("print(\"begin \",newprog)")
print("")
print("if structure.GetCount() == ", structure.GetCount()," then -- num segs match")
print(" Make_All_Loop()")
print(" Restore_Structs()")
print("else -- print warning line")
print(" print(\"segment number mismatch -- no action taken\")")
print("end")
print("")
print("print(\"end \",newprog)")
--
-- end autogen structure code V1