/// <summary>
        ///
        /// </summary>
        public void UpdatePfamChainClusterFiles()
        {
            ProtCidSettings.dataType = "pfam";

            DbBuilderHelper.Initialize();
            ProtCidSettings.progressInfo.Reset();

            //           int[] updateSuperGroups = GetUpdateClusterChainGroups ();
            int[] updateSuperGroups = { 3171, 9125, 13844, 16380, 28272, 29578 };
            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Updating super groups clustering.");
            ChainInterfaceCluster interfaceCluster = new ChainInterfaceCluster();

            interfaceCluster.UpdateSuperGroupClusters(updateSuperGroups);

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Updating the summary data.");
            ChainClusterStat superClusterStat = new ChainClusterStat();

            superClusterStat.UpdateSupergroupInterfaceClustersSumInfo(updateSuperGroups, "pfam", true);

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Updating tar interface files.");
            InterfaceFilesReverser interfaceFileReverse = new InterfaceFilesReverser();
            string updateClusterReverseFileList         = interfaceFileReverse.ReverseClusterInterfaceFiles(updateSuperGroups);

            string updateClusterReverseFileList1     = "ReverseInterfacesInCluster.txt";
            ChainClusterCompress clusterFileCompress = new ChainClusterCompress();

            clusterFileCompress.CompressGroupClusterInterfaceFiles(updateSuperGroups, updateClusterReverseFileList1);
            clusterFileCompress.UpdateCrystInterfaceFilesNotInClusters(false);

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Updating sequence files.");
            SeqFastaGenerator seqFastaGen = new SeqFastaGenerator();

            seqFastaGen.UpdateSeqFastaFiles(updateSuperGroups);

            InterfaceImageGen imageGen = new InterfaceImageGen();

            imageGen.UpdateClusterInterfaceImages(updateSuperGroups);
        }
        public void BuildChainInterfaceClusters(int operationStep)
        {
            ProtCidSettings.dataType = "pfam";

            DbBuilderHelper.Initialize();
            switch (operationStep)
            {
            case 1:
                try
                {
                    ChainGroupsClassifier chainGroupClassifier = new ChainGroupsClassifier();
                    chainGroupClassifier.CombineFamilyGroups();
                }
                catch (Exception ex)
                {
                    ProtCidSettings.progressInfo.progStrQueue.Enqueue("Classify chain relations error: " +
                                                                      ex.Message);
                    ProtCidSettings.logWriter.WriteLine("Classify chain relations error: " +
                                                        ex.Message);
                    ProtCidSettings.logWriter.Flush();
                }

                break;
            //     goto case 2;


            case 2:
                ChainGroupRepEntryComp interGroupRepEntryComp = new ChainGroupRepEntryComp();
                interGroupRepEntryComp.CompareTwoEntries("5uvz", "2dww");
                interGroupRepEntryComp.CompareInterGroupRepEntryInterfacesInChainGroups();
                //     interGroupRepEntryComp.PrintMissingAlignInterGroupRepEntries();

                Alignments.GroupEntryAlignments groupAlign = new Alignments.GroupEntryAlignments();
                groupAlign.GetRepChainPairsToBeAligned();

                //             interGroupRepEntryComp.CompareMissingEntryPairs();
                break;
            //  goto case 3;

            case 3:
                ChainInterfaceCluster interfaceCluster = new ChainInterfaceCluster();
                interfaceCluster.ClusterChainGroupInterfaces();

                /*    int[] abSuperGroupIds = { 744};
                 *  interfaceCluster.UpdateSuperGroups(abSuperGroupIds);  */
                break;
            //       goto case 4;

            case 4:
                ChainClusterStat superClusterStat = new ChainClusterStat();
//                    superClusterStat.PrintChainClusterTextFilesFromDb ();
                superClusterStat.PrintSupergroupInterfaceClusters("pfam");
                //    goto case 5;
                break;

            case 5:
                InterfaceFilesReverser interfaceFileReverse = new InterfaceFilesReverser();
                //    interfaceFileReverse.UpdateIsSymmetry();
                string clusterReverseFileList = interfaceFileReverse.ReverseClusterInterfaceFiles();
#if DEBUG
                /////////////////////////////////////
                // the clusterReverseFileList is still a problem, file not exist exception, probably due to change the current directory
                // but I can not detect where the problem is.
                // should fix it.
                ////////////////////////////////////
                ProtCidSettings.progressInfo.progStrQueue.Enqueue("The current working directory: " + System.Environment.CurrentDirectory);
                ProtCidSettings.logWriter.WriteLine("The current working directory: " + System.Environment.CurrentDirectory);
                FileInfo fileInfo = new FileInfo(clusterReverseFileList);
                ProtCidSettings.logWriter.WriteLine("The directory of clusterReverseFileList file: " + fileInfo.DirectoryName);
#endif

                clusterReverseFileList = Path.Combine(ProtCidSettings.applicationStartPath, clusterReverseFileList);
                ChainClusterCompress clusterFileCompress = new ChainClusterCompress();
                clusterFileCompress.CompressClusterInterfaceFiles(clusterReverseFileList);
                // copy the interface files for those not in any clusters
                // save disk space in the web server
                clusterFileCompress.RetrieveCrystInterfaceFilesNotInClusters(false);

                InterfaceImageGen imageGen = new InterfaceImageGen();
                imageGen.GenerateInterfaceImages();

                //   goto case 6;
                break;

            case 6:
                SeqFastaGenerator seqFastaGen = new SeqFastaGenerator();
                seqFastaGen.WriteSequencesToFastaFiles();
                break;
            //   goto case 7;

            case 7:
                BiolUnitSumInfo buSumInfo = new BiolUnitSumInfo();
                //             buSumInfo.UpdateBiolUnits();
                buSumInfo.RetrieveBiolUnits();
                break;

            default:
                break;
            }
            DbBuilderHelper.UpdateIndexes("PfamSuper", ProtCidSettings.protcidDbConnection);

            ProtCidSettings.progressInfo.threadFinished = true;
        }
        /// <summary>
        ///
        /// </summary>
        public void UpdateChainInterfaceClusters()
        {
            ProtCidSettings.dataType = "pfam";

            DbBuilderHelper.Initialize();
            ProtCidSettings.progressInfo.Reset();
            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Updating PFAM SuperGroup Interfaces Data.");

            //          ChainClusterStat clusterStat = new ChainClusterStat();
            //          clusterStat.UpdateMinSeqIdentityInDb();

            ProtCidSettings.logWriter.WriteLine(DateTime.Today.ToShortTimeString());
            ProtCidSettings.logWriter.WriteLine("Updating PFAM SuperGroup Interfaces Data.");
            ProtCidSettings.logWriter.Flush();

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Update biological units and entrypfamarch data for protcid");

            Dictionary <int, string[]> updatedGroupHash = GetUpdatedGroups();
            Dictionary <int, Dictionary <int, string[]> > updateSuperGroupHash = null;

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Updating Super Groups.");
//            ChainGroupsClassifier superGroupClassifier = new ChainGroupsClassifier();
//            updateSuperGroupHash = superGroupClassifier.UpdateFamilySuperGroups(updatedGroupHash);

            if (updateSuperGroupHash == null)
            {
                updateSuperGroupHash = new Dictionary <int, Dictionary <int, string[]> > ();
                StreamReader dataReader   = new StreamReader("UpdateSuperGroups.txt");
                string       line         = "";
                int          superGroupId = -1;
                Dictionary <int, string[]> updateGroupEntryHash = null;
                while ((line = dataReader.ReadLine()) != null)
                {
                    if (line == "")
                    {
                        continue;
                    }
                    if (line.Substring(0, 1) == "#")
                    {
                        if (superGroupId != -1)
                        {
                            updateSuperGroupHash.Add(superGroupId, updateGroupEntryHash);
                        }
                        superGroupId         = Convert.ToInt32(line.Substring(1, line.Length - 1));
                        updateGroupEntryHash = new Dictionary <int, string[]> ();
                    }
                    if (line.IndexOf(":") > -1)
                    {
                        string[] fields  = line.Split(':');
                        string[] entries = fields[1].Split(',');
                        updateGroupEntryHash.Add(Convert.ToInt32(fields[0]), entries);
                    }
                }
                if (superGroupId > -1)
                {
                    updateSuperGroupHash.Add(superGroupId, updateGroupEntryHash);
                }
                dataReader.Close();
            }
            List <int> updateSuperGroupList = new List <int> (updateSuperGroupHash.Keys);

            updateSuperGroupList.Sort();
            int[] updateSuperGroups = new int[updateSuperGroupList.Count];
            updateSuperGroupList.CopyTo(updateSuperGroups);

            string[] updateEntries = GetUpdateEntries(updateSuperGroupHash);

            // clear the any existing file to make sure the file containing only those from
            // the comparison between groups.
            string nonAlignedPairFile = "NonAlignedEntryPairs.txt";

            if (File.Exists(nonAlignedPairFile))
            {
                // before delete, make a copy
                File.Copy(nonAlignedPairFile, Path.Combine(ProtCidSettings.dirSettings.fatcatPath, nonAlignedPairFile), true);
                File.Delete(nonAlignedPairFile);
            }

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Updating the interface comparisons between groups.");
            ChainGroupRepEntryComp interGroupRepEntryComp = new ChainGroupRepEntryComp();

            interGroupRepEntryComp.UpdateEntryComparisonInSuperGroups(updateSuperGroupHash);
            //        interGroupRepEntryComp.CompareMissingEntryPairs();

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Updating super groups clustering.");
            ChainInterfaceCluster interfaceCluster = new ChainInterfaceCluster();

            interfaceCluster.UpdateSuperGroupClusters(updateSuperGroups);

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Updating the summary data.");
            ChainClusterStat superClusterStat = new ChainClusterStat();

            superClusterStat.UpdateSupergroupInterfaceClustersSumInfo(updateSuperGroups, updateEntries, "pfam", true);
//            superClusterStat.UpdateProtCidChainMetaData("pfam", updateEntries, updateSuperGroups);


            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Updating tar interface files.");
            InterfaceFilesReverser interfaceFileReverse = new InterfaceFilesReverser();
            string updateClusterReverseFileList         = interfaceFileReverse.ReverseClusterInterfaceFiles(updateSuperGroupHash);

            //       string updateClusterReverseFileList = "ReverseInterfacesInCluster.txt";
            updateClusterReverseFileList = Path.Combine(ProtCidSettings.applicationStartPath, updateClusterReverseFileList);

            //          string updateClusterReverseFileList = "ReverseInterfacesInCluster.txt";
            ChainClusterCompress clusterFileCompress = new ChainClusterCompress();

            clusterFileCompress.CompressGroupClusterInterfaceFiles(updateSuperGroups, updateClusterReverseFileList);
            clusterFileCompress.UpdateCrystInterfaceFilesNotInClusters(updateSuperGroups, false);

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Updating sequence files.");
            ProtCidSettings.logWriter.WriteLine("Updating sequence files.");
            SeqFastaGenerator seqFastaGen = new SeqFastaGenerator();

            seqFastaGen.UpdateSeqFastaFiles(updateSuperGroups);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Update biological units and entrypfamarch data for protcid");
            ProtCidSettings.logWriter.WriteLine("Update biological units and entrypfamarch data for protcid");
            BiolUnitSumInfo buSumInfo = new BiolUnitSumInfo();

            buSumInfo.UpdateBiolUnits(updateEntries);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Updating cluster interface images.");
            ProtCidSettings.logWriter.WriteLine("Updating cluster interface images.");
            InterfaceImageGen imageGen = new InterfaceImageGen();

            imageGen.UpdateClusterInterfaceImages(updateSuperGroups);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Updating indexes for super groups tables");
            ProtCidSettings.logWriter.WriteLine("Updating indexes for super groups tables");
            DbBuilderHelper.UpdateIndexes("PfamSuper", ProtCidSettings.protcidDbConnection);
            ProtCidSettings.logWriter.WriteLine("Done!");
            ProtCidSettings.logWriter.Flush();

            ProtCidSettings.progressInfo.progStrQueue.Enqueue("Update Done!");
            ProtCidSettings.progressInfo.threadFinished = true;
            ProtCidSettings.logWriter.Flush();
        }