Icon representing a recipe

Recipe: Rav Compress 2.2.1 for Symmetry -- Brow42

created by brow42

Profile


Name
Rav Compress 2.2.1 for Symmetry -- Brow42
ID
41615
Shared with
Public
Parent
Comp 2.2 from Rav3n V1toV2
Children
Created on
April 03, 2013 at 18:44 PM UTC
Updated on
April 03, 2013 at 18:44 PM UTC
Description

Rav3n_pl Comp 2.2 modified to use band endpoints as compression targets. Could be BiS or symmetry segments! Now disables filters.

Best for


Code


--[[ Rav3n_pl Compressor v2.2 trying to compress/decompress protein accepting loss of points between pulls Modified to band to band endpoints by Brow42 May 13 2012 ]]-- ----------- options below VVVVV --------------------- cLoops=150 --<<< SET NUMBER OF LOOPS decomp=false --true --true for DeCompression instead of compression modualtor=false --true --changing compression/decompression each loop useRegions={ --set regions that have to be used in all bands --{1,20}, --there are exaples, uncomment and edit --{25,55}, --{100,210}, } compressFrac=5 --make bands shorter by that much points minSkip=15 --minimum segment distance between banded segments numBands=8 --how many bands use at once fastQstab=true --false --true --only 1 shake and 1 wiglle when true useFuze=true -- use Fuze after qStab fuzeScore=-1 --how close we have to be to run Fuze allLoop=true --false --work in all-loop mode. sometimes work better than structure mode :) S2H=false --all bands are between sheets and helixes struct=false --all bands have at least one end on structure (not loop) noLoops=false --band can`t have any end on loop lastBands=0.3 --starting band str bandStr=1.0 -- max band strenght minDist=7 --mimum band length maxDist=20 --maximum band length slowBands=true --bands strenght are raised few times till score drops by % percLoss=1 --pulling stops when score drops by that percent (ie 2=200pts when 10k pts) repeatGood=false --true --repeating good bands if true pullingCI=1 --0.6--clash impotrance during pull maxLoss=30 --maximum acceptable LOSS after Fuze. also lowering fuzeScore reloadBest=5 --reload best solution after that many worst ones normal=true --set false for exploration puzles ------------ end of options ^^^^^^ -- "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 -- -------- Special Band Overrides to preserve initial bands -- This makes initial bands invisible to lua1 script bndCnt = band.GetCount() function band_delete(bndIdx) if bndIdx~= nil then band.Delete(bndCnt + bndIdx) else while band.GetCount() > bndCnt do band.Delete(bndCnt + 1) end end end function band_disable(bndIdx) if bndIdx~=nil then band.Disable(bndCnt + bndIdx) else while band.GetCount() > bndCnt do band.Disable(bndCnt + 1) end end end function band_enable(bndIdx) if bndIdx~=nil then band.Enable(bndCnt + bndIdx) else while band.GetCount() > bndCnt do band.Enable(bndCnt + 1) end end end function band_set_length(bndIdx, len) band.SetGoalLength(bndCnt + bndIdx, len) end function band_set_strength(bndIdx, str) band.SetStrength(bndCnt + bndIdx, str) end function get_band_count() return band.GetCount() - bndCnt end -- End overrides UsingFilters = true and behavior.SetSlowFiltersDisabled ~= nil OriginalFilterSetting = behavior.GetSlowFiltersDisabled() function DisableFilters() -- before wiggle if not UsingFilters then return end behavior.SetSlowFiltersDisabled(true) end function EnableFilters() -- before scoring if not UsingFilters then return end behavior.SetSlowFiltersDisabled(false) -- structure.WiggleAll(1,false,true) -- wiggle SC 1 iteration to force a rescore -- doesn't seem to be necessary end function RestoreFilters() -- at end of script if UsingFilters then behavior.SetSlowFiltersDisabled(OriginalFilterSetting) end end 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 EnableFilters() 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=6 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) else DisableFilters() if how == "wb" then do_global_wiggle_backbone(2) elseif how == "ws" then do_global_wiggle_sidechains(2) elseif how == "wa" then do_global_wiggle_all(2) end EnableFilters() end if Score()-sp > minppi then return Wiggle(how, iters, minppi) end end end function AllLoop() --turning entire structure to loops DisableFilters() 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 ]]-- EnableFilters() bestScore=Score() function SaveBest() EnableFilters() local g=Score()-bestScore if g>0 then p("Gained another ",round(g)," pts.") bestScore=Score() quicksave(3) end end targetList={} -- list of segments and endpoints, to be built later -- note, s1,s2 are now tables function MakeBands(num) DisableFilters() function RegionCheck(seg) -- local function to see if seg satisfies region requirement for a = 1,#useRegions do local ss=useRegions[a][1] local se=useRegions[a][2] if seg>=ss and seg<=se then return true end end return false end local done=true local s1={'s',math.random(segCnt)} -- s1 always a segment local s2=targetList[math.random(#targetList)] -- s2 is a segment or band endpoint -- if both are segments, check distance if s2[1] == 's' then local s1,s2 = s1[2],s2[2] if s2 < s1 then s1,s2 = s2,s1 end local d=get_segment_distance(s1,s2) if s2-s1 < minSkip or d < minDist or d > maxDist then done=false end end if (done and #useRegions>0) then done = done and (RegionCheck(s1[2]) or (s2[1] == 's' and RegionCheck(s2[2]))) end if get_ss(s1[2]) == "L" and noLoops then done = false end -- band endpoints automatically pass all structure checks...if segment, do checks if (done and s2[1] == 's') then local ss1=get_ss(s1[2]) local ss2=get_ss(s2[2]) if (done and noLoops) then if ss1=="L" or ss2=="L" then done=false end end if (done and struct) then if ss1=="L" and ss2=="L" then done=false end end if (done and S2H) then if (ss1=="E" or ss2=="E") and (ss1=="H" or ss2=="H") and (ss1~=ss2) then done=true else done=false end end end if done then num=num-1 mkBand(s1,s2) bands[#bands+1]={s1,s2} end if num>0 then return MakeBands(num) end --tail call end -- note, s1,s2 are now tables function mkBand(s1,s2) -- using Lua v2 to catch failures local iBand if s2[1] == "s" then iBand = band.AddBetweenSegments(s1[2],s2[2]) else iBand = band.AddToBandEndpoint(s1[2],s2[2]) end if iBand > 0 then local len=band.GetLength(iBand) if decomp==true then len=len+compressFrac else len=len-compressFrac end if len<3 then len=3 end --not make shorter band, useless band.SetGoalLength(iBand,len) band.Enable(iBand) -- bands to bands are not enabled initially end end bands={} function Repeat_bands(bands) DisableFilters() band_delete() for i=1,#bands do local s1=bands[i][1] local s2=bands[i][2] mkBand(s1,s2) end end function Bandstr(str) --set all band strengt if str<0.1 then str=0.1 end if str>10 then str=10 end for i=1, get_band_count() do band_set_strength(i, str) end end function SaveRB(slot) EnableFilters() quicksave(slot) restore_recent_best() SaveBest() quickload(slot) end function BuildTargetList() for i = 1,segCnt do targetList[#targetList + 1] = {"s",i} end for i = 1,bndCnt do targetList[#targetList + 1] = {"b",i} end end function WelcomeMessage() -- for new users msg = { "Hi! This is a special version of Compressor 2.2. It", "not only bands segments to other segments, but to", "other bands too! You need to make some bands (to", "symmetry segments or a place you want to pull to).", "Don't worry, they'll be disabled, and they won't be", "deleted or messed up, even if you cancel!" } d = dialog.CreateDialog("Compressor 2.2 (band to bands version)") for i = 1,#msg do print(msg[i]) d[tostring(i)]=dialog.AddLabel(msg[i]) end d.okay = dialog.AddButton("Okay",1) dialog.Show(d) return end sscore=Score() --starting score function Compressor(numTries) print(puzzle.GetName(),os.date()) p("Starting Compressor v2 (band to bands version), ",numTries," loops.") if normal==false then p("Using exploration options.") end if UsingFilters then p("Disabling filters during wiggle.") end if allLoop then AllLoop() end quicksave(3) quicksave(7) --save state for losing badOnes=0 reset_recent_best() BuildTargetList() local best=false for i=1,numTries do loss=Score()*percLoss/100 --pulling till we lost some points band_delete() if best==true and repeatGood==true then p("Repeating last bands.") if modualtor==true then --switch decomp= not decomp end Repeat_bands(bands) else bands={} MakeBands(numBands) end local ls=Score() local tx="ompressing." if decomp==true then tx="Dec"..tx else tx="C"..tx end p("Loop ",i," of ",numTries," started. ",tx," Current score: ",round(ls)) CI(pullingCI) select_all() reset_recent_best() if slowBands then for str=lastBands,bandStr,0.07 do--search enough band strenght to move restore_recent_best() --because sometimes it makes points during pull :D local ss=Score() Bandstr(str) Wiggle("wb",1) if ss-Score()>loss then lastBands=str-0.1 break end end else Bandstr(bandStr) Wiggle("wb",1) end SaveRB(4) --because sometimes it missing fractions band_delete() qStab() if useFuze and Score()> (bestScore-fuzeScore) then SaveBest() Fuze(4) else SaveBest() end if maxLoss>0 then if Score()>bestScore-maxLoss then quicksave(7) --save actual "best" solution else badOnes=badOnes+1 if badOnes >=reloadBest then badOnes=0 quickload(3) else quickload(7) --load "second best" end end else quickload(3) end local es=Score() if es>ls then best=true else best=false end --repeating all bands making points p("Loop ",i," gain ",round(es-ls)," ;total gain ",round(bestScore-sscore)) if modualtor==true then --switch decomp= not decomp end end quickload(3) if allLoop then load_structure() end p("Total gain: ",round(Score()-sscore)) end -- cancel trap function OnError(err) -- Thanks to Tlaloc for example print('Removing bands') band_delete() print('Bands Removed') print('ERROR',err) if allLoop then load_structure() end return err end --main call band.DisableAll() if (bndCnt == 0) then WelcomeMessage() return end xpcall(function() Compressor(cLoops) end,OnError) band_delete() --end of scipt

Comments


brow42 Lv 1

Once upon a time, you could run compression scripts on symmetry puzzles, which was cool, but other scripts would crash, which was not. The fix was to make symmetry segments invisible to scripts. So now we had to move things by hand with the move tool, which I'm not very good at. This is a hack, but it works!

Compress for symmetry will band segments to other segments OR other bands. The endpoints (not the start points) of any bands that you have drawn before you start the script, will be used for random compression bands just like other segments. These can be bands that you drew to symmetry segments, or to points in space that you'd like to be part of the compression. If the endpoint is attached to a segment, then the compression band will also be attached to the segment, even if it is a mirror segment!

It can be a lot of work to make all those bands. You don't have to band every symmetry segment (in fact, I advise against it, see below). However, you only have to do it once. The bands will be automatically disabled, and never deleted by the script. All the new compression bands WILL be deleted, even if you cancel the script!

Band endpoints don't quite behave the same as segments (yet): They ignore minimum distance, segment skipping, and all structure checks for structure-mode compression. Because segment-to-segment bands still have to pass all these checks, you may actually get many more segment-to-band than segment-to-segment!

If you don't make any bands, a dialog will pop up to remind you. :)