| 1 |
package dna; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
|
| 6 |
import fileIO.TextStreamWriter; |
| 7 |
import shared.PreParser; |
| 8 |
import shared.Timer; |
| 9 |
import shared.Tools; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
public class ChromToFasta { |
| 17 |
|
| 18 |
public static void main(String[] args){ |
| 19 |
{ |
| 20 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 21 |
args=pp.args; |
| 22 |
|
| 23 |
} |
| 24 |
|
| 25 |
Timer t=new Timer(); |
| 26 |
|
| 27 |
if(args[0].contains("=") && (args[0].startsWith("build") || args[0].startsWith("genome"))){ |
| 28 |
int build=Integer.parseInt(args[0].split("=")[1]); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
args[0]=Data.chromFname(1, build); |
| 37 |
args[0]=args[0].substring(0, args[0].lastIndexOf('/')); |
| 38 |
} |
| 39 |
|
| 40 |
String[] chromfiles=args[0].split(","); |
| 41 |
|
| 42 |
if(chromfiles.length==1){ |
| 43 |
File f=new File(chromfiles[0]); |
| 44 |
if(f.isDirectory()){ |
| 45 |
ArrayList<String> list=new ArrayList<String>(4); |
| 46 |
for(File f2 : f.listFiles()){ |
| 47 |
if(!f2.isDirectory() && f2.isFile()){ |
| 48 |
String s=f2.getAbsolutePath(); |
| 49 |
if(s.endsWith(".chrom") || s.endsWith(".chromC") || s.contains(".chrom.") || s.contains(".chromC.")){ |
| 50 |
list.add(s); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
chromfiles=list.toArray(new String[list.size()]); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
String outfile=args[1]; |
| 59 |
int blocklen=Integer.parseInt(args[2]); |
| 60 |
int trigger=(args.length>3 ? Integer.parseInt(args[3]) : 0); |
| 61 |
|
| 62 |
TextStreamWriter tsw=new TextStreamWriter(outfile, true, false, false); |
| 63 |
tsw.start(); |
| 64 |
|
| 65 |
if(trigger<=0){ |
| 66 |
for(int i=0; i<chromfiles.length; i++){ |
| 67 |
ChromosomeArray cha=ChromosomeArray.read(chromfiles[i]); |
| 68 |
writeChrom(cha, tsw, blocklen); |
| 69 |
} |
| 70 |
}else{ |
| 71 |
int contig=1; |
| 72 |
for(int i=0; i<chromfiles.length; i++){ |
| 73 |
ChromosomeArray cha=ChromosomeArray.read(chromfiles[i]); |
| 74 |
contig=writeContigs(cha, contig, trigger, blocklen, tsw); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
tsw.poison(); |
| 79 |
|
| 80 |
try {tsw.join();} |
| 81 |
catch (InterruptedException e) {e.printStackTrace();} |
| 82 |
|
| 83 |
t.stop(); |
| 84 |
System.err.println("Time:\t"+t); |
| 85 |
} |
| 86 |
|
| 87 |
public static int writeContigs(ChromosomeArray cha, int contig, int trigger, int fastaBlocklen, TextStreamWriter tsw){ |
| 88 |
|
| 89 |
StringBuilder sb=new StringBuilder(4000); |
| 90 |
|
| 91 |
int ns=0; |
| 92 |
|
| 93 |
for(int aloc=cha.minIndex; aloc<=cha.maxIndex; aloc++){ |
| 94 |
byte b=cha.get(aloc); |
| 95 |
if(b=='N'){ |
| 96 |
ns++; |
| 97 |
if(sb.length()>0){ |
| 98 |
sb.append('N'); |
| 99 |
if(ns==trigger){ |
| 100 |
sb.setLength(sb.length()-ns); |
| 101 |
tsw.print(">"+contig+"\n"); |
| 102 |
contig++; |
| 103 |
writeContig(sb, tsw, fastaBlocklen); |
| 104 |
sb.setLength(0); |
| 105 |
} |
| 106 |
} |
| 107 |
}else{ |
| 108 |
sb.append((char)b); |
| 109 |
ns=0; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
if(sb.length()>0){ |
| 115 |
sb.setLength(sb.length()-ns); |
| 116 |
tsw.print(">"+contig+"\n"); |
| 117 |
contig++; |
| 118 |
writeContig(sb, tsw, fastaBlocklen); |
| 119 |
sb.setLength(0); |
| 120 |
} |
| 121 |
|
| 122 |
return contig; |
| 123 |
} |
| 124 |
|
| 125 |
public static void writeContig(StringBuilder sb, TextStreamWriter tsw, int blocklen){ |
| 126 |
for(int i=0; i<sb.length(); i+=blocklen){ |
| 127 |
int max=Tools.min(i+blocklen, sb.length()); |
| 128 |
tsw.println(sb.substring(i, max)); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
public static void writeChrom(ChromosomeArray cha, String fname, int blocklen){ |
| 133 |
TextStreamWriter tsw=new TextStreamWriter(fname, true, false, false); |
| 134 |
tsw.start(); |
| 135 |
tsw.print(">"+cha.chromosome+"\n"); |
| 136 |
writeChrom(cha, tsw, blocklen); |
| 137 |
tsw.poison(); |
| 138 |
} |
| 139 |
|
| 140 |
public static void writeChrom(ChromosomeArray cha, TextStreamWriter tsw, int blocklen){ |
| 141 |
tsw.println(">"+cha.chromosome); |
| 142 |
for(int i=0; i<=cha.maxIndex; i+=blocklen){ |
| 143 |
int max=Tools.min(i+blocklen-1, cha.maxIndex); |
| 144 |
tsw.println(cha.getString(i, max)); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
} |