Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/21/10 02:58:15 (14 years ago)
Author:
swagner
Message:

Worked on allele frequency analysis (#1234)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Analysis/3.3/AlleleFrequencyAnalyzer.cs

    r4623 r4631  
    4444      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
    4545    }
     46    public LookupParameter<T> BestKnownSolutionParameter {
     47      get { return (LookupParameter<T>)Parameters["BestKnownSolution"]; }
     48    }
    4649    public ValueLookupParameter<ResultCollection> ResultsParameter {
    4750      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
    4851    }
    49     public LookupParameter<T> BestKnownSolutionParameter {
    50       get { return (LookupParameter<T>)Parameters["BestKnownSolution"]; }
     52    public ValueParameter<BoolValue> StoreAlleleFrequenciesHistoryParameter {
     53      get { return (ValueParameter<BoolValue>)Parameters["StoreAlleleFrequenciesHistory"]; }
    5154    }
    52     public LookupParameter<AlleleFrequencyArray> AlleleFrequenciesParameter {
    53       get { return (LookupParameter<AlleleFrequencyArray>)Parameters["AlleleFrequencies"]; }
     55    public ValueParameter<IntValue> UpdateIntervalParameter {
     56      get { return (ValueParameter<IntValue>)Parameters["UpdateInterval"]; }
     57    }
     58    public LookupParameter<IntValue> UpdateCounterParameter {
     59      get { return (LookupParameter<IntValue>)Parameters["UpdateCounter"]; }
    5460    }
    5561
     
    5965      Parameters.Add(new ScopeTreeLookupParameter<T>("Solution", "The solutions whose alleles should be analyzed."));
    6066      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the solutions which should be analyzed."));
     67      Parameters.Add(new LookupParameter<T>("BestKnownSolution", "The best known solution."));
    6168      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the allele frequency analysis results should be stored."));
    62       Parameters.Add(new LookupParameter<T>("BestKnownSolution", "The best known solution."));
    63       Parameters.Add(new LookupParameter<AlleleFrequencyArray>("AlleleFrequencies", "The frequencies of the alleles in the current iteration."));
     69      Parameters.Add(new ValueParameter<BoolValue>("StoreAlleleFrequenciesHistory", "True if the history of all allele frequencies should be stored.", new BoolValue(false)));
     70      Parameters.Add(new ValueParameter<IntValue>("UpdateInterval", "The interval in which the allele frequency analysis should be applied.", new IntValue(1)));
     71      Parameters.Add(new LookupParameter<IntValue>("UpdateCounter", "The value which counts how many times the operator was called since the last update."));
    6472    }
    6573
    6674    public override IOperation Apply() {
    67       bool max = MaximizationParameter.ActualValue.Value;
    68       ItemArray<T> solutions = SolutionParameter.ActualValue;
    69       ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
    70       ResultCollection results = ResultsParameter.ActualValue;
    71       T bestKnownSolution = BestKnownSolutionParameter.ActualValue;
     75      int updateInterval = UpdateIntervalParameter.Value.Value;
     76      IntValue updateCounter = UpdateCounterParameter.ActualValue;
     77      if (updateCounter == null) {
     78        updateCounter = new IntValue(updateInterval);
     79        UpdateCounterParameter.ActualValue = updateCounter;
     80      } else updateCounter.Value++;
    7281
    73       int bestIndex = -1;
    74       if (!max) bestIndex = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
    75       else bestIndex = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
     82      if (updateCounter.Value == updateInterval) {
     83        updateCounter.Value = 0;
    7684
    77       Allele[] bestAlleles = CalculateAlleles(solutions[bestIndex]);
    78       Allele[] bestKnownAlleles = null;
    79       if (bestKnownSolution != null) {
    80         bestKnownAlleles = CalculateAlleles(bestKnownSolution);
     85        bool max = MaximizationParameter.ActualValue.Value;
     86        ItemArray<T> solutions = SolutionParameter.ActualValue;
     87        ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
     88        T bestKnownSolution = BestKnownSolutionParameter.ActualValue;
     89        bool storeHistory = StoreAlleleFrequenciesHistoryParameter.Value.Value;
     90
     91        // calculate index of current best solution
     92        int bestIndex = -1;
     93        if (!max) bestIndex = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
     94        else bestIndex = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
     95
     96        // calculate allels of current best and (if available) best known solution
     97        Allele[] bestAlleles = CalculateAlleles(solutions[bestIndex]);
     98        Allele[] bestKnownAlleles = null;
     99        if (bestKnownSolution != null)
     100          bestKnownAlleles = CalculateAlleles(bestKnownSolution);
     101
     102        // calculate allele frequencies
     103        var frequencies = solutions.SelectMany((s, index) => CalculateAlleles(s).Select(a => new { Allele = a, Quality = qualities[index] })).
     104                          GroupBy(x => x.Allele.Id).
     105                          Select(x => new AlleleFrequency(x.Key,
     106                                                          x.Count() / ((double)solutions.Length),
     107                                                          x.Average(a => a.Allele.Impact),
     108                                                          x.Average(a => a.Quality.Value),
     109                                                          bestKnownAlleles == null ? false : bestKnownAlleles.Any(a => a.Id == x.Key),
     110                                                          bestAlleles.Any(a => a.Id == x.Key)));
     111
     112        // fetch results collection
     113        ResultCollection results;
     114        if (!ResultsParameter.ActualValue.ContainsKey("Allele Frequency Analysis Results")) {
     115          results = new ResultCollection();
     116          ResultsParameter.ActualValue.Add(new Result("Allele Frequency Analysis Results", results));
     117        } else {
     118          results = (ResultCollection)ResultsParameter.ActualValue["Allele Frequency Analysis Results"].Value;
     119        }
     120
     121        // store allele frequencies
     122        AlleleFrequencyArray frequenciesArray = new AlleleFrequencyArray(frequencies);
     123        if (!results.ContainsKey("Allele Frequencies"))
     124          results.Add(new Result("Allele Frequencies", frequenciesArray));
     125        else
     126          results["Allele Frequencies"].Value = frequenciesArray;
     127
     128        // store allele frequencies history
     129        if (storeHistory) {
     130          if (!results.ContainsKey("Allele Frequencies History")) {
     131            ItemCollection<AlleleFrequencyArray> history = new ItemCollection<AlleleFrequencyArray>();
     132            history.Add(frequenciesArray);
     133            results.Add(new Result("Allele Frequencies History", history));
     134          } else {
     135            ((ItemCollection<AlleleFrequencyArray>)results["Allele Frequencies History"].Value).Add(frequenciesArray);
     136          }
     137        }
     138
     139        // store alleles data table
     140        DataTable allelesTable;
     141        if (!results.ContainsKey("Alleles")) {
     142          allelesTable = new DataTable("Alleles");
     143          results.Add(new Result("Alleles", allelesTable));
     144          allelesTable.Rows.Add(new DataRow("Unique Alleles"));
     145        } else {
     146          allelesTable = (DataTable)results["Alleles"].Value;
     147        }
     148        allelesTable.Rows["Unique Alleles"].Values.Add(frequenciesArray.Length);
    81149      }
    82 
    83       var frequencies = solutions.SelectMany((s, index) => CalculateAlleles(s).Select(a => new { Allele = a, Quality = qualities[index] })).
    84                         GroupBy(x => x.Allele.Id).
    85                         Select(x => new AlleleFrequency(x.Key,
    86                                                         x.Count() / ((double)solutions.Length),
    87                                                         x.Average(a => a.Allele.Impact),
    88                                                         x.Average(a => a.Quality.Value),
    89                                                         bestKnownAlleles == null ? false : bestKnownAlleles.Any(a => a.Id == x.Key),
    90                                                         bestAlleles.Any(a => a.Id == x.Key)));
    91 
    92       AlleleFrequencyArray frequenciesArray = new AlleleFrequencyArray(frequencies.OrderBy(x => x.AverageImpact));
    93       AlleleFrequenciesParameter.ActualValue = frequenciesArray;
    94       if (results.ContainsKey("Allele Frequencies"))
    95         results["Allele Frequencies"].Value = frequenciesArray;
    96       else
    97         results.Add(new Result("Allele Frequencies", frequenciesArray));
    98 
    99150      return base.Apply();
    100151    }
Note: See TracChangeset for help on using the changeset viewer.