/// <summary>
        ///
        /// </summary>
        public void BuildPfamPeptideInterfaceClusters()
        {
            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Retrieve Pfam-Peptide interfaces and cluster interfaces");
            ProtCidSettings.logWriter.WriteLine("Retrieve Pfam-Peptide interfaces and cluster interfaces");

            // retreive peptide interfaces
            PfamPeptideInterfaces pfamPepInterfaces = new PfamPeptideInterfaces();

            pfamPepInterfaces.RetrievePfamPeptideInterfaces();
            //         pfamPepInterfaces.GetPfamPeptideInPdbMetaData();

            // write peptide interfaces into PDB format files
            PfamPepInterfaceWriter pepInterfaceWriter = new PfamPepInterfaceWriter();

            pepInterfaceWriter.GeneratePfamPeptideInterfaceFiles();

            // calculate the SA by NACCESS
            DomainInterfaceSA pepInterfaceSa = new DomainInterfaceSA();

            pepInterfaceSa.domainInterfaceTableName = "PfamPeptideInterfaces";
            pepInterfaceSa.CalculateDomainInterfaceSAs();

            // common hmm positions
            PfamHmmSites commonHmmSites = new PfamHmmSites();

            // common hmm positions between peptide interfaces
            commonHmmSites.CountPeptideInterfaceHmmSites();
            // common hmmm positions between domain and peptide interfaces
            //   commonHmmSites.CountPfamCommonHmmSites();

            // Peptide-peptide RMSD
            PeptideInterfaceRmsd pepInterfaceRmsd = new PeptideInterfaceRmsd();

            pepInterfaceRmsd.CalculateDomainInterfacePeptideRmsd();
            pepInterfaceRmsd.CalculateMissingPfamDomainInterfacePeptideRmsd();

            // cluster peptide interfaces, them compress the peptide interfaces into tar files
            PeptideInterfaceCluster pepCluster = new PeptideInterfaceCluster();

            pepCluster.ClusterPeptideInterfaces();

            PeptideInterfaceClusterStat pepClusterStat = new PeptideInterfaceClusterStat();

            pepClusterStat.GetPepInterfaceClusterStat();

            PepClusterInterfaceCompress pepClusterFileCompress = new PepClusterInterfaceCompress();

            pepClusterFileCompress.CompressClusterPeptideInterfaceFiles();

            PfamPepSeqFasta pepSeqFasta = new PfamPepSeqFasta();

            pepSeqFasta.PrintPfamPeptideClusterSequences();

            PfamPepInterfaceFileCompress fileCompress = new PfamPepInterfaceFileCompress();

            fileCompress.CompressPfamPeptideInterfaces();

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Clustering peptide interfaces done!");
            ProtCidSettings.logWriter.WriteLine("Clustering peptide interfaces done! ");
        }
        public void UpdateSomePfamPeptideInteractionsDebug()
        {
            string[] updateEntries = GetMissingPfamPepInterfacesEntries();
            //     string[] updateEntries = {"4tq1" };
            PfamPeptideInterfaces         pfamPepInterfaces   = new PfamPeptideInterfaces();
            Dictionary <string, string[]> updatePfamEntryDict = pfamPepInterfaces.UpdatePfamPeptideInterfaces(updateEntries);

            //        PeptideInterfaceClusterStat pepClusterStat1 = new PeptideInterfaceClusterStat();
            //        pepClusterStat1.AddNumCFs();


            string[] updatePfamIds = { "MHC_II_alpha" };

            ProtCidSettings.logWriter.WriteLine("Update peptide interface clusters");
            // cluster peptide interfaces,
            PeptideInterfaceCluster pepCluster = new PeptideInterfaceCluster();

            pepCluster.UpdatePeptideInterfaceClusters(updatePfamIds);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            // the pymol sessions for each pfam-peptide interface clusters
            ProtCidSettings.logWriter.WriteLine("Compress peptide interface files of clusters");
            PepClusterInterfaceCompress pepClusterFileCompress = new PepClusterInterfaceCompress();

            pepClusterFileCompress.UpdateClusterPeptideInterfaceFiles(updatePfamIds);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            ProtCidSettings.logWriter.WriteLine("Sequences for peptide interface clusters");
            // the sequences for both pfam domains and peptides
            PfamPepSeqFasta pepSeqFasta = new PfamPepSeqFasta();

            pepSeqFasta.UpdatePfamPeptideClusterSequences(updatePfamIds);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            ProtCidSettings.logWriter.WriteLine("Update peptide interface clusters");
            // the summary info for the updated pfams
            PeptideInterfaceClusterStat pepClusterStat = new PeptideInterfaceClusterStat();

            pepClusterStat.UpdatePepInterfaceClusterStat(updatePfamIds);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            // update pfam-peptide interfaces, align pfam domains, peptides are not aligned
            ProtCidSettings.logWriter.WriteLine("Compress all Pfam-peptide interfaces");
            PfamPepInterfaceFileCompress fileCompress = new PfamPepInterfaceFileCompress();

            fileCompress.UpdatePfamPeptideInterfaces(updatePfamIds);
            ProtCidSettings.logWriter.WriteLine("Update pfam-peptide data done!");
            ProtCidSettings.logWriter.Flush();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="stepNum"></param>
        public void BuildDomainInterfaces(int stepNum)
        {
            InitializeThread();

            DomainInterfaceTables.InitializeTables();
            ProtCidSettings.progressInfo.Reset();
            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Retrieving domain-domain interactions.");

            /*    ProtCidSettings.progressInfo.progStrQueue.Enqueue("Creating database tables.");
             *  DomainInterfaceTables.InitializeDbTables();
             *  ProtCidSettings.progressInfo.progStrQueue.Enqueue("Done.");
             */
            switch (stepNum)
            {
            case 1:
                ProtCidSettings.progressInfo.progStrQueue.Enqueue("Detecting domain-domain interactions from cryst chain interfaces.");
                DomainClassifier domainClassifier = new DomainClassifier();
                domainClassifier.RetrieveDomainInterfaces();
                ProtCidSettings.progressInfo.currentOperationIndex++;
                //	goto case 1;
                break;

            case 2:
                ProtCidSettings.progressInfo.progStrQueue.Enqueue("Write domain interface files.");
                DomainInterfaceWriter domainInterfaceWriter = new DomainInterfaceWriter();
                domainInterfaceWriter.WriteDomainInterfaceFiles();
                //      domainInterfaceWriter.UpdateDomainInterfaceFiles();
                //        domainInterfaceWriter.WriteMultiChainDomainInterfaces();
                ProtCidSettings.progressInfo.currentOperationIndex++;

                ProtCidSettings.progressInfo.progStrQueue.Enqueue("Calculate SAs of domain interface files.");
                DomainInterfaceSA interfaceSa = new DomainInterfaceSA();
                //   interfaceSa.UpdateDomainInterfaceSAs();
                interfaceSa.CalculateDomainInterfaceSAs();
                ProtCidSettings.progressInfo.currentOperationIndex++;
                //   goto case 2;
                break;

            case 3:
                ProtCidSettings.progressInfo.progStrQueue.Enqueue("Comparing domain interfaces.");
                PfamDomainInterfaceComp domainComp = new PfamDomainInterfaceComp();
                domainComp.CompareDomainInterfaces();

                /*   domainComp.SynchronizeDomainChainInterfaceComp();
                 *  domainComp.CompareSpecificDomainInterfaces ();
                 *  domainComp.UpdateMultiChainDomainInterfaces();
                 */
                ProtCidSettings.progressInfo.currentOperationIndex++;
                break;

            case 4:
                ProtCidSettings.progressInfo.progStrQueue.Enqueue("Comparing entry domain interfaces.");
                DomainInterfaceBuComp.CrystBuDomainInterfaceComp domainInterfaceComp =
                    new InterfaceClusterLib.DomainInterfaces.DomainInterfaceBuComp.CrystBuDomainInterfaceComp();
                //    int[] relSeqIds = {10515 };
                //     domainInterfaceComp.UpdateCrystBuDomainInterfaceComp(relSeqIds);
                domainInterfaceComp.CompareCrystBuDomainInterfaces();
                ProtCidSettings.progressInfo.currentOperationIndex++;
                break;


            case 5:
                ProtCidSettings.progressInfo.progStrQueue.Enqueue("Cluster domain interfaces.");
                DomainInterfaceCluster interfaceCluster = new DomainInterfaceCluster();
                int[] relSeqIds = { 2 };
                interfaceCluster.UpdateDomainInterfaceClusters(relSeqIds);
//                    interfaceCluster.ClusterDomainInterfaces();
                // interfaceCluster.ClusterLeftRelations();
                break;
            //    goto case 4;

            case 6:
                ProtCidSettings.progressInfo.progStrQueue.Enqueue("Print Domain Interface Cluster Info.");
                DomainClusterStat clusterStat     = new DomainClusterStat();
                int[]             updateRelSeqIds = { 2 };
                clusterStat.UpdateDomainClusterInfo(updateRelSeqIds);
                //                   clusterStat.PrintDomainClusterInfo();
                //            clusterStat.PrintDomainDbSumInfo("PfamDomain");
                //           clusterStat.AddNumCFsToIPfamInPdbMetaData();



                //      clusterStat.PrintPartialDomainClusterInfo();
                //      clusterStat.GetPfamDomainSumInfo();

                /*                  DomainInterfaceStatInfo statInfo = new DomainInterfaceStatInfo();
                 *          //      statInfo.PrintPepInteractingHmmSites();
                 *                //     statInfo.GetPfamPepInterfaceClusterInfo();
                 *                //    statInfo.PrintPepLigandHmmSites ();
                 *                    statInfo.PrintPfamDomainRelationInfo();
                 *                //    statInfo.GetGenAsymChainDomainInterfaces();*/
                break;
            //    goto case 7;

            case 7:
                PfamClusterFilesCompress clusterFileCompress = new PfamClusterFilesCompress();
                clusterFileCompress.CompressPfamClusterChainInterfaceFiles();
                clusterFileCompress.RetrieveCrystInterfaceFilesNotInClusters(true);

                DomainSeqFasta seqFasta = new DomainSeqFasta();
                seqFasta.PrintClusterDomainSequences();

                DomainInterfaceImageGen imageGen = new DomainInterfaceImageGen();
                imageGen.GenerateDomainInterfaceImages();

                PfamRelNetwork pfamNetWriter = new PfamRelNetwork();
                pfamNetWriter.GeneratePfamNetworkGraphmlFiles();

                // build the unp-unp interaction table based on pdb domain interfaces
                UnpInteractionStr unpInteract = new UnpInteractionStr();
                unpInteract.BuildUnpInteractionNetTable();
                break;

            case 8:      // about peptide interfaces
                PfamPeptideInterfaces pepInterfaces = new PfamPeptideInterfaces();
                pepInterfaceBuilder.BuildPfamPeptideInterfaces();
                break;

            case 9:
                //          ligandInteractBuilder.AddClusterInfoToPfamDomainAlign();

                ligandInteractBuilder.BuildPfamLigandInteractions();
                break;

            default:
                break;
            }
            ProtCidSettings.logWriter.Close();

            ProtCidSettings.progressInfo.threadFinished = true;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="updateEntries"></param>
        public void UpdatePfamPeptideInteractions(string[] updateEntries)
        {
            // retreive peptide interfaces
            PfamPeptideInterfaces pfamPepInterfaces = new PfamPeptideInterfaces();

            ProtCidSettings.logWriter.WriteLine("Update Pfam-peptide interfaces");
            ProtCidSettings.logWriter.WriteLine("#entries=" + updateEntries.Length);
            ProtCidSettings.logWriter.Flush();

            Dictionary <string, string[]> updatePfamEntryDict = pfamPepInterfaces.UpdatePfamPeptideInterfaces(updateEntries);
            //       Dictionary<string, string[]> updatePfamEntryDict = ReadUpdatePfamPepEntryHash();
            List <string> updatePfamIdList = new List <string>(updatePfamEntryDict.Keys);

            updatePfamIdList.Sort();
            string[] updatePfamIds = new string[updatePfamIdList.Count];
            updatePfamIdList.CopyTo(updatePfamIds);
            string[] updatePepEntries = GetUpdateEntriesFromHash(updatePfamEntryDict);

            ProtCidSettings.logWriter.WriteLine("Update Pfam-peptide in pdb meta data.");
            pfamPepInterfaces.UpdatePfamPeptideInPdbMetaData(updatePfamIds);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            ProtCidSettings.logWriter.WriteLine("Update Pfam-peptide coordinate interface files.");
            // write peptide interfaces into PDB format files
            PfamPepInterfaceWriter pepInterfaceWriter = new PfamPepInterfaceWriter();

            pepInterfaceWriter.UpdatePfamPeptideInterfaceFiles(updatePepEntries);
            //           pepInterfaceWriter.WriteDomainPeptideInterfaceFiles();
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            // calculate the SA by NACCESS
            ProtCidSettings.logWriter.WriteLine("Update surface areas of pfam-peptide interfaces.");
            DomainInterfaceSA pepInterfaceSa = new DomainInterfaceSA();

            pepInterfaceSa.domainInterfaceTableName = "PfamPeptideInterfaces";
            pepInterfaceSa.UpdateDomainInterfaceSAs(updatePepEntries);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            ProtCidSettings.logWriter.WriteLine("Update peptide interface hmm sites");
            // common hmm positions
            PfamHmmSites commonHmmSites = new PfamHmmSites();

            // common hmm positions between peptide interfaces
            commonHmmSites.UpdatePeptideInterfaceHmmSites(updatePepEntries);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            // Peptide-peptide RMSD
            ProtCidSettings.logWriter.WriteLine("Update peptide RMSDs");
            PeptideInterfaceRmsd pepInterfaceRmsd = new PeptideInterfaceRmsd();

            pepInterfaceRmsd.UpdateDomainInterfacePeptideRmsd(updatePfamEntryDict);
            ProtCidSettings.logWriter.Flush();

            ProtCidSettings.logWriter.WriteLine("Update peptide interface clusters");
            // cluster peptide interfaces,
            PeptideInterfaceCluster pepCluster = new PeptideInterfaceCluster();

            pepCluster.UpdatePeptideInterfaceClusters(updatePfamIds);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            // the pymol sessions for each pfam-peptide interface clusters
            ProtCidSettings.logWriter.WriteLine("Compress peptide interface files of clusters");
            PepClusterInterfaceCompress pepClusterFileCompress = new PepClusterInterfaceCompress();

            pepClusterFileCompress.UpdateClusterPeptideInterfaceFiles(updatePfamIds);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            ProtCidSettings.logWriter.WriteLine("Sequences for peptide interface clusters");
            // the sequences for both pfam domains and peptides
            PfamPepSeqFasta pepSeqFasta = new PfamPepSeqFasta();

            pepSeqFasta.UpdatePfamPeptideClusterSequences(updatePfamIds);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            ProtCidSettings.logWriter.WriteLine("Update peptide interface clusters");
            // the summary info for the updated pfams
            PeptideInterfaceClusterStat pepClusterStat = new PeptideInterfaceClusterStat();

            pepClusterStat.UpdatePepInterfaceClusterStat(updatePfamIds);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            // update pfam-peptide interfaces, align pfam domains, peptides are not aligned
            ProtCidSettings.logWriter.WriteLine("Compress all Pfam-peptide interfaces");
            PfamPepInterfaceFileCompress fileCompress = new PfamPepInterfaceFileCompress();

            fileCompress.UpdatePfamPeptideInterfaces(updatePfamIds);
            ProtCidSettings.logWriter.WriteLine("Update pfam-peptide data done!");
            ProtCidSettings.logWriter.Flush();
        }