Profile
- Name
- Reverser 1.0.1 -- Brow42
- ID
- 46756
- Shared with
- Public
- Parent
- None
- Children
- None
- Created on
- August 10, 2013 at 17:19 PM UTC
- Updated on
- August 10, 2013 at 17:19 PM UTC
- Description
Uses bands to pull segments to their opposite segment. If protein knots, make (disabled) cutpoints first! Good for ED puzzles.
Best for
Code
--[[
====================================
* Reverser
* Original Author: Brow42
* Version 1.0 Aug 10, 2013
* Bands segments to segment opposite position
* Mainly for ED Puzzles
--]]
-- =================================== Begin Options
options = {skip = 0, strength = 10, sidechains = false, disabled = true}
-- Globals
version = '1.0.1'
title='Sequence Reverser '..version
max_skip = 5 -- for the skip sliders
nseg = structure.GetCount()
-- ================================== End Defaults
-- ================================== Begin New Dialog Library Functions
--[[
Throws up a dialog containing just text, provided as a string or table
of strings in the first argument. Title and buttons are optional. Return
value is 1 if the first button is clicked and 0 if the second button is clicked
or the window is closed.
--]]
function dialog.MessageBox(msg,title,buttontext1, buttontext0)
title = title or ''
local d = dialog.CreateDialog(title)
if type(msg) == 'string' then d['1'] = dialog.AddLabel(msg)
else for i = 1,#msg do
d[tostring(i)] = dialog.AddLabel(msg[i])
end
end
buttontext1 = buttontext1 or 'Ok'
d.button = dialog.AddButton(buttontext1,1)
if buttontext0 then d.button0 = dialog.AddButton(buttontext0,0) end
return d
end
function dialog.ShowMessageBox(msg,title,buttontext1,buttontext0)
return dialog.Show(dialog.MessageBox(msg,title,buttontext1,buttontext0))
end
-- ================================== End New Dialog Library Functions
-- ================================== Begin Zero Length Bands
-- ==================== Begin Dialog Code
-- This function defines our dialog and calls the functions to set and read values
-- First arg is the current option state and optional second arg is a remembered
-- default.
function ShowMenu()
local d = dialog.CreateDialog(title)
d.l1 = dialog.AddLabel("Make protein reverse on Wiggle")
d.l2 = dialog.AddLabel("If protein turns into not, try cut points")
d.l3 = dialog.AddLabel("(at u-turns and helices) Disable Them!")
d.skip = dialog.AddSlider("Skip Segments:",options.skip,0,max_skip,0)
d.str = dialog.AddSlider("Strength:",options.strength,1,10,0)
d.sc = dialog.AddCheckbox("Band Sidechains Too",options.sidechains)
d.dis = dialog.AddCheckbox("Sidechain bands initially Disabled",options.disabled)
d.ok = dialog.AddButton("Band",1)
d.cancel = dialog.AddButton("Cancel", 0)
local rc = dialog.Show(d)
options.sidechains = d.sc.value
options.skip = d.skip.value
options.strength = d.str.value
options.disabled = d.dis.value
return rc
end
-- Recursive table printer
function PrintTable(tab,indent)
indent = indent or ''
for i,v in pairs(tab) do
if type(v) == 'table' then do print(indent,i) PrintTable(v,indent..' ') end
else print(indent,i,v) end
end
end
--[[
-- True if nothing is specified
function AllSegments()
return AllStructures() and allFrozen()
and not (option.do_selected or option.uturn) and #_seglist == 0
end
-- True if no structures or all structures specified
function AllStructures()
return not (option.do_loop or option.do_sheet or option.do_helix) or
(option.do_loop and option.do_sheet and option.do_helix)
end
-- True of neither or both are checked
function AllFrozen()
return (option.do_frozen and option.do_notfrozen) or not (option.do_frozen or option.do_notfrozen)
end
-- Returns true if a segment satisfies all conditions (except range)
function DoSegment(i)
local s = structure.GetSecondaryStructure(i)
local f = freeze.IsFrozen(i) or structure.IsLocked(i)
local retval = true
retval = retval and s ~= 'M'
retval = retval and (_allstructures or ( option.do_loop and s == 'L') or (option.do_sheet and s == 'E') or (option.do_helix and s == 'H'))
retval = retval and (_allfrozen or (option.do_frozen and f) or (option.do_notfrozen and not f))
retval = retval and (not option.do_selected or selection.IsSelected(i))
retval = retval and (not option.uturn or IsUTurn(i))
retval = retval and ( (i - option.skip_offset -1) % (1+option.skip) == 0)
return retval
end
--]]
fsl = fsl or {}
fsl.atom = fsl.atom or {}
fsl.atom.atomcount = { -- shorter table just for identifying terminals
a=10, c=11, d=12, e=15, f=20, g=7, h=17, i=19, k=22, l=19,
m=17, n=14, p=15, q=17, r=24, s=11, t=14, v=16, w=24, y=21
}
function fsl.atom._IsDisulfideBonded(iSeg)
local s = current.GetSegmentEnergySubscore(iSeg,'disulfides')
return tostring(s) ~= '-0'
end
function fsl.atom._IsTerminalTest(aa,count,disulfide)
local diff = count - fsl.atom.atomcount[aa]
if disulfide then diff = diff + 1 end -- because count is one less because a H was removed
if diff == 0 then return false, false, disulfide
elseif diff == 1 then return false, true, disulfide
elseif diff == 2 then return true, false, disulfide
elseif diff == 3 then return true, true, disulfide
end
error('Strange atom count, report this!')
end
function ZLB(iSeg,atom,weight)
local nSeg = structure.GetCount()
local seg2, seg3
local minlen = 0.001
atom = atom or 0
weight = weight or 1.0
if (iSeg == 1) then seg2,seg3 = 2,3
elseif iSeg == nSeg then seg2,seg3 = nSeg-1,nSeg-2
else seg2,seg3 = iSeg-1, iSeg+1 end
local iBand = band.Add(iSeg,seg2,seg3,minlen,0,0,atom)
if iBand > 0 then
band.SetGoalLength(iBand,minlen)
band.SetStrength(iBand,weight)
end
return iBand
end
function GetSidechainAtom(iSeg)
local first,last,disulfide = fsl.atom._IsTerminalTest(
structure.GetAminoAcid(iSeg), structure.GetAtomCount(iSeg), fsl.atom._IsDisulfideBonded(iSeg))
local offset = last and 5 or 4 -- number slider is added to (min 1) to get C-Beta atom
return offset+1
end
function MakeSwapBand(src,dest)
local aa1,aa2 = structure.GetAminoAcid(src),structure.GetAminoAcid(dest)
local iband
if structure.GetSecondaryStructure(src) == 'M' or structure.GetSecondaryStructure(dest) == 'M' then return end
iband = ZLB(dest,2,options.strength)
if iband == 0 then return
else
local iband2 = band.AddToBandEndpoint(src,iband)
if iband2 > 0 then
band.SetGoalLength(iband2,0.001)
end
band.Delete(iband)
end
if options.sidechains and structure.GetAminoAcid(src) ~= 'g' and structure.GetAminoAcid(dest) ~= 'g' then
local atom1 = GetSidechainAtom(src)
local atom2 = GetSidechainAtom(dest)
iband = ZLB(dest,atom2,options.strength)
if iband == 0 then return
else
local iband2 = band.AddToBandEndpoint(src,iband,atom1)
if iband2 ~= 0 then
band.SetGoalLength(iband2,0.001)
if options.disabled then band.Disable(iband2) end
end
band.Delete(iband)
end
end
end
function round(x,n)
return math.floor(0.5 + x * 10^n) / 10^n
end
_NOrigBands = band.GetCount()
function DeleteBands()
print("Deleting bands")
if _NOrigBands == 0 then band.DeleteAll() end
while band.GetCount() > _NOrigBands do
band.Delete(band.GetCount())
end
end
function OnError(err)
if err:find("Cancel") then
print("Cancelled")
DeleteBands()
end
return err
end
-- ==================== Begin Main
rc = ShowMenu()
if rc == 0 then return end
rc,err = xpcall(function()
for i = 1,nseg,1+options.skip do
MakeSwapBand(i,nseg-i+1)
end
end, OnError)
print("Done.")