Profile
- Name
- Helix Mutator 0.1 -- brow42
- ID
- 48794
- Shared with
- Public
- Parent
- None
- Children
- None
- Created on
- April 05, 2014 at 20:53 PM UTC
- Updated on
- April 05, 2014 at 20:53 PM UTC
- Description
Mutates helices so that they are part polar and half hydrophobic. Choose a helix by freezing it or selecting it. orange = 30% ala 70% leu. blue = 50/50 lys/glutamate
Best for
Code
--[[
* Helix Mutator 0.1
* Original Author: Brow42
* Version 0.1 April 5 2014
* Mutate hidden residues to alanine or leucine,
* mutate exposed residues to lysine or glutamate
*
* Also various functions for researching helices
--]]
title = "Helix Mutator 0.1"
-- acidic
D = 'd' -- aspartate
E = 'e' -- glutamate
-- basic
K = 'k' -- lycine
R = 'r' -- arginine
-- hydrophobic
A = 'a' -- alanine
L = 'l' -- leucine
HydroThreshold = 40 -- negative hiding score for leu
sequence = "DDAAEEA" -- default heptad
IgnoreSS = true
function IsHelix(i) return structure.GetSecondaryStructure(i) == 'H' or IgnoreSS end
_lastloop = 0
function ResetSeq(i) _lastloop = i and i or 0 end
function Seq(i,thesequence)
thesequence = thesequence or sequence
if not IsHelix(i) then _lastloop = i end
i = i - _lastloop
if i == 0 then return nil end
i = 1 + (i - 1) % thesequence:len()
return thesequence:sub(i,i)
end
NSeg = structure.GetCount()
DeselectAll = selection.DeselectAll
GetAA = structure.GetAminoAcid
GetHiding = function(i) return current.GetSegmentEnergySubscore(i,'hiding') end
function True(j) return true end
function SelectIf(predicate,range)
if range == nil then range = {1,NSeg} end
ResetSeq(range[1]-1)
for i = range[1],range[2] do
if predicate(i) then selection.Select(i) end
end
end
function Unique(tab)
local tmp1, tmp2 = {}, {}
for i = 1,tab:len() do tmp1[tab:sub(i,i)] = true end
for u,v in pairs(tmp1) do tmp2[#tmp2 + 1] = u end
return tmp2
end
function Apply(thesequence,range,predicate)
predicate = predicate or True
thesequence = thesequence or sequence
local types = Unique(thesequence)
DeselectAll()
for i = 1,#types do
SelectIf(function(j) return IsHelix(j) and Seq(j,thesequence) == types[i] and predicate(j) end,range)
structure.SetAminoAcidSelected(types[i])
DeselectAll()
end
end
ERROR_BOTH = -1
function FindSelected()
local appending = false
local f,s,h
local range
local list = {}
for i = 1, NSeg do
f,s = freeze.IsFrozen(i), selection.IsSelected(i)
h = IsHelix(i)
if s and f then return ERROR_BOTH end
if (f or s) and h then
if appending then range[2] = i
else
range = {i,i}
list[#list+1] = range
appending = true
end
else
if appending then appending = false end
end
end
return list
end
-- ================================== Begin New Dialog Library Functions
-- Add a wall of text from a table
function dialog.AddLabels(d,msg,nlabels) -- pass in # of existing autolabels
local nlabels = nlabels or #(d._Order or {}) -- default, valid if never delete dialog elements
if type(msg) == 'string' then
msg = { msg }
end
for i = 1,#msg do
d['autolabel'..tostring(i+nlabels)] = dialog.AddLabel(msg[i])
end
end
-- Create but don't display a wall of text and 1 or 2 buttons
function dialog.CreateMessageBox(msg,title,buttontext1,buttontext0)
title = title or ''
local d = dialog.CreateDialog(title)
dialog.AddLabels(d,msg)
buttontext1 = buttontext1 or 'Ok'
d.button = dialog.AddButton(buttontext1,1)
if buttontext0 ~= nil then d.button0 = dialog.AddButton(buttontext0,0) end
return d
end
-- Display a dialog box
function dialog.ShowMessageBox(msg,title,buttontext1,buttontext0)
return dialog.Show(dialog.CreateMessageBox(msg,title,buttontext1,buttontext0))
end
-- Display a box AND print to the output
function ErrorMessage(msg,buttontext1,buttontext0)
if type(msg) == 'string' then
msg = { msg }
end
for i = 1,#msg do
print(msg[i])
end
return dialog.ShowMessageBox(msg,title,buttontext1,buttontext0)
end
-- ================ Begin Main ================
list = FindSelected()
if list == ERROR_BOTH then
rc = ErrorMessage("Used both selection and freezing. Clear both?","Yes","No")
if rc == 1 then
selection.DeselectAll()
freeze.UnfreezeAll()
end
return
end
if #list == 0 then
ErrorMessage({"Nothing selected. Choose 1 helix","by freezing or selecting."})
return
end
if #list ~= 1 then
ErrorMessage("Need exactly 1 helix selected.")
return
end
print(title)
print(puzzle.GetName())
print(os.date())
seed = (os.time() * 5779) % 10000
math.randomseed(seed)
print("random seed =",seed)
math.random()
Apply(L,list[1])
--Apply(K,list[1],function(j) return GetHiding(j) <= -HydroThreshold end)
new_seq = {}
for i = list[1][1],list[1][2] do
r = math.random()
if GetHiding(i) <= - HydroThreshold then
aa = r < 0.5 and K or E
else
aa = r < 0.3 and A or L
end
new_seq[#new_seq+1] = aa
end
Apply(table.concat(new_seq,''),list[1])