/// <summary> /// Given a multistate matrix, returns a mapping of multistateKeys to lists of binary keys. Keys are assumed to be sequences of the form /// pos@aa1#aa2#...#aaN.. Each item is the multistateKey of the format pos@aa1#aa2#...#aaN. Each value is /// an enumeration of binary keys that map to that multistate key. /// </summary> /// <param name="binaryMatrix"></param> /// <returns></returns> public static IEnumerable <KeyValuePair <string, IEnumerable <string> > > MultistateToBinaryMapping(Matrix <string, string, SufficientStatistics> multistateMatrix) { var multToBinaryPositions = from multKey in multistateMatrix.RowKeys where multKey.Contains('@') let merAndPos = Tabulate.GetMerAndPos(multKey) let binaryKeys = merAndPos.Key.Split('#').Select(aa => (int)merAndPos.Value + "@" + aa) select new KeyValuePair <string, IEnumerable <string> >(multKey, binaryKeys); var nonAaKeys = from key in multistateMatrix.RowKeys where !key.Contains('@') select new KeyValuePair <string, IEnumerable <string> >(key, new List <string>() { key }); var allKeys = nonAaKeys.Concat(multToBinaryPositions); return(allKeys); //var temp = multistateMatrix.RowKeys.Select(key => // Tabulate.GetMerAndPos(key)).Select(merAndPos => // new KeyValuePair<string, IEnumerable<string>>(merAndPos.Value + "@" + merAndPos.Key, // merAndPos.Key.Split('#').Select(aa => (int)merAndPos.Value + "@" + aa))); }
/// <summary> /// Given a binary matrix, returns a mapping of multistateKeys to lists of binary keys. Keys are assumed to be sequences of the form /// pos@AA or AA@pos. All other keys will be left as binary. Each item is the multistateKey of the format pos@aa1#aa2#...#aaN. Each value is /// an enumeration of binary keys that map to that multistate key. /// </summary> /// <param name="binaryMatrix"></param> /// <returns></returns> public static IEnumerable <KeyValuePair <string, IEnumerable <string> > > BinaryToMultistateMapping(Matrix <string, string, SufficientStatistics> binaryMatrix) { var multToBinaryPositions = from binaryKey in binaryMatrix.RowKeys where binaryKey.Contains('@') let merAndPos = Tabulate.GetMerAndPos(binaryKey) let pos = (int)merAndPos.Value group binaryKey by pos into g select new KeyValuePair <string, IEnumerable <string> > ( g.Key + "@" + g.OrderBy(bk => bk).StringJoin("#").Replace(g.Key + "@", ""), g.ToList() ); var nonAaKeys = from key in binaryMatrix.RowKeys where !key.Contains('@') select new KeyValuePair <string, IEnumerable <string> >(key, new List <string>() { key }); var allKeys = nonAaKeys.Concat(multToBinaryPositions); return(allKeys); }