//This class helps the generateHypo classin the Additional Rules section by translating letters in the artable into numbers.
 private String translet(CompositionHypothesisEntry one, Boundary ar)
 {
     String Ans = "";
     foreach (char i in ar.Formula)
     {
         if (char.IsUpper(i))
         {
             try
             {
                 Ans = Ans + Convert.ToString(one.eqCount[Convert.ToString(i)]);
             }
             catch
             {
                 MessageBox.Show("Invalid letter in the additional rules table.");
             }
         }
         else
         {
             Ans = Ans + Convert.ToString(i);
         }
     }
     return Ans;
 }
        //This function reads the generate composition page and save it to a variable of class "generator".
        public GlycanHypothesisCombinatorialGenerator getGenerator()
        {
            //compotable is used to store the data from the composition table before they are used.
            List<GlycanCompositionTable> compotable = new List<GlycanCompositionTable>();
            //arTable is used to store the data from additional rules table before they're used.
            List<Boundary> arTable = new List<Boundary>();
            //GD is used to store all the data that will be returned.
            GlycanHypothesisCombinatorialGenerator GD = new GlycanHypothesisCombinatorialGenerator();

            String currentpath = Application.StartupPath + "\\compositionsCurrent.cpos";
            try
            {
                FileStream reading = new FileStream(currentpath, FileMode.Open, FileAccess.Read);
                StreamReader readcompo = new StreamReader(reading);
                //Read the first line to skip the column names:
                String Line1 = readcompo.ReadLine();
                String[] headers = Line1.Split(',');
                List<string> elementIDs = new List<string>();
                bool moreElements = true;
                int sh= 2;
                while (moreElements)
                {
                    if (headers[sh] != "Lower Bound")
                    {
                        elementIDs.Add(headers[sh]);
                        sh++;
                    }
                    else
                        moreElements = false;
                }

                bool firstrow = true;
                //Read the other lines for compTable data.
                while (readcompo.Peek() >= 0)
                {
                    GlycanCompositionTable compTable = new GlycanCompositionTable();
                    if (firstrow)
                    {
                        compTable.elementIDs = elementIDs;
                        firstrow = false;
                    }
                    String Line = readcompo.ReadLine();
                    String[] eachentry = Line.Split(',');
                    //When next line is the Modification line, it breaks.
                    if (eachentry[0] == "Modification%^&!@#*()iop")
                        break;
                    compTable.Letter = eachentry[0];
                    compTable.Molecule = eachentry[1];
                    for (int i = 2; i < elementIDs.Count + 2; i++)
                    {
                        compTable.elementAmount.Add(Convert.ToInt32(eachentry[i]));
                    }
                    List<String> bounds = new List<String>();
                    bounds.Add(eachentry[elementIDs.Count + 2]);
                    bounds.Add(eachentry[elementIDs.Count + 3]);
                    compTable.Bound = bounds;
                    compotable.Add(compTable);
                }
                //Send compotable data to GD
                GD.comTable = compotable;
                //Populate the Modification Table
                String modiLine = readcompo.ReadLine();
                //Send Modification data to GD
                GD.Modification = modiLine.Split(',');

                //Populate the additional rules table
                while (readcompo.Peek() >= 0)
                {
                    String Line = readcompo.ReadLine();
                    String[] eachentry = Line.Split(',');
                    if (eachentry[0] != "" && eachentry[2] != "")
                    {
                        Boundary areTable = new Boundary();
                        areTable.Formula = eachentry[0];
                        areTable.Relationship = eachentry[1];
                        areTable.Constraint = Convert.ToString(eachentry[2]);
                        arTable.Add(areTable);
                    }
                }
                //Send arTable data to GD.
                GD.aTable = arTable;

                readcompo.Close();
                reading.Close();
            }
            catch (Exception compoex)
            {
                MessageBox.Show("Error in loading GlycanCompositions Table. Error:" + compoex);
            }
            return GD;
        }
        //genHypo runs these following functions one by one to do its task://///////////////////////////////////////////////////////
        private GlycanHypothesisCombinatorialGenerator obtainNewGD(GlycanHypothesisCombinatorialGenerator GD)
        {
            List<GlycanCompositionTable> Table = new List<GlycanCompositionTable>();
            Table.AddRange(GD.comTable);
            Boolean hasLetter = false;
            for (int i = 0; i < Table.Count(); i++)
            {
                String bound = Table[i].Bound[0];
                foreach (char j in bound)
                {
                    if (char.IsUpper(j))
                    {
                        hasLetter = true;
                    }
                }
                if (hasLetter == true)
                {
                    hasLetter = false;
                    Boundary AT = new Boundary();
                    AT.Constraint = Table[i].Letter;
                    AT.Relationship = "≤";
                    AT.Formula = bound;
                    GD.aTable.Add(AT);
                }
                String bound2 = Table[i].Bound[1];
                foreach (char j in bound2)
                {
                    if (char.IsUpper(j))
                    {
                        hasLetter = true;
                    }
                }
                if (hasLetter == true)
                {
                    hasLetter = false;
                    Boundary AT = new Boundary();
                    AT.Constraint = Table[i].Letter;
                    AT.Relationship = "≥";
                    AT.Formula = bound2;
                    GD.aTable.Add(AT);
                }

            }
            return GD;
        }