public void TestFilter()
    {
      var pro1 = new IdentifiedProtein("P1");
      pro1.Peptides.Add(new IdentifiedPeptide(new IdentifiedSpectrum() { Charge = 1 }));
      pro1.Peptides.Add(new IdentifiedPeptide(new IdentifiedSpectrum() { Charge = 2 }));

      var pro2 = new IdentifiedProtein("P2");
      pro2.Peptides.Add(new IdentifiedPeptide(new IdentifiedSpectrum() { Charge = 3 }));

      var g1 = new IdentifiedProteinGroup();
      g1.Add(pro1);

      var g2 = new IdentifiedProteinGroup();
      g2.Add(pro2);

      IdentifiedResult ir = new IdentifiedResult();
      ir.Add(g1);
      ir.Add(g2);

      Assert.AreEqual(2, ir.Count);
      Assert.AreEqual(3, ir.GetSpectra().Count);

      ir.Filter(m => { return m.Spectrum.Query.Charge > 1; });

      Assert.AreEqual(2, ir.Count);
      Assert.AreEqual(2, ir.GetSpectra().Count);
      ir.GetSpectra().All(m => { return m.Charge > 1; });

      ir.Filter(m => { return m.Spectrum.Query.Charge > 2; });
      Assert.AreEqual(1, ir.Count);
      Assert.AreEqual(1, ir.GetSpectra().Count);
      ir.GetSpectra().All(m => { return m.Charge > 2; });

      Assert.AreEqual("P2", ir[0][0].Name);
    }
    public void TestKeepDistinctPeptideOnly()
    {
      var spectrum1 = new IdentifiedSpectrum();
      var pep1 = spectrum1.NewPeptide();

      var spectrum2 = new IdentifiedSpectrum();
      var pep2 = spectrum2.NewPeptide();

      var spectrum3 = new IdentifiedSpectrum();
      var pep3 = spectrum3.NewPeptide();

      var spectrum4 = new IdentifiedSpectrum();
      var pep4 = spectrum4.NewPeptide();

      var protein1 = new IdentifiedProtein();
      protein1.Peptides.Add(pep1);
      protein1.Peptides.Add(pep2);

      var protein2 = new IdentifiedProtein();
      protein2.Peptides.Add(pep1);
      protein2.Peptides.Add(pep3);

      var protein3 = new IdentifiedProtein();
      protein3.Peptides.Add(pep2);
      protein3.Peptides.Add(pep4);

      var g1 = new IdentifiedProteinGroup();
      g1.Add(protein1);

      var g2 = new IdentifiedProteinGroup();
      g2.Add(protein2);

      var g3 = new IdentifiedProteinGroup();
      g3.Add(protein3);

      IIdentifiedResult ir = new IdentifiedResult();
      ir.Add(g1);
      ir.Add(g2);
      ir.Add(g3);

      new DistinctResultDistiller().KeepDistinctPeptideOnly(ir);

      Assert.AreEqual(2, ir.Count);
      Assert.AreEqual(g2, ir[0]);
      Assert.AreEqual(g3, ir[1]);

      Assert.AreEqual(1, ir[0].GetPeptides().Count);
      Assert.AreEqual(spectrum3, ir[0].GetPeptides()[0]);

      Assert.AreEqual(1, ir[1].GetPeptides().Count);
      Assert.AreEqual(spectrum4, ir[1].GetPeptides()[0]);
    }
    public void TestFilter2()
    {
      var spectrum = new IdentifiedSpectrum();
      spectrum.Query.FileScan.LongFileName = "ABDCDD.12.123.2.dat";

      var pro1 = new IdentifiedProtein("P1");
      pro1.Peptides.Add(new IdentifiedPeptide(spectrum) { Sequence = "AAAAAAA" });

      var pro2 = new IdentifiedProtein("P2");
      pro2.Peptides.Add(new IdentifiedPeptide(spectrum) { Sequence = "BBBBBBB" });

      var g1 = new IdentifiedProteinGroup();
      g1.Add(pro1);
      g1.Add(pro2);

      IdentifiedResult ir = new IdentifiedResult();
      ir.Add(g1);

      Assert.AreEqual(1, ir.Count);
      Assert.AreEqual(2, ir[0].Count);
      Assert.AreEqual(1, ir.GetSpectra().Count);

      ir.Filter(m =>
      {
        return m.Sequence.Contains('A');
      });

      Assert.AreEqual(1, ir.Count);
      Assert.AreEqual(1, ir[0].Count);
      Assert.AreEqual(1, ir.GetSpectra().Count);
      Assert.AreSame(pro1, ir[0][0]);

      ir.Filter(m =>
      {
        return m.Sequence.Contains('C');
      });

      Assert.AreEqual(0, ir.Count);
    }
        //======================================================================
        // Write
        /// <summary>
        /// Writes the value, quality and timestamp for a set of items.
        /// </summary>
        /// <param name="items">The set of item values to write.</param>
        /// <returns>The results of the write operation for each item.</returns>
        public IdentifiedResult[] Write(ItemValue[] items)
        {
            if (items == null) throw new ArgumentNullException("items");

            if (items.Length == 0)
            {
                return new Opc.IdentifiedResult[0];
            }

            lock (this)
            {
                ArrayList results = new ArrayList(items.Length);

                foreach (ItemValue item in items)
                {
                    IdentifiedResult result = m_cache.Write(item.ItemName, m_culture.Name, item);

                    if (result == null)
                    {
                        result = new IdentifiedResult(item, ResultID.E_FAIL);
                    }

                    result.ClientHandle = item.ClientHandle;

                    results.Add(result);
                }

                return (IdentifiedResult[])results.ToArray(typeof(IdentifiedResult));
            }
        }
        /// <summary>
        /// Writes the value of the specified item property.
        /// </summary>
        public Opc.IdentifiedResult Write(
            PropertyID       propertyID,
            Opc.Da.ItemValue value)
        {
            // initialize result and validate property.
            IdentifiedResult result = new IdentifiedResult();

            result.ItemName       = m_itemID;
            result.ItemPath       = null;
            result.ResultID       = ValidatePropertyID(propertyID, accessRights.writable);
            result.DiagnosticInfo = null;

            // handle value writes.
            if (propertyID == Property.VALUE)
            {
                // copy value.
                m_value = Opc.Convert.Clone(value.Value);

                // update quality if specified.
                if (value.QualitySpecified)
                {
                    m_quality = value.Quality;
                }

                // update timestamp if specified.
                if (value.TimestampSpecified)
                {
                    m_timestamp = value.Timestamp;
                }

                // return results.
                return result;
            }

            // lookup property description.
            PropertyDescription property = PropertyDescription.Find(propertyID);

            if (property == null)
            {
                result.ResultID = ResultID.Da.E_INVALID_PID;
                return result;
            }

            // check datatype.
            if (!property.Type.IsInstanceOfType(value.Value))
            {
                result.ResultID = ResultID.Da.E_BADTYPE;
                return result;
            }

            // write non-value
            switch (propertyID.Code)
            {
                // standard properties.
                case DATATYPE:      { m_datatype     = (System.Type)value.Value;                 return result; }
                case QUALITY:       { m_quality      = (Quality)value.Value;                     return result; }
                case TIMESTAMP:     { m_timestamp    = (DateTime)value.Value;                    return result; }
                case ACCESSRIGHTS:  { m_accessRights = (accessRights)value.Value;                return result; }
                case SCANRATE:      { m_scanRate     = (float)value.Value;                       return result; }
                case EUTYPE:        { m_euType       = (euType)value.Value;                      return result; }
                case EUINFO:        { m_euInfo       = (string[])Opc.Convert.Clone(value.Value); return result; }
                case HIGHEU:        { m_maxValue     = (double)value.Value;                      return result; }
                case LOWEU:         { m_minValue     = (double)value.Value;                      return result; }

                // other defined properties.
                default:
                {
                    if (!m_properties.Contains(propertyID))
                    {
                        result.ResultID = ResultID.Da.E_INVALID_PID;
                        return result;
                    }

                    m_properties[propertyID] = Opc.Convert.Clone(value.Value);
                    break;
                }
            }

            // write complete.
            return result;
        }