File: RunCommandLine.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 (209 lines) | stat: -rw-r--r-- 5,215 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
 * 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.io.File;
import java.util.HashMap;

public class RunCommandLine {

    ProAlign pa;
    PwAlignment pwa;
    PwAlignmentLoop pwal;
    AlignmentNode root;

    public RunCommandLine(ProAlign pa) throws Exception {

	ProAlign.log("RunCommandLine");

	this.pa = pa;
	SequenceReader sr = new SequenceReader();

	if(pa.seqfile != null && new File(pa.seqfile).exists()) {
	    
	    // ..and it's a valid file,
	    if(sr.fromFile(pa.seqfile)) {
		pa.seqs = sr.getSequences();

		CheckSequence cs = new CheckSequence();
		if(cs.isDna(pa.seqs)){
		    pa.sm.jcDnaModel();
		    ProAlign.isDna = true;
		} else {
		    if(ProAlign.protModel.equals("dayhoff")) {
			pa.sm.dayhoffProteinModel();
		    } else if(ProAlign.protModel.equals("jtt")) {
			pa.sm.jttProteinModel();
		    } else {
			pa.sm.wagProteinModel();
		    }
		    ProAlign.isDna = false;
		}
		if(!cs.isFromAlphabet(pa.seqs,pa.sm.equateAlphabet)) {
		    ProAlign.log.println("Sequence reading error: Illegal characters!");
		    System.out.println("Sequence reading error: Illegal characters!");
		    if(ProAlign.exitInError) {
			System.exit(0);
		    } else {
			throw new Exception("Sequence reading error: Illegal characters");
		    }
		}
	    } else {
		String msg = sr.getErrors();
		ProAlign.log.println(msg);
		System.out.println(msg);
		if(ProAlign.exitInError) {
		    System.exit(0);
		} else {
		    throw new Exception("Sequence reading error");
		}
	    }
	} else {
	    ProAlign.log.println("Sequence file doesn't exist!");
	    System.out.println("Sequence file doesn't exist!");
	    if(ProAlign.exitInError) {
		System.exit(0);
	    } else {
		throw new Exception("Sequence file doesn't exist");
	    }
	}

	String tree = new String();
	TreeReader tr = new TreeReader();

	// do a new tree

	PwSubstitutionMatrix psm = new PwSubstitutionMatrix();
	String pwAlphabet;
	int[][] pwSubst;
	int gOpen, gExt;
	
	if(ProAlign.isDna){
	    pwAlphabet = psm.dnaAlphabet;
	    pwSubst = psm.swdna;
	    gOpen = -1*pa.pwDnaOpen;
	    gExt = -1*pa.pwDnaExt;

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

	pwa = new PwAlignment(pwSubst,gOpen,gExt,pwAlphabet,ProAlign.isDna);

	if(pa.doTree) {

	    pwal = new PwAlignmentLoop(pwa,pa.seqs);	    
	    tree = pwal.getTree();

	    if(pa.treefile!=null) {
		try {
		    OutFile newtree = new OutFile(pa.treefile);
		    newtree.println(tree);
		    newtree.close();
		} catch(Exception e) { }

	    }
	    // open an existing tree file
	} else if(pa.treefile != null && new File(pa.treefile).exists()) {

	    String[] treeNodes = tr.getAllNodes(pa.treefile);
	    CheckTreeAndData chk = new CheckTreeAndData(treeNodes,pa.seqs);
	    
	    // ..and it's a valid file, set the tree.
	    if(chk.nodesAreSame()) {
		tree = tr.readFile(pa.treefile);
		if(tr.isUnRooted) {
		    TreeNode tn = new TreeNode(tree);
		    tree = tn.findMiddlePoint();
		}
	    } else {
		ProAlign.log.println("Sequence and tree files don't match!");
		System.out.println("Sequence and tree files don't match!");
		if(ProAlign.exitInError) {
		    System.exit(0);
		} else {
		    throw new Exception("Sequence and tree files don't match");
		}
	    }
	} else {
	    ProAlign.log.println("Tree file doesn't exist!");
	    System.out.println("Tree file doesn't exist!");
	    if(ProAlign.exitInError) {
		System.exit(0);
	    }else {
		throw new Exception("Tree file doesn't exist");
	    }
	}

	
	if(tree != null) {
	    
	    root = new AlignmentNode(pa,tree,0f);

//
	    if(ProAlign.estimateParameters) {
		ParameterEstimates pe = new ParameterEstimates(RunCommandLine.this);

	    }
//

	    try {
		root.align();
	    } catch(TraceBackException tbe) {
		String msg = tbe.getMessage();
		ProAlign.log.println(msg);
		System.out.println(msg);
		if(ProAlign.exitInError) {
		    System.exit(0);
		}else {
		    throw new Exception(msg);
		}
	    } catch(Exception e) {
//		System.out.println("RunCommandLine: throws an exception");
		throw e;
	    }

	    // seems to be fine - save results.
	    
	    if(pa.outformat==1) {
		SaveData sd = new SaveData(pa.outfile,1,root);
	    } else if(pa.outformat==2){
		SaveData sd = new SaveData(pa.outfile,2,root);
	    } else if(pa.outformat==3){
		SaveData sd = new SaveData(pa.outfile,3,root);	    
	    } else if(pa.outformat==4){
		SaveData sd = new SaveData(pa.outfile,4,root);	    
	    }	   

	} else {

	    ProAlign.log.println("Problems with tree!");
	    System.out.println("Problems with tree!");
	    if(ProAlign.exitInError) {
		System.exit(0);
	    } else {
		throw new Exception("Problems with tree");
	    }
	}
    }
}