Icon representing a recipe

Recipe: Selected Bands

created by joshmiller

Profile


Name
Selected Bands
ID
104210
Shared with
Public
Parent
None
Children
None
Created on
December 12, 2020 at 16:56 PM UTC
Updated on
December 12, 2020 at 16:56 PM UTC
Description

Performs band operations only on bands touching selected segments.

Best for


Code


-- Selected Bands by joshmiller -- Performs band operations only on bands touching selected segments -- v 1.0, 12/6/2020 function GetSelectedBands() --recipeOnly bands = {} selectedResidues = {} for i = 1, structure.GetCount() do if selection.IsSelected(i) then selectedResidues[i] = true -- use sets instead of lists to avoid duplication end end for j = 1, band.GetCount() do if isInSet(band.GetResidueBase(j), selectedResidues) or isInSet(band.GetResidueEnd(j), selectedResidues) --and (not recipeOnly or band.IsRecipeBand(j)) -- not working right for some reason then bands[j] = true end end return bands end function isInSet(element, set) for key,value in pairs(set) do if element == key and value then return true end end return false end -- because deleting a band changes the indices of some others, -- we keep track of what we delete and account for that function SafeDeleteBands(bandSet) deletedBands = {} for key,value in pairs(bandSet) do realIndex = key - GetOffset(key, deletedBands) band.Delete(realIndex) deletedBands[key] = true end end -- returns the change in index to element after deleting deletedElementsSet function GetOffset(element, deletedElementsSet) offset = 0 for key,value in pairs(deletedElementsSet) do if element > key and value then offset = offset + 1 end end return offset end function DoToBands(bandSet, func, ...) for i,inSet in pairs(bandSet) do if inSet then func(i, ...) -- assume the first arg of a band function is the band index end end end function ToggleBand(index) if band.IsEnabled(index) then band.Disable(index) else band.Enable(index) end end local ask = dialog.CreateDialog("Selected Bands") ask.Instructions = dialog.AddLabel("1. Select segments connected to desired bands.") ask.Instructions2 = dialog.AddLabel("2. Check options below to modify those bands.") ask.Toggle = dialog.AddCheckbox("Toggle", false) ask.Enable = dialog.AddCheckbox("Enable", false) ask.Disable = dialog.AddCheckbox("Disable", false) ask.SetStrength = dialog.AddCheckbox("Set Strength", false) ask.Strength = dialog.AddSlider("Strength", 1, 0, 10, 1) ask.SetLength = dialog.AddCheckbox("Set Length", false) ask.Length = dialog.AddSlider("Length", 2, 0, 10, 1) ask.Delete = dialog.AddCheckbox("Delete", false) --ask.RecipeOnly = dialog.AddCheckbox("Modify Recipe Bands Only", false) -- not working right for some reason, removing ask.OK = dialog.AddButton("OK", 1) ask.Cancel = dialog.AddButton("Cancel", 0) if (dialog.Show(ask) > 0) then bands = GetSelectedBands() --ask.RecipeOnly.value if ask.Toggle.value then DoToBands(bands, ToggleBand) end if ask.Enable.value then DoToBands(bands, band.Enable) end if ask.Disable.value then DoToBands(bands, band.Disable) end if ask.SetStrength.value then DoToBands(bands, band.SetStrength, ask.Strength.value) end if ask.SetLength.value then DoToBands(bands, band.SetGoalLength, ask.Length.value) end if ask.Delete.value then SafeDeleteBands(bands) end end

Comments