File: ParameterEstimates.java

package info (click to toggle)
proalign 0.603-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 492 kB
  • sloc: java: 8,673; sh: 27; makefile: 4
file content (195 lines) | stat: -rw-r--r-- 4,251 bytes parent folder | download | duplicates (5)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
 * Title:        ProAlign<p>
 * Description:  <p>
 * Copyright:    Copyright (c) Ari Loytynoja<p>
 * License:      GNU GENERAL PUBLIC LICENSE<p>
 * @see          http://www.gnu.org/copyleft/gpl.html
 * Company:      ULB<p>
 * @author       Ari Loytynoja
 * @version      1.0
 */
package proalign;

import java.util.HashMap;

public class ParameterEstimates {

    String[][] pairs;
    HashMap seqs;
    PwAlignment pwa;

    ParameterEstimates(ResultWindow rw,SetParameters sp) { 

	this.seqs = rw.seqs;

	PwSubstitutionMatrix psm = new PwSubstitutionMatrix();
	String pwAlphabet;
	int[][] pwSubst;
	int gOpen, gExt;

	if(ProAlign.isDna){
	    pwAlphabet = psm.dnaAlphabet;
	    pwSubst = psm.swdna;
	    gOpen = -1*rw.pa.pwDnaOpen;
	    gExt = -1*rw.pa.pwDnaExt;

	} else {

	    pwAlphabet = psm.protAlphabet;
	    if(rw.pa.pwProtMatrix.equals("pam60")) {
		pwSubst = psm.pam60;
	    } else if(rw.pa.pwProtMatrix.equals("pam160")) {
		pwSubst = psm.pam160;
	    } else if(rw.pa.pwProtMatrix.equals("pam250")) {
		pwSubst = psm.pam250;
	    } else {
		pwSubst = psm.pam120;
	    }
	    gOpen = -1*rw.pa.pwProtOpen;
	    gExt = -1*rw.pa.pwProtExt;
	}
	
	pwa = new PwAlignment(pwSubst,gOpen,gExt,pwAlphabet,ProAlign.isDna);

	pairs = rw.root.getTerminalPairNames();

	this.estimate();

	sp.textDelta.setText((""+ProAlign.modelDelta+"      ").substring(0,5));
	sp.textEpsil.setText((""+ProAlign.modelEpsilon+"      ").substring(0,5));
	
    }

    ParameterEstimates(RunCommandLine rcl) {

	ProAlign.log("ParameterEstimates");

	AlignmentNode root = rcl.root;

	seqs = rcl.pa.seqs;
	pwa = rcl.pwa;

	pairs = root.getTerminalPairNames();

	this.estimate();
    }

    void estimate() {

	double gapFreq = 0d;
	double sumDelta = 0d;
	double sumEpsilon = 0d;

	for(int i=0; i<pairs.length; i++) {

	    String s0 = (String)seqs.get(pairs[i][0]);
	    String s1 = (String)seqs.get(pairs[i][1]);

	    String[] revSeq =  pwa.revAligned(s0,s1);
	    
	    int all = 0;
	    int ms = 0; // match sum
	    int mn = 1; // match numebr
	    int ml = 0; // match length
	    int gs = 0;
	    int gn = 1;
	    int gl = 0;
	    double mm = 5d; // match mean
	    double mg = 5d;
	    boolean isM = false;
	    
	    for(int k=0; k<revSeq[0].length(); k++) {
		if(k==0) {
		    if(revSeq[0].charAt(k)=='-' || revSeq[1].charAt(k)=='-') {
			gn++;
		    } else {
			mn++;
		    }
		}
		
		if(revSeq[0].charAt(k)=='-' || revSeq[1].charAt(k)=='-') {
		    gs++;
		    gl++;
		    if(isM) {
			if(k>0) {
			    if(mn>1) {
				mm = ((double)mm*(mn-1)+ml)/mn;
			    } else {
				mm = (mm+ml)/2;
			    }
			    ml=0;
			    gn++;
			}
			isM = false;				
		    }
		} else {
		    ms++;
		    ml++;
		    if(!isM) {		    
			if(k>0) {
			    if(gn>1) {
				mg = ((double)mg*(gn-1)+gl)/gn;
			    } else {
				mg = (mg+gl)/2;
			    }
			    gl=0;
			    mn++;
			}
			isM = true;				
		    }
		}
		
		if(k+1==revSeq[0].length()) {
		    if(isM) {
			if(mn>1) {
			    mm = ((double)mm*(mn-1)+ml)/mn;
			} else {
			    mm = ml;
			}
		    } else {
			if(gn>1) {
			    mg = ((double)mg*(gn-1)+gl)/gn;
			} else {
			    mg = gl;
			}
		    }
		}
		all++;
	    }
	    gapFreq += (double)gs/(2*all);
	    sumDelta += mm;
	    sumEpsilon += mg;
	}

	gapFreq /= pairs.length;
	sumDelta /= pairs.length;
	sumEpsilon /= pairs.length;

	if(ProAlign.estimateDelta) {
	    ProAlign.modelDelta = 0.5d/(sumDelta+1);
	    ProAlign.log.println(" HMM delta estimate: "+ProAlign.modelDelta);
//	    System.out.println("modelDelta: "+ProAlign.modelDelta);	    
	}
	if(ProAlign.estimateEpsilon) {
	    ProAlign.modelEpsilon = (1d-1d/(sumEpsilon+1));
	    ProAlign.log.println(" HMM epsilon estimate: "+ProAlign.modelEpsilon);
//	    System.out.println("modelEpsilon: "+ProAlign.modelEpsilon);
	}
	if(ProAlign.estimateGapFreq) {
	    ProAlign.gapFreq = gapFreq;
	    ProAlign.log.println(" gap frequency estimate: "+ProAlign.gapFreq);
//	    System.out.println("gapFreq: "+ProAlign.gapFreq);
	}
	if(ProAlign.estimateGapProb) {
	    if(ProAlign.isDna) {
		ProAlign.gapProb = gapFreq/4d;
	    } else {
		ProAlign.gapProb = gapFreq/20d;
	    }
	    ProAlign.log.println(" gap probability estimate: "+ProAlign.gapProb);
//	    System.out.println("gapProb: "+ProAlign.gapProb);
	}

    }	

}