File: RunClustalw.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 (186 lines) | stat: -rw-r--r-- 4,087 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
/**
 * 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.io.IOException;
import java.math.BigDecimal;
import java.io.DataInputStream;
import java.io.BufferedInputStream;

/**
 * Running native ClustalW.
 */
public class RunClustalw extends Thread {


    String tempFolder;
    String clustalwPath;
    String cmdParameter;

    ResultWindow rw;

    Process process;
    DataInputStream clustalwOut;

     /**
     * Argument list describing the ClustalW loop.
     */
    RunClustalw(ResultWindow rw) {
	
	this.rw = rw;

	ProAlign.log("RunClustalw");

	clustalwPath = ProAlign.clustalwPath;
	tempFolder = ProAlign.tempFolder+File.separator;

	if(System.getProperty("os.name").startsWith("Windows")){
	    File oldFile = new File(tempFolder+"PROALIGN.TRE");
	    if(oldFile.exists()) {
		try {
		    oldFile.delete();
		} catch (Exception ie) {
		    ProAlign.log.println(" can not delete old guide tree-file!");
		}
	    }
	} else {
	    File oldFile = new File(tempFolder+"proalign.tre");
	    if(oldFile.exists()) {
		try {
		    oldFile.delete();
		} catch (Exception ie) { 
		    ProAlign.log.println(" can not delete old guide tree-file!");
		}
	    }
	}

	if(System.getProperty("os.name").startsWith("Windows")){
	    cmdParameter = "clustalw /tree /newtree="+tempFolder+"PROALIGN.TRE /infile="+
		tempFolder+"proalign.seq";
	} else {
	    cmdParameter = " -tree -newtree="+tempFolder+"proalign.tre -infile="+
		tempFolder+"proalign.seq";
	}
	
	start();
    }

    public void run(){
	/** 
	 *  Clustalw - make guide tree only!
	 */
	if(System.getProperty("os.name").startsWith("Windows")) {


	    int narg = numArg(cmdParameter,"/");
	    try {
		WinClustalw wc = new WinClustalw(narg,cmdParameter);
		
		while(wc.running) {
		    try{
			Thread.currentThread().sleep(500);
		    } catch(InterruptedException e) {}
		}
	    } catch(Exception ex) {
		ProAlign.log("WinClustalw error");
	    }

	} else {
	    try {

		process = Runtime.getRuntime().exec(clustalwPath+cmdParameter);
		clustalwOut = 
		    new DataInputStream(new BufferedInputStream(process.getInputStream()));
		
		String str;	
		while((str = clustalwOut.readLine()) != null) {         
		    if (str.length() > 0) {
			ProAlign.log(str);
		    }
		}
	    } catch(IOException e) { }  
	}
	if(ProAlign.DEBUG) {
	    ProAlign.log.flush();
	}
	
	String filepath;
	if(System.getProperty("os.name").startsWith("Windows")){
	    filepath = ProAlign.tempFolder+File.separator+"PROALIGN.TRE";
	} else {
	    filepath = ProAlign.tempFolder+File.separator+"proalign.tre"; 
	}
	
	File newFile = new File(filepath);
	if(!newFile.exists()) {  // for some reason ClustalW failed.

	    String text = "\n      ClustalW output not found!\n";
	    OpenDialog od = new OpenDialog(rw);
	    od.showDialog("Error!", text);
	    rw.setRawData(rw.seqs);
	    rw.setScrollPanes();
	    
	} else { // guide tree is fine.

	    TreeReader tr = new TreeReader();
	    String[] treeNodes = tr.getAllNodes(filepath);
	    CheckTreeAndData chk = new CheckTreeAndData(treeNodes, rw.seqs);

	    if(chk.nodesAreSame()) {
		String tree = tr.readFile(filepath);
		TreeNode tn = new TreeNode(tree);
		tree = tn.findMiddlePoint();

		rw.setRawDataAndTree(tree);
		rw.setScrollPanes();
		
	    } else {

		String text = new String("\n   Guide tree and sequence \n"+
					 "    file do not match!\n");
		OpenDialog od = new OpenDialog(rw);
		od.showDialog("Warning!", text); 

		ProAlign.log.println("ClustalW: nodes and names do not match!");

	    }
	}
    }

    public int numArg(String cmnd,String csep) {
        int narg = 0;
        for(int i = 0, j = 0; i < cmnd.length();) {
            j = cmnd.indexOf(csep, i);
            if (j >= i){
                narg++;
                i = j;
            }
            i++;
        }
        return narg;
    }
}