| 1 | package driver; |
| 2 | |
| 3 | import java.io.OutputStream; |
| 4 | import java.io.PrintWriter; |
| 5 | |
| 6 | import fileIO.ReadWrite; |
| 7 | import fileIO.TextFile; |
| 8 | |
| 9 | public class ConvertSamToAln { |
| 10 | |
| 11 | public static void main(String[] args){ |
| 12 | for(String s : args){ |
| 13 | convert(s); |
| 14 | System.out.println("Converted "+s); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | public static final void convert(String fname){ |
| 19 | TextFile tf=new TextFile(fname, false); |
| 20 | |
| 21 | |
| 22 | |
| 23 | String outname=fname; |
| 24 | if(outname.toLowerCase().endsWith(".zip")){outname=outname.substring(0, outname.length()-4);} |
| 25 | if(outname.toLowerCase().endsWith(".gz")){outname=outname.substring(0, outname.length()-3);} |
| 26 | if(outname.toLowerCase().endsWith(".bz2")){outname=outname.substring(0, outname.length()-4);} |
| 27 | if(outname.toLowerCase().endsWith(".sam")){outname=outname.substring(0, outname.length()-4);} |
| 28 | outname=outname+".aln.gz"; |
| 29 | |
| 30 | String s=null; |
| 31 | |
| 32 | OutputStream os=ReadWrite.getOutputStream(outname, false, true, true); |
| 33 | PrintWriter out=new PrintWriter(os); |
| 34 | |
| 35 | for(s=tf.nextLine(); s!=null; s=tf.nextLine()){ |
| 36 | if(!s.startsWith("@")){ |
| 37 | String[] line=s.split("\t"); |
| 38 | assert(line.length>1) : s; |
| 39 | |
| 40 | boolean success=true; |
| 41 | boolean nomap=false; |
| 42 | boolean reverse=false; |
| 43 | |
| 44 | int flag=-1; |
| 45 | String chrom=null; |
| 46 | int loc=-1; |
| 47 | |
| 48 | try { |
| 49 | flag=Integer.parseInt(line[1]); |
| 50 | chrom=line[2]; |
| 51 | loc=Integer.parseInt(line[3]); |
| 52 | nomap=((flag&0x4)!=0); |
| 53 | reverse=((flag&0x10)!=0); |
| 54 | } catch (NumberFormatException e) { |
| 55 | success=false; |
| 56 | } |
| 57 | |
| 58 | if(success && !nomap){ |
| 59 | String aln=chrom+"\t"+loc+"\t"+(reverse ? "R" : "F")+"\n"; |
| 60 | out.print(aln); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | tf.close(); |
| 68 | out.flush(); |
| 69 | out.close(); |
| 70 | |
| 71 | } |
| 72 | |
| 73 | } |