Ejemplo n.º 1
0
        private void VerifyFoldChanges(SrmDocument testDocument, GroupComparisonDef groupComparisonDef,
                                       IDictionary <string, LinearFitResult> expectedResults)
        {
            var groupComparer = new GroupComparer(groupComparisonDef, testDocument, new QrFactorizationCache());

            foreach (var protein in testDocument.MoleculeGroups)
            {
                var             groupComparisonResult = groupComparer.CalculateFoldChanges(protein, null).FirstOrDefault();
                LinearFitResult expectedResult;
                if (expectedResults.TryGetValue(protein.Name, out expectedResult))
                {
                    Assert.IsNotNull(groupComparisonResult);
                    var foldChange = groupComparisonResult.LinearFitResult;
                    Assert.AreEqual(expectedResult.EstimatedValue, foldChange.EstimatedValue, 1E-5);
                    Assert.AreEqual(expectedResult.DegreesOfFreedom, foldChange.DegreesOfFreedom);
                    Assert.AreEqual(expectedResult.StandardError, foldChange.StandardError, 1E-5);
                    Assert.AreEqual(expectedResult.TValue, foldChange.TValue, 1E-5);
                    Assert.AreEqual(expectedResult.PValue, foldChange.PValue, 1E-5);
                }
                else
                {
                    Assert.IsNull(groupComparisonResult);
                    var standardPeptides = protein.Molecules.Where(mol => !string.IsNullOrEmpty(mol.GlobalStandardType)).ToArray();
                    Assert.AreNotEqual(0, standardPeptides.Length);
                }
            }
        }
Ejemplo n.º 2
0
        public void CompareAveragingWithMSstats()
        {
            var srmDocument       = LoadRatPlasmaDocument();
            var documentContainer = new MemoryDocumentContainer();

            documentContainer.SetDocument(documentContainer.Document, srmDocument);
            GroupComparisonModel model = new GroupComparisonModel(documentContainer, null);

            model.GroupComparisonDef = GroupComparisonDef.EMPTY.ChangeControlAnnotation("Condition")
                                       .ChangeControlValue("Healthy")
                                       .ChangeIdentityAnnotation("BioReplicate")
                                       .ChangeSummarizationMethod(SummarizationMethod.AVERAGING)
                                       .ChangePerProtein(false);
            var expectedValues = MsStatsTestUtil.ReadExpectedResults(typeof(MSstatsAveragingTest),
                                                                     "RatPlasmaTestingResult.csv");
            var groupComparer = new GroupComparer(model.GroupComparisonDef, srmDocument, new QrFactorizationCache());

            foreach (var protein in srmDocument.PeptideGroups)
            {
                foreach (var peptide in protein.Peptides)
                {
                    var result         = groupComparer.CalculateFoldChange(new GroupComparisonSelector(protein, peptide, IsotopeLabelType.light, null, new GroupIdentifier("Diseased")), null);
                    var expectedResult = expectedValues[peptide.Peptide.Sequence];
                    Assert.AreEqual(expectedResult.EstimatedValue, result.LinearFitResult.EstimatedValue,
                                    (expectedResult.StandardError + result.LinearFitResult.StandardError) * 2, peptide.Peptide.Sequence);
                }
            }
        }
Ejemplo n.º 3
0
        protected GroupComparer CreateGroupHeader(BaseComparer sc)
        {
            GroupComparer gc = new GroupComparer(sc.ColumnCollection, sc.ListIndex, sc.ObjectArray);

            IndexList.Add(gc);
            return(gc);
        }
Ejemplo n.º 4
0
 public void CompareAveragingWithMSstats()
 {
     var srmDocument = LoadRatPlasmaDocument();
     var documentContainer = new MemoryDocumentContainer();
     documentContainer.SetDocument(documentContainer.Document, srmDocument);
     GroupComparisonModel model = new GroupComparisonModel(documentContainer, null);
     model.GroupComparisonDef = GroupComparisonDef.EMPTY.ChangeControlAnnotation("Condition")
         .ChangeControlValue("Healthy")
         .ChangeIdentityAnnotation("BioReplicate")
         .ChangeSummarizationMethod(SummarizationMethod.AVERAGING)
         .ChangePerProtein(false);
     var expectedValues = MsStatsTestUtil.ReadExpectedResults(typeof (MSstatsAveragingTest),
         "RatPlasmaTestingResult.csv");
     var groupComparer = new GroupComparer(model.GroupComparisonDef, srmDocument, new QrFactorizationCache());
     foreach (var protein in srmDocument.PeptideGroups)
     {
         foreach (var peptide in protein.Peptides)
         {
             var result = groupComparer.CalculateFoldChange(new GroupComparisonSelector(protein, peptide, IsotopeLabelType.light, null, new GroupIdentifier("Diseased")), null);
             var expectedResult = expectedValues[peptide.Peptide.Sequence];
             Assert.AreEqual(expectedResult.EstimatedValue, result.LinearFitResult.EstimatedValue,
                 (expectedResult.StandardError + result.LinearFitResult.StandardError) * 2, peptide.Peptide.Sequence);
         }
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataManager"/> class.
 /// </summary>
 private DataManager()
 {
     this.groups           = new List <Group>();
     this.comparer         = new GroupComparer();
     this.customers        = new List <Customer>();
     this.customerComparer = new CustomerComparer();
 }
Ejemplo n.º 6
0
        public override bool is_condition_met(List <List <int> > hand, params object[] args)
        {
            int                winTile  = ((int)args[0]) / 4;
            List <Meld>        melds    = (List <Meld>)args[1];
            bool               isTsumo  = (bool)args[2];
            var                gc       = new GroupComparer <int>();
            List <List <int> > openSets = melds.Select(x => x.tiles_34).ToList();
            var                chiSets  = hand.Where(x => Utils.is_chi(x) && x.Contains(winTile) && !(openSets.Contains(x, gc)));
            var                ponSets  = hand.Where(x => Utils.is_pon_or_kan(x));

            var closed_pons = new List <List <int> >();

            foreach (var pon in ponSets)
            {
                if (openSets.Contains(pon, gc))
                {
                    continue;
                }
                //if we do the ron on syanpon wait our pon will be consider as open
                // and it is not 789999 set
                if (pon.Contains(winTile) && !isTsumo && chiSets.Count() == 0)
                {
                    continue;
                }
                closed_pons.Add(pon);
            }
            return(closed_pons.Count == 3);
        }
Ejemplo n.º 7
0
        public void TestProteinAbundance()
        {
            var srmDocument       = LoadRatPlasmaDocument();
            var documentContainer = new MemoryDocumentContainer();

            documentContainer.SetDocument(srmDocument, documentContainer.Document);
            var skylineDataSchema      = new SkylineDataSchema(documentContainer, DataSchemaLocalizer.INVARIANT);
            GroupComparisonModel model = new GroupComparisonModel(documentContainer, null);

            model.GroupComparisonDef = GroupComparisonDef.EMPTY.ChangeControlAnnotation("Condition")
                                       .ChangeControlValue("Healthy")
                                       .ChangeSummarizationMethod(SummarizationMethod.AVERAGING)
                                       .ChangePerProtein(true);
            var groupComparer = new GroupComparer(model.GroupComparisonDef, srmDocument, new QrFactorizationCache());

            foreach (var moleculeGroup in srmDocument.MoleculeGroups)
            {
                if (moleculeGroup.Molecules.Any(mol => null != mol.GlobalStandardType))
                {
                    continue;
                }
                var foldChangeResult = groupComparer.CalculateFoldChange(new GroupComparisonSelector(moleculeGroup, null, IsotopeLabelType.light, null, new GroupIdentifier("Diseased")), null);
                var xValues          = new List <double>();
                var yValues          = new List <double>();
                var protein          = new Protein(skylineDataSchema, new IdentityPath(moleculeGroup.PeptideGroup));
                foreach (var proteinResult in protein.Results.Values)
                {
                    var abundance = proteinResult.Abundance;
                    if (!abundance.HasValue)
                    {
                        continue;
                    }

                    var condition = proteinResult.Replicate.ChromatogramSet.Annotations.GetAnnotation("Condition");
                    if (condition == "Healthy")
                    {
                        xValues.Add(0);
                    }
                    else if (condition == "Diseased")
                    {
                        xValues.Add(1);
                    }
                    yValues.Add(Math.Log(abundance.Value));
                }
                Assert.AreEqual(xValues.Count, foldChangeResult.ReplicateCount);

                if (!xValues.Any())
                {
                    continue;
                }
                var yStatistics        = new Statistics(yValues);
                var xStatistics        = new Statistics(xValues);
                var slope              = yStatistics.Slope(xStatistics);
                var actualFoldChange   = Math.Exp(slope);
                var expectedFoldChange = Math.Pow(2.0, foldChangeResult.LinearFitResult.EstimatedValue);

                AssertEx.AreEqual(expectedFoldChange, actualFoldChange, .01);
            }
        }
Ejemplo n.º 8
0
 public GroupComparisonResults(
     GroupComparer groupComparer,
     IEnumerable<GroupComparisonResult> resultRows,
     DateTime startTime,
     DateTime endTime)
 {
     GroupComparer = groupComparer;
     ResultRows = ImmutableList.ValueOf(resultRows);
     StartTime = startTime;
     EndTime = endTime;
 }
Ejemplo n.º 9
0
        public void CanGroup_All_Elements_Contains_Children()
        {
            GroupColumn groupComparer = new GroupColumn("GroupItem", 1, ListSortDirection.Ascending);
            var         tableStrategy = GroupTableStrategyFactory(groupComparer);

            tableStrategy.Bind();
            foreach (BaseComparer element in tableStrategy.IndexList)
            {
                GroupComparer checkComparer = element as GroupComparer;
                Assert.That(checkComparer.IndexList.Count > 0);
            }
        }
Ejemplo n.º 10
0
        public void CanGroup_All_Elements_are_GroupComparer()
        {
            GroupColumn groupComparer = new GroupColumn("GroupItem", 1, ListSortDirection.Ascending);

            ICSharpCode.Reports.Core.ListStrategy.TableStrategy tableStrategy = GroupTableStrategyFactory(groupComparer);
            tableStrategy.Bind();
            foreach (BaseComparer element in tableStrategy.IndexList)
            {
                GroupComparer checkComparer = element as GroupComparer;
                Assert.IsNotNull(checkComparer);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Index, group, sort and insert an entity to the collection.
        /// If group doesn't exist, it will be created and sorted.
        /// </summary>
        /// <param name="item">The item to insert into.</param>
        /// <param name="ignoreBackendFlatList">Whether ignore backend list or not.</param>
        private void AddInternal(CommonViewItemModel item, bool ignoreBackendFlatList = false)
        {
            if (item != null)
            {
                var index = Indexer.GetIndexForGroup(item);
                // If group exists
                if (_itemsIndex.ContainsKey(index))
                {
                    _itemsIndex[index].Add(item);
                }
                // Or create group
                else
                {
                    var newCreatedGroup = new GroupedItems(index, Indexer, ItemComparer);
                    _itemsIndex.Add(index, newCreatedGroup);
                    var processed = false;
                    if (Items.Count != 0)
                    {
                        for (int i = 0; i < Items.Count; i++)
                        {
                            if (GroupComparer.Compare(Items[i].Title, index) >= 0)
                            {
                                Items.Insert(i, newCreatedGroup);
                                processed = true;
                                break;
                            }
                        }
                    }

                    if (!processed)
                    {
                        Items.Add(newCreatedGroup);
                    }

                    // Add item
                    newCreatedGroup.Add(item);
                }

                // Add backend item (optional)
                if (!ignoreBackendFlatList)
                {
                    _entityCopies.Add(item);
                }

                // Notify change if required (precondition: IsEmpty == true)
                if (IsEmpty)
                {
                    IsEmpty = false;
                }
            }
        }
Ejemplo n.º 12
0
        private int RenderGroupIfNeeded(object item, object previous, int index)
        {
            bool isAlternateRow = index % 2 != 0 && (AlternatingGroupFooterTemplate != null || AlternatingGroupHeaderTemplate != null);

            if (GroupComparer.Compare(item, previous) == 1)
            {
                RenderTemplate(isAlternateRow ? AlternatingGroupFooterTemplate : GroupFooterTemplate, previous, index);
            }
            if (GroupComparer.Compare(previous, item) == -1)
            {
                RenderTemplate(isAlternateRow ? AlternatingGroupHeaderTemplate : GroupHeaderTemplate, item, index++);
            }

            return(index);
        }
Ejemplo n.º 13
0
        public override bool is_condition_met(List <List <int> > hand, params object[] args)
        {
            var ponSets = hand.Where(x => Utils.is_pon_or_kan(x));
            var sous    = new List <List <int> >();
            var pins    = new List <List <int> >();
            var mans    = new List <List <int> >();

            foreach (var pon in ponSets)
            {
                if (Utils.is_sou(pon[0]))
                {
                    sous.Add(pon);
                }
                if (Utils.is_man(pon[0]))
                {
                    mans.Add(pon);
                }
                if (Utils.is_pin(pon[0]))
                {
                    pins.Add(pon);
                }
            }

            var gc = new GroupComparer <int>();

            foreach (var sou in sous)
            {
                var simpleSou = sou.Select(x => Utils.simplify(x)).ToList();
                foreach (var man in mans)
                {
                    var simpleMan = man.Select(x => Utils.simplify(x)).ToList();
                    foreach (var pin in pins)
                    {
                        var simplePin = pin.Select(x => Utils.simplify(x)).ToList();
                        if (simpleMan.SequenceEqual(simplePin) && simpleMan.SequenceEqual(simpleSou))
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 14
0
 protected static void ShowIndexList(IndexList list)
 {
     foreach (BaseComparer element in list)
     {
         string        s  = String.Format("{0} ", element.ObjectArray[0]);
         GroupComparer gc = element as GroupComparer;
         if (gc != null)
         {
             s = s + "GroupHeader";
             if (gc.IndexList != null)
             {
                 s = s + String.Format(" <{0}> Childs", gc.IndexList.Count);
             }
             System.Console.WriteLine(s);
             foreach (BaseComparer c in gc.IndexList)
             {
                 Console.WriteLine("---- {0}", c.ObjectArray[0]);
             }
         }
     }
 }
        public GroupComparisonRefinementData(SrmDocument document, double adjustedPValCutoff,
                                             double foldChangeCutoff, int?msLevel, List <GroupComparisonDef> groupComparisonDefs,
                                             SrmSettingsChangeMonitor progressMonitor)
        {
            _progressMonitor    = progressMonitor;
            _foldChangeCutoff   = foldChangeCutoff;
            _adjustedPValCutoff = adjustedPValCutoff;
            if (msLevel.HasValue)
            {
                _msLevel = msLevel.Value;
            }
            else
            {
                _msLevel = document.PeptideTransitions.Any(t => !t.IsMs1) ? 2 : 1;
            }
            Data = new List <List <GroupComparisonRow> >();

            foreach (var groupComparisonDef in groupComparisonDefs)
            {
                var groupComparer = new GroupComparer(groupComparisonDef, document, _qrFactorizationCache);
                var results       = GroupComparisonModel.ComputeResults(groupComparer, document, null, null, progressMonitor);


                var adjustedPValues = PValues.AdjustPValues(results.Select(
                                                                row => row.LinearFitResult.PValue)).ToArray();

                var foldChangeList = new List <GroupComparisonRow>();
                for (int iRow = 0; iRow < results.Count; iRow++)
                {
                    var resultRow = results[iRow];
                    FoldChangeResult foldChangeResult = new FoldChangeResult(groupComparisonDef.ConfidenceLevel,
                                                                             adjustedPValues[iRow], resultRow.LinearFitResult, double.NaN);
                    foldChangeList.Add(new GroupComparisonRow(resultRow.Selector.Protein, resultRow.Selector.Peptide,
                                                              resultRow.Selector.MsLevel,
                                                              foldChangeResult));
                }

                Data.Add(foldChangeList);
            }
        }
Ejemplo n.º 16
0
        protected void BuildGroup(IndexList list)
        {
            string compVal = String.Empty;

            IndexList.Clear();
            IndexList childList = null;

            foreach (BaseComparer element in list)
            {
                string groupValue = ExtractValue(element);

                if (compVal != groupValue)
                {
                    childList = new IndexList();
                    GroupComparer gc = CreateGroupHeader(element);
                    gc.IndexList = childList;
                }
                CreateGroupedChildren(childList, element);
                compVal = groupValue;
            }
//			ShowIndexList(IndexList);
        }
Ejemplo n.º 17
0
        public override bool is_condition_met(List <List <int> > hand, params object[] args)
        {
            var chis = hand.Where(x => Utils.is_chi(x));

            if (chis.Count() < 3)
            {
                return(false);
            }
            var sou = new List <List <int> >();
            var pin = new List <List <int> >();
            var man = new List <List <int> >();

            foreach (var chi in chis)
            {
                if (U.is_sou(chi[0]))
                {
                    sou.Add(chi);
                }
                else if (U.is_man(chi[0]))
                {
                    man.Add(chi);
                }
                else if (U.is_pin(chi[0]))
                {
                    pin.Add(chi);
                }
            }
            var suits = new List <List <List <int> > >
            {
                sou, man, pin
            };

            var one = new List <int> {
                0, 1, 2
            };
            var two = new List <int> {
                3, 4, 5
            };
            var three = new List <int> {
                6, 7, 8
            };
            var comp = new GroupComparer <int>();

            foreach (var suit in suits)
            {
                if (suit.Count() < 3)
                {
                    continue;
                }
                var simpleSets = new List <List <int> >();
                foreach (var set in suit)
                {
                    simpleSets.Add(new List <int> {
                        U.simplify(set[0]), U.simplify(set[1]), U.simplify(set[2])
                    });
                }
                if (simpleSets.Contains(one, comp) && simpleSets.Contains(two, comp) && simpleSets.Contains(three, comp))
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 18
0
 public GroupMerger(Sort groupSort)
 {
     groupComp  = new GroupComparer <T>(groupSort);
     queue      = new TreeSet <MergedGroup <T> >(groupComp);
     groupsSeen = new HashMap <T, MergedGroup <T> >();
 }
Ejemplo n.º 19
0
		protected GroupComparer CreateGroupHeader (BaseComparer sc)
		{
			GroupComparer gc = new GroupComparer(sc.ColumnCollection,sc.ListIndex,sc.ObjectArray);
			IndexList.Add(gc);
			return gc;
		}
Ejemplo n.º 20
0
 public GroupMerger(Sort groupSort)
 {
     groupComp  = new GroupComparer <T>(groupSort);
     queue      = new JCG.SortedSet <MergedGroup <T> >(groupComp);
     groupsSeen = new JCG.Dictionary <T, MergedGroup <T> >();
 }
Ejemplo n.º 21
0
        private void GetGroupsAction()
        {
            bool IsUserGroupsExist = false;
            IsError = false;

            if (IsUsersFound && SelectedSearchedUser != null && SelectedSearchedUser.AccountName != null)
            {
                IsBusy = true;
                UpdateUserGroupsCommand.IsEnabled = false;
                UserGroups.Clear();

                UserGroupServiceAgent.GetUserGroups(SelectedSearchedUser.AccountName, (userGroups,oe) =>
                {
                    _dispatcher.BeginInvoke(new Action(() =>
                       {
                           //IsBusy = true;
                           UserGroups.Clear();
                           //if (userGroups != null && userGroups.Count > 0)
                           if (userGroups != null)
                           {
                               IsUserGroupsExist = true;
                               userGroups.ForEach(group =>
                               {
                                   var igroup = new Group()
                                   {
                                       Id = group.Id,
                                       Name = group.Name,
                                       IsEnabled = false,
                                       OriginalIsSelected = true,
                                       IsSelected = true,
                                       IsModified = false,
                                       Owner = group.Owner,
                                       OwnerTitle = group.OwnerTitle
                                   };

                                   UserGroups.Add(igroup);
                               });

                               UserGroupServiceAgent.GetAllGroups((groups, ie) =>
                               {
                                   _dispatcher.BeginInvoke(new Action(() =>
                                   {
                                       if (!IsUserGroupsExist) UserGroups.Clear();

                                       if (groups != null && groups.Count > 0)
                                       {
                                           groups.ForEach(group =>
                                           {
                                               var igroup = new Group()
                                               {
                                                   Id = group.Id,
                                                   Name = group.Title,
                                                   IsEnabled = false,
                                                   OriginalIsSelected = false,
                                                   IsSelected = false,
                                                   IsModified = false,
                                                   EditOwner = group.Owner,
                                                   OwnerTitle= group.OwnerTitle
                                               };

                                               if (IsUserGroupsExist)
                                               {
                                                   GroupComparer comparer = new GroupComparer();
                                                   if (!UserGroups.Contains(igroup, comparer))
                                                       UserGroups.Add(igroup);
                                               }
                                               else
                                               {
                                                   UserGroups.Add(igroup);
                                               }
                                           });
                                       }
                                       else
                                       {
                                           IsError = true;
                                           ErrorMessage = Logger.BuildLogMessageForUser(ie, "No groups found!");
                                       }
                                       IsBusy = false;
                                   }));
                               });
                           }
                           else
                           {
                               IsError = true;
                               ErrorMessage = Logger.BuildLogMessageForUser(oe, "No groups found!");
                               IsBusy = false;
                           }

                       }));

                    
                }
                );

            }

        }
Ejemplo n.º 22
0
        private GroupComparisonResults ComputeComparisonResults(GroupComparer groupComparer, SrmDocument document,
            CancellationToken cancellationToken)
        {
            DateTime startTime = DateTime.Now;

            List<GroupComparisonResult> results = new List<GroupComparisonResult>();
            if (groupComparer.IsValid)
            {
                var peptideGroups = document.PeptideGroups.ToArray();
                for (int i = 0; i < peptideGroups.Length; i++)
                {
                    int percentComplete = 100 * i / peptideGroups.Length;
                    lock (_lock)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        _percentComplete = percentComplete;
                    }
                    var peptideGroup = peptideGroups[i];
                    IEnumerable<PeptideDocNode> peptides;
                    if (groupComparer.ComparisonDef.PerProtein)
                    {
                        peptides = new PeptideDocNode[] {null};
                    }
                    else
                    {
                        peptides = peptideGroup.Peptides;
                    }
                    foreach (var peptide in peptides)
                    {
                        results.AddRange(groupComparer.CalculateFoldChanges(peptideGroup, peptide));
                    }
                }
            }
            DateTime endTime = DateTime.Now;
            return new GroupComparisonResults(groupComparer, results, startTime, endTime);
        }
Ejemplo n.º 23
0
        // synchronously process the request, fill up responseBuffer, and return statusCode
        public int Process(HttpCtx ctx, int dummy)
        {
            // first step: check if the cache already has the calculated result for this set of parameters
            var cacheKey = ctx.Params.GetParamKey();

            if (CachedResults.TryGetValue(cacheKey, out var cachedList))
            {
                // bingo! already calculated before
                composeResponse(ctx, cachedList, ctx.Params.Limit);
                return(200);
            }

            var  limit                = 0;
            var  order                = 0;
            Keys keyMask              = new Keys();
            var  keys                 = new List <Keys>();
            var  queryMask            = new GroupQueryMask();
            bool empty                = false;
            ArraySegment <int> likers = null;

            int locationFrom = -1, locationTo = -1;
            int statusFrom = -1, statusTo = -1;
            int sexFrom = -1, sexTo = -1;
            int yearFrom = -1, yearTo = -1;
            int interestFrom = -1, interestTo = -1;



            foreach (var query in ctx.Params)
            {
                var value = query.Value;
                if (value.IsEmpty)
                {
                    return(400);
                }
                if (query.Key == "query_id")
                {
                }   // ignore
                else
                if (query.Key == "limit")
                {
                    if (!value.TryToInt(out limit))
                    {
                        return(400);
                    }
                    if (limit <= 0 || limit > 50)
                    {
                        return(400);
                    }
                }
                else
                if (query.Key == "keys")
                {
                    foreach (var key in value.Split(','))
                    {
                        var bit = Keys.None;
                        if (key == "sex")
                        {
                            bit = Keys.Sex;
                            keys.Add(Keys.Sex);
                            queryMask |= GroupQueryMask.KeySex;
                        }
                        else
                        if (key == "status")
                        {
                            bit = Keys.Status;
                            keys.Add(Keys.Status);
                            queryMask |= GroupQueryMask.KeyStatus;
                        }
                        else
                        if (key == "interests")
                        {
                            bit = Keys.Interests;
                            keys.Add(Keys.Interests);
                            queryMask |= GroupQueryMask.KeyInterest;
                        }
                        else
                        if (key == "country")
                        {
                            bit = Keys.Country;
                            keys.Add(Keys.Country);
                            queryMask |= GroupQueryMask.KeyCountry;
                        }
                        else
                        if (key == "city")
                        {
                            bit = Keys.City;
                            keys.Add(Keys.City);
                            queryMask |= GroupQueryMask.KeyCity;
                        }
                        if (bit == Keys.None)
                        {
                            return(400);
                        }
                        else
                        {
                            keyMask |= bit;
                        }
                    }
                    if (keyMask == Keys.None)
                    {
                        return(400);
                    }
                }
                else
                if (query.Key == "order")
                {
                    if (!value.TryToInt(out order))
                    {
                        return(400);
                    }
                    if (order != 1 && order != -1)
                    {
                        return(400);
                    }
                }
                else
                if (query.Key == "sex")
                {
                    if (value == "m")
                    {
                        sexFrom = sexTo = 1;
                    }
                    else
                    if (value == "f")
                    {
                        sexFrom = sexTo = 2;
                    }
                    else
                    {
                        return(400);
                    }
                    queryMask |= GroupQueryMask.Sex;
                }
                else
                if (query.Key == "status")
                {
                    if (value == DtoAccount.s_Free)
                    {
                        statusFrom = statusTo = 1;
                    }
                    else
                    if (value == DtoAccount.s_Taken)
                    {
                        statusFrom = statusTo = 2;
                    }
                    else
                    if (value == DtoAccount.s_Complicated)
                    {
                        statusFrom = statusTo = 3;
                    }
                    else
                    {
                        return(400);
                    }
                    queryMask |= GroupQueryMask.Status;
                }
                else
                if (query.Key == "country")
                {
                    if (store.Countries.TryGetValue(value, out IRange countryInd))
                    {
                        locationFrom = locationTo = countryInd.Index;
                    }
                    else
                    {
                        empty = true;
                    }
                    queryMask |= GroupQueryMask.Country;
                }
                else
                if (query.Key == "city")
                {
                    if (store.Cities.TryGetValue(value, out IRange cityInd))
                    {
                        locationFrom = locationTo = cityInd.Index;
                    }
                    else
                    {
                        empty = true;
                    }
                    queryMask |= GroupQueryMask.City;
                }
                else
                if (query.Key == "interests")
                {
                    if (store.Interests.TryGetValue(value, out IRange interestRange))
                    {
                        interestFrom = interestTo = interestRange.Index;
                    }
                    else
                    {
                        empty = true;
                    }
                    queryMask |= GroupQueryMask.Interests;
                }
                else
                if (query.Key == "likes")
                {
                    if (!value.TryToInt(out int extId))
                    {
                        return(400);
                    }
                    if (!Mapper.ExtIdToIntId(extId, out int id))
                    {
                        return(404);
                    }
                    var acct = store.Accounts[id];
                    if (acct.LikedByIdx == 0 || acct.LikedByCount == 0)
                    {
                        empty = true;
                    }
                    else
                    {
                        likers = new ArraySegment <int>(store.LikedBy, acct.LikedByIdx, acct.LikedByCount);
                    }
                    queryMask |= GroupQueryMask.Likes;
                }
                else
                if (query.Key == "birth")
                {
                    if (value.TryToInt(out int birthYear))
                    {
                        if (store.BirthYears.TryGetValue(birthYear, out IRange yearMap))
                        {
                            yearFrom = yearTo = yearMap.Index;
                        }
                        else
                        {
                            empty = true;
                        }
                    }
                    else
                    {
                        return(400);
                    }
                    queryMask |= GroupQueryMask.Birth;
                }
                else
                if (query.Key == "joined")
                {
                    if (value.TryToInt(out int joinYear))
                    {
                        if (store.JoinYears.TryGetValue(joinYear, out IRange yearMap))
                        {
                            yearFrom = yearTo = yearMap.Index;
                        }
                        else
                        {
                            empty = true;
                        }
                    }
                    else
                    {
                        return(400);
                    }
                    queryMask |= GroupQueryMask.Joined;
                }
                else // all other parameters are invalid
                {
                    return(400);
                }
            }

            if (limit == 0 || order == 0)
            {
                return(400);
            }

            if (empty)       // shortcut, no groups will be found
            {
                return(212); // empty groups
            }
            // extend the from/to ranges based on the keys
            {
                if (keyMask.HasFlag(Keys.City) && locationFrom < 0)
                {
                    locationFrom = 0;
                    locationTo   = store.Cities.Count;
                }
                if (keyMask.HasFlag(Keys.Country) && locationFrom < 0)
                {
                    locationFrom = 0;
                    locationTo   = store.Countries.Count;
                }
                if (keyMask.HasFlag(Keys.Sex) && sexFrom < 0)
                {
                    sexFrom = 1;
                    sexTo   = 2;
                }
                if (keyMask.HasFlag(Keys.Status) && statusFrom < 0)
                {
                    statusFrom = 1;
                    statusTo   = 3;
                }
                if (keyMask.HasFlag(Keys.Interests) && interestFrom < 0)
                {
                    interestFrom = 1;
                    interestTo   = store.Interests.Count - 1;
                }
            }

            // find the correct hypercube out of 4, or create new one for likes
            IHypercube cube     = null;
            CubeKind   cubeKind = CubeKind.None;

            if (keyMask.HasFlag(Keys.City) || queryMask.HasFlag(GroupQueryMask.City))
            {
                if (queryMask.HasFlag(GroupQueryMask.Birth))
                {
                    if (queryMask.HasFlag(GroupQueryMask.Likes))
                    {
                        cubeKind = CubeKind.CityBirth;
                    }
                    else
                    {
                        cube = store.CubeCityBirth;
                    }
                }
                else
                {
                    if (queryMask.HasFlag(GroupQueryMask.Likes))
                    {
                        cubeKind = CubeKind.CityJoined;
                    }
                    else
                    {
                        cube = store.CubeCityJoined;
                    }
                }
            }
            else
            {
                if (queryMask.HasFlag(GroupQueryMask.Birth))
                {
                    if (queryMask.HasFlag(GroupQueryMask.Likes))
                    {
                        cubeKind = CubeKind.CountryBirth;
                    }
                    else
                    {
                        cube = store.CubeCountryBirth;
                    }
                }
                else
                {
                    if (queryMask.HasFlag(GroupQueryMask.Likes))
                    {
                        cubeKind = CubeKind.CountryJoined;
                    }
                    else
                    {
                        cube = store.CubeCountryJoined;
                    }
                }
            }

            HypercubeHash cubeHash = null;

            // for likes, create a custom hypercube and fill it up
            if (cube == null && likers != null)
            {
                if (!Pool <HypercubeHash> .TryGet(out cubeHash))
                {
                    cubeHash = new HypercubeHash(cubeKind, 20000);
                }
                else
                {
                    cubeHash.Reset(cubeKind);
                }
                cube = cubeHash;
                foreach (var id in likers)
                {
                    var acct        = store.Accounts[id];
                    int statusIdx   = (acct.Flags >> 1) & 3;
                    int sexIdx      = (acct.Flags & Account.Male) > 0 ? 1 : 2;
                    int locationIdx = 0;
                    int yearIdx     = 0;
                    switch (cubeKind)
                    {
                    case CubeKind.CityBirth:
                        locationIdx = acct.CityIdx;
                        yearIdx     = acct.BirthIdx;
                        break;

                    case CubeKind.CityJoined:
                        locationIdx = acct.CityIdx;
                        yearIdx     = acct.JoinedIdx;
                        break;

                    case CubeKind.CountryBirth:
                        locationIdx = acct.CountryIdx;
                        yearIdx     = acct.BirthIdx;
                        break;

                    case CubeKind.CountryJoined:
                        locationIdx = acct.CountryIdx;
                        yearIdx     = acct.JoinedIdx;
                        break;

                    default:
                        throw new Exception("Unexpected CubeKind");
                    }

                    cubeHash.Include(locationIdx, statusIdx, sexIdx, yearIdx, acct.InterestMask);
                }
            }
            else
            {
                cubeKind = cube.Kind;
            }

            // calculate the size of the group
            var size =
                (locationTo - locationFrom + 1) *
                (statusTo - statusFrom + 1) *
                (sexTo - sexFrom + 1) *
                (yearTo - yearFrom + 1) *
                (interestTo - interestFrom + 1);

            var groupList = new List <GroupItem>(size);

            cube.Slice(
                locationFrom, locationTo,
                statusFrom, statusTo,
                sexFrom, sexTo,
                yearFrom, yearTo,
                interestFrom, interestTo,
                visit);

            // local function, called by the cube
            void visit(int location, int status, int sex, int year, int interest, int count)
            {
                var gi = new GroupItem {
                    store = store, Count = count
                };

                if (keyMask.HasFlag(Keys.Interests))
                {
                    gi.interest = (byte)interest;
                }
                if (keyMask.HasFlag(Keys.Status))
                {
                    gi.status = (byte)status;
                }
                if (keyMask.HasFlag(Keys.Sex))
                {
                    gi.sex = (byte)sex;
                }
                if (keyMask.HasFlag(Keys.City))
                {
                    gi.city = (short)location;
                }
                if (keyMask.HasFlag(Keys.Country))
                {
                    gi.country = (byte)location;
                }
                groupList.Add(gi);
            }

            if (groupList.Count == 0)
            {
                if (cubeHash != null)
                {
                    Pool <HypercubeHash> .Release(cubeHash);
                }
                CachedResults.TryAdd(cacheKey, groupList);
                return(212);
            }

            var groupComparer = new GroupComparer(order, keys);

            groupList.Sort(groupComparer);
            composeResponse(ctx, groupList, limit);

            // add to the cache for later reuse
            if (groupList.Count > 50)
            {
                groupList.RemoveRange(50, groupList.Count - 50);
            }
            CachedResults.TryAdd(cacheKey, groupList);

            // clean up
            if (cubeHash != null)
            {
                Pool <HypercubeHash> .Release(cubeHash);
            }

            ctx.ContextType = "GetGroup";
            return(200);
        }
Ejemplo n.º 24
0
 private void RestartCalculation()
 {
     var srmDocument = Document;
     var groupComparisonDef = GroupComparisonDef;
     if (_results != null)
     {
         if (Equals(_results.Document, srmDocument) && Equals(_results.GroupComparer.ComparisonDef, groupComparisonDef))
         {
             return;
         }
     }
     if (null != _cancellationTokenSource)
     {
         _cancellationTokenSource.Cancel();
         _cancellationTokenSource = null;
     }
     if (0 == _modelChangedListeners.Count)
     {
         return;
     }
     if (_groupComparer == null || !Equals(srmDocument, _groupComparer.SrmDocument) ||
         !Equals(groupComparisonDef, _groupComparer.ComparisonDef))
     {
         _groupComparer = new GroupComparer(groupComparisonDef, srmDocument, _qrFactorizationCache);
     }
     _cancellationTokenSource = new CancellationTokenSource();
     if (null != GroupComparisonDef && null != Document)
     {
         _percentComplete = 0;
         GroupComparer groupComparer = _groupComparer;
         var cancellationToken = _cancellationTokenSource.Token;
         AddErrorHandler(Task.Factory.StartNew(() =>
         {
             var results = ComputeComparisonResults(groupComparer, srmDocument, cancellationToken);
             lock (_lock)
             {
                 if (!cancellationToken.IsCancellationRequested)
                 {
                     Results = results;
                     _percentComplete = 100;
                 }
             }
         }, _cancellationTokenSource.Token));
     }
 }