Example #1
0
        public List <NamedSequence> ConvertToSequences(Matrix <string, string, SufficientStatistics> matrix)
        {
            bool isMultistate = matrix.RowKeys.Any(key => key.Contains('#'));

            if (!isMultistate)
            {
                matrix = new BinaryToMultistateView <string, string, SufficientStatistics>(matrix, Tabulate.BinaryToMultistateMapping(matrix), MBT.Escience.ValueConverter.SufficientStatisticsToInt);
            }

            var rowKeysAsMerAndPos = (from key in matrix.RowKeys
                                      let merAndPos = Tabulate.GetMerAndPos(key)
                                                      orderby merAndPos.Value
                                                      select new
            {
                Position0 = (int)merAndPos.Value - 1,
                Chars = merAndPos.Key.Split('#'),
                Key = key
            }).ToList();

            List <NamedSequence> result = new List <NamedSequence>(matrix.ColCount);

            for (int pid = 0; pid < matrix.ColCount; pid++)
            {
                StringBuilder seq = new StringBuilder(matrix.RowCount);
                foreach (var rowKey in rowKeysAsMerAndPos)
                {
                    Helper.CheckCondition(seq.Length <= rowKey.Position0, "There appears to be multiple keys with the same position.");
                    while (seq.Length < rowKey.Position0)
                    {
                        seq.Append(MissingChar);
                    }
                    DiscreteStatistics stats = matrix.GetValueOrMissing(rowKey.Key, matrix.ColKeys[pid]).AsDiscreteStatistics();
                    if (stats.IsMissing())
                    {
                        seq.Append(MissingChar);
                    }
                    else
                    {
                        string thisChar;
                        if (rowKey.Chars.Length == 1)
                        {
                            thisChar = rowKey.Chars[0];
                            Helper.CheckCondition((int)stats == 1);
                        }
                        else
                        {
                            thisChar = rowKey.Chars[stats];
                        }
                        Helper.CheckCondition(thisChar.Length == 1, "State {0} is too long. Must be a single character.", thisChar);
                        seq.Append(thisChar);
                    }
                }
                result.Add(new NamedSequence(matrix.ColKeys[pid], seq.ToString()));
            }

            return(result);
        }
        public void ValidateDiscreteStatisticsGetMissingInstance()
        {
            DiscreteStatistics discreteStat = DiscreteStatistics.GetMissingInstance;

            Assert.IsTrue(discreteStat.IsMissing());
        }