Icon representing a recipe

Recipe: Rav3n_pl Voids LuaV1&V2 with toggle filters

created by Vredeman

Profile


Name
Rav3n_pl Voids LuaV1&V2 with toggle filters
ID
100458
Shared with
Public
Parent
None
Children
Created on
December 30, 2014 at 17:45 PM UTC
Updated on
December 30, 2014 at 17:45 PM UTC
Description

Adapted for V2 and filters using Rav's v1 in v2 and bits overload filter toggle

Best for


Code


--[[ Voids Killer by rav3n_pl script is searching for possible banding across voids. Because we not have any voids detection in Lua, I do some math to found them Not always perfect, but mostly works :) ]]-- normal=true --set false if want use exploration score not energy score --rest of options at end -- "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 of library -- function to copy class/table function CopyTable(orig) local copy = {} for orig_key, orig_value in pairs(orig) do copy[orig_key] = orig_value end return copy end -- functions for filters function FiltersOn() if behavior.GetSlowFiltersDisabled() then behavior.SetSlowFiltersDisabled(false) end end function FiltersOff() if behavior.GetSlowFiltersDisabled()==false then behavior.SetSlowFiltersDisabled(true) end end -- function to overload a funtion function mutFunction(func) local currentfunc = func local function mutate(func, newfunc) local lastfunc = currentfunc currentfunc = function(...) return newfunc(lastfunc, ...) end end local wrapper = function(...) return currentfunc(...) end return wrapper, mutate end -- function to overload a class -- to do: set the name of function classes_copied = 0 myclcp = {} function MutClass(cl, filters) classes_copied = classes_copied+1 myclcp[classes_copied] = CopyTable(cl) local mycl =myclcp[classes_copied] for orig_key, orig_value in pairs(cl) do myfunc, mutate = mutFunction(mycl[orig_key]) if filters==true then mutate(myfunc, function(...) FiltersOn() if table.getn(arg)>1 then -- first arg is self (function pointer), we pack from second argument local arguments = {} for i=2,table.getn(arg) do arguments[i-1]=arg[i] end return mycl[orig_key](unpack(arguments)) else --print("No arguments") return mycl[orig_key]() end end) cl[orig_key] = myfunc else mutate(myfunc, function(...) FiltersOff() if table.getn(arg)>1 then local arguments = {} for i=2, table.getn(arg) do arguments[i-1]=arg[i] end return mycl[orig_key](unpack(arguments)) else return mycl[orig_key]() end end) cl[orig_key] = myfunc end end end -- how to use: MutClass(structure, false) MutClass(band, false) MutClass(current, true) p=print CI=set_behavior_clash_importance segCnt=get_segment_count() while get_ss(segCnt)=="M" do segCnt=segCnt-1 end function Score() local s=0 if normal==true then s=get_score(true) else s=get_ranked_score(true) end return s end function round(x)--cut all afer 3-rd place return x-x%0.001 end function abs(x) if x<0 then x=-x end return x end function round(x)--cut all afer 3-rd place return x-x%0.001 end function down(x) return x-x%1 end function Wiggle(how, iters, minppi) --score conditioned recursive wiggle/shake if how==nil then how="wa" end if iters==nil then iters=8 end if minppi==nil then minppi=0.1 end if iters>0 then iters=iters-1 local sp=Score() if how == "s" then do_shake(1) elseif how == "wb" then do_global_wiggle_backbone(30) elseif how == "ws" then do_global_wiggle_sidechains(30) elseif how == "wa" then do_global_wiggle_all(30) end if Score()-sp > minppi then return Wiggle(how, iters, minppi) end end end function AllLoop() --turning entire structure to loops local ok=false for i=1, segCnt do local s=get_ss(i) if s~="L" then save_structure() ok=true break end end if ok then select_all() replace_ss("L") end end function qStab() CI(0.1) Wiggle("s",1) select_all() if fastQstab==false then CI(0.4) Wiggle("wa",1) CI(1) Wiggle("s",1) end CI(1) Wiggle() end function FuzeEnd() CI(1) Wiggle("wa",1) Wiggle("s",1) Wiggle() SaveBest() end function Fuze1(ci1,ci2) CI(ci1) Wiggle("s",1) CI(ci2) Wiggle("wa",1) end function Fuze2(ci1,ci2) CI(ci1) Wiggle("wa",1) CI(1) Wiggle("wa",1) CI(ci2) Wiggle("wa",1) end function reFuze(scr,slot) local s=Score() if s<scr then quickload(slot) else scr=s quicksave(slot) end return scr end function Fuze(slot) local scr=Score() quicksave(slot) select_all() Fuze1(0.3,0.6) FuzeEnd() scr=reFuze(scr,slot) Fuze2(0.3,1) SaveBest() scr=reFuze(scr,slot) Fuze1(0.05,1) SaveBest() scr=reFuze(scr,slot) Fuze2(0.7,0.5) FuzeEnd() scr=reFuze(scr,slot) Fuze1(0.07,1) SaveBest() reFuze(scr,slot) end --[[ Tlaloc`s math library ------------------------------------------------------------------------ The original random script this was ported from has the following notices: Copyright (c) 2007 Richard L. Mueller Hilltop Lab web site - http://www.rlmueller.net Version 1.0 - January 2, 2007 You have a royalty-free right to use, modify, reproduce, and distribute this script file in any way you find useful, provided that you agree that the copyright owner above has no warranty, obligations, or liability for such use. ------------------------------------------------------------------------ ]]-- local lngX = 1000 local lngC = 48313 local function _random(m,n) local A_Hi = 63551 local A_Lo = 25354 local M = 4294967296 local H = 65536 function _MWC() local S_Hi = math.floor(lngX / H) local S_Lo = lngX - (S_Hi * H) local C_Hi = math.floor(lngC / H) local F1 = A_Hi * S_Hi local F2 = (A_Hi * S_Lo) + (A_Lo * S_Hi) + C_Hi lngX = ((F2 - (math.floor(F2 / H) * H)) * H) + (A_Lo * S_Lo) + lngC - (C_Hi * H) lngX = lngX - (math.floor(lngX / M) * M) lngC = math.floor((F2 / H) + F1) return lngX end if n == nil and m ~= nil then n = m m = 1 end if (m == nil) and (n == nil) then return _MWC() / M else if n < m then return nil end return math.floor((_MWC() / M) * (n - m + 1)) + m end end local function _abs(value) if value < 0 then return -value else return value end end local function _floor(value) return value - (value % 1) end local function _randomseed(s) if s==nil then s=math.abs(Score()) s=s%0.001 s=1/s while s<10000000 do s=s*10 end s=s-s%1 end lngX = s end math= { abs = _abs, floor = _floor, random = _random, randomseed = _randomseed, } math.randomseed() --[[ End math library ]]-- bestScore=Score() function SaveBest() local g=Score()-bestScore if g>0 then p("Gained another ",round(g)," pts.") bestScore=Score() quicksave(3) end end distances={} distScore=Score() function getDist() if #distances<1 or distScore~=Score() then --run only if needed p("Calculating distances...") for i=1,segCnt do --filling table distances[i]={} --need to delclare second dimension for j=i+1,segCnt do --not counting from beginning - not need :) distances[i][j]=get_segment_distance(i,j) end end p("Done") distScore=Score() end end function dist(a,b) if a==b then return 0 end if a>b then a,b=b,a end return distances[a][b] end function Sort(tab,items) for x=1,items do for y=x+1,#tab do if tab[x][2]>tab[y][2] then tab[x],tab[y]=tab[y],tab[x] end end end return tab end function mkBand(a) --make band if found void in area of that segment p("Banding segment ", a) getDist() local t={}--there we store possible sehments for b=1,segCnt do --test all segments local ab=dist(a,b) --distance between segments if ab>minLenght then --no voind if less --p(a," ",b," ",ab) local void=true for c=1,segCnt do --searhing that is any segment between them local ac=dist(a,c) local bc=dist(b,c) if ac~=0 and bc~=0 and ac<ab and bc<ab and ac>4 and bc>4 then if ac+bc<ab+1.5 then void=false break --no void there for sure end end end if void==true then if math.abs(a-b)>=minDist then t[#t+1]={a,b} end end end end if #t>0 then p("Found ",#t," possible bands across voids") for i=1,#t do local s=t[i][1] local e=t[i][2] band_add_segment_segment(s,e) local d=get_segment_distance(s,e) d=d-compFrac if d<3 then d=3 end if d>20 then d=20 end band_set_length(get_band_count(),d) end else p("No voids found") end end function bandstr(str) --set all band strengt for i=1, get_band_count() do band_set_strength(i, str) end end function SaveRB() quicksave(4) restore_recent_best() SaveBest() quickload(4) end function Pull(sn) band_delete() mkBand(sn) if get_band_count()>0 then select_all() CI(pullingCI) reset_recent_best() local loss=math.abs(down(Score()*maxLoss/100)) for str=lastBS,maxBS,0.11 do--search enough band strenght to move if normal then restore_recent_best() end--because sometimes it makes points during pull :D ss=Score() bandstr(str) do_global_wiggle_backbone(1) if ss-Score()>loss then lastBS=str-0.1 if lastBS<minBS then lastBS=minBS end break end end band_delete() SaveRB() --because sometimes it missing fractions p("Stabilizing...") CI(1) reset_recent_best() --after pulling qStab() if bestScore-Score()<doFuze then SaveBest() p("Fuzing....") Fuze(4) end SaveBest() quickload(3) --load best state p("Current score: ",round(Score())," Total gain: ",round(Score()-qsc)) end end function KillVoids() if se==nil then se=segCnt end quicksave(3) qsc=Score() lastBS=minBS local s={} p("Starting Void Killer v0.4 8.1 by rav3n_pl Start score:",round(qsc)) getDist() --calculate distances now for i=ss,se do s[#s+1]={i,get_segment_score(i)} end if Vworst==true then Sort(s,#s) end if Vrandom==true then for i=1,#s do local r=math.random(#s) s[i],s[r]=s[r],s[i] end end for i=1,#s do p("Kill ",i," of ",#s," started.") Pull(s[i][1]) end end --- OPTIONS THERE! vvv minDist=15 --minimum segment distance minLenght=10 --minimum spatial distance Vworst=true --start form worst scoring one Vrandom=false --true --go it in random order -- if both are false then it will band form 1st to last doFuze=-1 --run fuzes when close to saved best (negative - run if fain after qstab) pullingCI=0.8 --clash improtance during pull fastQstab = true --1s1w then true, double if false minBS=0.3 --startng band strength maxBS=1 --maximum bands strength compFrac=3 --how much shorter band should be maxLoss=1 --pull till perc loss (ie 1 on 10`000 ptsy = pull till 100 pt loss after pull) ss=1 --from segment 1 se=nil --to last segment if nil --END OF OPTOPNS ^^^ KillVoids()

Comments