public static CTestgrad Testgrad(string testgradpath , Tinker.Xyz xyz , Tinker.Prm prm , string tempbase //=null , string[] keys , Dictionary <string, string[]> optOutSource // = null ) { var tmpdir = HDirectory.CreateTempDirectory(tempbase); string currpath = HEnvironment.CurrentDirectory; CTestgrad testgrad; { HEnvironment.CurrentDirectory = tmpdir.FullName; if (testgradpath == null) { string resbase = "HTLib2.Bioinfo.HTLib2.Bioinfo.External.Tinker.Resources.tinker_6_2_06."; HResource.CopyResourceTo <Tinker>(resbase + "testgrad.exe", "testgrad.exe"); testgradpath = "testgrad.exe"; } xyz.ToFile("test.xyz", false); prm.ToFile("test.prm"); string keypath = null; if (keys != null && keys.Length > 0) { keypath = "test.key"; HFile.WriteAllLines(keypath, keys); } testgrad = TestgradImpl(testgradpath, "test.xyz", "test.prm", keypath, optOutSource); } HEnvironment.CurrentDirectory = currpath; try{ tmpdir.Delete(true); } catch {} return(testgrad); }
public static CTesthess Testhess (string testhesspath , Tinker.Xyz xyz , Tinker.Prm prm , string tempbase //=null , string[] keys , Dictionary <string, string[]> optOutSource // =null , Func <int, int, HessMatrix> HessMatrixZeros // =null ) { var tmpdir = HDirectory.CreateTempDirectory(tempbase); string currpath = HEnvironment.CurrentDirectory; CTesthess testhess; { HEnvironment.CurrentDirectory = tmpdir.FullName; if (testhesspath == null) { string resbase = "HTLib2.Bioinfo.HTLib2.Bioinfo.External.Tinker.Resources.tinker_6_2_01."; HResource.CopyResourceTo <Tinker>(resbase + "testhess.exe", "testhess.exe"); testhesspath = "testhess.exe"; } xyz.ToFile("test.xyz", false); prm.ToFile("test.prm"); string keypath = null; if ((keys != null) && (keys.Length > 0)) { keypath = "test.key"; HFile.WriteAllLines(keypath, keys); } testhess = Testhess(testhesspath, "test.xyz", "test.prm", keypath, optOutSource, HessMatrixZeros); testhess.xyz = xyz; testhess.prm = prm; } HEnvironment.CurrentDirectory = currpath; try{ tmpdir.Delete(true); } catch {} string test_eig = "true"; if (test_eig == "false") { Vector D; using (new Matlab.NamedLock("")) { Matlab.PutSparseMatrix("testeig.H", testhess.hess.GetMatrixSparse(), 3, 3); Matlab.Execute("testeig.H = (testeig.H + testeig.H')/2;"); Matlab.Execute("[testeig.V, testeig.D] = eig(full(testeig.H));"); Matlab.Execute("testeig.D = diag(testeig.D);"); D = Matlab.GetVector("testeig.D"); Matlab.Execute("clear testeig;"); } } return(testhess); }
public static CPsfgen Psfgen (IList <Tuple <string, string, Pdb.IAtom[]> > lstSegFileAtoms // segname, filename, pdbatoms , string tempbase //=null , string parameters //=null , string namdversion //="2.8" , IList <string> infiles , IList <string> outfiles , string topology , IList <string> psfgen_lines = null , string psfgen_workdir = null , HOptions options = null ) { if (options == null) { options = new HOptions((string)null); } Dictionary <System.IO.FileInfo, string[]> infile_lines = new Dictionary <System.IO.FileInfo, string[]>(); foreach (string infile in infiles) { infile_lines.Add(HFile.GetFileInfo(infile), HFile.ReadAllLines(infile)); } string currpath = HEnvironment.CurrentDirectory; System.IO.DirectoryInfo tmpdir = null; if (psfgen_workdir != null) { HEnvironment.CurrentDirectory = psfgen_workdir; } else { tmpdir = HDirectory.CreateTempDirectory(tempbase); HEnvironment.CurrentDirectory = tmpdir.FullName; } string[] lines = null; if ((psfgen_lines != null) && (psfgen_lines.Count > 0)) { lines = psfgen_lines.ToArray(); } else { lines = GetPsfgenLines (custom_pdbalias: null , custom_patches: null ); } if (topology != null) { lines = lines.ToArray().HReplace("$topology$", topology); } List <string> psf_lines; List <string> pdb_lines; { { //foreach(var respath_filename in GetResourcePaths("2.8", "psfgen")) foreach (var respath_filename in GetResourcePaths(namdversion, "psfgen")) { string respath = respath_filename.Item1; string filename = respath_filename.Item2; HResource.CopyResourceTo <Tinker>(respath, filename); } } // Dictionary<string, Tuple<string, Pdb.IAtom[]>> segname_filename_pdbatoms = new Dictionary<string, Tuple<string, Pdb.IAtom[]>>(); // //if(pdbs.Length != 1) throw new ArgumentException(); // for(int i=0; i<lstSegFilePdb.Count; i++) // { // string segnameprefix = lstSegFilePdb[i].Item1; if( segnameprefix == null) segnameprefix = string.Format("{0:00}", i); // string filenameprefix = lstSegFilePdb[i].Item2; if(filenameprefix == null) filenameprefix = string.Format("{0:00}", i); // Pdb pdb = lstSegFilePdb[i].Item3; // List<Pdb.IAtom> pdb_atoms = new List<Pdb.IAtom>(); // pdb_atoms.AddRange(pdb.atoms); // pdb_atoms.AddRange(pdb.hetatms); // char[] chains = pdb_atoms.ListChainID().HToHashSet().ToArray(); // // HDebug.AssertIf(chains.Length> 1, segnameprefix.Length <= 5); // HDebug.AssertIf(chains.Length<=1, segnameprefix.Length <= 6); // foreach(char chain in chains) // { // Pdb.IAtom[] chain_atoms = pdb_atoms.SelectByChainID(chain).SelectByAltLoc().ToArray(); // string suffix = null; // if(('a' <= chain) && (chain <= 'z')) suffix = string.Format("L{0}", chain); // if(('A' <= chain) && (chain <= 'Z')) suffix = string.Format("U{0}", chain); // if(('0' <= chain) && (chain <= '9')) suffix = string.Format("N{0}", chain); // string segname = segnameprefix + ((chains.Length <= 1) ? "" : suffix); // string filename = filenameprefix + ((chains.Length <= 1) ? "" : suffix); // segname_filename_pdbatoms.Add(segname, new Tuple<string,Pdb.IAtom[]>(filename, chain_atoms)); // } // } foreach (var finfo_line in infile_lines) { string inname = finfo_line.Key.Name; string[] inlines = finfo_line.Value; HFile.WriteAllLines(inname, inlines); } HashSet <string> segnames = new HashSet <string>(); int segindex = 0; foreach (var seg_file_atoms in lstSegFileAtoms) { string segname = seg_file_atoms.Item1; string filename = seg_file_atoms.Item2; Pdb.IAtom[] chain_atoms = seg_file_atoms.Item3.SelectByAltLoc().ToArray(); HDebug.Exception(chain_atoms.ListChainID().HToHashSet().Count == 1); if (segname == null) { while (segindex <= 9999) { if (segnames.Contains(segindex.ToString()) == false) { segname = segindex.ToString(); segnames.Add(segname); break; } segindex++; } } if (filename == null) { filename = segname; } Pdb.ToFile (filename + ".pdb" , chain_atoms.HToType <Pdb.IAtom, Pdb.Element>() , false ); for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains("$segname$")) { string insert = lines[i]; insert = insert.Replace("$segname$", segname); insert = insert.Replace("$segfilename$", filename); lines = lines.HInsert(i, insert); i++; } } } lines = lines.HRemoveAllContains("$segname$"); HFile.WriteAllLines("prot.inp", lines); string command0 = string.Format("psfgen < prot.inp"); bool pause = options.Contains("psfgen pause"); HProcess.StartAsBatchInConsole(null, pause, command0); psf_lines = System.IO.File.ReadLines("prot.psf").ToList(); pdb_lines = System.IO.File.ReadLines("prot.pdb").ToList(); } HEnvironment.CurrentDirectory = currpath; if (tmpdir != null) { try{ tmpdir.Delete(true); } catch {} } return(new CPsfgen { psf_lines = psf_lines, pdb_lines = pdb_lines, }); }
public static CPdbxyz Pdbxyz (Pdb pdb , Tinker.Prm prm , string tempbase //=null , string parameters //=null , string tinkerversion //="6.2.1" , params string[] keys ) { var tmpdir = HDirectory.CreateTempDirectory(tempbase); string currpath = HEnvironment.CurrentDirectory; Tinker.Xyz xyz; string[] seq; string[] capture; { HEnvironment.CurrentDirectory = tmpdir.FullName; { foreach (var respath_filename in GetResourcePaths("6.2.1", "pdbxyz")) { string respath = respath_filename.Item1; string filename = respath_filename.Item2; HResource.CopyResourceTo <Tinker>(respath, filename); } } pdb.ToFile("prot.pdb"); prm.ToFile("prot.prm"); string keypath = null; if ((keys != null) && (keys.Length > 0)) { keypath = "prot.key"; HFile.WriteAllLines(keypath, keys); } { //bool ComputeAnalyticalGradientVector = true; //bool ComputeNumericalGradientVector = false; //bool OutputBreakdownByGradientComponent = false; string command = ""; command += "pdbxyz.exe"; command += " prot.pdb"; command += " prot.prm"; if (keypath != null) { command += " -k " + keypath; } command += " > output.txt"; List <string> errors = new List <string>(); int exitcode = HProcess.StartAsBatchSilent(null, null, errors, command); capture = HFile.ReadAllLines("output.txt"); capture = capture.HAddRange(errors.ToArray()); xyz = Tinker.Xyz.FromFile("prot.xyz", false); if (HFile.Exists("prot.seq")) { seq = HFile.ReadAllLines("prot.seq"); } else { seq = null; } } } HEnvironment.CurrentDirectory = currpath; try{ tmpdir.Delete(true); } catch {} return(new CPdbxyz { xyz = xyz, seq = seq, capture = capture, }); }
public static CNamd2 Namd2 (IList <string> pdb_lines , IList <string> psf_lines , IList <string> prm_lines , string tempbase // = null , string namd2version // = "2.8" , string option // = "+p3" , IList <string> infiles = null , IList <string> outfiles = null , IList <string> conf_lines = null ) { string[] lines = null; if ((conf_lines != null) && (conf_lines.Count > 0)) { lines = conf_lines.ToArray(); } else { // http://www.msg.ucsf.edu/local/programs/Vega/pages/tu_namdmin.htm lines = new string[] { "numsteps 10000 " // minimization steps //{ "numsteps 1000 " // minimization steps , "minimization on " , "dielectric 1.0 " , "coordinates prot.pdb " // coordinate file , "outputname output " // output file: prot.coor , "outputEnergies 1000 " , "binaryoutput no " , "DCDFreq 1000 " , "restartFreq 1000 " , "structure prot.psf " // psf file , "paraTypeCharmm on " , "parameters prot.prm " // parameter file //, "parameters par_all22_vega.inp " , "exclude scaled1-4 " , "1-4scaling 1.0 " , "switching on " , "switchdist 8.0 " , "cutoff 22.0 " //, "cutoff 12.0 " , "pairlistdist 23.5 " //, "pairlistdist 13.5 " , "margin 0.0 " , "stepspercycle 20 " , "fixedAtoms on " // fix atoms , "fixedAtomsCol O " // select fixing atoms from O }; } var tmpdir = HDirectory.CreateTempDirectory(tempbase); string currpath = HEnvironment.CurrentDirectory; HEnvironment.CurrentDirectory = tmpdir.FullName; string[] coor_lines = null; double coor_rmsd = double.NaN; { { //foreach(var respath_filename in GetResourcePaths("2.8", "psfgen")) foreach (var respath_filename in GetResourcePaths(namd2version, "namd2")) { string respath = respath_filename.Item1; string filename = respath_filename.Item2; HResource.CopyResourceTo <Tinker>(respath, filename); } } Vector[] coords0 = Pdb.FromLines(pdb_lines).atoms.ListCoord().ToArray(); System.IO.File.WriteAllLines("prot.pdb", pdb_lines); System.IO.File.WriteAllLines("prot.psf", psf_lines); System.IO.File.WriteAllLines("prot.prm", prm_lines); System.IO.File.WriteAllLines("prot.conf", lines); if (option == null) { option = ""; } string command = string.Format("namd2 {0} prot.conf", option); HProcess.StartAsBatchInConsole(null, false, command); if (HFile.Exists("output.coor")) { coor_lines = HFile.ReadAllLines("output.coor"); Vector[] coords1 = Pdb.FromLines(coor_lines).atoms.ListCoord().ToArray(); Vector[] coords1x = Align.MinRMSD.Align(coords0, coords1); coor_rmsd = Align.MinRMSD.GetRMSD(coords0, coords1x); } } HEnvironment.CurrentDirectory = currpath; try{ tmpdir.Delete(true); } catch {} if (coor_lines == null) { return(null); } return(new CNamd2 { coor_lines = coor_lines, coor_rmsd = coor_rmsd, }); }
public static ONewton Newton(string tinkerpath , Tinker.Xyz xyz , Tinker.Xyz.Atom.Format xyz_atoms_format , Tinker.Prm prm , string tempbase , string copytemp // = null , string param //, string precondition // = "A" // Precondition via Auto/None/Diag/Block/SSOR/ICCG [A] : A //, double gradCriterion // = 0.01 // Enter RMS Gradient per Atom Criterion [0.01] : 0.001 , IList <Tinker.Xyz.Atom> atomsToFix // = null , bool pause // = false , params string[] keys ) { if (HDebug.IsDebuggerAttached && atomsToFix != null) { Dictionary <int, Tinker.Xyz.Atom> xyzatoms = xyz.atoms.ToIdDictionary(); foreach (var atom in atomsToFix) { HDebug.Assert(object.ReferenceEquals(atom, xyzatoms[atom.Id])); } } Tinker.Xyz minxyz; string[] minlog; using (var temp = new HTempDirectory(tempbase, copytemp)) { temp.EnterTemp(); if (tinkerpath == null) { string resbase = "HTLib2.Bioinfo.HTLib2.Bioinfo.External.Tinker.Resources.tinker_6_2_06."; HResource.CopyResourceTo <Tinker>(resbase + "newton.exe", "newton.exe"); tinkerpath = "newton.exe"; } xyz.ToFile("prot.xyz", false); prm.ToFile("prot.prm"); List <string> keylines = new List <string>(keys); if (atomsToFix != null) { foreach (var atom in atomsToFix) { Vector coord = atom.Coord; double force_constant = 10000; // force constant in kcal/Å2 for the harmonic restraint potential string keyline = string.Format("RESTRAIN-POSITION {0} {1} {2} {3} {4}", atom.Id, coord[0], coord[1], coord[2], force_constant); keylines.Add(keyline); } } HFile.WriteAllLines("prot.key", keylines); // Precondition via Auto/None/Diag/Block/SSOR/ICCG [A] : A // Enter RMS Gradient per Atom Criterion [0.01] : 0.001 //bool pause = false; string command = tinkerpath; command += string.Format(" prot.xyz prot.prm"); command += string.Format(" -k prot.key"); command += string.Format(" {0}", param); HProcess.StartAsBatchInConsole("newton.bat", pause , "time /t >> prot.log" , command //+" >> prot.log" , "time /t >> prot.log" ); HDebug.Assert(HFile.Exists("prot.xyz_2")); HDebug.Assert(HFile.Exists("prot.xyz_3") == false); minxyz = Tinker.Xyz.FromFile("prot.xyz_2", false, xyz_atoms_format); minlog = HFile.ReadAllLines("prot.log"); temp.QuitTemp(); } return(new ONewton { minxyz = minxyz, minlog = minlog }); }
public static void SelfTest(string pdbname, string psfname, string prmname, string xyzname) { using (var temp = new HTempDirectory(@"C:\temp\", null)) { temp.EnterTemp(); { string resbase = "HTLib2.Bioinfo.HTLib2.Bioinfo.External.Tinker.Resources.selftest."; HResource.CopyResourceTo <Tinker>(resbase + pdbname, pdbname); HResource.CopyResourceTo <Tinker>(resbase + psfname, psfname); HResource.CopyResourceTo <Tinker>(resbase + "par_all27_prot_lipid.inp", prmname); if (xyzname != null) { HResource.CopyResourceTo <Tinker>(resbase + xyzname, xyzname); } HResource.CopyResourceTo <Tinker>(resbase + "charmm22.prm", "charmm22.prm"); { var pdb = Pdb.FromFile(pdbname); var psf = Namd.Psf.FromFile(psfname); var prm = Namd.Prm.FromFile(prmname); var xyz_prm = BuildFromNamd(pdb, psf, prm); xyz_prm.Item1.ToFile("TinkFromNamd.xyz", false); xyz_prm.Item2.ToFile("TinkFromNamd.prm"); } if (xyzname != null) { var xyz0 = Xyz.FromFile("TinkFromNamd.xyz", false); var prm0 = Prm.FromFile("TinkFromNamd.prm"); var grad0 = Run.Testgrad(xyz0, prm0, @"C:\temp\" //, "VDWTERM NONE" //, "CHARGETERM NONE" //, "BONDTERM NONE" //, "ANGLETERM NONE" //, "UREYTERM NONE" //, "IMPROPTERM NONE" //, "TORSIONTERM NONE" ); var forc0 = grad0.anlyts.GetForces(xyz0.atoms); var xyz1 = Xyz.FromFile(xyzname, false); var prm1 = Prm.FromFile("charmm22.prm"); var grad1 = Run.Testgrad(xyz1, prm1, @"C:\temp\" //, "VDWTERM NONE" //, "CHARGETERM NONE" //, "BONDTERM NONE" //, "ANGLETERM NONE" //, "UREYTERM NONE" //, "IMPROPTERM NONE" //, "TORSIONTERM NONE" ); var forc1 = grad1.anlyts.GetForces(xyz1.atoms); { KDTree.KDTree <object> kdtree = new KDTree.KDTree <object>(3); var atoms0 = xyz0.atoms; for (int i = 0; i < atoms0.Length; i++) { kdtree.insert(atoms0[i].Coord, i); } var atoms1 = xyz1.atoms; int[] idx1to0 = new int[atoms1.Length]; for (int i1 = 0; i1 < atoms1.Length; i1++) { Vector coord1 = atoms1[i1].Coord; int i0 = (int)kdtree.nearest(coord1); Vector coord0 = atoms0[i0].Coord; kdtree.delete(coord0); idx1to0[i0] = i1; } atoms1 = atoms1.HSelectByIndex(idx1to0); forc1 = forc1.HSelectByIndex(idx1to0); } Vector[] dforc = VectorBlock.PwSub(forc0, forc1).ToArray(); double[] dforcl = dforc.Dist(); double max_dforc = dforc.Dist().Max(); HDebug.Exception(max_dforc < 1); // 0.72682794387667848 { double EB = Math.Abs(grad0.enrgCmpnt.EB - grad1.enrgCmpnt.EB); HDebug.Exception(EB < 0.1); double EA = Math.Abs(grad0.enrgCmpnt.EA - grad1.enrgCmpnt.EA); HDebug.Exception(EA < 0.1); double EBA = Math.Abs(grad0.enrgCmpnt.EBA - grad1.enrgCmpnt.EBA); HDebug.Exception(EBA < 0.1); double EUB = Math.Abs(grad0.enrgCmpnt.EUB - grad1.enrgCmpnt.EUB); HDebug.Exception(EUB < 0.1); double EAA = Math.Abs(grad0.enrgCmpnt.EAA - grad1.enrgCmpnt.EAA); HDebug.Exception(EAA < 0.1); double EOPB = Math.Abs(grad0.enrgCmpnt.EOPB - grad1.enrgCmpnt.EOPB); HDebug.Exception(EOPB < 0.1); double EOPD = Math.Abs(grad0.enrgCmpnt.EOPD - grad1.enrgCmpnt.EOPD); HDebug.Exception(EOPD < 0.1); double EID = Math.Abs(grad0.enrgCmpnt.EID - grad1.enrgCmpnt.EID); HDebug.Exception(EID < 0.1); // 0.0019000000000000128 : N-terminus (and C-terminus) information is/are inconsistent betweeen namd-charmm and tink-charmm22 double EIT = Math.Abs(grad0.enrgCmpnt.EIT - grad1.enrgCmpnt.EIT); HDebug.Exception(EIT < 0.1); double ET = Math.Abs(grad0.enrgCmpnt.ET - grad1.enrgCmpnt.ET); HDebug.Exception(ET < 0.5); // 0.33029999999999404 : N-terminus (and C-terminus) information is/are inconsistent betweeen namd-charmm and tink-charmm22 double EPT = Math.Abs(grad0.enrgCmpnt.EPT - grad1.enrgCmpnt.EPT); HDebug.Exception(EPT < 0.1); double EBT = Math.Abs(grad0.enrgCmpnt.EBT - grad1.enrgCmpnt.EBT); HDebug.Exception(EBT < 0.1); double ETT = Math.Abs(grad0.enrgCmpnt.ETT - grad1.enrgCmpnt.ETT); HDebug.Exception(ETT < 0.1); double EV = Math.Abs(grad0.enrgCmpnt.EV - grad1.enrgCmpnt.EV); HDebug.Exception(EV < 0.1); double EC = Math.Abs(grad0.enrgCmpnt.EC - grad1.enrgCmpnt.EC); HDebug.Exception(EC < 0.5); // 0.37830000000002428 double ECD = Math.Abs(grad0.enrgCmpnt.ECD - grad1.enrgCmpnt.ECD); HDebug.Exception(ECD < 0.1); double ED = Math.Abs(grad0.enrgCmpnt.ED - grad1.enrgCmpnt.ED); HDebug.Exception(ED < 0.1); double EM = Math.Abs(grad0.enrgCmpnt.EM - grad1.enrgCmpnt.EM); HDebug.Exception(EM < 0.1); double EP = Math.Abs(grad0.enrgCmpnt.EP - grad1.enrgCmpnt.EP); HDebug.Exception(EP < 0.1); double ER = Math.Abs(grad0.enrgCmpnt.ER - grad1.enrgCmpnt.ER); HDebug.Exception(ER < 0.1); double ES = Math.Abs(grad0.enrgCmpnt.ES - grad1.enrgCmpnt.ES); HDebug.Exception(ES < 0.1); double ELF = Math.Abs(grad0.enrgCmpnt.ELF - grad1.enrgCmpnt.ELF); HDebug.Exception(ELF < 0.1); double EG = Math.Abs(grad0.enrgCmpnt.EG - grad1.enrgCmpnt.EG); HDebug.Exception(EG < 0.1); double EX = Math.Abs(grad0.enrgCmpnt.EX - grad1.enrgCmpnt.EX); HDebug.Exception(EX < 0.1); } } } temp.QuitTemp(); } //{ // //string pathbase = @"C:\Users\htna.IASTATE\svn\htnasvn_htna\Research.bioinfo.prog.NAMD\1A6G\New folder\"; // string pathbase = @"C:\Users\htna.IASTATE\svn\htnasvn_htna\Research.bioinfo.prog.NAMD\1A6G\"; // string toplbase = @"C:\Users\htna.IASTATE\svn\htnasvn_htna\Research.bioinfo.prog.NAMD\top_all27_prot_lipid\"; // var pdb = Pdb .FromFile(pathbase+"1A6G.psfgen.pdb"); // var psf = Namd.Psf.FromFile(pathbase+"1A6G.psfgen.psf"); // var prm = Namd.Prm.FromFile(toplbase+"par_all27_prot_na.prm"); // // pdb = Pdb .FromFile(@"K:\Tim-8TIM,1TPH,1M6J\Tim-1M6J\1M6J.psfgen.pdb"); // psf = Namd.Psf.FromFile(@"K:\Tim-8TIM,1TPH,1M6J\Tim-1M6J\1M6J.psfgen.psf"); // prm = Namd.Prm.FromFile(@"K:\Tim-8TIM,1TPH,1M6J\Tim-1M6J\par_all27_prot_lipid.inp"); // var xyz_prm = BuildFromNamd(pdb, psf, prm); // xyz_prm.Item1.ToFile(@"C:\temp\TinkFromNamd.xyz", false); // xyz_prm.Item2.ToFile(@"C:\temp\TinkFromNamd.prm"); //} }