Code
-- SetStructures
-- Asks for segmentranges of secondary structures to be set.
-- Option to set all non-specified to loops
-- Tvdl, 11-05-2012 Free to use for noncommercial purposes
-- Segment set and list module
-- Notice that most functions assume that the sets are well formed
-- (=ordered and no overlaps)
-- 02-05-2012 TvdL Free to use for non commercial purposes
function SegmentListToSet(list)
local result={}
local f=0
local l=-1
table.sort(list)
for i=1,#list do
if list[i] ~= l+1 and list[i] ~= l then
-- note: duplicates are removed
if l>0 then result[#result+1]={f,l} end
f=list[i]
end
l=list[i]
end
if l>0 then result[#result+1]={f,l} end
--print("list to set")
--SegmentPrintSet(result)
return result
end
function SegmentSetToList(set)
local result={}
for i=1,#set do
--print(set[i][1],set[i][2])
for k=set[i][1],set[i][2] do
result[#result+1]=k
end
end
return result
end
function SegmentCleanSet(set)
-- Makes it well formed
return SegmentListToSet(SegmentSetToList(set))
end
function SegmentInvertSet(set,maxseg)
-- Gives back all segments not in the set
-- maxseg is added for ligand
local result={}
if maxseg==nil then maxseg=structure.GetCount() end
if #set==0 then return {{1,maxseg}} end
if set[1][1] ~= 1 then result[1]={1,set[1][1]-1} end
for i=2,#set do
result[#result+1]={set[i-1][2]+1,set[i][1]-1}
end
if set[#set][2] ~= maxseg then result[#result+1]={set[#set][2]+1,maxseg} end
return result
end
function SegmentInList(s,list)
table.sort(list)
for i=1,#list do
if list[i]==s then return true
elseif list[i]>s then return false
end
end
return false
end
function SegmentInSet(set,s)
for i=1,#set do
if s>=set[i][1] and s<=set[i][2] then return true
elseif s<set[i][1] then return false
end
end
return false
end
function SegmentJoinList(list1,list2)
local result=list1
if result == nil then return list2 end
for i=1,#list2 do result[#result+1]=list2[i] end
table.sort(result)
return result
end
function SegmentJoinSet(set1,set2)
return SegmentListToSet(SegmentJoinList(SegmentSetToList(set1),SegmentSetToList(set2)))
end
function SegmentCommList(list1,list2)
local result={}
table.sort(list1)
table.sort(list2)
if #list2==0 then return result end
local j=1
for i=1,#list1 do
while list2[j]<list1[i] do
j=j+1
if j>#list2 then return result end
end
if list1[i]==list2[j] then result[#result+1]=list1[i] end
end
return result
end
function SegmentCommSet(set1,set2)
return SegmentListToSet(SegmentCommList(SegmentSetToList(set1),SegmentSetToList(set2)))
end
function SegmentSetMinus(set1,set2)
return SegmentCommSet(set1,SegmentInvertSet(set2))
end
function SegmentPrintSet(set)
print(SegmentSetToString(set))
end
function SegmentSetToString(set)
local line = ""
for i=1,#set do
if i~=1 then line=line..", " end
line=line..set[i][1].."-"..set[i][2]
end
return line
end
function SegmentSetInSet(set,sub)
if sub==nil then return true end
-- Checks if sub is a proper subset of set
for i=1,#sub do
if not SegmentRangeInSet(set,sub[i]) then return false end
end
return true
end
function SegmentRangeInSet(set,range)
if range==nil or #range==0 then return true end
local b=range[1]
local e=range[2]
for i=1,#set do
if b>=set[i][1] and b<=set[i][2] then
return (e<=set[i][2])
elseif e<=set[i][1] then return false end
end
return false
end
--- End of Segment Set module
-- Module Find Segment Types
function FindMutables()
local result={}
for i=1,structure.GetCount() do if structure.IsMutable(i) then result[#result+1]=i end end
return SegmentListToSet(result)
end
function FindFrozen()
local result={}
for i=1,structure.GetCount() do if freeze.IsFrozen(i) then result[#result+1]=i end end
return SegmentListToSet(result)
end
function FindLocked()
local result={}
for i=1,structure.GetCount() do if structure.IsLocked(i) then result[#result+1]=i end end
return SegmentListToSet(result)
end
function FindSelected()
local result={}
for i=1,structure.GetCount() do if selection.IsSelected(i) then result[#result+1]=i end end
return SegmentListToSet(result)
end
function FindAAtype(aa)
local result={}
for i=1,structure.GetCount() do
if structure.GetSecondaryStructure(i)== aa then result[#result+1]=i end
end
return SegmentListToSet(result)
end
-- end Module Find Segment Types
-- Module setsegmentset
-- Tvdl, 11-05-2012 Free to use for noncommercial purposes
function SetSelection(set)
selection.DeselectAll()
for i=1,#set do
selection.SelectRange(set[i][1],set[i][2])
end
end
function SetAAtype(set,aa)
local saveselected=FindSelected()
SetSelection(set)
structure.SetSecondaryStructureSelected(aa)
SetSelection(saveselected)
end
Loops={}
Helixes={}
Sheets={}
Allloops=true
function Checknums(nums,where)
-- Now checking
if #nums%2 ~= 0 then
print("In "..where.." not an even number of segments found")
return false
end
for i=1,#nums do
if nums[i]==0 or nums[i]>structure.GetCount() then
print("Number "..nums[i].." is not a segment in "..where)
end
end
return true
end
function askSecStruct(title)
local Input=""
local Firsttime=true
local Errfound=false
local function ReadSegmentSet(data,what)
local nums = {}
local NoNegatives='%d+' -- - is not part of a number
local result={}
for v in string.gfind(data,NoNegatives) do
table.insert(nums, tonumber(v))
end
if Checknums(nums,what) then
for i=1,#nums/2 do
result[i]={nums[2*i-1],nums[2*i]}
end
result=SegmentCleanSet(result)
else Errfound=true result={} end
return result
end
local function CheckOverlap(set1,set2)
local Commset=SegmentCommSet(set1,set2)
if #Commset >0 then
print("Overlap found. First is "..Commset[1][1].."-"..Commset[1][2])
Errfound=true
end
end
repeat
local ask = dialog.CreateDialog(title)
if Errfound then
ask.subE=dialog.AddLabel("====ERROR(S) FOUND, see output window")
Firsttime=true
Errfound=false
end
-- Build resultstring from pairs found
local listH=SegmentSetToString(Helixes)
local listS=SegmentSetToString(Sheets)
local listL=SegmentSetToString(Loops)
ask.sub1=dialog.AddLabel("Fill in lists of segments to set")
ask.sub2=dialog.AddLabel("Example: 1-3,8-10")
ask.numH=dialog.AddTextbox("Helixes",listH)
ask.numS=dialog.AddTextbox("Sheets",listS)
ask.numL=dialog.AddTextbox("Loops",listL)
ask.sub4=dialog.AddLabel("Start with setting all to loops")
ask.allloops=dialog.AddCheckbox("Default loops",true)
if not Firsttime then ask.OK = dialog.AddButton("OK",1) end
ask.Check = dialog.AddButton("Check",2)
ask.Cancel = dialog.AddButton("Cancel",0)
local result=dialog.Show(ask)
if result > 0 then
Helixes=ReadSegmentSet(ask.numH.value,"helixes")
Sheets=ReadSegmentSet(ask.numS.value,"sheets")
Loops=ReadSegmentSet(ask.numL.value,"loops")
Allloops=ask.allloops.value
CheckOverlap(Helixes,Loops)
CheckOverlap(Sheets,Loops)
CheckOverlap(Helixes,Sheets)
end
Firsttime=false
if result==0 then return end
until result==1 and Errfound==false
-- and now the real work
if Allloops then
local ALL={{1,structure.GetCount()}}
SetAAtype(ALL,"L")
end
SetAAtype(Helixes,"H")
SetAAtype(Sheets,"E")
SetAAtype(Loops,"L")
print("Setting structures done")
end
askSecStruct("Tvdl Set secondary Strucs 1.0")