protected override void WriteDistilled(IEnumerable <KeyValuePair <string, List <HlaIGenotype> > > patientsAndGenotypes, bool multipleGenotypesPerPatient) { var patientsAndGenotypesAsList = patientsAndGenotypes.ToList(); Dictionary <string, MultinomialStatistics> pidToMultinomial = new Dictionary <string, MultinomialStatistics>(); Dictionary <string, HlaIGenotype> pidGenoNameToGeno = new Dictionary <string, HlaIGenotype>(patientsAndGenotypesAsList.Count); foreach (var pidAndGenos in patientsAndGenotypes) { if (pidAndGenos.Value.Count == 1) { pidGenoNameToGeno.Add(pidAndGenos.Key, pidAndGenos.Value.Single()); } else { pidToMultinomial.Add(pidAndGenos.Key, MultinomialStatistics.GetInstance(pidAndGenos.Value.Select(g => g.Probability))); // keep track of the probabilities in this dictionary HlaIGenotype unionGenotype = HlaIGenotype.Union(pidAndGenos.Value.ToArray()); pidGenoNameToGeno.Add(pidAndGenos.Key, unionGenotype); //Add the union genotype to the matrix. for (int i = 0; i < pidAndGenos.Value.Count; i++) { string pidGenoId = pidAndGenos.Key + "_" + i; pidGenoNameToGeno.Add(pidGenoId, pidAndGenos.Value[i]); } } } Matrix <string, string, SufficientStatistics> m = DenseMatrix <string, string, SufficientStatistics> .CreateDefaultInstance( HlaEnumerator.GenerateHlas(patientsAndGenotypes.SelectMany(pidAndGenos => pidAndGenos.Value)), pidGenoNameToGeno.Keys, MissingStatistics.GetInstance()); foreach (string hlastr in m.RowKeys) { HlaI hla = HlaI.Parse(hlastr); foreach (string pid in m.ColKeys) { bool?match = pidGenoNameToGeno[pid].Matches(hla, MixtureSemantics); m.SetValueOrMissing(hlastr, pid, BooleanStatistics.GetInstance(match)); //m.SetValueOrMissing(hlastr, pid, !match.HasValue ? "?" : match.Value ? "1" : "0"); } } m.WriteDense(this.Out.CreateTextOrUseConsole()); string baseName = Path.GetFileNameWithoutExtension(this.Out.ToString()); string probabilityFile = this.Out.ToString() == "-" ? "HaplotypeCompletionProbs.txt" : this.Out.ToString().Replace(baseName, baseName + "_haplotypeProbs"); pidToMultinomial.WriteDelimitedFile(probabilityFile); }
public MultinomialToSufficientStatisticsConverter() : base( m => m.IsDefinite ? (SufficientStatistics)DiscreteStatistics.GetInstance(m.BestOutcome) : (SufficientStatistics)MultinomialStatistics.GetInstance(m), s => { var mult = s as MultinomialStatistics; if (mult != null) { return(mult.Multinomial); } int i = (int)s.AsDiscreteStatistics(); return(new Multinomial(i + 1, i)); } ) { }
public static Matrix <string, string, SufficientStatistics> ConvertToMatrix(List <NamedSequence> sequences, MixtureSemantics mix, BinaryOrMultistate dataType, bool keepOneValueVariables) { var colNames = sequences.Select(s => s.Name); //var rowNames = (from posAndAa in sequences.SelectMany(s => s.AASeq) // where !AASeq.IsMissing(posAndAa.Value) // let pos = posAndAa.Key // let aas = posAndAa.Value // from c in aas // let merAndPos = pos + "@" + c // orderby pos, c // select merAndPos).Distinct(); var rowNames = (from posAndAa in sequences.SelectMany(s => s.AASeq) where posAndAa.Value.Count == 1 && !AASeq.IsMissing(posAndAa.Value) let pos = posAndAa.Key let aas = posAndAa.Value let c = aas.First() let merAndPos = pos + "@" + c orderby pos, c select merAndPos).Distinct(); var posToRowNames = (from row in rowNames let pos = (int)Tabulate.GetMerAndPos(row).Value group row by pos into g select new KeyValuePair <int, List <string> >(g.Key, g.ToList())).ToDictionary(); Matrix <string, string, SufficientStatistics> m = DenseMatrix <string, string, SufficientStatistics> .CreateDefaultInstance(rowNames, colNames, MissingStatistics.GetInstance()); foreach (var seq in sequences) { foreach (var posAndAa in seq.AASeq) { int pos = posAndAa.Key; if (!posToRowNames.ContainsKey(pos)) { Helper.CheckCondition(AASeq.IsMissing(posAndAa.Value), "Something's wrong. We thinking everyone is missing at position {0}, but {1} has {2}", pos, seq.Name, posAndAa.Value); continue; } var relevantRows = posToRowNames[pos]; bool isMissing = AASeq.IsMissing(posAndAa.Value); var myRows = posAndAa.Value.Select(c => pos + "@" + c).ToList(); foreach (var row in relevantRows) { SufficientStatistics value; if (isMissing) { value = MissingStatistics.GetInstance(); } else if (!myRows.Contains(row)) { value = (BooleanStatistics)false; //in all cases, this is false } else if (myRows.Count == 1) { value = (BooleanStatistics)true; } else { switch (mix) { case MixtureSemantics.Any: value = (BooleanStatistics)true; break; //Any means we say you have both case MixtureSemantics.Pure: value = (BooleanStatistics)false; break; //Pure means we say you have neither case MixtureSemantics.Uncertainty: value = MissingStatistics.GetInstance(); break; //Uncertainty says we don't know which you have. case MixtureSemantics.Distribution: double pTrue = 1.0 / myRows.Count; value = MultinomialStatistics.GetInstance(new double[] { 1 - pTrue, pTrue }); break; default: throw new NotImplementedException("Missing a case."); } } m.SetValueOrMissing(row, seq.Name, value); } } } if (!keepOneValueVariables) { m = m.SelectRowsView(m.RowKeys.Where(row => m.RowView(row).Values.Distinct().Count() > 1)); } switch (dataType) { case BinaryOrMultistate.Binary: return(m); case BinaryOrMultistate.Multistate: return(new BinaryToMultistateView <string, string, SufficientStatistics>(m, Tabulate.BinaryToMultistateMapping(m), ValueConverter.SufficientStatisticsToMultinomial)); default: throw new NotImplementedException("Missing a case"); } }