Esempio n. 1
0
        /// <summary>
        /// A variant which returns an OBMol based on host.
        /// </summary>
        /// <param name="host"></param>
        /// <returns></returns>
        public static OBMol designgraphtomol(designGraph host)
        {
            var mol = new OBMol();
            Dictionary <string, int> nodeatomlookup = new Dictionary <string, int>(); //dictionary for looking up which nodes go to which atoms by their names
            int i = 0;

            foreach (node n in host.nodes)
            {
                OBAtom atom = new OBAtom();
                double x    = scale * n.X;
                double y    = scale * n.Y;
                double z    = scale * n.Z; //set coordinates of atom
                atom.SetVector(x, y, z);
                n.localLabels.Sort();
                foreach (string label in n.localLabels)
                {
                    if (label.Length < 4)
                    {
                        if (label != "sp2" && label != "sp3")       ///so openbabel gets less crap
                        {
                            atom.SetAtomicNum(elementTabel[label]); //set atomic number
                            break;
                        }
                    }
                }
                i++;

                nodeatomlookup.Add(n.name, i); //add the atom to dictionary and mol
                mol.AddAtom(atom);
            }
            foreach (arc a in host.arcs)
            {
                int  bondorder = 0;
                bool hassingle = a.localLabels.Contains("s");
                bool hasdouble = a.localLabels.Contains("d");
                bool hastriple = a.localLabels.Contains("t"); //bonds will probably not be anything other than single, double, or triple
                //although we could have dative bonds
                if (hassingle ^ hasdouble ^ hastriple)        //we do not want more than one bond type
                {
                    if (hassingle)
                    {
                        bondorder = 1;
                    }
                    if (hasdouble)
                    {
                        bondorder = 2;
                    }
                    if (hastriple)
                    {
                        bondorder = 3;
                    }
                }
                mol.AddBond(nodeatomlookup[a.To.name], nodeatomlookup[a.From.name], bondorder);
            }
            return(mol);
        }
        public static OBMol addmol2(OBMol source, OBMol dest)
        {
            //combines two OBMols into one molecule
            // this is designed to do the same thing as the += operator in mol.cpp, in other words this implements functionality present in openbabel, but not in the C# api
            //modifies atom and bond indices in the process
            //dest.BeginModify();
            uint prevatms = dest.NumAtoms();
            uint nextatms = source.NumAtoms();
            uint acount   = prevatms;
            uint bcount   = dest.NumBonds();

            // First, handle atoms and bonds
            foreach (OBAtom atom in source.Atoms())
            {
                atom.SetId(acount); ////Need to remove ID which relates to source mol rather than this mol// But in the C++ it had a NoId thing I couldn't figure out
                dest.AddAtom(atom);
                acount++;
                //var foooo = dest.Atoms ();
            }
            //writeatominfotoscreen (dest);
            foreach (OBBond bond in source.Bonds())
            {
                bond.SetId(bcount);
                OBBond b = new OBBond();
                //b.SetBegin(dest.GetAtom((int)(bond.GetBeginAtomIdx() + prevatms)));
                //b.SetEnd(dest.GetAtom((int)(bond.GetEndAtomIdx() + prevatms)));
                b.Set(0, dest.GetAtom((int)(bond.GetBeginAtomIdx() + prevatms)),
                      dest.GetAtom((int)(bond.GetEndAtomIdx() + prevatms)), (int)bond.GetBO(), (int)bond.GetFlags());
                if (bond.HasData("trueBO"))
                {
                    b.CloneData(bond.GetData("trueBO")); //clone true bond order so we can eventually do MOF-UFF
                }
                dest.AddBond(b);
                //dest.AddBond( (int)bond.GetBO(), (int)bond.GetFlags());

                bcount++;
            }

            //don't really understand what they mean by residues
            //I think it's an amino acid or nucleotide so you can build up proteins and stuff?
            //don't think I'm going to use them either, but it might be useful


            //dest.EndModify(); this tends to mangle up all the atom ids, which is bad

            return(dest);
        }
Esempio n. 3
0
        public static void trajectoryFrameToCIF(string frame, string outFile, List <int> esequence,
                                                OBConversion obconv)
        {
            //converts a frame from the above function into a CIF file
            //lammps trajectory files have unit cell dimension data for every frame
            //requires an element sequence for identifying element type from lammps atom types
            OBUnitCell  uc  = new OBUnitCell();
            OBMol       mol = new OBMol();
            OBMatrix3x3 mat = new OBMatrix3x3();

            using (StringReader reader = new StringReader(frame)) {
                string line        = "";
                bool   foundMatrix = false;

                do
                {
                    line = reader.ReadLine();
                    if (line.Contains("BOX BOUNDS"))
                    {
                        //data from lammps is in this format
                        //ITEM: BOX BOUNDS xy xz yz
                        //xlo_bound xhi_bound xy
                        //ylo_bound yhi_bound xz
                        //zlo_bound zhi_bound yz
                        string row0 = reader.ReadLine();

                        double[] row0d = OBFunctions.spacedStringToDoubleArray(row0);
                        double   xlo   = row0d[0];

                        double xhi = row0d[1];
                        double xy  = row0d[2];

                        string   row1  = reader.ReadLine();
                        double[] row1d = OBFunctions.spacedStringToDoubleArray(row1);
                        double   ylo   = row1d[0];
                        double   yhi   = row1d[1];
                        double   xz    = row1d[2];


                        string   row2  = reader.ReadLine();
                        double[] row2d = OBFunctions.spacedStringToDoubleArray(row2);
                        double   zlo   = row2d[0];
                        double   zhi   = row2d[1];
                        double   yz    = row2d[2];


                        //adjust box bounds, taken from VMD's source for reading lammps files

                        double xdelta = Math.Min(0, xy);
                        xdelta = Math.Min(xdelta, xz);
                        xdelta = Math.Min(xdelta, xy + xz);
                        xlo    = xlo - xdelta;
                        xdelta = Math.Max(0, xy);
                        xdelta = Math.Max(xdelta, xz);
                        xdelta = Math.Max(xdelta, xy + xz);
                        xhi    = xhi - xdelta;
                        ylo    = ylo - Math.Min(0, yz);
                        yhi    = yhi - Math.Max(0, yz);
                        OBVector3 A = new OBVector3(xhi - xlo, 0, 0);
                        OBVector3 B = new OBVector3(xy, yhi - ylo, 0);
                        OBVector3 C = new OBVector3(xz, yz, zhi - zlo);
                        //OBVector3 A = new OBVector3 (xhi-xlo, xy, xz);
                        //OBVector3 B = new OBVector3 (0, yhi-ylo, yz);
                        //OBVector3 C= new OBVector3 (0, 0, zhi-zlo);
                        mat = new OBMatrix3x3(A, B, C);
                        uc.SetData(A, B, C);

                        foundMatrix = true;
                    }
                } while (!foundMatrix);
                //uc.SetData (mat);
                bool foundAtoms = false;
                do
                {
                    line = reader.ReadLine();
                    if (line.Contains("ITEM: ATOMS"))
                    {
                        foundAtoms = true;
                    }
                } while (!foundAtoms);

                while ((line = reader.ReadLine()) != null)
                {
                    string[] splitline  = line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
                    int      lammpstype = Convert.ToInt32(splitline[1]) - 1;
                    int      atype      = esequence[lammpstype];
                    OBAtom   a          = new OBAtom();
                    a.SetAtomicNum(atype);
                    //position in fraction coordinates
                    OBVector3 fvec = new OBVector3(Convert.ToDouble(splitline[3]), Convert.ToDouble(splitline[4]),
                                                   Convert.ToDouble(splitline[5]));
                    //OBVector3 fvec = new OBVector3 (Convert.ToDouble (splitline [2]), Convert.ToDouble (splitline [3]), Convert.ToDouble (splitline [4]));
                    //convert to cartesian.
                    OBVector3 cvec = uc.FractionalToCartesian(fvec);
                    a.SetVector(cvec);
                    mol.AddAtom(a);
                }

                mol.CloneData(uc);
                obconv.SetOutFormat("cif");
                obconv.AddOption("b");
                obconv.WriteFile(mol, outFile);
            }
        }
        public static bool designgraphtomol(designGraph host, ref OBMol mol)
        {
            OBElementTable periodictable = new OBElementTable();

            //OBMol mol = new OBMol();
            Dictionary <string, int>
            nodeatomlookup =
                new Dictionary <string, int>();    //dictionary for looking up which nodes go to which atoms by their names
            int i = 0;

            foreach (node n in host.nodes)
            {
                OBAtom atom = new OBAtom();

                double x = scale * n.X;
                double y = scale * n.Y;
                double z = scale * n.Z; //set coordinates of atom
                atom.SetVector(x, y, z);
                int anum = 0;
                n.localLabels.Sort();
                foreach (string label in n.localLabels)
                {
                    if (label.Length < 4)
                    {
                        if (label != "sp2" && label != "sp3") ///so openbabel gets less crap
                        {
                            anum = periodictable.GetAtomicNum(label);
                            if (anum > 0)
                            {
                                break;
                            }
                        }
                    }
                }
                i++;
                atom.SetAtomicNum(anum);       //set atomic number
                nodeatomlookup.Add(n.name, i); //add the atom to dictionary and mol
                mol.AddAtom(atom);
            }
            foreach (arc a in host.arcs)
            {
                OBBond bond      = new OBBond();
                int    bondorder = 0;
                bool   hassingle = a.localLabels.Contains("s");
                bool   hasdouble = a.localLabels.Contains("d");
                bool   hastriple =
                    a.localLabels
                    .Contains("t");                    //bonds will probably not be anything other than single, double, or triple
                //although we could have dative bonds
                if (hassingle ^ hasdouble ^ hastriple) //we do not want more than one bond type
                {
                    if (hassingle)
                    {
                        bondorder = 1;
                    }
                    if (hasdouble)
                    {
                        bondorder = 2;
                    }
                    if (hastriple)
                    {
                        bondorder = 3;
                    }
                }
                mol.AddBond(nodeatomlookup[a.To.name], nodeatomlookup[a.From.name], bondorder);
//                OBAtom begin = nodeatomlookup[a.From.name];
//                OBAtom end = nodeatomlookup[a.To.name];
//                bond.SetBegin(begin);
//                bond.SetEnd(end);
//                bond.SetBondOrder(bondorder);
//                mol.AddBond(bond);
//                mol.Bonds();
            }

            //mol.FindRingAtomsAndBonds();

            return(true);
        }
        public static OBMol addmol(OBMol source, OBMol dest)
        {
            //combines two OBMols into one molecule
            // this is designed to do the same thing as the += operator in mol.cpp, in other words this implements functionality present in openbabel, but not in the C# api
            dest.BeginModify();
            uint prevatms = dest.NumAtoms();
            uint nextatms = source.NumAtoms();

            // First, handle atoms and bonds
            foreach (OBAtom atom in source.Atoms())
            {
                atom.SetId(0); ////Need to remove ID which relates to source mol rather than this mol// But in the C++ it had a NoId thing I couldn't figure out
                dest.AddAtom(atom);
                //var foooo = dest.Atoms ();
            }
            //writeatominfotoscreen (dest);
            foreach (OBBond bond in source.Bonds())
            {
                bond.SetId(0);

                dest.AddBond((int)(bond.GetBeginAtomIdx() + prevatms), (int)(bond.GetEndAtomIdx() + prevatms),
                             (int)bond.GetBO(), (int)bond.GetFlags());
            }

            //don't really understand what they mean by residues
            //I think it's an amino acid or nucleotide so you can build up proteins and stuff?
            //don't think I'm going to use them either, but it might be useful

            foreach (OBResidue residue in source.Residues())
            {
                OBResidue newres = new OBResidue();
                dest.AddResidue(newres);

                OBResidueAtomIter ai = new OBResidueAtomIter(residue);
                //dammit why didn't they implement a residue.atoms sort of thing? I don't want to play with enumerators
                ////#define FOR_ATOMS_OF_RESIDUE(a,r) for( OBResidueAtomIter a(r); a; ++a )
                while (ai.MoveNext())
                {
                    OBAtom resatom = new OBAtom();
                    resatom = ai.Current;
                    // This is the equivalent atom in our combined molecule
                    OBAtom atom = dest.GetAtom((int)(resatom.GetIdx() + prevatms));
                    // So we add this to the last-added residue
                    // (i.e., what we just copied)
                    //[dest.NumResidues () - 1]

                    var res = dest.Residues().GetEnumerator();
                    while (!res.MoveNext())
                    {
                    }                           //move to the last residue
                    res.Current.AddAtom(atom);

                    //var item = dest.Cast<RMSRequestProcessor.RMSMedia> ().ElementAt (1);
                }


                //for(OBAtom resatom in )
            }
            dest.EndModify();

            return(dest);
        }