| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.oldevomodel.sitemodel; |
| 27 |
|
| 28 |
import dr.evolution.alignment.Alignment; |
| 29 |
import dr.evolution.alignment.ExtractPairs; |
| 30 |
import dr.evolution.alignment.GapUtils; |
| 31 |
import dr.evolution.alignment.SitePatterns; |
| 32 |
import dr.evolution.datatype.Nucleotides; |
| 33 |
import dr.evolution.io.Importer; |
| 34 |
import dr.evolution.io.NexusImporter; |
| 35 |
import dr.oldevomodel.substmodel.FrequencyModel; |
| 36 |
import dr.oldevomodel.substmodel.HKY; |
| 37 |
import dr.oldevomodel.substmodel.SubstitutionModel; |
| 38 |
import dr.inference.model.Parameter; |
| 39 |
import dr.math.DifferentialEvolution; |
| 40 |
import dr.math.MultivariateFunction; |
| 41 |
import dr.math.UnivariateFunction; |
| 42 |
import dr.math.UnivariateMinimum; |
| 43 |
|
| 44 |
import java.io.FileReader; |
| 45 |
import java.util.*; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class AlignmentScore implements UnivariateFunction, MultivariateFunction { |
| 52 |
|
| 53 |
SiteModel siteModel; |
| 54 |
ScoreMatrix scoreMatrix; |
| 55 |
SitePatterns sitePatterns; |
| 56 |
|
| 57 |
public AlignmentScore(ScoreMatrix scoreMatrix, SitePatterns sitePatterns) { |
| 58 |
this.scoreMatrix = scoreMatrix; |
| 59 |
this.siteModel = scoreMatrix.siteModel; |
| 60 |
this.sitePatterns = sitePatterns; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public double evaluate(double[] argument) { |
| 70 |
|
| 71 |
double kappa = argument[0]; |
| 72 |
double time = argument[1]; |
| 73 |
|
| 74 |
((HKY)((GammaSiteModel)siteModel).getSubstitutionModel()).setKappa(kappa); |
| 75 |
|
| 76 |
scoreMatrix.setTime(time); |
| 77 |
|
| 78 |
|
| 79 |
return -scoreMatrix.getScore(sitePatterns); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public double evaluate(double time) { |
| 89 |
scoreMatrix.setTime(time); |
| 90 |
|
| 91 |
|
| 92 |
return -scoreMatrix.getScore(sitePatterns); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public int getNumArguments() { |
| 101 |
return 2; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public double getLowerBound() { |
| 110 |
return 0.0; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public double getUpperBound() { |
| 119 |
return 10.0; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public double getLowerBound(int n) { |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public double getUpperBound(int n) { |
| 140 |
if (n == 0) return 100; |
| 141 |
if (n == 1) return 100; |
| 142 |
return 10; |
| 143 |
} |
| 144 |
|
| 145 |
public static double[] getAlignmentScore(ScoreMatrix scoreMatrix, SitePatterns sitePatterns) { |
| 146 |
|
| 147 |
AlignmentScore alignmentScore = new AlignmentScore(scoreMatrix, sitePatterns); |
| 148 |
|
| 149 |
double[] params = new double[alignmentScore.getNumArguments()]; |
| 150 |
DifferentialEvolution de = new DifferentialEvolution(params.length,params.length*10); |
| 151 |
for (int i = 0; i < params.length; i++) params[i] = 0.5; |
| 152 |
|
| 153 |
de.optimize(alignmentScore, params,1e-6, 1e-6); |
| 154 |
double score = alignmentScore.evaluate(params); |
| 155 |
|
| 156 |
double[] results = new double[params.length + 1]; |
| 157 |
System.arraycopy(params, 0, results, 0, params.length); |
| 158 |
results[params.length] = score; |
| 159 |
|
| 160 |
return results; |
| 161 |
} |
| 162 |
|
| 163 |
public static double getGeneticDistance(ScoreMatrix scoreMatrix, SitePatterns sitePatterns) { |
| 164 |
|
| 165 |
AlignmentScore alignmentScore = new AlignmentScore(scoreMatrix, sitePatterns); |
| 166 |
|
| 167 |
double[] params = new double[alignmentScore.getNumArguments()]; |
| 168 |
DifferentialEvolution de = new DifferentialEvolution(params.length,params.length*10); |
| 169 |
for (int i = 0; i < params.length; i++) params[i] = 0.5; |
| 170 |
|
| 171 |
de.optimize(alignmentScore, params,1e-6, 1e-6); |
| 172 |
|
| 173 |
|
| 174 |
return params[params.length-1]; |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
public static double[] getFastAlignmentScore(ScoreMatrix scoreMatrix, SitePatterns sitePatterns) { |
| 179 |
|
| 180 |
AlignmentScore alignmentScore = new AlignmentScore(scoreMatrix, sitePatterns); |
| 181 |
|
| 182 |
UnivariateMinimum de = new UnivariateMinimum(); |
| 183 |
|
| 184 |
double time = de.optimize(alignmentScore, 1e-6); |
| 185 |
double score = alignmentScore.evaluate(time); |
| 186 |
|
| 187 |
return new double[] {time, score}; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
private static void printFrequencyTable(List<Integer> list) { |
| 192 |
int max = 0; |
| 193 |
int total = 0; |
| 194 |
for (Integer size : list) { |
| 195 |
if (size > max) max = size; |
| 196 |
total += size; |
| 197 |
} |
| 198 |
int[] freqs = new int[max+1]; |
| 199 |
for (Integer size : list) { |
| 200 |
freqs[size] += 1; |
| 201 |
} |
| 202 |
|
| 203 |
for (int i = 0; i < freqs.length; i++) { |
| 204 |
System.out.println(i + "\t" + freqs[i]); |
| 205 |
} |
| 206 |
System.out.println("Total = " + total); |
| 207 |
} |
| 208 |
|
| 209 |
public static void main(String[] args) throws java.io.IOException, Importer.ImportException { |
| 210 |
|
| 211 |
NexusImporter importer = new NexusImporter(new FileReader(args[0])); |
| 212 |
|
| 213 |
Alignment alignment = importer.importAlignment(); |
| 214 |
|
| 215 |
ExtractPairs pairs = new ExtractPairs(alignment); |
| 216 |
|
| 217 |
Parameter muParam = new Parameter.Default(1.0); |
| 218 |
Parameter kappaParam = new Parameter.Default(1.0); |
| 219 |
kappaParam.addBounds(new Parameter.DefaultBounds(100.0, 0.0, 1)); |
| 220 |
muParam.addBounds(new Parameter.DefaultBounds(1.0, 1.0, 1)); |
| 221 |
|
| 222 |
Parameter freqParam = new Parameter.Default(alignment.getStateFrequencies()); |
| 223 |
|
| 224 |
FrequencyModel freqModel = new FrequencyModel(Nucleotides.INSTANCE,freqParam); |
| 225 |
|
| 226 |
SubstitutionModel substModel = new HKY(kappaParam, freqModel); |
| 227 |
|
| 228 |
SiteModel siteModel = new GammaSiteModel(substModel, muParam, null, 1, null); |
| 229 |
|
| 230 |
ScoreMatrix scoreMatrix = new ScoreMatrix(siteModel, 0.1); |
| 231 |
|
| 232 |
|
| 233 |
double threshold = 0.1; |
| 234 |
|
| 235 |
List<PairDistance> pairDistances = new ArrayList<PairDistance>(); |
| 236 |
Set<Integer> sequencesUsed = new HashSet<Integer>(); |
| 237 |
List<Integer> allGaps = new ArrayList<Integer>(); |
| 238 |
for (int i = 0; i < alignment.getSequenceCount(); i++) { |
| 239 |
for (int j = i+1; j < alignment.getSequenceCount(); j++) { |
| 240 |
Alignment pairAlignment = pairs.getPairAlignment(i,j); |
| 241 |
if (pairAlignment != null) { |
| 242 |
SitePatterns patterns = new SitePatterns(pairAlignment); |
| 243 |
|
| 244 |
double distance = getGeneticDistance(scoreMatrix, patterns); |
| 245 |
if (distance < threshold) { |
| 246 |
List gaps = new ArrayList(); |
| 247 |
GapUtils.getGapSizes(pairAlignment, gaps); |
| 248 |
pairDistances.add(new PairDistance(i,j,distance, gaps, pairAlignment.getSiteCount())); |
| 249 |
System.out.print("."); |
| 250 |
} else { |
| 251 |
System.out.print("*"); |
| 252 |
} |
| 253 |
} else { |
| 254 |
System.out.print("x"); |
| 255 |
} |
| 256 |
} |
| 257 |
System.out.println(); |
| 258 |
} |
| 259 |
|
| 260 |
Collections.sort(pairDistances); |
| 261 |
int totalLength = 0; |
| 262 |
for (PairDistance pairDistance : pairDistances) { |
| 263 |
|
| 264 |
Integer x = pairDistance.x; |
| 265 |
Integer y = pairDistance.y; |
| 266 |
|
| 267 |
if (!sequencesUsed.contains(x) && !sequencesUsed.contains(y)) { |
| 268 |
allGaps.addAll(pairDistance.gaps); |
| 269 |
sequencesUsed.add(x); |
| 270 |
sequencesUsed.add(y); |
| 271 |
System.out.println("Added pair (" + x + "," + y + ") d=" + pairDistance.distance + " L=" + pairDistance.alignmentLength); |
| 272 |
totalLength += pairDistance.alignmentLength; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
printFrequencyTable(allGaps); |
| 277 |
System.out.println("total length=" + totalLength); |
| 278 |
} |
| 279 |
} |