//*** Membrane Ingredients ****//



    public void AddMembrane(string filePath, Vector3 position, Quaternion rotation)
    {
        var groupName = "membrane";
        var groupPath = "root.envelope." + groupName;

        var nameInner = "inner_membrane";
        var nameOuter = "outer_membrane";

        var pathInner = groupPath + "." + nameInner;
        var pathOuter = groupPath + "." + nameOuter;


        RemoveElementFromHierarchy("membrane");
        RemoveIngredientGroupFromName("membrane");
        RemoveIngredientGroupFromName("membrane");

        _ingredientNames.Clear();
        LipidIngredientNames.Clear();
        RemoveIngredientGroup(groupPath);
        RemoveElementFromHierarchy(groupPath);

        CPUBuffers.Get.LipidAtomPositions.Clear();
        CPUBuffers.Get.LipidInstanceInfos.Clear();
        CPUBuffers.Get.LipidInstanceInfos2.Clear();
        CPUBuffers.Get.LipidInstancePositions.Clear();

        //*****//

        AddIngredientToHierarchy(pathInner);
        AddIngredientToHierarchy(pathOuter);
        LipidIngredientNames.Add(pathInner);
        LipidIngredientNames.Add(pathOuter);

        var ingredientIdInner = AllIngredientNames.IndexOf(pathInner);
        var ingredientIdOuter = AllIngredientNames.IndexOf(pathOuter);

        var envelopeSurfaceGroup  = GetIngredientGroup("root.envelope.surface");
        var envelopeMembraneGroup = new IngredientGroup();

        envelopeMembraneGroup.Ingredients    = new List <Ingredient>();
        envelopeMembraneGroup.compartment_id = envelopeSurfaceGroup.compartment_id;
        envelopeMembraneGroup.unique_id      = IngredientGroups.Count;
        envelopeMembraneGroup._groupType     = -1;
        envelopeMembraneGroup.name           = groupName;
        envelopeMembraneGroup.path           = groupPath;
        envelopeMembraneGroup.NumIngredients = 2;

        var innerMembraneIngredient = new Ingredient();

        innerMembraneIngredient.nbMol              = 1;
        innerMembraneIngredient._nbChains          = 1;
        innerMembraneIngredient._ingredientGroupId = envelopeMembraneGroup.unique_id;
        innerMembraneIngredient._ingredientId      = ingredientIdInner;
        innerMembraneIngredient.name = nameInner;
        innerMembraneIngredient.path = pathInner;

        var outerMembraneIngredient = new Ingredient();

        outerMembraneIngredient.nbMol               = 1;
        outerMembraneIngredient.nbChains            = 1;
        outerMembraneIngredient.ingredient_group_id = envelopeMembraneGroup.unique_id;
        outerMembraneIngredient._ingredientId       = ingredientIdOuter;
        outerMembraneIngredient.name = nameOuter;
        outerMembraneIngredient.path = pathOuter;

        envelopeMembraneGroup.Ingredients.Add(innerMembraneIngredient);
        envelopeMembraneGroup.Ingredients.Add(outerMembraneIngredient);
        IngredientGroups.Add(envelopeMembraneGroup);

        ColorManager.Get.InitColors();

        //int a = 0;

        var currentLipidAtoms = new List <Vector4>();
        var membraneData      = MyUtility.ReadBytesAsFloats(filePath);

        var step            = 5;
        var dataIndex       = 0;
        var lipidAtomStart  = 0;
        var previousLipidId = -1;

        while (true)
        {
            var flushCurrentBatch  = false;
            var breakAfterFlushing = false;

            if (dataIndex >= membraneData.Count())
            {
                flushCurrentBatch  = true;
                breakAfterFlushing = true;
            }
            else
            {
                var lipidId = (int)membraneData[dataIndex + 4];
                if (previousLipidId < 0)
                {
                    previousLipidId = lipidId;
                }
                if (lipidId != previousLipidId)
                {
                    flushCurrentBatch = true;
                    previousLipidId   = lipidId;
                }
            }

            if (flushCurrentBatch)
            {
                var bounds = AtomHelper.ComputeBounds(currentLipidAtoms);
                var center = new Vector4(bounds.center.x, bounds.center.y, bounds.center.z, 0);
                for (var j = 0; j < currentLipidAtoms.Count; j++)
                {
                    currentLipidAtoms[j] -= center;
                }

                var innerMembrane = Vector3.Magnitude(bounds.center) < 727;

                Vector4 batchPosition = position + bounds.center;
                batchPosition.w = Vector3.Magnitude(bounds.extents);

                CPUBuffers.Get.LipidInstancePositions.Add(batchPosition);
                CPUBuffers.Get.LipidInstanceInfos.Add(new Vector4(innerMembrane ? ingredientIdInner : ingredientIdOuter, lipidAtomStart, currentLipidAtoms.Count));
                CPUBuffers.Get.LipidInstanceInfos2.Add(new Vector4((int)InstanceState.Normal, 0, 0));

                lipidAtomStart += currentLipidAtoms.Count;
                CPUBuffers.Get.LipidAtomPositions.AddRange(currentLipidAtoms);
                currentLipidAtoms.Clear();

                if (breakAfterFlushing)
                {
                    break;
                }
            }

            var currentAtom = new Vector4(membraneData[dataIndex], membraneData[dataIndex + 1], membraneData[dataIndex + 2], membraneData[dataIndex + 3]);
            currentLipidAtoms.Add(currentAtom);
            dataIndex += step;
        }
    }
示例#2
0
        public static void AddProteinIngredient(ref Ingredient ingredient)
        {
            Debug.Log("CellPackLoader: AddProteinIngredient");
            var path    = ingredient.path;
            var biomt   = ingredient.source.biomt;
            var pdbName = ingredient.source.pdb.Replace(".pdb", "");

            Debug.Log("*****");
            Debug.Log("Ingredient: " + ingredient.ingredient_id);
            Debug.Log("Name: " + ingredient.path);
            Debug.Log("Pdb id: " + ingredient.source.pdb);


            // ***** Load atoms *****//

            var atoms = GetAtoms(ingredient);

            var numChains = AtomHelper.GetNumChains(atoms);

            Debug.Log("Num chains: " + numChains);

            ingredient.nbChains = numChains;

            var isFromCustomStructureFile = AtomHelper.IsFromCustomStructureFile(atoms);

            if (isFromCustomStructureFile)
            {
                Debug.Log("From custom structure file");
            }

            var alphaCarbonsOnly = AtomHelper.ContainsCarbonAlphaOnly(atoms);

            if (alphaCarbonsOnly)
            {
                AtomHelper.OverwriteRadii(ref atoms, 3);
            }
            if (alphaCarbonsOnly)
            {
                Debug.Log("Alpha carbons only");
            }


            // ***** Compute lod proxies *****//

            var lodProxies = new List <List <Vector4> >();

            // Define cluster decimation levels
            var clusterLevelFactors = new List <float>()
            {
                0.15f, 0.10f, 0.05f
            };

            if (alphaCarbonsOnly || isFromCustomStructureFile)
            {
                clusterLevelFactors = new List <float>()
                {
                    1, 1, 1
                }
            }
            ;

            if (!biomt)
            {
                // Center atoms before computing the lod proxies
                AtomHelper.CenterAtoms(ref atoms);

                var atomSpheres = AtomHelper.GetAtomSpheres(atoms);
                lodProxies = AtomHelper.ComputeLodProxies(atomSpheres, clusterLevelFactors);
            }
            else
            {
                var atomSpheres     = AtomHelper.GetAtomSpheres(atoms);
                var biomtTransforms = PdbLoader.LoadBiomtTransforms(pdbName);

                // Compute centered lod proxies
                lodProxies = AtomHelper.ComputeLodProxiesBiomt(atomSpheres, biomtTransforms, clusterLevelFactors);

                // Assemble the atom set from biomt transforms and center
                atoms = AtomHelper.BuildBiomt(atoms, biomtTransforms);

                var centerPosition = AtomHelper.ComputeBounds(atoms).center;

                // Center atoms
                AtomHelper.OffsetAtoms(ref atoms, centerPosition);

                // Center proxies
                for (int i = 0; i < lodProxies.Count; i++)
                {
                    var t = lodProxies[i];
                    AtomHelper.OffsetSpheres(ref t, centerPosition);
                }
            }

            SceneManager.Get.AddProteinIngredientToCPUBuffer(ingredient, atoms, lodProxies);
            Debug.Log("Ingredient added succesfully");
        }
示例#3
0
    public static void AddProteinIngredient(JSONNode ingredientDictionary, params string[] pathElements)
    {
        var name    = ingredientDictionary["name"];
        var path    = MyUtility.GetUrlPath(pathElements.ToList(), name);
        var biomt   = (bool)ingredientDictionary["source"]["biomt"].AsBool;
        var center  = (bool)ingredientDictionary["source"]["transform"]["center"].AsBool;
        var pdbName = ingredientDictionary["source"]["pdb"].Value.Replace(".pdb", "");

        List <Vector4>   atomSpheres;
        List <Matrix4x4> biomtTransforms     = new List <Matrix4x4>();
        Vector3          biomtCenter         = Vector3.zero;
        bool             containsACarbonOnly = false;
        bool             oneLOD = false;

        if ((pdbName == "") || (pdbName == "null") || (pdbName == "None") || pdbName.StartsWith("EMDB"))
        {
            return;

            ////check for sphere file//information in the file. if not in file is it on disk ? on repo ?
            ////possibly read the actuall recipe definition ?
            ////check if bin exist
            //var filePath = PdbLoader.DefaultPdbDirectory + ingredientDictionary["name"] + ".bin";
            //if (File.Exists(filePath)){
            //	atomSpheres = new List<Vector4>();
            //	var points = MyUtility.ReadBytesAsFloats(filePath);
            //	for (var i = 0; i < points.Length; i += 4) {
            //		var currentAtom = new Vector4 (points [i], points [i + 1], points [i + 2], points [i + 3]);
            //		atomSpheres.Add (currentAtom);
            //	}
            //	containsACarbonOnly = true;
            //	oneLOD = true;
            //}
            //else if (ingredientDictionary ["radii"] != null) {
            //	atomSpheres = MyUtility.gatherSphereTree(ingredientDictionary)[0];
            //	Debug.Log ("nbprim "+atomSpheres.Count.ToString());//one sphere
            //	oneLOD = true;
            //} else {
            //	float radius = 30.0f;
            //	if (name.Contains("dLDL"))
            //		radius = 108.08f;//or use the mesh? or make sphere from the mesh ?
            //	if (name.Contains("iLDL"))
            //		radius = 105.41f;//or use the mesh? or make sphere from the mesh ?
            //	atomSpheres = new List<Vector4>();
            //	atomSpheres.Add (new Vector4(0,0,0,radius));
            //	//No LOD since only one sphere
            //	oneLOD = true;
            //}
        }
        else
        {
            //if (pdbName.StartsWith("EMDB")) return;
            //if (pdbName.Contains("1PI7_1vpu_biounit")) return;//??
            // Load atom set from pdb file
            var atomSet = PdbLoader.LoadAtomSet(pdbName);

            // If the set is empty return
            if (atomSet.Count == 0)
            {
                return;
            }

            atomSpheres         = AtomHelper.GetAtomSpheres(atomSet);
            containsACarbonOnly = AtomHelper.ContainsCarbonAlphaOnly(atomSet);
        }
        var centerPosition = AtomHelper.ComputeBounds(atomSpheres).center;

        // Center atoms
        AtomHelper.OffsetSpheres(ref atomSpheres, centerPosition);

        // Compute bounds
        var bounds = AtomHelper.ComputeBounds(atomSpheres);

        biomtTransforms = biomt ? PdbLoader.LoadBiomtTransforms(pdbName) : new List <Matrix4x4>();
        biomtCenter     = AtomHelper.GetBiomtCenter(biomtTransforms, centerPosition);

        //if (!pdbName.Contains("1TWT_1TWV")) return;

        // Disable biomts until loading problem is resolved
        //if (!biomt) return;


        // Get ingredient color
        // TODO: Move color palette code into dedicated function
        var cid = ColorPaletteGenerator.GetRandomUniqFromSample(current_color, usedColors[current_color]);

        usedColors[current_color].Add(cid);
        var sample = ColorPaletteGenerator.colorSamples[cid];
        var c      = ColorPaletteGenerator.lab2rgb(sample) / 255.0f;
        var color  = new Color(c[0], c[1], c[2]);

        // Define cluster decimation levels
        var clusterLevels = (containsACarbonOnly)
                ? new List <float>()
        {
            0.85f, 0.25f, 0.1f
        }
            : new List <float>()
        {
            0.15f, 0.10f, 0.05f
        };

        if (oneLOD)
        {
            clusterLevels = new List <float> ()
            {
                1, 1, 1
            }
        }
        ;
        // Add ingredient type
        //SceneManager.Instance.AddIngredient(name, bounds, atomSpheres, color);

        SceneManager.Get.AddProteinIngredient(path, bounds, atomSpheres, color, clusterLevels, oneLOD);
        int instanceCount = 0;

        for (int k = 0; k < ingredientDictionary["results"].Count; k++)
        {
            var p = ingredientDictionary["results"][k][0];
            var r = ingredientDictionary["results"][k][1];

            var position = new Vector3(-p[0].AsFloat, p[1].AsFloat, p[2].AsFloat);
            var rotation = new Quaternion(r[0].AsFloat, r[1].AsFloat, r[2].AsFloat, r[3].AsFloat);

            var mat   = MyUtility.quaternion_matrix(rotation);
            var euler = MyUtility.euler_from_matrix(mat);
            rotation = MyUtility.MayaRotationToUnity(euler);

            if (!biomt)
            {
                // Find centered position
                if (!center)
                {
                    position += MyUtility.QuaternionTransform(rotation, centerPosition);
                }
                SceneManager.Get.AddProteinInstance(path, position, rotation);
                instanceCount++;
            }
            else
            {
                foreach (var transform in biomtTransforms)
                {
                    var biomteuler = MyUtility.euler_from_matrix(transform);
                    var rotBiomt   = MyUtility.MayaRotationToUnity(biomteuler);
                    var offset     = MyUtility.QuaternionTransform(rotBiomt, centerPosition);               //Helper.RotationMatrixToQuaternion(matBiomt), GetCenter());
                    var posBiomt   = new Vector3(-transform.m03, transform.m13, transform.m23) + offset - biomtCenter;

                    var biomtOffset      = MyUtility.RotationMatrixToQuaternion(transform) * centerPosition;
                    var biomtInstanceRot = rotation * rotBiomt;                    //Helper.RotationMatrixToQuaternion(transform);
                    var biomtInstancePos = rotation * posBiomt + position;

                    SceneManager.Get.AddProteinInstance(path, biomtInstancePos, biomtInstanceRot);
                    instanceCount++;
                }
            }
        }

        SceneManager.Get.AddIngredientProperties(atomSpheres.Count, instanceCount);

        Debug.Log("*****");
        Debug.Log("Added ingredient: " + path);

        //if (isCarbonAlphaOnly) Debug.Log("Alpha-carbons only");
        //Debug.Log("Pdb name: " + pdbName + " *** " + "Num atoms: " + atomSet.Count + " *** " + "Num instances: " + instanceCount + " *** " + "Total atom count: " + atomSet.Count * instanceCount);
    }
示例#4
0
    public static void AddProteinIngredient(JSONNode ingredientDictionary, string prefix)
    {
        var name    = prefix + "_" + ingredientDictionary["name"];
        var biomt   = (bool)ingredientDictionary["source"]["biomt"].AsBool;
        var center  = (bool)ingredientDictionary["source"]["transform"]["center"].AsBool;
        var pdbName = ingredientDictionary["source"]["pdb"].Value.Replace(".pdb", "");

        if (pdbName == "")
        {
            return;
        }
        if (pdbName == "null")
        {
            return;
        }
        if (pdbName == "None")
        {
            return;
        }
        if (pdbName.StartsWith("EMDB"))
        {
            return;
        }
        if (pdbName.Contains("1PI7_1vpu_biounit"))
        {
            return;
        }

        // Disable biomts until loading problem is resolved
        if (biomt)
        {
            return;
        }

        // Load atom set from pdb file
        var atomSet = PdbLoader.LoadAtomSet(pdbName);

        // If the set is empty return
        if (atomSet.Count == 0)
        {
            return;
        }

        var atomSpheres    = AtomHelper.GetAtomSpheres(atomSet);
        var centerPosition = AtomHelper.ComputeBounds(atomSpheres).center;

        var biomtTransforms = biomt ? PdbLoader.LoadBiomtTransforms(pdbName) : new List <Matrix4x4>();
        var biomtCenter     = AtomHelper.GetBiomtCenter(biomtTransforms);

        var containsACarbonOnly = AtomHelper.ContainsACarbonOnly(atomSet);

        // Center atoms
        AtomHelper.OffsetSpheres(ref atomSpheres, centerPosition);

        // Compute bounds
        var bounds = AtomHelper.ComputeBounds(atomSpheres);

        // Get ingredient color
        // TODO: Move color palette code into dedicated function
        var cid = ColorPaletteGenerator.GetRandomUniqFromSample(current_color, usedColors[current_color]);

        usedColors[current_color].Add(cid);
        var sample = ColorPaletteGenerator.colorSamples[cid];
        var c      = ColorPaletteGenerator.lab2rgb(sample) / 255.0f;
        var color  = new Color(c[0], c[1], c[2]);

        // Define cluster decimation levels
        var clusterLevels = (containsACarbonOnly)
            ? new List <float>()
        {
            0.85f, 0.25f, 0.1f
        }
            : new List <float>()
        {
            0.10f, 0.05f, 0.01f
        };

        // Add ingredient type
        //SceneManager.Instance.AddIngredient(name, bounds, atomSpheres, color);
        SceneManager.Instance.AddIngredient(name, bounds, atomSpheres, color, clusterLevels);

        int instanceCount = 0;

        for (int k = 0; k < ingredientDictionary["results"].Count; k++)
        {
            var p = ingredientDictionary["results"][k][0];
            var r = ingredientDictionary["results"][k][1];

            var position = new Vector3(-p[0].AsFloat, p[1].AsFloat, p[2].AsFloat);
            var rotation = new Quaternion(r[0].AsFloat, r[1].AsFloat, r[2].AsFloat, r[3].AsFloat);

            var mat   = Helper.quaternion_matrix(rotation);
            var euler = Helper.euler_from_matrix(mat);
            rotation = Helper.MayaRotationToUnity(euler);

            if (!biomt)
            {
                // Find centered position
                if (!center)
                {
                    position += Helper.QuaternionTransform(rotation, centerPosition);
                }
                SceneManager.Instance.AddIngredientInstance(name, position, rotation);
                instanceCount++;
            }
            else
            {
                foreach (var transform in biomtTransforms)
                {
                    var biomtOffset      = Helper.RotationMatrixToQuaternion(transform) * centerPosition;
                    var biomtInstanceRot = rotation * Helper.RotationMatrixToQuaternion(transform);
                    var biomtInstancePos = rotation * (new Vector3(transform.m03, transform.m13, transform.m23) + biomtOffset) + position - biomtCenter;

                    SceneManager.Instance.AddIngredientInstance(name, biomtInstancePos, biomtInstanceRot);
                    instanceCount++;
                }
            }
        }

        Debug.Log("*****");
        Debug.Log("Added ingredient: " + name);
        if (containsACarbonOnly)
        {
            Debug.Log("Alpha-carbons only");
        }
        Debug.Log("Pdb name: " + pdbName + " *** " + "Num atoms: " + atomSpheres.Count + " *** " + "Num instances: " + instanceCount + " *** " + "Total atom count: " + atomSpheres.Count * instanceCount);
    }
示例#5
0
    //*** Membrane Ingredients ****//

    public void AddMembrane(string filePath, Vector3 position, Quaternion rotation)
    {
        var pathInner = "root.membrane.inner_membrane";
        var pathOuter = "root.membrane.outer_membrane";

        AddIngredientToHierarchy(pathInner);
        AddIngredientToHierarchy(pathOuter);

        LipidIngredientNames.Clear();
        LipidIngredientNames.Add(pathInner);
        LipidIngredientNames.Add(pathOuter);

        LipidAtomPositions.Clear();
        LipidInstanceInfos.Clear();
        LipidInstancePositions.Clear();

        var currentLipidAtoms = new List <Vector4>();
        var membraneData      = MyUtility.ReadBytesAsFloats(filePath);

        var ingredientIdInner = AllIngredientNames.IndexOf(pathInner);
        var ingredientIdOuter = AllIngredientNames.IndexOf(pathOuter);

        var step            = 5;
        var dataIndex       = 0;
        var lipidAtomStart  = 0;
        var previousLipidId = -1;

        while (true)
        {
            var flushCurrentBatch  = false;
            var breakAfterFlushing = false;

            if (dataIndex >= membraneData.Count())
            {
                flushCurrentBatch  = true;
                breakAfterFlushing = true;
            }
            else
            {
                var lipidId = (int)membraneData[dataIndex + 4];
                if (previousLipidId < 0)
                {
                    previousLipidId = lipidId;
                }
                if (lipidId != previousLipidId)
                {
                    flushCurrentBatch = true;
                    previousLipidId   = lipidId;
                }
            }

            if (flushCurrentBatch)
            {
                var bounds = AtomHelper.ComputeBounds(currentLipidAtoms);
                var center = new Vector4(bounds.center.x, bounds.center.y, bounds.center.z, 0);
                for (var j = 0; j < currentLipidAtoms.Count; j++)
                {
                    currentLipidAtoms[j] -= center;
                }

                var innerMembrane = Vector3.Magnitude(bounds.center) < 727;

                Vector4 batchPosition = position + bounds.center;
                batchPosition.w = Vector3.Magnitude(bounds.extents);

                LipidInstancePositions.Add(batchPosition);
                LipidInstanceInfos.Add(new Vector4(innerMembrane ? ingredientIdInner : ingredientIdOuter, lipidAtomStart, currentLipidAtoms.Count));

                lipidAtomStart += currentLipidAtoms.Count;
                LipidAtomPositions.AddRange(currentLipidAtoms);
                currentLipidAtoms.Clear();

                if (breakAfterFlushing)
                {
                    break;
                }
            }

            var currentAtom = new Vector4(membraneData[dataIndex], membraneData[dataIndex + 1], membraneData[dataIndex + 2], AtomHelper.AtomRadii[(int)membraneData[dataIndex + 3]]);
            currentLipidAtoms.Add(currentAtom);
            dataIndex += step;
        }

        int a = 0;
    }