Icon representing a recipe

Recipe: Gary Rebuild Worst lua v2

created by Susume

Profile


Name
Gary Rebuild Worst lua v2
ID
102437
Shared with
Public
Parent
Gary Rebuild Worst NC v1&v2 Filter Toggle
Children
Created on
August 01, 2017 at 02:56 AM UTC
Updated on
August 01, 2017 at 02:56 AM UTC
Description

Rebuilding Worst Delta . Need 5 to reset delta. Based upon Walkin Rebuild but focuses on most impacted. Leaves secondary structure as is. Converted to lua v2 in order to make further improvements possible.

Best for


Code


--Gary Rebuild worst delta --A modified version of Walkin Rebuild V4 --Now converted to Lua v2 ---------------------------------------- -- BEGIN V1 TO V2 CONVERSION LIBRARY ---------------------------------------- -- "lua v1 in v2" library by rav3n_pl --just add it in front of your v1 code and use v2 and v1 code in v2 scripts :) -- print(arg1[,...,argN]) no change :) function are_conditions_met() return current.AreConditionsMet() end function band_add_segment_segment(sgi1, sgi2) band.AddBetweenSegments(sgi1, sgi2) end function band_delete(bndIdx) if bndIdx~= nil then band.Delete(bndIdx) else band.DeleteAll() end end function band_disable(bndIdx) if bndIdx~=nil then band.Disable(bndIdx) else band.DisableAll() end end function band_enable(bndIdx) if bndIdx~=nil then band.Enable(bndIdx) else band.EnableAll() end end function band_set_length(bndIdx, len) band.SetGoalLength(bndIdx, len) end function band_set_strength(bndIdx, str) band.SetStrength(bndIdx, str) end function get_band_count() return band.GetCount() end function deselect_all() selection.DeselectAll() end function deselect_index(sgn) selection.Deselect(sgn) end function select_all() selection.SelectAll() end function select_index(sgn) selection.Select(sgn) end function select_index_range(sg1,sg2) selection.SelectRange(sg1,sg2) end function do_freeze(bbone,schain) freeze.FreezeSelected(bbone,schain) end function do_unfreeze_all() freeze.UnfreezeAll() end function do_global_wiggle_all(iters) structure.WiggleAll(iters,true,true) end function do_global_wiggle_backbone(iters) structure.WiggleAll(iters, true,false) end function do_global_wiggle_sidechains(iters) structure.WiggleAll(iters,false,true) end function do_local_rebuild(iters) structure.RebuildSelected(iters) end function do_local_wiggle(iters) structure.LocalWiggleSelected(iters,true,true) end function do_mutate(iters) structure.MutateSidechainsSelected(iters) end function do_shake(iters) structure.ShakeSidechainsSelected(iters) end function do_sidechain_snap(sgn, snap) rotamer.SetRotamer(sgn, snap) end function get_sidechain_snap_count(sgn) return rotamer.GetCount(sgn) end function load_structure() save.LoadSecondaryStructure() end function save_structure() save.SaveSecondaryStructure() end function quickload(slot) save.Quickload(slot) end function quicksave(slot) save.Quicksave(slot) end function get_exploration_score() return current.GetExplorationMultiplier() end function get_ranked_score() return current.GetScore() end function get_score() return current.GetEnergyScore() end function get_segment_distance(sg1,sg2) return structure.GetDistance(sg1,sg2) end function get_aa(sn) return structure.GetAminoAcid(sn) end function get_segment_count() return structure.GetCount() end function get_ss(sn) return structure.GetSecondaryStructure(sn) end function is_hydrophobic(sn) return structure.IsHydrophobic(sn) end function replace_aa(aa) for i=1,structure.GetCount() do if selection.IsSelected(i) then structure.SetAminoAcid(i, aa) end end end function replace_ss(ss) for i=1,structure.GetCount() do if selection.IsSelected(i) then structure.SetSecondaryStructure(i,ss) end end end function get_segment_score(sg) return current.GetSegmentEnergyScore(sg) end function get_segment_score_part(score_part,sg) return current.GetSegmentEnergySubscore(sg,score_part) end function reset_puzzle() puzzle.StartOver() end function restore_abs_best() absolutebest.Restore() end function restore_credit_best() creditbest.Restore() end function reset_recent_best() recentbest.Save() end function restore_recent_best() recentbest.Restore() end function set_behavior_clash_importance(ci) behavior.SetClashImportance(ci) end ----------------------------------- -- END V1 TO V2 CONVERSION LIBRARY ----------------------------------- CG303={} game={} gary={ -- sort a table of talbes on element y -- of the embedded tables BubbleSort = function(x, y) s=gary.CopyScores(x) for i=1,#s - 1 do for j =#s, i + 1, -1 do if s[i][y] < s[j][y] then s[j], s[i] = s[i], s[j] end end end return s end, CopyScores = function(x) copy={} for i=1, #x do copy[i]={x[i][1], x[i][2]} end return copy end, GetSegScores = function() -- set of segment # and score at seg # local scores={} for i=1, game.segCount do --fill score table scores[i]={i, get_segment_score(i)} end return scores end, GetBadRange = function(segW,maxLen) -- get a set of segments up to maxLen with scores worst than segW local segWScore=get_segment_score(segW) local worstFound=true local len=1 local segS=segW local segE=segW while (len<maxLen) and worstFound do worstFound=false if segS>1 then if get_segment_score(segS-1) < segWScore then segS=segS-1 worstFound=true len=len+1 end end if (segE<game.segCount) and (len<maxLen) then if get_segment_score(segE+1) < segWScore then segE=segE+1 worstFound=true len=len+1 end end end return segS, segE end, ZeroScores = function() -- set of segment # and score at seg # local scores={} for i=1, game.segCount do --fill score table with zeroes scores[i]={i, 0} end return scores end, AddScores = function(s1,s2) -- s1 and s2 need to be same size and order -- no test is performed local delta={} for i=1, #s1 do delta[i]={s1[i][1],s1[i][2] + s2[i][2]} end return delta end, SubtractScores = function(s1,s2) -- s1 and s2 need to be same size and order -- no test is performed local delta={} for i=1, #s1 do delta[i]={s1[i][1], s1[i][2] - s2[i][2]} end return delta end } function CG303.detect_ligand(flag) --[[ ligand puzzle detection normally, segments have a secondary structure of "E", "H" or "L" and they always have a spatial distance of about 3.75 to 3.85 to their next index neighbour. a ligand is more far away. this function should respond true if this is a ligand puzzle, and false if it is not. if flag is nil, ligand auto-detection is enabled, distance of last two segments is checked if flag is not nil, ligand auto-detection is disabled, result is flag It also returns the last segment index which is no ligand ]]-- local flag=flag local LastPos=get_segment_count() -- fetch very last segment index number if flag==nil then -- print("Detecting if there is a ligand.") local ss=get_ss(LastPos) flag=not(ss=="L" or ss=="H" or ss=="E" ) -- if last segment's ss is neither "L" nor "H" nor "E" flag=flag or (get_segment_distance(LastPos-1,LastPos)>=3.9) -- or distance to second last segment is bigger or equal than 3.9 end -- if local os="This should " if flag then LastPos=LastPos-1 else os=os.."not " end -- if flag print(os,"be a ligand puzzle.") return flag,LastPos end -- function function Round(x)--cut all afer 3-rd place return x-x%0.001 end function SelectSphere(sg, radius,nodeselect) if nodeselect~=true then deselect_all() end for i=1, game.segCount do if get_segment_distance(sg,i)<radius then select_index(i) end end end function AllLoop() do_unfreeze_all() select_all() replace_ss("L") end mingain = 0.05 --minimum gain per 2 wiggle iterations buddies = 2 -- how many segments asida should be wiggled too (n, n+1 then n, n-1) -- ie worst segment is no 44 and buddies set to 1 then -- willging seg 43+44; later seg 44+45 function wig() local change=false ss=get_score(true) repeat sc=get_score(true) do_local_wiggle(4) se=get_score(true) wg=se-sc until wg<mingain es=get_score(true) if es<ss then restore_recent_best() else change=true end deselect_all() return change end function wiggle(s) local change=false deselect_all() do_unfreeze_all() if s+1<=game.segCount then select_index(s+1)end if s-1>=1 then select_index(s-1)end do_freeze(true, true) deselect_all() select_index(s) if wig() then change=true end if buddies > 0 then --select buddies for b=1, buddies do deselect_all() do_unfreeze_all() if s+b+1<=game.segCount then select_index(s+b+1)end if s-1>=1 then select_index(s-1)end do_freeze(true, true) deselect_all() if s+b>game.segCount then select_index_range(s,game.segCount) else select_index_range(s,s+b)end if wig() then change=true end deselect_all() do_unfreeze_all() if s+1<=game.segCount then select_index(s+1)end if s-b-1>=1 then select_index(s-b-1)end do_freeze(true, true) deselect_all() if s-b<1 then select_index_range(1,s) else select_index_range(s-b,s)end if wig() then change=true end deselect_all() do_unfreeze_all() end end return change end function Gibaj(jak, iters, minppi) --score conditioned recursive wiggle/shake if jak==nil then jak="wa" end if iters==nil then iters=8 end if minppi==nil then minppi=0.04 end if iters>0 then iters=iters-1 local sp=get_score(true) if jak == "s" then do_shake(1) elseif jak == "wb" then do_global_wiggle_backbone(25) elseif jak == "ws" then do_global_wiggle_sidechains(25) elseif jak == "wa" then do_global_wiggle_all(25) end local ep = get_score(true) local ig=ep-sp if ig > minppi then Gibaj(jak, iters, minppi) end end end function BlueFuse(locally) reset_recent_best() if locally ~=true then select_all() end set_behavior_clash_importance(.05) do_shake(1) set_behavior_clash_importance(1) Gibaj() set_behavior_clash_importance(.07) do_shake(1) set_behavior_clash_importance(1) Gibaj() restore_recent_best() set_behavior_clash_importance(.3) do_global_wiggle_all(1) set_behavior_clash_importance(1) Gibaj() restore_recent_best() end function Lws(mingain) --score conditioned local wiggle, local change=false set_behavior_clash_importance(1) if mingain==nil then mingain=1 end repeat --wiggles selected segments local ss=get_score(true) do_local_wiggle(4) local se=get_score(true) local wg=se-ss if wg<0 then restore_recent_best() else change=true end until wg<mingain return change end function AfterRebuild(lws, bf,locally) if lws==nil then lws=true end if bf==nil then bf=true end reset_recent_best() set_behavior_clash_importance(1) Gibaj("s",1) Gibaj("ws",1) Gibaj("s",1) Gibaj("ws",1) if lws then Lws(4) end if bf then BlueFuse(locally) end select_all() Gibaj() end function Rebuild(maxIters)--local rebuild until any change if maxIters==nil then maxIters=5 end local s=get_score(true) local i=0 repeat i=i+1 if i>maxIters then break end--impossible to rebuild! do_local_rebuild(i) until get_score(true)~=s if get_score(true)==s then return false else return true end end function LocalRebuild(ss,se,maxiters,sphere, lws, bf) local change=false deselect_all() select_index_range(ss,se) local sc=get_score(true) local ok=Rebuild(maxIters) if ok then for s=ss, se do SelectSphere(s,sphere,true) end AfterRebuild(lws,bf,true) end local gain =get_score(true)-sc if gain>0 then quicksave(3) print("Gained: ",get_score(true)-sc," NewScore: ",get_score(true)) change=true elseif gain<0 then quickload(3) else print("Unable to rebuild.") end return change end function Build(sSeg, eSeg, backward, len, maxiters, loopmode, sphere, lws, bf) if backward then for i=eSeg, sSeg,-1 do local ss=i-len+1 if ss>sSeg then LocalRebuild(ss,i,maxiters,sphere, lws, bf) end end else for i=sSeg, eSeg do local se=i+len-1 if se<game.segCount then LocalRebuild(i,se,maxiters,sphere, lws, bf) end end end end function gary.RebuildDelta(maxLen, maxiters, sphere, lws, bf, minChange, initRebuilds) local sscore = get_score(true) local rebuilds=initRebuilds print("Gary Rebuild Delta started. Score: ",Round(sscore)) print("delta calculated after ",initRebuilds," good rebuilds") reset_recent_best() quicksave(3) do_unfreeze_all() deselect_all() local oscores=gary.GetSegScores() local delta=gary.BubbleSort(oscores,2) local i=#delta local segW=delta[i][1] local segS, segE = gary.GetBadRange(segW,maxLen) repeat if segS~=segE then print("Working on sgmnts ",segS,"-",segE," with delta ", delta[i][2]) else print("Working on sgmnt ",segS," with delta ", delta[i][2]) end local oldScore=get_score(true) if LocalRebuild(segS,segE,maxiters,sphere, lws, bf) then if get_score(true) - oldScore > minChange then minChange=minChange / minChangeDelta if rebuilds>2 then rebuilds=rebuilds-1 print("Need ", rebuilds," more.") i=i-1 else rebuilds=initRebuilds for x=segS,segE do oscores[x][2]=get_segment_score(x) end nscores=gary.GetSegScores() delta=gary.BubbleSort(gary.SubtractScores(nscores,oscores),2) i=#delta end else i=i-1 end if i > 0 then segW=delta[i][1] segS, segE = gary.GetBadRange(segW,maxLen) end else minChange=minChange * minChangeDelta if segS < segE then segS, segE=gary.GetBadRange(segW,segE-segS) else i=i-1 if i > 0 then segW=delta[i][1] segS, segE = gary.GetBadRange(segW,maxLen) end end end until i==0 print("Total rebuild gain: ",Round(get_score(true)-sscore)) end function ReBuild(sSeg, eSeg, backward, minLen, maxLen, maxiters, loopmode, sphere, lws, bf) local sscore = get_score(true) print("Walking Rebuild started. Score: ",Round(sscore)) do_unfreeze_all() deselect_all() if loopmode then save_structure() AllLoop() end reset_recent_best() quicksave(3) for i=minLen, maxLen do Build(sSeg, eSeg, backward, i, maxiters, loopmode, sphere, lws, bf) end print("Total rebuild gain: ",Round(get_score(true)-sscore)) if loopmode then load_structure() end end --[[ ReBuild(sSeg, eSeg, backward, minLen, maxLen, maxiters, loopmode, sphere, lws, bf) sSeg- start segment to rebuild eSeg- end segment to rebuild backward- start from end minLen- minimum sgmnts len to rebuild (may be even 1) maxLen- maximum len to rebuild (over 7 seems useless) maxiters- maximum tries to rebuild loopmode- work in all-loop mode sphere- sphere size for shake/wiggle after rebuild lws- LWS sphere around (min gain 2pts, not LWSing totally) bf- BlueFuze sphere around minChange- minimum change to accept as a change initRebuilds- Rebuilds needed to calcualte delta. ]]-- game.isLigand, game.segCount=CG303.detect_ligand() sSeg=1 eSeg=game.segCount backward=false minLen=1 maxLen=2 maxiters=5 loopmode=true sphere=9 lws=true bf=true minChange=0.5 minChangeDelta=.99 initRebuilds=5 --ReBuild(sSeg, eSeg, backward, minLen, maxLen, maxiters, loopmode, sphere, lws, bf) gary.RebuildDelta(maxLen, maxiters, sphere, lws, bf, minChange, initRebuilds)

Comments