Exemplo n.º 1
0
        public static Mode[] GetModes(MatrixByArr hess, string cachepath = null)
        {
            Vector[] eigvec;
            double[] eigval;
            if (cachepath != null && HFile.Exists(cachepath))
            {
                HSerialize.Deserialize(cachepath, null, out eigval, out eigvec);
            }
            else
            {
                HDebug.Verify(NumericSolver.Eig(hess, out eigvec, out eigval));
                HSerialize.SerializeDepreciated(cachepath, null, eigval, eigvec);
            }

            List <Mode> modes;

            {   // sort by eigenvalues
                int[] idx = eigval.HAbs().HIdxSorted();
                modes = new List <Mode>(idx.Length);
                for (int i = 0; i < eigval.Length; i++)
                {
                    Mode mode = new Mode {
                        eigval = eigval[idx[i]],
                        eigvec = eigvec[idx[i]]
                    };
                    modes.Add(mode);
                }
            }

            return(modes.ToArray());
        }
Exemplo n.º 2
0
            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);
            }
Exemplo n.º 3
0
        public static void ToFile(string filepath
                                  , Element[] elements
                                  , bool append //= false
                                  , IList <string> additionalHeaders = null
                                  )
        {
            List <string> lines = new List <string>();

            if (additionalHeaders != null)
            {
                lines.AddRange(additionalHeaders);
            }
            lines.Add("MODEL        1                                                                  ");
            lines.AddRange(elements.ToLines());
            lines.Add("ENDMDL                                                                          ");
            if (append == false)
            {
                HFile.WriteAllLines(filepath, lines);
            }
            else
            {
                StringBuilder text = new StringBuilder();
                foreach (string line in lines)
                {
                    text.AppendLine(line);
                }
                HFile.AppendAllText(filepath, text.ToString());
            }
        }
Exemplo n.º 4
0
 public override bool Read()
 {
     if (HFile == null)
     {
         return(false);
     }
     try {
         if (Buffer == null)
         {
             Buffer = (char[])System.Array.CreateInstance(typeof(System.Char), RecordSize);
         }
         if (Buffer.Length < RecordSize)
         {
             Buffer = (char[])System.Array.CreateInstance(typeof(System.Char), RecordSize);
         }
         if (HFile.ReadBlock(Buffer, 0, RecordSize) != RecordSize)
         {
             return(false);
         }
         StrBuilder.Length = 0;
         StrBuilder.Append(Buffer);
         Record = StrBuilder.ToString();
     } catch (System.Exception Excpt) {
         Err.Add(Excpt);
         return(false);
     }
     return(true);
 }
Exemplo n.º 5
0
 private void SetTemplates()
 {
     _ordIssueFuture = Template.TwOrdIssueFuture;
     if (_configObj.Market.Contains("TWSE"))
     {
         _ordIdn         = TemplateIdn.TwOrdIdnTwse;
         _ordNda         = Template.TwOrdAddTwse;
         _ordQuoteFuture = Template.TwOrdQuoteFutureTwse;
         _ordFm          = TemplateFm.TwOrdTwse;
     }
     else if (_configObj.Market.Contains("GTSM"))
     {
         _ordIdn         = TemplateIdn.TwOrdIdnGtsm;
         _ordNda         = Template.TwOrdAddGtsm;
         _ordQuoteFuture = Template.TwOrdQuoteFutureGtsm;
         _ordFm          = TemplateFm.TwOrdGtsm;
     }
     else if (_configObj.Market.Contains("EMG"))
     {
         _ordIdn         = TemplateIdn.TwOrdIdnEmg;
         _ordNda         = Template.TwOrdAddEmg;
         _ordQuoteFuture = Template.TwOrdQuoteFutureEmg;
         _ordFm          = TemplateFm.TwOrdEmg;
     }
     else
     {
         throw new Exception("The Market you choose does not exist, please change the configuration and choose an existing one.");
     }
 }
Exemplo n.º 6
0
            public static string[] LinesFromFile(string path, bool loadLatest)
            {
                string loadpath = path;

                if (loadLatest)
                {
                    System.IO.FileInfo   fileinfo  = HFile.GetFileInfo(path);
                    System.IO.FileInfo[] fileinfos = fileinfo.Directory.GetFiles(fileinfo.Name + "*");
                    if (fileinfos.Length != 0)
                    {
                        List <string> filepaths = new List <string>();
                        foreach (System.IO.FileInfo lfileinfo in fileinfos)
                        {
                            filepaths.Add(lfileinfo.FullName);
                        }
                        filepaths.Sort();
                        loadpath = filepaths.Last();
                    }
                }
                if (HFile.Exists(loadpath) == false)
                {
                    return(null);
                }
                string[] lines = HFile.ReadAllLines(loadpath);
                return(lines);
            }
Exemplo n.º 7
0
        public static bool GetModesSelftest()
        {
            if (GetModesSelftest_DoTest == false)
            {
                return(true);
            }
            GetModesSelftest_DoTest = false;

            string pdbpath = @"C:\Users\htna\htnasvn_htna\VisualStudioSolutions\Library2\HTLib2.Bioinfo\Bioinfo.Data\pdb\1MJC.pdb";

            if (HFile.Exists(pdbpath) == false)
            {
                return(false);
            }

            Pdb pdb = Pdb.FromFile(pdbpath);

            for (int i = 0; i < pdb.atoms.Length; i++)
            {
                HDebug.Assert(pdb.atoms[0].altLoc == pdb.atoms[i].altLoc);
                HDebug.Assert(pdb.atoms[0].chainID == pdb.atoms[i].chainID);
            }
            List <Vector> coords = pdb.atoms.ListCoord();
            double        cutoff = 13;
            Matrix        hess   = Hess.GetHessAnm(coords.ToArray(), cutoff).ToArray();

            Matrix modes;
            Vector freqs;

            GetModes(hess, out modes, out freqs);

            return(true);
        }
        public void WriteFile(HFile file)
        {
            WriteEndDirectoriesArray();
            WriteStartFilesArray();

            JsonFileWriter jsonFileWriter = new JsonFileWriter(Writer);

            jsonFileWriter.Write(file);
        }
Exemplo n.º 9
0
            public void ToFile(string filepath)
            {
                string[] lines = new string[elements.Length];
                for (int i = 0; i < elements.Length; i++)
                {
                    lines[i] = elements[i].line;
                }

                HFile.WriteAllLines(filepath, lines);
            }
Exemplo n.º 10
0
        public JsonFile(HFile file)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            Name = file.Name;
            Hash = file.Hash;
        }
Exemplo n.º 11
0
            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);
            }
Exemplo n.º 12
0
        public void Add(HFile file)
        {
            if (directoryStack.Count == 0)
            {
                throw new Exception("There is no directory added.");
            }

            HDirectory topDirectory = directoryStack.Peek();

            topDirectory.Files.Add(file);
        }
        public void Add(HFile file)
        {
            if (directoryStack.Count == 0)
            {
                throw new Exception("There is no directory added that can be used as parent for this file.");
            }

            HDirectory topDirectory = directoryStack.Peek();

            topDirectory.Files.Add(file);
        }
Exemplo n.º 14
0
            //public Mode[] ToModesUnnormalized_wrong(double[] masses)
            //{
            //    Mode[] modes   = ToModes_wrong();
            //    Mode[] unmodes = modes.ToModesUnnormalized(masses);
            //    return unmodes;
            //}

            public static Vibrate FromFile(string filepath)
            {
                Vibrate vib;

                {
                    List <string> lines = HFile.ReadLines(filepath).ToList();
                    vib = FromLines(lines);
                }
                System.GC.Collect();
                return(vib);
            }
Exemplo n.º 15
0
            public static AlignData FromFiles(string path_pdb, string path_hess, IList <double> masses, string cachebase = null)
            {
                AlignData data = new AlignData();

                data.pdb       = Pdb.FromFile(path_pdb);
                data.coords    = data.pdb.atoms.ListCoord();
                data.cachebase = cachebase;
                int n = data.coords.Count;

                {   // data.hess
                    List <string> lines = HFile.ReadLines(path_hess).ToList();
                    int           row, col;
                    {
                        string[] tokens = lines[1].Split(new char[] { ' ', ',', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                        HDebug.Assert(tokens.Length == 2);
                        row = int.Parse(tokens[0]);
                        col = int.Parse(tokens[1]);
                        HDebug.Assert(row == col);
                    }
                    HDebug.Assert(row == n * 3, col == n * 3);
                    MatrixByArr[,] hess = new MatrixByArr[n, n];
                    for (int c = 0; c < n; c++)
                    {
                        for (int r = 0; r < n; r++)
                        {
                            hess[c, r] = new double[3, 3];
                        }
                    }
                    for (int c = 0; c < col; c++)
                    {
                        string   line   = lines[c + 2];
                        string[] tokens = line.Split(new char[] { ' ', ',', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                        HDebug.Assert(tokens.Length == row);
                        for (int r = 0; r < row; r++)
                        {
                            double val = double.Parse(tokens[r]);
                            hess[c / 3, r / 3][c % 3, r % 3] += val;
                            hess[r / 3, c / 3][r % 3, c % 3] += val;
                        }
                    }
                    for (int c = 0; c < n; c++)
                    {
                        for (int r = 0; r < n; r++)
                        {
                            hess[c, r] /= 2;
                        }
                    }
                    data.hess = hess;
                }

                data.masses = masses.ToArray();

                return(data);
            }
Exemplo n.º 16
0
        public static Pdb FromPdbidCache(string pdbid, string cachepath)
        {
            Pdb pdb;

            if (HFile.Exists(cachepath))
            {
                pdb = FromPdbid(pdbid);
                pdb.ToFile(cachepath);
            }
            pdb = FromFile(cachepath);
            return(pdb);
        }
Exemplo n.º 17
0
        public void Write(HFile file)
        {
            Writer.WriteStartObject();

            Writer.WritePropertyName("n");
            Writer.WriteValue(file.Name);

            Writer.WritePropertyName("h");
            Writer.WriteValue(file.Hash);

            Writer.WriteEndObject();
        }
Exemplo n.º 18
0
        private IEnumerable <FilePair> GeneratePairs(IReadOnlyList <HFile> filesLeft, IReadOnlyList <HFile> filesRight)
        {
            for (int i = 0; i < filesLeft.Count; i++)
            {
                HFile fileLeft = filesLeft[i];

                for (int j = 0; j < filesRight.Count; j++)
                {
                    HFile fileRight = filesRight[j];
                    yield return(new FilePair(fileLeft, fileRight, CheckFilesExistence));
                }
            }
        }
Exemplo n.º 19
0
 public static void CopyPdb(string pathbase, params string[] pdbids)
 {
     if (pathbase.EndsWith("\\") == false)
     {
         pathbase += "\\";
     }
     foreach (string pdbid in pdbids)
     {
         string        pdbpath  = pathbase + pdbid.ToUpper() + ".pdb";
         List <string> pdblines = GetPdbLines(pdbid);
         HFile.WriteAllLines(pdbpath, pdblines);
     }
 }
Exemplo n.º 20
0
 public static void WriteMdp(string mdppath
                             , string integrator               // = "steep"
                             , double emtol                    // = 5    // Stop minimization when the maximum force < 1.0 kJ/mol/nm
                             , double emstep = 0.01            // Energy step size
                             , long nsteps   = 500000          // Maximum number of (minimization) steps to perform
                                                               // Parameters describing how to find the neighbors of each atom and how to calculate the interactions
                             , int nstlist            = 1      // Frequency to update the neighbor list and long range forces
                             , string ns_type         = "grid" // Method to determine neighbor list (simple, grid)
                             , double rlist           = 1.0    // Cut-off for making neighbor list (short range forces) ; htna 1.0-1.2
                             , string coulombtype     = "Shift"
                             , double rcoulomb        = 1.0
                             , double rcoulomb_switch = 0.0
                             , string vdwtype         = "Shift"
                             , double rvdw            = 1.0
                             , double rvdw_switch     = 0.0
                             )
 {
     string[] lines = new string[]
     {
         "; Parameters describing what to do, when to stop and what to save                                                               ",
         "integrator      = $$integrator$$                                                                                                             ",
         "emtol           = $$emtol$$              ; Stop minimization when the maximum force < 1.0 kJ/mol/nm                                          ",
         "emstep          = $$emstep$$             ; Energy step size                                                                                  ",
         "nsteps          = $$nsteps$$             ; Maximum number of (minimization) steps to perform                                                 ",
         "                                         ; Parameters describing how to find the neighbors of each atom and how to calculate the interactions",
         "nstlist         = $$nstlist$$            ; Frequency to update the neighbor list and long range forces                                       ",
         "ns_type         = $$ns_type$$            ; Method to determine neighbor list (simple, grid)                                                  ",
         "rlist           = $$rlist$$              ; Cut-off for making neighbor list (short range forces) ; htna 1.0-1.2                              ",
         "coulombtype     = $$coulombtype$$                                                                                                            ",
         "rcoulomb        = $$rcoulomb$$                                                                                                               ",
         "rcoulomb_switch = $$rcoulomb_switch$$                                                                                                        ",
         "vdwtype         = $$vdwtype$$                                                                                                                ",
         "rvdw            = $$rvdw$$                                                                                                                   ",
         "rvdw_switch     = $$rvdw_switch$$                                                                                                            ",
     }
     .HReplace("$$integrator$$     ", integrator)
     .HReplace("$$emtol$$          ", emtol)
     .HReplace("$$emstep$$         ", emstep)
     .HReplace("$$nsteps$$         ", nsteps)
     .HReplace("$$nstlist$$        ", nstlist)
     .HReplace("$$ns_type$$        ", ns_type)
     .HReplace("$$rlist$$          ", rlist)
     .HReplace("$$coulombtype$$    ", coulombtype)
     .HReplace("$$rcoulomb$$       ", rcoulomb)
     .HReplace("$$rcoulomb_switch$$", rcoulomb_switch)
     .HReplace("$$vdwtype$$        ", vdwtype)
     .HReplace("$$rvdw$$           ", rvdw)
     .HReplace("$$rvdw_switch$$    ", rvdw_switch)
     ;
     HFile.WriteAllLines(mdppath, lines);
 }
Exemplo n.º 21
0
            public static string[] ReadAllLines(params string[] filepaths)
            {
                List <string> lines = new List <string>();

                foreach (string path in filepaths)
                {
                    foreach (string line in HFile.HEnumAllLines(path))
                    {
                        lines.Add(line);
                    }
                }

                return(lines.ToArray());
            }
        public void HavingFileWithMatchingName_WhenChecked_ThenMatches()
        {
            // pattern: file-or-dir-1
            // path:    /file-or-dir-1 (file)

            HFile hFile = new HFile
            {
                Name = "file-or-dir-1"
            };

            bool actual = blackPath.Matches(hFile);

            Assert.That(actual, Is.True);
        }
        public void HavingFileWithMatchingName_WhenChecked_ThenDoesNotMatch()
        {
            // pattern: item-1/
            // path:    /item-1 (file)

            HFile hFile = new HFile
            {
                Name = "item-1"
            };

            bool actual = blackPath.Matches(hFile);

            Assert.That(actual, Is.False);
        }
Exemplo n.º 24
0
        public static JFile ToJFile(this HFile file)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            return(new JFile
            {
                Name = file.Name,
                Size = file.Size,
                Hash = file.Hash,
                LastModifiedTime = file.LastModifiedTime
            });
        }
Exemplo n.º 25
0
            public static Crd FromFile(string path)
            {
                string[] lines = HFile.ReadAllLines(path);

                int?        num_elems = null;
                List <Atom> elems     = null;

                foreach (string line in lines)
                {
                    if (line.StartsWith("*"))
                    {
                        // comment
                        continue;
                    }

                    // "    358272  EXT"
                    // "         1         1  TIP3      OH2           -79.8000000000      -20.4000000000       -4.2000000000  BWAT      1               0.0000000000"
                    //  01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
                    //  0         1         2         3         4         5         6         7         8         9         10        11        12        13
                    // "  %8d  %8d  %-8s  %-8s  %18.10f  %18.10f  %18.10f  %-8s  %-8d  %18.10f"
                    if (elems == null)
                    {
                        // "    358272  EXT"
                        //  012345678901234
                        //  0         1
                        string[] tokens = new string[]
                        {
                            line.Substring(0, 10),
                            line.Substring(10),
                        };
                        num_elems = int.Parse(tokens[0]);
                        HDebug.Exception(tokens[1] == "  EXT");
                        elems = new List <Atom>(num_elems.Value);
                    }
                    else
                    {
                        elems.Add(new Atom {
                            line = line
                        });
                    }
                }
                HDebug.Assert(elems.Count == num_elems);

                return(new Crd
                {
                    atoms = elems.ToArray(),
                });
            }
Exemplo n.º 26
0
        public static Pdb FromPdbid
            (string pdbid
            , string cachebase  // null or @"K:\PdbUnzippeds\$PDBID$.pdb"
            , bool?download     // null or false
            )
        {
            if (cachebase == null)
            {
                cachebase = @"K:\PdbUnzippeds\$PDBID$.pdb";
            }
            if (download == null)
            {
                download = false;
            }

            string pdbpath = cachebase.Replace("$PDBID$", pdbid);

            if (HFile.Exists(pdbpath))
            {
                var pdb  = Pdb.FromFile(pdbpath);
                var last = pdb.elements.Last();
                if (last is Pdb.End)
                {
                    return(pdb);
                }
                if (last.line.Length == 80)
                {
                    return(pdb);
                }

                if (download.Value)
                {   // redownload
                    pdb = Pdb.FromPdbid(pdbid);
                    pdb.ToFile(pdbpath);
                }
                return(pdb);
            }
            else if (download.Value)
            {
                Pdb pdb = Pdb.FromPdbid(pdbid);
                if (pdb != null)
                {
                    pdb.ToFile(pdbpath);
                    return(pdb);
                }
            }
            return(null);
        }
Exemplo n.º 27
0
                public static void WriteImport(string pypath, bool append = false)
                {
                    StreamWriter file;

                    if (append)
                    {
                        file = HFile.AppendText(pypath);
                    }
                    else
                    {
                        file = HFile.CreateText(pypath);
                    }
                    file.WriteLine("from pymol.cgo import *");
                    file.WriteLine("from pymol import cmd");
                    file.Close();
                }
Exemplo n.º 28
0
 private void SetTemplates()
 {
     _ordIdn = TemplateIdn.TwOrdDrop;
     if (_configObj.Market.ToLower().Contains("twse"))
     {
         _ordNda = Template.TwOrdDropTwse;
     }
     else if (_configObj.Market.ToLower().Contains("gtsm"))
     {
         _ordNda = Template.TwOrdDropGtsm;
     }
     else
     {
         _ordNda = Template.TwOrdDropEmg;
     }
 }
Exemplo n.º 29
0
                public static void WriteBlank(string pmlpath
                                              , bool append
                                              )
                {
                    StreamWriter file;

                    if (append)
                    {
                        file = HFile.AppendText(pmlpath);
                    }
                    else
                    {
                        file = HFile.CreateText(pmlpath);
                    }
                    file.Close();
                }
Exemplo n.º 30
0
            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);
            }