private void openCustomResidueRenderSettings(int residueID)
        {
            List <Residue> residues = primaryStructure.GetResiduesByID(residueID);

            if (residues.Count == 0)
            {
                return;
            }

            List <int> residueIDs = new List <int>()
            {
                residueID
            };
            List <string> atomNames = new List <string>();

            foreach (Atom atom in residues[0].Atoms.Values)
            {
                atomNames.Add(atom.Name);
            }

            ResidueRenderSettings residueSettings = new ResidueRenderSettings();

            if (moleculeRenderSettings.CustomResidueRenderSettings.ContainsKey(residueID))
            {
                residueSettings = moleculeRenderSettings.CustomResidueRenderSettings[residueID].Clone();
            }

            customSettingsPanel.Initialise(residueIDs, residueName, atomNames, ResidueUpdateType.ID, residueSettings, saveCustomResidueIDSettings, onCloseCustomResidueSettings);

            //residueIDsPanel.gameObject.SetActive(false);
        }
        public bool Equals(ResidueRenderSettings other)
        {
            if (AtomSettings.Count != other.AtomSettings.Count)
            {
                return(false);
            }

            foreach (KeyValuePair <string, AtomRenderSettings> atom in AtomSettings)
            {
                if (other.AtomSettings.ContainsKey(atom.Key) && other.AtomSettings[atom.Key].Equals(atom.Value))
                {
                    continue;
                }

                return(false);
            }

            if (other.ColourAtoms != ColourAtoms ||
                other.AtomRepresentation != AtomRepresentation ||
                !other.ColourBonds.Equals(ColourBonds) ||
                other.LargeBonds != LargeBonds ||
                other.ColourSecondaryStructure != ColourSecondaryStructure ||
                other.ResidueColour != ResidueColour)
            {
                return(false);
            }

            return(true);
        }
        private void saveCustomResidueIDSettings(List <int> residueIDs, ResidueRenderSettings customResidueSettings, ResidueUpdateType updateType)
        {
            saveCustomResidueSettings(residueIDs, customResidueSettings, updateType);

            residueIDsPanel.SetActive(false); // removes partial button updates on screen
            renderResidueIDButtons();
            residueIDsPanel.SetActive(true);
        }
Beispiel #4
0
        public void SaveRenderSettings()
        {
            savePanelToRenderSettings();

            if (!renderSettings.Equals(savedRenderSettings) || residueUpdateType != ResidueUpdateType.ID)
            {
                savedRenderSettings = renderSettings.Clone();
                saveSettingsCallback(residueIDs, renderSettings, residueUpdateType);
            }
        }
        public void OnUpdateAllResiduesButton()
        {
            List <string> atomNames = new List <string>();

            // atom names for each residue should be the same, but terminating residues can have a few extra atoms.
            // the residue ID with the most amount of atoms will be the terminating residue
            List <Residue> residues = primaryStructure.GetResiduesByName(residueName);

            if (residues == null || residues.Count == 0)
            {
                return;
            }

            Residue residueMostAtoms          = residues[0];
            int     firstResidueID            = residues[0].ID;
            bool    residueSettingsAllTheSame = true;

            foreach (Residue residue in residues)
            {
                if (residue.Atoms.Count > residueMostAtoms.Atoms.Count)
                {
                    residueMostAtoms = residue;
                    break;
                }

                if (!moleculeRenderSettings.CustomResidueRenderSettings.ContainsKey(residue.ID) ||
                    !moleculeRenderSettings.CustomResidueRenderSettings[residue.ID].Equals(moleculeRenderSettings.CustomResidueRenderSettings[firstResidueID]))
                {
                    residueSettingsAllTheSame = false;
                    break;
                }
            }

            foreach (Atom atom in residueMostAtoms.Atoms.Values)
            {
                atomNames.Add(atom.Name);
            }

            ResidueRenderSettings panelResidueSettings = new ResidueRenderSettings();

            if (residueSettingsAllTheSame)
            {
                if (moleculeRenderSettings.CustomResidueRenderSettings.ContainsKey(residueMostAtoms.ID))
                {
                    panelResidueSettings = moleculeRenderSettings.CustomResidueRenderSettings[residueMostAtoms.ID].Clone();
                }
            }

            customSettingsPanel.Initialise(residueIDs, residueName, atomNames, ResidueUpdateType.Name, panelResidueSettings, saveCustomResidueIDSettings, onCloseCustomResidueSettings);
        }
Beispiel #6
0
        private float getAtomRadius(Atom atom)
        {
            MolecularRepresentation?customRepresentation = null;

            if (renderSettings.CustomResidueRenderSettings != null && renderSettings.CustomResidueRenderSettings.ContainsKey(atom.ResidueID))
            {
                ResidueRenderSettings residueSettings = renderSettings.CustomResidueRenderSettings[atom.ResidueID];

                if (residueSettings != null)
                {
                    // use the atom specific settings if available.
                    if (residueSettings.AtomSettings.ContainsKey(atom.Name))
                    {
                        AtomRenderSettings atomSettings = residueSettings.AtomSettings[atom.Name];
                        if (atomSettings.Representation != MolecularRepresentation.None)
                        {
                            customRepresentation = atomSettings.Representation;
                        }
                    }

                    // if we didn't get from atom specific settings then try residue settings
                    if (customRepresentation == null)
                    {
                        if (residueSettings.AtomRepresentation != MolecularRepresentation.None)
                        {
                            customRepresentation = residueSettings.AtomRepresentation;
                        }
                    }
                }
            }

            float atomRadius;

            if (!renderSettings.ShowAtoms)
            {
                atomRadius = 0.001f;
            }
            else if (customRepresentation != null && customRepresentation != MolecularRepresentation.None)
            {
                atomRadius  = (MolecularRepresentation)customRepresentation == MolecularRepresentation.VDW ? atom.VDWRadius : atom.AtomicRadius / 2f;
                atomRadius *= renderSettings.AtomScale;
            }
            else
            {
                atomRadius  = renderSettings.Representation == MolecularRepresentation.VDW ? atom.VDWRadius : atom.AtomicRadius / 2f;
                atomRadius *= renderSettings.AtomScale;
            }

            return(atomRadius);
        }
Beispiel #7
0
        public void Initialise(List <int> residueIDs, string residueName, List <string> atomNames, ResidueUpdateType residueUpdateType, ResidueRenderSettings renderSettings, SaveCustomResidueSettingsDelegate saveSettings, CloseCustomResidueSettingsDelegate onClose)
        {
            this.residueIDs           = residueIDs;
            this.residueName          = residueName;
            this.atomNames            = atomNames;
            this.renderSettings       = renderSettings;
            this.residueUpdateType    = residueUpdateType;
            this.saveSettingsCallback = saveSettings;
            this.onCloseCallback      = onClose;

            this.savedRenderSettings = renderSettings.Clone();

            loadSettings();
            customSettingsPanel.SetActive(true);

            autoSaveSettingUpdates = true;
        }
        public ResidueRenderSettings Clone()
        {
            ResidueRenderSettings clone = new ResidueRenderSettings();

            clone.ColourAtoms              = ColourAtoms;
            clone.AtomRepresentation       = AtomRepresentation;
            clone.ColourBonds              = ColourBonds;
            clone.LargeBonds               = LargeBonds;
            clone.ColourSecondaryStructure = ColourSecondaryStructure;
            clone.ResidueColour            = ResidueColour;

            clone.AtomSettings = new Dictionary <string, AtomRenderSettings>();
            foreach (KeyValuePair <string, AtomRenderSettings> atom in AtomSettings)
            {
                clone.AtomSettings.Add(atom.Key, atom.Value.Clone());
            }

            return(clone);
        }
        private void saveCustomResidueSettings(List <int> residueIDs, ResidueRenderSettings customResidueSettings, ResidueUpdateType updateType)
        {
            if (!customResidueSettings.IsDefault())
            {
                //Debug.Log("Saving residue settings - not default ");

                foreach (int residueID in residueIDs)
                {
                    if (moleculeRenderSettings.CustomResidueRenderSettings.ContainsKey(residueID))
                    {
                        moleculeRenderSettings.CustomResidueRenderSettings[residueID] = customResidueSettings;
                    }
                    else
                    {
                        moleculeRenderSettings.CustomResidueRenderSettings.Add(residueID, customResidueSettings);
                    }
                }
            }
            else
            {
                //Debug.Log("Removing residue settings - are default");

                foreach (int residueID in residueIDs)
                {
                    if (moleculeRenderSettings.CustomResidueRenderSettings.ContainsKey(residueID))
                    {
                        moleculeRenderSettings.CustomResidueRenderSettings.Remove(residueID);
                    }
                }
            }

            updateCustomResidueNameStatus(residueIDs);

            //if (updateType == ResidueUpdateType.All) {
            //    renderResidueNameButtons();
            //}

            settingsUpdatedCallback();
        }
        private IEnumerator createModelBonds(MoleculeRenderSettings renderSettings, PrimaryStructureFrame frame, int meshQuality)
        {
            // set colour for bonds
            Color32 bondColour;

            if (!MolecularConstants.CPKColors.TryGetValue("Bond", out bondColour))
            {
                MolecularConstants.CPKColors.TryGetValue("Other", out bondColour);
            }

            List <Matrix4x4> standardTransforms = new List <Matrix4x4>();
            Dictionary <Color, List <Matrix4x4> > highlightedTransforms = new Dictionary <Color, List <Matrix4x4> >();

            float standardCylinderWidth = 0.015f * renderSettings.BondScale;
            float enlargedCylinderWidth = 0.040f * renderSettings.BondScale;

            // get atoms for bonds
            Dictionary <int, Atom> atoms;
            Dictionary <int, Atom> highLightedAtoms = new Dictionary <int, Atom>();

            atoms = primaryStructure.GetAtoms(renderSettings.ShowStandardResidues, renderSettings.ShowNonStandardResidues, renderSettings.EnabledElements, renderSettings.EnabledResidueNames, renderSettings.EnabledResidueIDs);

            if (renderSettings.CustomResidueRenderSettings != null)
            {
                HashSet <int> customResidueIDs = new HashSet <int>(renderSettings.CustomResidueRenderSettings.Keys.ToList());
                highLightedAtoms = primaryStructure.GetAtoms(renderSettings.ShowStandardResidues, renderSettings.ShowNonStandardResidues, renderSettings.EnabledElements, renderSettings.EnabledResidueNames, customResidueIDs);
            }

            foreach (KeyValuePair <int, Bond> bond in bonds)
            {
                Vector3 atom1pos, atom2pos;
                Atom    atom1 = null;
                Atom    atom2 = null;

                if (!atoms.TryGetValue(bond.Value.Atom1Index, out atom1) || !atoms.TryGetValue(bond.Value.Atom2Index, out atom2))
                {
                    continue;
                }

                if (frame == null)
                {
                    atom1pos = new Vector3(atom1.Position.x, atom1.Position.y, atom1.Position.z);
                    atom2pos = new Vector3(atom2.Position.x, atom2.Position.y, atom2.Position.z);
                }
                else
                {
                    if (bond.Value.Atom1Index >= frame.AtomCount || bond.Value.Atom2Index >= frame.AtomCount)
                    {
                        // no need to send error message as this will have already been done in the atom render
                        yield break;
                    }

                    atom1pos = new Vector3(frame.Coords[bond.Value.Atom1Index * 3], frame.Coords[(bond.Value.Atom1Index * 3) + 1], frame.Coords[(bond.Value.Atom1Index * 3) + 2]);
                    atom2pos = new Vector3(frame.Coords[bond.Value.Atom2Index * 3], frame.Coords[(bond.Value.Atom2Index * 3) + 1], frame.Coords[(bond.Value.Atom2Index * 3) + 2]);
                }

                // flip coord system for Unity
                atom1pos.z *= -1;
                atom2pos.z *= -1;

                // bonds aren't recalculated on each frame. In some frames atoms jump from one side of the simulation box to another. When this happens need to disable bond view
                float bondLength = (atom2pos - atom1pos).magnitude / 2;
                if (bondLength > BondLengths.MaximumLengthAllElements)
                {
                    continue;
                }

                Vector3 position = ((atom1pos - atom2pos) / 2.0f) + atom2pos;
                float   length   = (atom2pos - atom1pos).magnitude;

                Quaternion rotation = Quaternion.FromToRotation(Vector3.up, atom1pos - atom2pos);

                if (highLightedAtoms != null && highLightedAtoms.Count > 0 && highLightedAtoms.ContainsKey(atom1.Index) && highLightedAtoms.ContainsKey(atom2.Index))
                {
                    int atom1residue = atom1.ResidueID;
                    int atom2residue = atom2.ResidueID;

                    // only colour or highlight bonds between atoms of the same residue
                    if (atom1residue == atom2residue && renderSettings.CustomResidueRenderSettings.ContainsKey(atom1residue) && renderSettings.CustomResidueRenderSettings[atom1residue].ColourBonds)
                    {
                        ResidueRenderSettings options = renderSettings.CustomResidueRenderSettings[atom1residue];

                        float cylinderWidth = standardCylinderWidth;
                        if (options.LargeBonds)
                        {
                            cylinderWidth = enlargedCylinderWidth;
                        }

                        Vector3 localScale = new Vector3(cylinderWidth, length, cylinderWidth);

                        Matrix4x4 bondTransform = Matrix4x4.TRS(position, rotation, localScale);

                        if (!highlightedTransforms.ContainsKey(options.ResidueColour))
                        {
                            highlightedTransforms.Add(options.ResidueColour, new List <Matrix4x4>());
                        }

                        highlightedTransforms[options.ResidueColour].Add(bondTransform);
                    }
                    else
                    {
                        float cylinderWidth = standardCylinderWidth;
                        if (atom1residue == atom2residue && renderSettings.CustomResidueRenderSettings.ContainsKey(atom1residue) && renderSettings.CustomResidueRenderSettings[atom1residue].LargeBonds)
                        {
                            cylinderWidth = enlargedCylinderWidth;
                        }

                        Vector3 localScale = new Vector3(cylinderWidth, length, cylinderWidth);
                        standardTransforms.Add(Matrix4x4.TRS(position, rotation, localScale));
                    }
                }
                else
                {
                    Vector3 localScale = new Vector3(standardCylinderWidth, length, standardCylinderWidth);
                    standardTransforms.Add(Matrix4x4.TRS(position, rotation, localScale));
                }
            }

            GameObject prefab = BondPrefabs[meshQuality];

            GameObject parent = new GameObject("StandardCombinedMeshParent");

            parent.SetActive(false);
            yield return(StartCoroutine(meshBuilder.CombinedMesh(prefab, standardTransforms.ToArray(), bondColour, parent)));

            parent.transform.SetParent(BondParent.transform, false);

            parent = new GameObject("HighligtedCombinedMeshParent");
            parent.SetActive(false);

            foreach (KeyValuePair <Color, List <Matrix4x4> > item in highlightedTransforms)
            {
                yield return(StartCoroutine(meshBuilder.CombinedMesh(prefab, item.Value.ToArray(), item.Key, parent)));
            }

            parent.transform.SetParent(BondParent.transform, false);
        }
        private IEnumerator createModelAtomsByElement(MoleculeRenderSettings renderSettings, PrimaryStructureFrame frame, int meshQuality)
        {
            Quaternion atomOrientation = Quaternion.Euler(45, 45, 45);

            // generate combined meshes (i.e single GameObject) for atoms with same element/colour
            Dictionary <Color, List <Matrix4x4> > mergeTransforms = new Dictionary <Color, List <Matrix4x4> >();

            Dictionary <int, Atom> atoms = primaryStructure.GetAtoms(renderSettings.ShowStandardResidues, renderSettings.ShowNonStandardResidues, renderSettings.EnabledElements, renderSettings.EnabledResidueNames, renderSettings.EnabledResidueIDs);

            foreach (KeyValuePair <int, Atom> item in atoms)
            {
                Atom atom = item.Value;

                Vector3 position;

                // if no frame use the base structure coordinates.
                if (frame == null)
                {
                    position = new Vector3(atom.Position.x, atom.Position.y, atom.Position.z);
                }
                else
                {
                    if (atom.Index >= frame.AtomCount)
                    {
                        MoleculeEvents.RaiseShowMessage("Atoms not found in frame record. Aborting frame render.", true);
                        yield break;
                    }
                    position = new Vector3(frame.Coords[atom.Index * 3], frame.Coords[(atom.Index * 3) + 1], frame.Coords[(atom.Index * 3) + 2]);
                }

                // flip coord system for Unity
                position.z *= -1;

                Color32?customColour = null;
                MolecularRepresentation?customRepresentation = null;

                if (renderSettings.CustomResidueRenderSettings != null && renderSettings.CustomResidueRenderSettings.ContainsKey(atom.ResidueID))
                {
                    ResidueRenderSettings residueSettings = renderSettings.CustomResidueRenderSettings[atom.ResidueID];

                    if (residueSettings != null)
                    {
                        // use the atom specific settings if available.
                        if (residueSettings.AtomSettings.ContainsKey(atom.Name))
                        {
                            AtomRenderSettings atomSettings = residueSettings.AtomSettings[atom.Name];

                            if (atomSettings.CustomColour)
                            {
                                customColour = atomSettings.AtomColour;
                            }

                            if (atomSettings.Representation != MolecularRepresentation.None)
                            {
                                customRepresentation = atomSettings.Representation;
                            }
                        }

                        // if we didn't get from atom specific settings then get from residue settings
                        if (customColour == null && residueSettings.ColourAtoms)
                        {
                            customColour = residueSettings.ResidueColour;
                        }

                        if (customRepresentation == null)
                        {
                            if (residueSettings.AtomRepresentation != MolecularRepresentation.None)
                            {
                                customRepresentation = residueSettings.AtomRepresentation;
                            }
                        }
                    }
                }

                Color32 atomColour = Color.white;

                if (customColour != null)
                {
                    atomColour = (Color)customColour;
                }
                else
                {
                    if (!MolecularConstants.CPKColors.TryGetValue(atom.Element.ToString(), out atomColour))
                    {
                        MolecularConstants.CPKColors.TryGetValue("Other", out atomColour);
                    }
                }

                float   atomDiameter = getAtomRadius(atom, renderSettings, customRepresentation) * 2;
                Vector3 scale        = new Vector3(atomDiameter, atomDiameter, atomDiameter);

                Matrix4x4 atomTransform = Matrix4x4.TRS(position, atomOrientation, scale);

                if (!mergeTransforms.ContainsKey(atomColour))
                {
                    mergeTransforms.Add(atomColour, new List <Matrix4x4>());
                }

                mergeTransforms[atomColour].Add(atomTransform);
            }


            // create the meshes by colour
            GameObject prefab = AtomPrefabs[meshQuality];
            GameObject parent = new GameObject("CombinedMeshParent");

            parent.SetActive(false);

            foreach (KeyValuePair <Color, List <Matrix4x4> > item in mergeTransforms)
            {
                yield return(StartCoroutine(meshBuilder.CombinedMesh(prefab, item.Value.ToArray(), item.Key, parent)));
            }

            parent.transform.SetParent(AtomParent.transform, false);

            yield break;
        }
        private Mesh BuildStructureMesh(MoleculeRenderSettings renderSettings, Chain chain, PrimaryStructureFrame frame, SecondaryStructure secondaryStructure)
        {
            Mesh  structureMesh = null;
            int   interpolation = 20;
            int   resolution    = 6; // should be in config
            float radius        = 0.015f;

            List <DynamicMeshNode> nodes = new List <DynamicMeshNode>();

            Vector3 lastPosition            = Vector3.zero;
            Vector3 lastNormal              = Vector3.zero;
            Vector3 averagedNormal          = Vector3.zero;
            SecondaryStructureType lastType = SecondaryStructureType.Coil;

            for (int i = 0; i < chain.MainChainResidues.Count; i++)
            {
                DynamicMeshNode node = new DynamicMeshNode();

                Residue residue = chain.MainChainResidues[i];

                // check if residue mainchain information is complete. Ignore if not
                if (residue.AlphaCarbon == null || residue.CarbonylCarbon == null || residue.CarbonylOxygen == null)
                {
                    continue;
                }

                // set position
                Atom atom = residue.AlphaCarbon;

                // if no frame number use the base structure coordinates.
                Vector3 position;
                if (frame == null)
                {
                    position = new Vector3(atom.Position.x, atom.Position.y, atom.Position.z);
                }
                else
                {
                    position = new Vector3(frame.Coords[atom.Index * 3], frame.Coords[(atom.Index * 3) + 1], frame.Coords[(atom.Index * 3) + 2]);
                }

                // flip coord system for Unity
                position.z *= -1;

                node.Position = position;

                SecondaryStructureInfomation structureInformation = secondaryStructure.GetStructureInformation(residue.Index);

                Residue nextResidue = null;
                if (i + 1 < chain.MainChainResidues.Count)
                {
                    nextResidue = chain.MainChainResidues[i + 1];
                }

                SecondaryStructureInfomation nextResidueStructureInfo = null;
                if (nextResidue != null)
                {
                    nextResidueStructureInfo = secondaryStructure.GetStructureInformation(nextResidue.Index);
                }

                // store the node type
                if (structureInformation != null)
                {
                    if (renderSettings.ShowHelices &&
                        (structureInformation.type == SecondaryStructureType.ThreeHelix ||
                         structureInformation.type == SecondaryStructureType.AlphaHelix ||
                         structureInformation.type == SecondaryStructureType.FiveHelix))
                    {
                        node.Type = DynamicMeshNodeType.SpiralRibbon;
                    }
                    else if (renderSettings.ShowSheets &&
                             structureInformation.type == SecondaryStructureType.BetaSheet)
                    {
                        if (nextResidue == null || (nextResidueStructureInfo != null && nextResidueStructureInfo.type != SecondaryStructureType.BetaSheet))
                        {
                            node.Type = DynamicMeshNodeType.RibbonHead;
                        }
                        else
                        {
                            node.Type = DynamicMeshNodeType.Ribbon;
                        }
                    }
                    else if (renderSettings.ShowTurns &&
                             structureInformation.type == SecondaryStructureType.Turn)
                    {
                        node.Type = DynamicMeshNodeType.LargeTube;
                    }
                    else
                    {
                        node.Type = DynamicMeshNodeType.Tube;
                    }


                    // calculate and store the node color

                    bool foundColour = false;

                    if (renderSettings.CustomResidueRenderSettings != null && renderSettings.CustomResidueRenderSettings.ContainsKey(residue.ID))
                    {
                        ResidueRenderSettings residueRenderSettings = renderSettings.CustomResidueRenderSettings[residue.ID];

                        if (residueRenderSettings != null && residueRenderSettings.ColourSecondaryStructure)
                        {
                            node.VertexColor = residueRenderSettings.ResidueColour;
                            foundColour      = true;
                        }
                    }

                    if (foundColour == false)
                    {
                        switch (structureInformation.type)
                        {
                        case SecondaryStructureType.ThreeHelix:
                            node.VertexColor = Settings.ThreeHelixColour;
                            break;

                        case SecondaryStructureType.AlphaHelix:
                            node.VertexColor = Settings.AlphaHelixColour;
                            break;

                        case SecondaryStructureType.FiveHelix:
                            node.VertexColor = Settings.FiveHelixColour;
                            break;

                        case SecondaryStructureType.Turn:
                            node.VertexColor = Settings.TurnColour;
                            break;

                        case SecondaryStructureType.BetaSheet:
                            node.VertexColor = Settings.BetaSheetColour;
                            break;

                        case SecondaryStructureType.BetaBridge:
                            node.VertexColor = Settings.BetaBridgeColour;
                            break;

                        case SecondaryStructureType.Bend:
                            node.VertexColor = Settings.BendColour;
                            break;

                        case SecondaryStructureType.Coil:
                            node.VertexColor = Settings.CoilColour;
                            break;

                        default:
                            node.VertexColor = ErrorColor;
                            break;
                        }
                    }
                }
                else
                {
                    Debug.Log("*** Structure info null: assigning defaults");
                    node.Type        = DynamicMeshNodeType.Tube;
                    node.VertexColor = ErrorColor;
                }

                // determine the node rotation
                // calculate the normal from the peptide plane and store as the node rotation
                Vector3 vertexA = residue.AlphaCarbon.Position;
                Vector3 vertexB = residue.CarbonylCarbon.Position;
                Vector3 vertexC = residue.CarbonylOxygen.Position;

                // flip coord system for Unity
                vertexA.z *= -1;
                vertexB.z *= -1;
                vertexC.z *= -1;

                //// create a triangle to show the peptide plane on the model for debugging purposes
                //GameObject residuePlane = createTriangle(vertexA, vertexB, vertexC);
                //residuePlane.name = "ResiduePlane";
                //AddMeshToModel(residuePlane, StructureParent);

                Vector3 direction = Vector3.Cross(vertexB - vertexA, vertexC - vertexA);
                Vector3 normal    = Vector3.Normalize(direction);

                if (structureInformation != null && structureInformation.type == SecondaryStructureType.BetaSheet || lastType == SecondaryStructureType.BetaSheet)
                {
                    if (Vector3.Dot(normal, lastNormal) < 0)
                    {
                        normal *= -1;
                    }

                    if (lastType != SecondaryStructureType.BetaSheet)
                    {
                        averagedNormal = normal;
                    }
                    else
                    {
                        averagedNormal += normal;
                        averagedNormal.Normalize();
                        normal = averagedNormal;
                    }
                }

                node.Rotation = normal;

                lastNormal = normal;
                if (structureInformation != null)
                {
                    lastType = structureInformation.type;
                }
                else
                {
                    lastType = SecondaryStructureType.Coil;
                }

                nodes.Add(node);
            }


            if (renderSettings.SmoothNodes)
            {
                nodes = smoothMeshNodes(nodes);
            }

            //// draw debug line from node points along rotation vector
            //for (int q = 0; q < nodePositions.Count; q++) {

            //    Vector3 fromPosition = nodePositions[q];
            //    Vector3 toPosition = fromPosition + nodeRotations[q] * 0.3f;
            //    GameObject line = createLine(fromPosition, toPosition, Color.white, Color.red);
            //    AddMeshToModel(line, StructureParent);
            //}

            List <Vector3> nodePositions = new List <Vector3>();

            foreach (DynamicMeshNode node in nodes)
            {
                nodePositions.Add(node.Position);
            }
            List <DynamicMeshNode> splineNodes  = new List <DynamicMeshNode>();
            IEnumerable            splinePoints = Interpolate.NewCatmullRom(nodePositions.ToArray(), interpolation, false);
            int j = 0;

            foreach (Vector3 position in splinePoints)
            {
                int nodeIndex = j / (interpolation + 1);
                int splinePointsSinceLastNode = j % (interpolation + 1);

                DynamicMeshNode node = new DynamicMeshNode();
                node.Position = position;

                //int colorIndex = nodeIndex % DebugColors.Count;
                //node.VertexColor = DebugColors[colorIndex];

                node.VertexColor = nodes[nodeIndex].VertexColor;
                node.Type        = nodes[nodeIndex].Type;

                // set the mesh rotations for the node
                // dont do rotations on tube structures
                switch (node.Type)
                {
                case DynamicMeshNodeType.Ribbon:
                case DynamicMeshNodeType.RibbonHead:
                case DynamicMeshNodeType.SpiralRibbon:

                    if (nodeIndex < nodes.Count - 1)
                    {
                        float percentThroughNode = (float)splinePointsSinceLastNode / ((float)interpolation + 1f);
                        node.Rotation = Vector3.Lerp((Vector3)nodes[nodeIndex].Rotation, (Vector3)nodes[nodeIndex + 1].Rotation, percentThroughNode);
                    }
                    else       // last node
                    {
                        node.Rotation = (Vector3)nodes[nodeIndex].Rotation;
                    }

                    break;
                }

                splineNodes.Add(node);
                j++;
            }

            DynamicMesh dynamicMesh = new DynamicMesh(splineNodes, radius, resolution, interpolation + 1);

            structureMesh = dynamicMesh.Build(Settings.DebugFlag);

            return(structureMesh);
        }
        private void openUpdateAllResiduesPanel(List <int> residueIDs)
        {
            if (residueIDs == null || residueIDs.Count == 0)
            {
                return;
            }

            List <Residue> residues = primaryStructure.GetResiduesByID(residueIDs);

            if (residues == null || residues.Count == 0)
            {
                return;
            }

            Residue selectedResidue = residues[0];

            bool             atomNamesAllTheSame      = true;
            HashSet <string> selectedResidueAtomNames = new HashSet <string>();

            foreach (Atom atom in selectedResidue.Atoms.Values)
            {
                selectedResidueAtomNames.Add(atom.Name);
            }

            foreach (Residue residue in residues)
            {
                HashSet <string> residueAtomNames = new HashSet <string>();
                foreach (Atom atom in residue.Atoms.Values)
                {
                    residueAtomNames.Add(atom.Name);
                }

                if (!residueAtomNames.SetEquals(selectedResidueAtomNames))
                {
                    atomNamesAllTheSame = false;
                }
            }

            List <string> atomNames = null;

            if (atomNamesAllTheSame)
            {
                atomNames = new List <string>();

                foreach (Atom atom in selectedResidue.Atoms.Values)
                {
                    atomNames.Add(atom.Name);
                }
            }

            bool residueSettingsAllTheSame = true;

            foreach (Residue residue in residues)
            {
                if (!moleculeRenderSettings.CustomResidueRenderSettings.ContainsKey(residue.ID) ||
                    !moleculeRenderSettings.CustomResidueRenderSettings[residue.ID].Equals(moleculeRenderSettings.CustomResidueRenderSettings[selectedResidue.ID]))
                {
                    residueSettingsAllTheSame = false;
                    break;
                }
            }
            ResidueRenderSettings panelResidueSettings = new ResidueRenderSettings();

            if (residueSettingsAllTheSame)
            {
                if (moleculeRenderSettings.CustomResidueRenderSettings.ContainsKey(selectedResidue.ID))
                {
                    panelResidueSettings = moleculeRenderSettings.CustomResidueRenderSettings[selectedResidue.ID].Clone();
                }
            }

            customSettingsPanel.Initialise(residueIDs, null, atomNames, ResidueUpdateType.All, panelResidueSettings, saveCustomResidueSettings, onCloseCustomResidueSettings);
        }