Esempio n. 1
0
        /// <summary>
        /// Create a DensePairAnsi object from a sequence of RowKeyColKeyValue triples.
        /// </summary>
        /// <param name="tripleEnumerable">a sequence of RowKeyColKeyValue</param>
        /// <returns>A DensePairAnsi object</returns>
        public static DensePairAnsi GetInstanceFromSparse(IEnumerable <RowKeyColKeyValue <string, string, UOPair <char> > > tripleEnumerable)
        {
            DensePairAnsi densePairAnsi = new DensePairAnsi();

            densePairAnsi.GetInstanceFromSparseInternal(tripleEnumerable);
            return(densePairAnsi);
        }
Esempio n. 2
0
        //Similar code elsewhere, but hard to move static code to common location
        //Would be nice to convert UOPair<double>, etc to UOPair<char>

        /// <summary>
        /// Write a matrix to DensePairAnsi file format
        /// </summary>
        /// <param name="matrix">The matrix to write</param>
        /// <param name="textWriter">The TextWriter to write to</param>
        /// <param name="parallelOptions">A ParallelOptions instance that configures the multithreaded behavior of this operation.</param>
        public static void WriteDensePairAnsi(this Matrix <string, string, UOPair <char> > matrix, TextWriter textWriter, ParallelOptions parallelOptions)
        {
            textWriter.WriteLine("var\t{0}", matrix.ColKeys.StringJoin("\t"));
            foreach (string rowKey in matrix.RowKeys)
            {
                textWriter.Write(rowKey);
                textWriter.Write("\t");
                int rowIndex = matrix.IndexOfRowKey[rowKey];
                List <UOPair <byte> > storeList = new List <UOPair <byte> >(matrix.ColCount);
                for (int colIndex = 0; colIndex < matrix.ColCount; ++colIndex)
                {
                    UOPair <char> value;
                    UOPair <byte> store;
                    if (matrix.TryGetValue(rowIndex, colIndex, out value))
                    {
                        store = DensePairAnsi.StaticValueToStore(value);
                    }
                    else
                    {
                        store = DensePairAnsi.StaticStoreMissingValue;
                    }
                    storeList.Add(store);
                }
                Helper.CheckCondition(storeList.Count == matrix.ColCount, Properties.Resource.ExpectedStoreListCountToEqualColCount, storeList.Count, matrix.ColCount);
                string s = DensePairAnsi.StoreListToString(storeList, matrix.ColCount);
                textWriter.WriteLine(s);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a DensePairAnsi object from a file in dense pair ansi format.
        /// </summary>
        /// <param name="densePairAnsiFileName">a file in dense pair ansi format</param>
        /// <param name="parallelOptions">A ParallelOptions instance that configures the multithreaded behavior of this operation.</param>
        /// <returns>the DensePairAnsi object</returns>
        public static DensePairAnsi GetInstance(string densePairAnsiFileName, ParallelOptions parallelOptions)
        {
            DensePairAnsi densePairAnsi = new DensePairAnsi();

            densePairAnsi.GetInstanceInternal(densePairAnsiFileName, parallelOptions);
            return(densePairAnsi);
        }
Esempio n. 4
0
        /// <summary>
        /// Parses directly from the SparseFile format. Useful if memory must be concerved.
        /// </summary>
        /// <param name="inputSparseFileName">Name of a file in Sparse format.</param>
        /// <returns>An instance of DensePairAnsi.</returns>
        public static DensePairAnsi GetInstanceFromSparse(string inputSparseFileName)
        {
            DensePairAnsi densePairAnsi = new DensePairAnsi();

            densePairAnsi.GetInstanceFromSparseInternal(inputSparseFileName);
            return(densePairAnsi);
        }
Esempio n. 5
0
        /// <summary>
        /// Creates an DensePairAnsi with values missing.
        /// </summary>
        /// <param name="rowKeySequence">A sequence of row keys. The items will become the RowKeys of the Matrix.</param>
        /// <param name="colKeySequence">A sequence of colKeys. The items will come the ColKeys of the Matrix.</param>
        /// <param name="missingValue">The special Missing value, which must be (UO '?','?')</param>
        /// <returns>A new, empty, DensePairAnsi</returns>
        static public DensePairAnsi CreateEmptyInstance(IEnumerable <string> rowKeySequence, IEnumerable <string> colKeySequence, UOPair <char> missingValue)
        {
            Helper.CheckCondition(missingValue.Equals(StaticMissingValue), "For DensePairAnsi the missingValue must be (UO '?', '?')"); //OK to use Equals because UOPair<char> can't be null.
            DensePairAnsi densePairAnsi = new DensePairAnsi();

            densePairAnsi.InternalCreateEmptyInstance(rowKeySequence, colKeySequence);
            return(densePairAnsi);
        }
Esempio n. 6
0
        /// <summary>
        /// Creates an DensePairAnsi with values missing.
        /// </summary>
        /// <param name="rowKeySequence">A sequence of row keys. The items will become the RowKeys of the Matrix.</param>
        /// <param name="colKeySequence">A sequence of colKeys. The items will come the ColKeys of the Matrix.</param>
        /// <param name="missingValue">The special Missing value, which must be (UO '?','?')</param>
        /// <returns>A new, empty, DensePairAnsi</returns>
        static public DensePairAnsi CreateEmptyInstance(IEnumerable <string> rowKeySequence, IEnumerable <string> colKeySequence, UOPair <char> missingValue)
        {
            //OK to use Equals because UOPair<char> can't be null.
            Helper.CheckCondition(missingValue.Equals(StaticMissingValue), Properties.Resource.DensePairAnsiMissingValueSignatureMustBe);
            DensePairAnsi densePairAnsi = new DensePairAnsi();

            densePairAnsi.InternalCreateEmptyInstance(rowKeySequence, colKeySequence);
            return(densePairAnsi);
        }
Esempio n. 7
0
        /// <summary>
        /// Converts matrix to a DensePairAnsi. Even if the matrix is already an densePairAnsi, a new one is created..
        /// </summary>
        /// <param name="matrix">The matrix to convert from</param>
        /// <param name="parallelOptions">Options for controlling any parallelism.</param>
        /// <returns>A densePairAnsi version of the matrix</returns>
        public static DensePairAnsi ToDensePairAnsi(this Matrix <string, string, UOPair <char> > matrix, ParallelOptions parallelOptions)
        {
            var densePairAnsi = DensePairAnsi.CreateEmptyInstance(matrix.RowKeys, matrix.ColKeys, DensePairAnsi.StaticMissingValue);

            //Console.WriteLine("Convert no more than {0} values", matrix.RowCount * matrix.ColCount);
            //CounterWithMessages counterWithMessages = new CounterWithMessages("adding value #{0}", 100000, null);
            Parallel.ForEach(matrix.RowKeyColKeyValues, parallelOptions, triple =>
            {
                //counterWithMessages.Increment();
                densePairAnsi[triple.RowKey, triple.ColKey] = triple.Value;
            });

            return(densePairAnsi);
        }
        public static DensePairAnsi GetInstance(
            IEnumerable <IGrouping <string, KeyValuePair <string, UOPair <char> > > > snpGroupsCidToNucPairEnumerable)
        {
            if (snpGroupsCidToNucPairEnumerable == null)
            {
                throw new ArgumentNullException("snpGroupsCidToNucPairEnumerable");
            }

            DensePairAnsi densePairAnsi = new DensePairAnsi();

            densePairAnsi.ColSerialNumbers  = new SerialNumbers <string>();
            densePairAnsi.RowKeyToStoreList = new Dictionary <string, List <UOPair <byte> > >();

            foreach (var snpGroupsCidToNucPair in snpGroupsCidToNucPairEnumerable)
            {
                List <UOPair <byte> > storeArray = Enumerable.Repeat(StaticStoreMissingValue, densePairAnsi.ColSerialNumbers.Count).ToList();
                string var = snpGroupsCidToNucPair.Key;
                densePairAnsi.RowKeyToStoreList.Add(var, storeArray);

                foreach (var cidAndNucPair in snpGroupsCidToNucPair)
                {
                    UOPair <char> valPair = cidAndNucPair.Value;

                    int colIndex = densePairAnsi.ColSerialNumbers.GetNewOrOld(cidAndNucPair.Key);
                    if (colIndex < storeArray.Count)
                    {
                        Helper.CheckCondition(storeArray[colIndex] == StaticStoreMissingValue, () => string.Format(CultureInfo.InvariantCulture, "Each pair of keys, i,e,.<{0},{1}>, should only be seen once", var, cidAndNucPair.Key));
                        storeArray[colIndex] = densePairAnsi.ValueToStore(valPair);
                    }
                    else
                    {
                        Debug.Assert(colIndex == storeArray.Count); // real assert
                        storeArray.Add(densePairAnsi.ValueToStore(valPair));
                    }
                }
                storeArray.TrimExcess();
            }

            densePairAnsi._rowKeys       = densePairAnsi.RowKeyToStoreList.Keys.ToList();
            densePairAnsi._indexOfRowKey = densePairAnsi.RowKeys.Select((key, index) => new { key, index }).ToDictionary(keyAndIndex => keyAndIndex.key, keyAndIndex => keyAndIndex.index);
            return(densePairAnsi);
        }
Esempio n. 9
0
        //!!! very, very similar to other code

        /// <summary>
        /// Creates a DensePairAnsi object from a sequence of groupings. A grouping here is a roew
        /// </summary>
        /// <param name="snpGroupsCidToNucPairEnumerable"></param>
        /// <param name="inputsMissingChar"></param>
        /// <returns></returns>
        public static DensePairAnsi GetInstance(IEnumerable <IGrouping <string, KeyValuePair <string, UOPair <char> > > >
                                                snpGroupsCidToNucPairEnumerable, char inputsMissingChar)
        {
            DensePairAnsi densePairAnsi = new DensePairAnsi();

            densePairAnsi.ColSerialNumbers  = new SerialNumbers <string>();
            densePairAnsi.RowKeyToStoreList = new Dictionary <string, List <UOPair <byte> > >();

            foreach (var snpGroupsCidToNucPair in snpGroupsCidToNucPairEnumerable)
            {
                //!!!const
                List <UOPair <byte> > storeArray = Enumerable.Repeat(StaticStoreMissingValue, densePairAnsi.ColSerialNumbers.Count).ToList();
                string var = snpGroupsCidToNucPair.Key;
                densePairAnsi.RowKeyToStoreList.Add(var, storeArray);



                foreach (var cidAndNucPair in snpGroupsCidToNucPair)
                {
                    UOPair <char> valPair = cidAndNucPair.Value;

                    int colIndex = densePairAnsi.ColSerialNumbers.GetNewOrOld(cidAndNucPair.Key);
                    if (colIndex < storeArray.Count)
                    {
                        storeArray[colIndex] = densePairAnsi.ValueToStore(valPair);
                    }
                    else
                    {
                        Debug.Assert(colIndex == storeArray.Count); // real assert
                        storeArray.Add(densePairAnsi.ValueToStore(valPair));
                    }
                }
                storeArray.TrimExcess();
            }

            densePairAnsi._rowKeys       = densePairAnsi.RowKeyToStoreList.Keys.ToList();
            densePairAnsi._indexOfRowKey = densePairAnsi.RowKeys.Select((key, index) => new { key, index }).ToDictionary(keyAndIndex => keyAndIndex.key, keyAndIndex => keyAndIndex.index);
            return(densePairAnsi);
        }