Icon representing a recipe

Recipe: Manual Rebuild 1.0.1 -- Brow42

created by brow42

Profile


Name
Manual Rebuild 1.0.1 -- Brow42
ID
41950
Shared with
Public
Parent
None
Children
Created on
June 12, 2012 at 01:32 AM UTC
Updated on
June 12, 2012 at 01:32 AM UTC
Description

Replacement for the built in GUI Rebuild...prints scores, does shake, sets 1 rebuild per undo, each rebuild is different.

Best for


Code


--[[ * Manual Rebuild Button * Original Author: Brow42 May 25, 2012 * Version 1.0 * Performs a rebuild on segments bounded by 2 frozen segments, or * selected in selection interface. * A shake is performed after each rebuild * It confirms that the score changed after rebuild * It saves rebuilds, then loads them to fill the undo buffer * with a rebuild at each undo step * It prints total and partial scores for each rebuild * On exit or cancel it removes the selection if frozen segments marked rebuild region * Version 1.0.1 * Now uses scores of just the rebuilt segmetns due to -9999999 minimum total score --]] -- Options NRebuild = 50 -- terminate normally after this many rebuilds title = 'Manual Rebuild Button 1.0.1' -- Error Message Box function Error(msg) d = dialog.CreateDialog('Error') d.msg = dialog.AddLabel(msg) d.okay = dialog.AddButton('Okay',1) print(msg) dialog.Show(d) end -- Print range score relative to init, return score as init for next call function ComputeScore(first,last,init) init = init or {0,0,0,0} local e,b,c,p = -init[1], -init[2], -init[3], -init[4] for i = first,last do e = e + current.GetSegmentEnergyScore(i) b = b + current.GetSegmentEnergySubscore(i,'backbone') c = c + current.GetSegmentEnergySubscore(i,'clashing') p = p + current.GetSegmentEnergySubscore(i,'packing') + current.GetSegmentEnergySubscore(i,'hiding') end return {e,b,c,p} end function PrintScore(scores) print( string.format("%10.2f %10.2f %10.2f %10.2f %10.2f",current.GetScore(),unpack(scores)) ) end -- Undo functionality NUndo = 25 -- 25 undo slots in the gui so max 25 iSlot = 1 -- the next slot to be saved into 1 to NUndo NSaved = 0 -- 0 to NUndo -- Pushes current state onto circular undo buffer, returns buffer size function Save() save.Quicksave(101 - iSlot) iSlot = 1 + iSlot % NUndo NSaved = math.min(NSaved + 1, NUndo) return NSaved end -- pops the top save from undo buffer, returns false if empty function Undo() if NSaved < 1 then error('No more saves in undo buffer') end NSaved = NSaved - 1 iSlot = 1 + (iSlot + NUndo - 2) % NUndo save.Quickload(101-iSlot) return NSaved > 0 end -- Pull the oldest save out (FIFO) function PopTail() if NSaved < 1 then error('No more saves in undo buffer') end local i = 1 + (iSlot - 1 - NSaved + NUndo) % NUndo save.Quickload(101-i) NSaved = NSaved - 1 return NSaved > 0 end -- Find rebuild range by: -- Space between to frozen segments -- Space including first and last selected segments -- Else error message ( none frozen on none selected, or some frozen but not exactly 2 frozen) -- returns true if used frozen segments, else false if selected segments FREEZE,SELECTION = true,false -- constants used in Cleanup function FindRange() local s = {} for i = 1,structure.GetCount() do if freeze.IsFrozen(i) then table.insert(s,i) end end if #s == 2 then s[1],s[2] = s[1]+1,s[2]-1 return s, FREEZE end if #s == 0 then for i = 1,structure.GetCount() do if selection.IsSelected(i) then table.insert(s,i) end end if #s > 0 then s = { s[1], s[#s] } end end return s, SELECTION end -- The core rebuild function...this runs at most 20 times (for 20 undo slots) init=nil function Rebuild(n) for i = 1,n do structure.RebuildSelected(1) structure.ShakeSidechainsSelected(1) local scores = ComputeScore(s[1],s[2],init) if scores[1] ~= 0 then PrintScore(scores) Save() else print('No change (',i,' of ',n,')') end end end -- Routine to be called when canceled or NRebuilds reached -- Remove selection if using freeze method, leave selection if using selection method how = nil -- need this name defined here for cleanup function Cleanup() if how == FREEZE then print ('Clearing selection') selection.DeselectAll() end if NSaved > 0 then print('Loading ', NSaved, 'rebuilds....') repeat until PopTail() == false end end -- Routine called on cancel or error function OnError(err) if err:find('Cancel') then print('Cancelled') Cleanup() else print('Error: ',err) end return err end -- Begin Main print(title) print("See recipe web page for instructions") print(puzzle.GetName(),os.date()) s,how = FindRange() if #s ~= 2 then Error('SELECT segments or FREEZE exactly 2 segments') return end if s[2] - s[1] < 0 then Error('Rebuild at least 1 segment') return end selection.DeselectAll() selection.SelectRange(s[1],s[2]) print('Rebuilding ', s[1],"-",s[2],' max ',NRebuild,' times.') print('Starting Score and energies:') init = ComputeScore(s[1],s[2]) PrintScore(init) print('Score and changes in energies from the start:') print(' Score : Energy Backbone Clashing Hiding+Packing') rc,err = xpcall(function() Rebuild(NRebuild) end, OnError) if rc == true then Cleanup() end

Comments


brow42 Lv 1

I find manual rebuilding really difficult. I can't tell which spot in the undo track is a better score, I have to fiddle with quicksaves to try out different rebuilds, and I'm convinced the GUI rebuild just sits there wiggling but producing no rebuilds at all far longer than rav3n_pl DRW, which finds a new shape almost instantly.

This script replaces the usual right-click rebuild function. You have to tell it where to rebuild by either selecting with the selection interface, or making two frozen segments around the section where you want to rebuild. It will rebuild between the frozen places, or between the first and last selection (including the endpoints).

The script will do the following:

For each rebuild, it prints the score (which identifies each rebuild), and the change in energy subscores.

A single shake is done at the end of each rebuild to fix up the clashing and sidechain scores. This should be very fast in most cases.

At the end of the script, each rebuild is given one spot in the undo menu, up to the last 25 rebuilds (the current length of the undo). The undo key will move backwards in the rebuild list as you'd expect.

It still does this if you hit cancel. If you don't hit cancel, it will do 50 rebuilds and exit.

Rebuilds that are identical to the previous rebuild are not included in the final undo list.

Quicksave slots 76-100 are used for the undo buffer.

It does not change the secondary structure, the clash importance, or look for the worst spots to rebuild. This is for manual rebuilding, it's just giving you the rebuilds in better way.

There is no dialog…it's just click-and-run.

brow42 Lv 1

By request, the script now looks at only the rebuilt segments' energy total instead of the protein total score when determining if the rebuild call actually changed the protein. This is because sometimes the score is stuck at -99999999.999.

Vredeman Lv 1

This is great
Brow Would you mind adding an option to shake and wiggle at a chosen Ci? It would really help at the beginning of a puzzle.
Thanks for this script
porky :)