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 Xyz Intxyz(string intpath, string tempbase = null) { string[] intlines = HFile.ReadAllLines(intpath); Xyz xyz; var tmpdir = HDirectory.CreateTempDirectory(tempbase); string currpath = HEnvironment.CurrentDirectory; { HEnvironment.CurrentDirectory = tmpdir.FullName; HFile.WriteAllLines("test.int", intlines); Intxyz("test.int"); xyz = Xyz.FromFile("test.xyz", true); } HEnvironment.CurrentDirectory = currpath; try{ tmpdir.Delete(true); } catch {} return(xyz); }
public static Top FromPdb(string pdbpath , string ff = "charmm27" , string water = "none" , HPack <Pdb> optout_confout = null ) { Top top; FileInfo pdbinfo = new FileInfo(pdbpath); string currdir = HEnvironment.CurrentDirectory; DirectoryInfo temproot = HDirectory.CreateTempDirectory(); HEnvironment.CurrentDirectory = temproot.FullName; { HFile.Copy(pdbinfo.FullName, "protein.pdb"); RunPdb2gmx(f: "protein.pdb" , o: "confout.pdb" , p: "topol.top" , q: "clean.pdb" , ff: ff , water: water , merge: "all" , lineStderr: null , lineStdout: null ); top = Top.FromFile("topol.top"); if (optout_confout != null) { optout_confout.value = Pdb.FromFile("confout.pdb"); } } HEnvironment.CurrentDirectory = currdir; Thread.Sleep(100); try{ temproot.Delete(true); } catch (Exception) {} return(top); }
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 Vector[] MinimizeLBfgs(IList <Pdb.Atom> atoms , IList <Vector> coords = null , string ff = null // -ff string select Force field, interactive by default. Use -h for information. // "charmm27", ... , string water = null // -water enum select Water model to use: select, none, spc, spce, tip3p, tip4p or tip5p , double mdp_emtol = 0.001 // the minimization is converged when the maximum force is smaller than this value , double mdp_emstep = 0.001 // initial step-size ) { Vector[] confout; { string currdir = HEnvironment.CurrentDirectory; System.IO.DirectoryInfo temproot = HDirectory.CreateTempDirectory(); HEnvironment.CurrentDirectory = temproot.FullName; { if (coords != null) { HDebug.Assert(atoms.Count == coords.Count); } Pdb.ToFile("conf.pdb", atoms, coords: coords , headers: new string[] { "CRYST1 " } ); HFile.WriteAllLines("grompp-nm.mdp" , new string[] { "; Parameters describing what to do, when to stop and what to save " , "integrator = l-bfgs " , "emtol = 0.001 " , "emstep = 0.001 ; Energy step size " , "nsteps = 5000000000 ; Maximum number of (minimization) steps to perform " , " ; Parameters describing how to find the neighbors of each atom and how to calculate the interactions " , "nstlist = 1 ; Frequency to update the neighbor list and long range forces " , "ns_type = grid ; Method to determine neighbor list (simple, grid) " , "rlist = 1.0 ; Cut-off for making neighbor list (short range forces) ; htna 1.0-1.2 " , "coulombtype = Shift " , "rcoulomb = 1.0 " , "rcoulomb_switch = 0.7 " , "vdwtype = Shift " , "rvdw = 1.0 " , "rvdw_switch = 0.7 " //," " //," " //,"nstxout = 1; (100) [steps] frequency to write coordinates to output trajectory file, the last coordinates are always written" //,"nstvout = 1; (100) [steps] frequency to write velocities to output trajectory, the last velocities are always written " //,"nstfout = 1; ( 0) [steps] frequency to write forces to output trajectory. " }); RunPdb2gmx(f: "conf.pdb" , o: "confout.pdb" , p: "topol.top" , i: "posre.itp" , n: "clean.ndx" , q: "clean.pdb" , ff: "charmm27" , water: "none" , merge: "all" , lineStderr: null , lineStdout: null ); RunGrompp(f: "grompp-nm.mdp" , p: "topol.top" , o: "topol-nm.tpr" , c: "confout.pdb" , lineStderr: null , lineStdout: null ); RunMdrun(s: "topol-nm.tpr" , c: "confout.pdb" , o: "traj.trr" , g: "md.log" //, mtx:"hessian.mtx" //, pf:"pullf.xvg" //, px:"pullx.xvg" //, nt:"3" , lineStderr: null , lineStdout: null ); Pdb pdbout = Pdb.FromFile("confout.pdb"); confout = pdbout.atoms.ListCoord().ToArray(); } HEnvironment.CurrentDirectory = currdir; Thread.Sleep(100); try{ temproot.Delete(true); } catch (Exception) {} } HDebug.Assert(confout.Length == atoms.Count); return(confout); }