File: ConvertSamToAln.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 (73 lines) | stat: -rwxr-xr-x 1,748 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
package driver;

import java.io.OutputStream;
import java.io.PrintWriter;

import fileIO.ReadWrite;
import fileIO.TextFile;

public class ConvertSamToAln {
	
	public static void main(String[] args){
		for(String s : args){
			convert(s);
			System.out.println("Converted "+s);
		}
	}
	
	public static final void convert(String fname){
		TextFile tf=new TextFile(fname, false);
		
		
		
		String outname=fname;
		if(outname.toLowerCase().endsWith(".zip")){outname=outname.substring(0, outname.length()-4);}
		if(outname.toLowerCase().endsWith(".gz")){outname=outname.substring(0, outname.length()-3);}
		if(outname.toLowerCase().endsWith(".bz2")){outname=outname.substring(0, outname.length()-4);}
		if(outname.toLowerCase().endsWith(".sam")){outname=outname.substring(0, outname.length()-4);}
		outname=outname+".aln.gz";
		
		String s=null;
		
		OutputStream os=ReadWrite.getOutputStream(outname, false, true, true);
		PrintWriter out=new PrintWriter(os);
		
		for(s=tf.nextLine(); s!=null; s=tf.nextLine()){
			if(!s.startsWith("@")){
				String[] line=s.split("\t");
				assert(line.length>1) : s;
				
				boolean success=true;
				boolean nomap=false;
				boolean reverse=false;
				
				int flag=-1;
				String chrom=null;
				int loc=-1;
				
				try {
					flag=Integer.parseInt(line[1]);
					chrom=line[2];
					loc=Integer.parseInt(line[3]);
					nomap=((flag&0x4)!=0);
					reverse=((flag&0x10)!=0);
				} catch (NumberFormatException e) {
					success=false;
				}
				
				if(success && !nomap){
					String aln=chrom+"\t"+loc+"\t"+(reverse ? "R" : "F")+"\n";
					out.print(aln);
				}
				
				
			}
		}

		tf.close();
		out.flush();
		out.close();
		
	}
	
}