Beispiel #1
0
//        public static bool isInCone(OBVector3 x, OBVector3 apex, OBVector3 axis, double aperture )
//        {
//            //got it from here:http://stackoverflow.com/questions/10768142/verify-if-point-is-inside-a-cone-in-3d-space
//            //test if point x is inside an infinite cone defined by apex point with normal vector axis and aperture angle(in radians)
//            double halfAperture=aperture/2;
//            OBVector3 apexToXVect = OBVector3(apex,x);
//            OBVector3.
//            bool insideCone = StarMath.dotProduct(apex,axis)/StarMath.norm2(apexToXVect)/StarMath.norm2(axis) > Math.Cos(aperture);
//            return insideCone;
//        }
        public static bool atomsInCarboxylateCone(OBAtom a, OBAtom carba, OBMol mol)
        {
            //angle should probably not be hardcoded
            double aperture = 120 * Math.PI / 180;

            double[] axis = obvec2dubs(OBVector3.Sub(carba.GetVector(), a.GetVector()));
            //axis = StarMath.divide (axis, StarMath.norm2 (axis));
            double[]    apex    = obvec2dubs(carba.GetVector());
            List <uint> exclude = new List <uint>();

            exclude.Add(carba.GetIdx());
            foreach (OBBond b in carba.Bonds())
            {
                OBAtom other = b.GetNbrAtom(carba);
                if (other != a)
                {
                    exclude.Add(other.GetIdx());
                }
            }
            foreach (OBAtom n in mol.Atoms())
            {
                if (!exclude.Contains(n.GetIdx()))
                {
                    double[] x = obvec2dubs(n.GetVector());
                    //if any point is found to be in the carboxylate's cone, return true
                    if (isInCone(x, apex, axis, aperture))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
        /// <summary>
        /// Calculates Hall-Kier alpha value for a molecule
        /// </summary>
        /// <param name="mol"></param>
        /// <returns></returns>
        public static double HallKierAlpha(this OBMol mol)
        {
            var accum = 0.0;

            foreach (var atom in mol.Atoms())
            {
                uint atomicNumber;
                try
                {
                    atomicNumber = atom.GetAtomicNum();
                }
                catch (Exception)
                {
                    continue;
                }
                double alpha;
                var    hkAlphas = AtomicConstants.GetHallKierAlphas(atomicNumber);
                if (hkAlphas != null)
                {
                    var hyb    = atom.Hybridization() - 2;
                    var length = hkAlphas.Length;
                    alpha = hyb < length ? (hkAlphas[hyb].HasValue ? hkAlphas[hyb].Value : hkAlphas[length - 1].Value) : hkAlphas[length - 1].Value;
                }
                else
                {
                    alpha = AtomicConstants.GetrVdW(atomicNumber) / AtomicConstants.GetrVdW(6) - 1;
                }
                accum += alpha;
            }
            return(accum);
        }
        /// <summary>
        /// Performs a mol copy and adds explicit hydrogens to each heavy atom to the copy
        /// </summary>
        /// <param name="mol"></param>
        /// <returns>Copy of original mol with explicit hydrogens</returns>
        public static OBMol AddExplicitHydrogens(this OBMol mol)
        {
            var newMol = new OBMol(mol);

            foreach (var atom in newMol.Atoms())
            {
                newMol.AddHydrogens(atom);
            }
            return(newMol);
        }
 public static void writeatominfotoscreen(OBMol mol)
 {
     Console.WriteLine("new");
     foreach (OBAtom a in mol.Atoms())
     {
         Console.WriteLine("atomtype: " + a.GetAtomType() + " id " + a.GetIdx() + " index " + a.GetIndex() +
                           " CIdx" + a.GetCIdx() + " title" + a.GetTitle() + " hash" + a.GetHashCode());
         var    ddd    = OpenBabel.OBBuilder.GetNewBondVector(a);
         string foobar = ddd.GetX() + " " + ddd.GetY() + " " + ddd.GetZ();
         Console.WriteLine(foobar);
     }
     Console.WriteLine("");
 }
        public static OBMol merge_atoms_at_same_location(OBMol mol)
        {
            double radius = 0.1;
            //when we make a unit_cell by copy and pasting base cases we end up with atoms at the same location that need to be merged
            //assumes only two atoms overlap at a time
            Dictionary <int, int> tomerge = new Dictionary <int, int>();

            foreach (OBAtom a in mol.Atoms())
            {
                if (!tomerge.ContainsKey((int)a.GetIdx()))
                {
                    foreach (OBAtom b in mol.Atoms())
                    {
                        if (a.GetIdx() == b.GetIdx())
                        {
                            continue;
                        }
                        else
                        {
                            double len =
                                Math.Round(StarMath.norm2(obvec2dubs(OBVector3.Sub(a.GetVector(), b.GetVector()))),
                                           7); //gets length
                            if (len <= radius)
                            {
                                tomerge.Add((int)b.GetIdx(), (int)a.GetIdx());
                            }
                        }
                    }
                }
            }
            int atomsdeleted = 0;

            foreach (int key in tomerge.Keys)
            {
                mol = molecule_merge(mol, mol.GetAtom(key - atomsdeleted), mol.GetAtom(tomerge[key] - atomsdeleted));
                atomsdeleted++;
            }
            return(mol);
        }
Beispiel #6
0
        public static OBVector3 mol_com(OBMol mol)
        {
            //center of mass of molecule
            OBVector3 sum  = OBVector3.VZero;
            double    molw = 0;

            foreach (OBAtom a in mol.Atoms())
            {
                double m = a.GetAtomicMass();
                sum  = OBVector3.Add(sum, OBVector3.Mul(m, a.GetVector()));
                molw = molw + m;
            }

            return(OBVector3.Mul(1 / molw, sum));
        }
        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);
        }
Beispiel #8
0
        public static double greatest_radial_distance(OBMol mol, OBAtom carbon1, OBAtom a1)
        {
            OBVector3 v2   = carbon1.GetVector();
            OBVector3 v3   = a1.GetVector();
            double    maxd = 0;

            foreach (OBAtom a in mol.Atoms())
            {
                OBVector3 v1 = a.GetVector();
                double    d  = openbabel_csharp.Point2Line(v1, v2, v3);
                if (d > maxd)
                {
                    maxd = d;
                }
            }
            return(maxd);
        }
 public static OBMol connect_within_radius(OBMol mol, OBAtom n, double radius)
 {
     foreach (OBAtom a in mol.Atoms())
     {
         if (a.GetIdx() == n.GetIdx())
         {
             continue;
         }
         else
         {
             double len = Math.Round(StarMath.norm2(obvec2dubs(OBVector3.Sub(a.GetVector(), n.GetVector()))),
                                     7); //gets length
             if (len <= radius)
             {
                 mol.AddBond((int)a.GetIdx(), (int)n.GetIdx(), 1);
             }
         }
     }
     return(mol);
 }
Beispiel #10
0
        public static double greatest_axial_distance(OBMol mol, OBAtom carbon1, OBAtom a1)
        {
            //finds the distance of the most distant atom along the axis defined by the carboxyl defined by carbon 1 and a1
            double[] vec = obvec2dubs(OBVector3.Sub(carbon1.GetVector(), a1.GetVector()));
            vec = StarMath.normalize(vec);
            double maxd = 0;

            foreach (OBAtom a in mol.Atoms())
            {
                if (a == carbon1)
                {
                    continue;
                }
                double d = StarMath.dotProduct(vec,
                                               obvec2dubs(OBVector3.Sub(carbon1.GetVector(),
                                                                        a.GetVector()))); // get pairwise axial distance by taking the scalar projection of the carbon's position to atom A
                if (d > maxd)
                {
                    maxd = d;
                }
            }
            return(maxd);
        }
Beispiel #11
0
        public static double syntheticaccessability(OBMol mol)
        {
            mol.FindChiralCenters();
            VectorpRing ringdata = mol.GetSSSR();
            double      fring    = 0;

            //int rings = (double)ringdata.Count;
            foreach (OBRing r in ringdata)
            {
                var n = r.Size();
                fring = fring + n * 6;
            }
            double ftype    = 0;
            double fconnect = 0;
            int    nchiral  = 0;

            foreach (OBAtom a in mol.Atoms())
            {
                if (a.GetAtomicNum() == 6)
                {
                    ftype = ftype + 3;
                }
                else
                {
                    ftype = ftype + 6;
                }

                int bcount = 0;
                foreach (OBBond b in a.Bonds())
                {
                    bcount++; //get the atom degree by counting bonds
                }
                switch (bcount)
                {
                case 4: {
                    fconnect = fconnect + 24;
                    break;
                }

                case 3: {
                    fconnect = fconnect + 12;
                    break;
                }

                case 2: {
                    fconnect = fconnect + 6;
                    break;
                }

                case 1: {
                    fconnect = fconnect + 3;
                    break;
                }
                }
                if (a.IsChiral())
                {
                    nchiral++;
                }
            }
            //for indole() ftype= 72, fring = 66, fconnect = 129
            //fring
            double fchiral = nchiral * 20;
            double score   = fchiral + fconnect + ftype + fring;

            return(score);
        }
        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);
        }
 /// <summary>
 /// Chi0n atomic valence number connectivity index (order 0)
 /// </summary>
 /// <param name="mol"></param>
 /// <returns></returns>
 public static double Chi0n(this OBMol mol)
 {
     return(mol.Atoms().Where(a => a.ValenceNumber() > 0).Select(a => Math.Sqrt(1 / (double)a.ValenceNumber())).Sum());
 }
        public designGraph moltoDesignGraph(OBMol mol)
        {
            //OBConversion obconv = new OBConversion ();
            designGraph  host   = new designGraph();
            OBConversion obconv = new OBConversion();

            Dictionary <uint, int> atomid2nodeid = new Dictionary <uint, int>();



            //obconv.SetInFormat("smi");
            //var format =obconv.GetInFormat();
            //uint d=mol.NumAtoms();
            OBElementTable ptable = new OBElementTable();

            //mol.FindTorsions ();
            int nodecount = 0;

            //loop through all atoms in the file and make them into nodes in the graph
            foreach (OBAtom a in mol.Atoms())
            {
                node n = new node();
                n.name = "n" + nodecount;


                var atype = a.GetAtomType();
                n.X = a.GetX() * (1 / scale);
                n.Y = a.GetY() * (1 / scale);
                n.Z = a.GetZ() * (1 / scale);

                uint   anum = a.GetAtomicNum();
                string sym  = ptable.GetSymbol((int)anum);
                //double test =ptable.GetIonization ((int)anum);
                int  maxbonds = ptable.GetMaxBonds((int)anum);
                uint val      = a.GetImplicitValence();
                n.localLabels.Add(sym);
                n.localLabels.Add(atype);
                n.localVariables.Add((double)maxbonds);
                n.localVariables.Add((double)val);
                host.addNode(n);
                atomid2nodeid.Add(a.GetId(), nodecount);
                //Console.WriteLine (x);
                nodecount++;
            }
            int arccount = 0;

            foreach (OBBond b in mol.Bonds())
            {
                arc ac = new arc();
                ac.directed = false;
                ac.name     = "a" + arccount;
                //uint bondorder = b.GetBondOrder ();

                if (b.IsSingle())
                {
                    ac.localLabels.Add("s");
                }
                if (b.IsTriple())
                {
                    ac.localLabels.Add("t");
                }
                if (b.IsDouble())
                {
                    ac.localLabels.Add("d");
                }
                uint toid   = b.GetEndAtomIdx();
                uint fromid = b.GetBeginAtomIdx();
                ac.To   = host.nodes[atomid2nodeid[toid - 1]];
                ac.From = host.nodes[atomid2nodeid[fromid - 1]];
                host.arcs.Add(ac);
                arccount++;
            }
            return(host);
        }