File: EstherFilter.java

package info (click to toggle)
bbmap 39.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,024 kB
  • sloc: java: 312,743; sh: 18,099; python: 5,247; ansic: 2,074; perl: 96; makefile: 39; xml: 38
file content (164 lines) | stat: -rwxr-xr-x 4,322 bytes parent folder | download | duplicates (4)
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
package driver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

import fileIO.FileFormat;
import fileIO.ReadWrite;
import shared.Shared;
import stream.ConcurrentReadInputStream;
import stream.Read;
import structures.ListNum;

/**
 * @author Brian Bushnell
 * @date Jul 29, 2014
 *
 */
public class EstherFilter {
	
	public static void main(String[] args){
		String query=args[0];
		String ref=args[1];
		float cutoff=Float.parseFloat(args[2]);
		boolean outputFasta=false;
		if(args.length>3 && args[3].equalsIgnoreCase("fasta")){
			outputFasta=true;
		}
		String command="blastall -p blastn -i "+query+" -d "+ref+" -e 0.00001 -m 8";
		
		ReadWrite.FORCE_KILL=true;

//		InputStream is=ReadWrite.getInputStreamFromProcess("stdin", command, false);
//		InputStream is=ReadWrite.getInputStreamFromProcess("", command, false);
		InputStream is=ReadWrite.getInputStreamFromProcess("foo", command, false, false, true);
		
		InputStreamReader isr=new InputStreamReader(is);
		BufferedReader b=new BufferedReader(isr, 32768);
		
//		System.out.println("Before");
		
		if(outputFasta){
//			System.out.println("A");
			processToFasta(b, cutoff, query);
		}else{
//			System.out.println("B");
			processToNames(b, cutoff);
		}
		
//		System.out.println("Finished");

//		ReadWrite.finishReading(is, "stdin", true, b, isr);
//		ReadWrite.finishReading(is, "", true, b, isr);
		ReadWrite.finishReading(is, "foo", true, b, isr);
		
	}
	
	public static void processToFasta(BufferedReader b, float cutoff, String query){
		String s=null;
		
		ArrayList<String> names=new ArrayList<String>();
//		System.out.println("Reading line 0");
		try {
			s=b.readLine();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
//		System.out.println("Starting");
		String prev="";
		
		while(s!=null){
			String[] split=s.split("\t");
			float value=0;
			try {
				value=Float.parseFloat(split[11].trim());
			} catch (NumberFormatException e) {
				e.printStackTrace();
//				System.err.println("Bad line:\n"+s);
			}
			if(value>=cutoff){
				if(!prev.equals(split[0])){
					prev=split[0];
					names.add(split[0]);
				}
			}
//			System.out.println("Reading line");
			try {
				s=b.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		outputFasta(query, names);
	}
	
	public static void processToNames(BufferedReader b, float cutoff){
		String s=null;
//		System.out.println("Reading line 0");
		try {
			s=b.readLine();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
//		System.out.println("Starting");
		String prev="";
		while(s!=null){
			String[] split=s.split("\t");
			float value=0;
			try {
				value=Float.parseFloat(split[11].trim());
			} catch (NumberFormatException e) {
				e.printStackTrace();
//				System.err.println("Bad line:\n"+s);
			}
			if(value>=cutoff){
				System.out.println(split[0]);
			}
//			System.out.println("Reading line");
			try {
				s=b.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void outputFasta(String fname, ArrayList<String> names){
		
		Shared.sort(names);
		
		FileFormat ff=FileFormat.testInput(fname, FileFormat.FASTA, null, false, true);
		ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(-1L, false, ff, null);
		cris.start(); //4567
		ListNum<Read> ln=cris.nextList();
		ArrayList<Read> reads=(ln!=null ? ln.list : null);
		
		/* Iterate through read lists from the input stream */
		while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
			
			for(Read r : reads){
				if(Collections.binarySearch(names, r.id)>=0){
					System.out.println(r.toFasta(70));
				}
			}
			
			/* Dispose of the old list and fetch a new one */
			cris.returnList(ln);
			ln=cris.nextList();
			reads=(ln!=null ? ln.list : null);
		}
		/* Cleanup */
		cris.returnList(ln);
	}
	
	
}