| 1 |
package gff; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
|
| 5 |
import fileIO.ByteFile; |
| 6 |
import fileIO.ByteStreamWriter; |
| 7 |
import fileIO.FileFormat; |
| 8 |
import shared.Shared; |
| 9 |
import shared.Tools; |
| 10 |
|
| 11 |
public class GbffFile { |
| 12 |
|
| 13 |
public static void main(String[] args){ |
| 14 |
String gbff=args[0]; |
| 15 |
String gff=(args.length>1 ? args[1] : "stdout.gff"); |
| 16 |
|
| 17 |
if(gbff.indexOf('=')>=0){gbff=gbff.split("=")[1];} |
| 18 |
if(gff.indexOf('=')>=0){gff=gff.split("=")[1];} |
| 19 |
|
| 20 |
FileFormat ffin=FileFormat.testInput(gbff, ".gbff", true); |
| 21 |
FileFormat ffout=FileFormat.testOutput(gff, FileFormat.GFF, null, true, true, false, false); |
| 22 |
GbffFile file=new GbffFile(ffin); |
| 23 |
ByteStreamWriter bsw=new ByteStreamWriter(ffout); |
| 24 |
bsw.start(); |
| 25 |
file.toGff(bsw, true); |
| 26 |
bsw.poisonAndWait(); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public void toGff(ByteStreamWriter bsw, boolean printHeader){ |
| 41 |
if(printHeader){ |
| 42 |
bsw.println("##gff-version 3".getBytes()); |
| 43 |
bsw.println(("#BBTools "+Shared.BBTOOLS_VERSION_STRING+" GbffToGff").getBytes()); |
| 44 |
bsw.println("#seqid source type start end score strand phase attributes".getBytes()); |
| 45 |
} |
| 46 |
for(GbffLocus locus=nextLocus(); locus!=null; locus=nextLocus()){ |
| 47 |
locus.toGff(bsw); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
public GbffFile(FileFormat ff_) { |
| 52 |
ff=ff_; |
| 53 |
assert(ff.format()==FileFormat.GBFF) : ff; |
| 54 |
reset(); |
| 55 |
} |
| 56 |
|
| 57 |
public synchronized void reset(){ |
| 58 |
if(bf!=null){ |
| 59 |
bf.close(); |
| 60 |
bf=null; |
| 61 |
} |
| 62 |
bf=ByteFile.makeByteFile(ff, FileFormat.GBFF); |
| 63 |
line=bf.nextLine(); |
| 64 |
if(line==null){bf.close();} |
| 65 |
} |
| 66 |
|
| 67 |
public GbffLocus nextLocus(){ |
| 68 |
assert(bf!=null); |
| 69 |
if(line==null){return null;} |
| 70 |
assert(Tools.startsWith(line, "LOCUS ")) : "Expecting: 'LOCUS ...'\nGot: '"+new String(line)+"'"; |
| 71 |
ArrayList<byte[]> lines=new ArrayList<byte[]>(); |
| 72 |
lines.add(line); |
| 73 |
boolean sequence=false; |
| 74 |
for(line=bf.nextLine(); line!=null && (line.length==0 || line[0]!='L' || !Tools.startsWith(line, "LOCUS ")); line=bf.nextLine()){ |
| 75 |
if(line.length>0){ |
| 76 |
final byte b=line[0]; |
| 77 |
if(b=='/'){ |
| 78 |
|
| 79 |
}else if(b=='O' && Tools.startsWith(line, "ORIGIN ")){ |
| 80 |
sequence=true; |
| 81 |
}else if(b==' ' && sequence){ |
| 82 |
|
| 83 |
}else{ |
| 84 |
sequence=false; |
| 85 |
lines.add(line); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
if(line==null){bf.close();} |
| 90 |
return new GbffLocus(lines); |
| 91 |
} |
| 92 |
|
| 93 |
private final FileFormat ff; |
| 94 |
private ByteFile bf; |
| 95 |
private byte[] line=null; |
| 96 |
|
| 97 |
} |