Code
TypeOf_Sheet = "e"
TypeOf_Helix = "h"
TypeOf_Loop = "l"
TypeOf_Molecule = "m" -- wtf is this?
Amino_Proline = "p"
helix_Likes = {"m","a","l","q","k"}
helix_Hates = {Amino_Proline,"g","d"}
amino_Red = {"n","d","q","e","h"}
amino_Blue = {"w","k","r","n","h","q"}
amino_Purple = {"t","s","y","g"}
function main ()
print ( "StackOverflow's starting structure builder" )
local d = dialog.CreateDialog ( "Setup Starting Structures" )
d.lbl1 = dialog.AddLabel ( "Click manual to specify number of helix or sheets" )
d.lbl2 = dialog.AddLabel ( "Click automatic to get an estimated structure" )
d.chk1 = dialog.AddCheckbox ( "Rebuild afterwards" , false )
d.btn1 = dialog.AddButton ( "Manual" , 1 )
d.btn2 = dialog.AddButton ( "Automatic" , 2 )
local cmd = dialog.Show ( d )
if ( cmd == 1 ) then
manual ()
else
automatic ()
end
if ( d.chk1.value ) then
print ( "quick rebuild" )
selection.SelectAll ()
structure.RebuildSelected ( 2 )
structure.WiggleAll ( 2 )
structure.ShakeSidechainsAll ( 1 )
structure.WiggleAll ( 2 )
print ( "rebuild finished. your starting position is ready" )
end
end
function manual ()
local d = dialog.CreateDialog ( "Setup Starting Structures" )
d.sldr1 = dialog.AddSlider ( "Number of Helices" , 4 , 1 , 8 , 0 )
d.sldr2 = dialog.AddSlider ( "Gap between Helices" , 3 , 1 , 5 , 0 )
d.chk1 = dialog.AddCheckbox ( "Sheets not Helices" , false )
d.btn1 = dialog.AddButton ( "Continue" , 1 )
d.btn2 = dialog.AddButton ( "Exit" , 0 )
local cmd = dialog.Show ( d )
if ( cmd == 1 ) then
local total = structure.GetCount ()
local count = d.sldr1.value
local gap = d.sldr2.value
local struct = TypeOf_Helix
if ( d.chk1.value == true ) then struct = TypeOf_Sheet end
local size = math.floor((total - (gap * (count - 1))) / count)
print("creating " .. count .. struct .. " puzzle")
print("structures will be " .. size .. " AAs long")
local ingap = false
local rem = size
local done = 0
for i = 1 , total do
if ( ingap or done == count ) then
structure.SetSecondaryStructure(i,TypeOf_Loop)
else
structure.SetSecondaryStructure(i,struct)
end
rem = rem - 1
if ( rem == 0 ) then
if ( ingap ) then
ingap = false
rem = size
else
ingap = true
rem = gap
done = done + 1
end
end
end
print ( "finished." )
else
print ( "user cancelled" )
end
end
function automatic ()
SetAllTo ( TypeOf_Loop )
FindHelices ( false )
FindSheets ()
FindHelices ( true )
FixJoins ()
end
function FixJoins ()
for i = 1 , structure.GetCount () - 1 do
ss1 = structure.GetSecondaryStructure ( i )
ss2 = structure.GetSecondaryStructure ( i + 1 )
if ( ( ss1 == TypeOf_Sheet and ss2 == TypeOf_Helix ) or ( ss1 == TypeOf_Helix and ss2 == TypeOf_Sheet ) ) then
-- can't have a helix next to a sheet without a loop inbetween them
if ( ss1 == TypeOf_Sheet ) then
structure.SetSecondaryStructure ( i , TypeOf_Loop )
else
structure.SetSecondaryStructure ( i + 1 , TypeOf_Loop )
end
end
end
end
function FindSheets ()
sc = structure.GetCount ()
start = 1
for i = 1 , sc - 1 do
amino1 = structure.GetAminoAcid ( i )
amino2 = structure.GetAminoAcid ( i + 1 )
hydro1 = structure.IsHydrophobic ( i )
hydro2 = structure.IsHydrophobic ( i + 1 )
if ( i < sc - 1
and (
-- hydro1 ~= hydro2
( notSame ( amino1 ) and mightBond ( amino2 ) ) or ( isBonder ( amino1 ) ~= isBonder ( amino2 ) )
)
) then
else
length = i - start + 1
if ( length > 2 ) then
SetTo ( start , length , TypeOf_Sheet )
end
start = i + 1
end
end
end
function FindHelices ( IdealOnly )
found = false
for i = 1 , structure.GetCount () - 7 do
helix = {}
phobic = {}
likes = 0 -- how many AA's are commonly found in helices
hates = 0 -- how many AA's do not like to form helices
ideal = 0 -- ideality rating, is this a strong helix pattern
for j = 1 , 7 do
helix [ j ] = structure.GetAminoAcid ( i + j - 1 )
phobic [ j ] = structure.IsHydrophobic ( i + j - 1 )
if ( inList ( helix [ j ] , helix_Likes ) ) then
likes = likes + 1
end
if ( inList ( helix [ j ] , helix_Hates ) ) then
hates = hates + 1
end
end
if ( helix [ 1 ] == Amino_Proline ) then
likes = likes + 1
hates = hates - 1
end
if ( phobic [ 1 ] and phobic [ 4 ] ) then
ideal = ideal + 1
end
if ( areOpposing ( helix [ 5 ] , helix [ 7 ] ) ) then
ideal = ideal + 1
end
if ( ( ideal >= 2 and hates == 0 and likes > 4 ) or ( IdealOnly == false and likes + ideal - hates > 4 ) ) then
found = true
SetTo ( i , 7 , TypeOf_Helix )
end
end
return found
end
function SetTo ( index , length , sstype )
selection.SelectRange ( index , index + length - 1 )
structure.SetSecondaryStructureSelected ( sstype )
selection.DeselectAll ()
end
function SetAllTo ( sstype )
selection.SelectAll ()
structure.SetSecondaryStructureSelected ( sstype )
selection.DeselectAll ()
end
function notSame ( amino1 , amino2 )
if ( isRed ( amino1 ) and isRed ( amino2 ) ) then
return false
end
if ( isBlue ( amino1 ) and isBlue ( amino2 ) ) then
return false
end
if ( isPurple ( amino1 ) and isPurple ( amino2 ) ) then
return false
end
return true
end
function mightBond ( amino1 , amino2 )
if ( isRed ( amino1 ) or isPurple ( amino1 ) ) then
if ( isBlue ( amino2 ) or isPurple ( amino2 ) ) then
return true
end
end
if ( isBlue ( amino1 ) or isPurple ( amino1 ) ) then
if ( isRed ( amino2 ) or isPurple ( amino2 ) ) then
return true
end
end
return false
end
function areOpposing ( amino1 , amino2 )
if ( isRed ( amino1 ) and isBlue ( amino2 ) ) then
return true
end
if ( isBlue ( amino1 ) and isRed ( amino2 ) ) then
return true
end
return false
end
function isRed ( amino )
return inList ( amino , amino_Red )
end
function isBlue ( amino )
return inList ( amino , amino_Blue )
end
function isPurple ( amino )
return inList ( amino , amino_Purple )
end
function isBonder ( amino )
return isRed ( amino ) or isBlue ( amino ) or isPurple ( amino )
end
function inList ( amino , list )
for a = 1 , #list do
if ( list [ a ] == amino ) then
return true
end
end
return false
end
main()