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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
package gff;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Locale;
import dna.Data;
import fileIO.ByteFile;
import fileIO.FileFormat;
import prok.ProkObject;
import shared.Parse;
import shared.Shared;
import shared.Tools;
import structures.ByteBuilder;
import var2.ScafMap;
import var2.VCFLine;
import var2.Var;
/**
* Used by both the var2 and prok packages for processing gff files.
* @author Brian Bushnell
* @date Sep 12, 2018
*
*/
public class GffLine {
//#seqid source type start end score strand phase attributes
public GffLine(byte[] line){
int a=0, b=0;
while(b<line.length && line[b]!='\t'){b++;}
assert(b>a) : "Missing field 0: "+new String(line);
seqid=parseSeqid ? intern(new String(line, a, b-a)) : null;
// assert(seqid==null || seqid.equals(new String(line, a, b-a)));
// assert(seqid!=null) : new String(line, a, b-a)+", "+a+", "+b+"\n"+line;
b++;
a=b;
while(b<line.length && line[b]!='\t'){b++;}
assert(b>a) : "Missing field 1: "+new String(line);
if(b==a+1 && line[a]=='.'){source=DOTS;}
else{source=paseSource ? intern(new String(line, a, b-a)) : null;}
b++;
a=b;
while(b<line.length && line[b]!='\t'){b++;}
assert(b>a) : "Missing field 2: "+new String(line);
if(b==a+1 && line[a]=='.'){type=DOTS;}
else{
try {//This was to catch a probably intermittent hardware error; can't replicate.
type=(parseType ? intern(new String(line, a, b-a)) : null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.err.println("\n"+new String(line)+"\n"+a+", "+b+", "+(b-a));
assert(false);
}
}
b++;
a=b;
while(b<line.length && line[b]!='\t'){b++;}
assert(b>a) : "Missing field 3: "+new String(line);
start=Parse.parseInt(line, a, b);
b++;
a=b;
while(b<line.length && line[b]!='\t'){b++;}
assert(b>a) : "Missing field 4: "+new String(line);
stop=Parse.parseInt(line, a, b);
b++;
a=b;
while(b<line.length && line[b]!='\t'){b++;}
if(b<=a){
//Badly formatted line; common in IMG
return;
}
assert(b>a) : "Missing field 5: "+new String(line);
if(b==a+1 && line[a]=='.'){score=-1;}
else{score=Parse.parseFloat(line, a, b);}
b++;
a=b;
while(b<line.length && line[b]!='\t'){b++;}
assert(b>a) : "Missing field 6: "+new String(line);
assert(b==a+1);
strand=find(line[a], STRANDS);
// assert(strand>0) : line[a]+", "+Arrays.toString(STRANDS)+", "+(char)line[b];
b++;
a=b;
while(b<line.length && line[b]!='\t'){b++;}
assert(b>a) : "Missing field 7: "+new String(line);
assert(b==a+1);
if(line[a]=='.'){phase=-1;}
else{phase=Parse.parseInt(line, a, b);}
b++;
a=b;
while(b<line.length && line[b]!='\t'){b++;}
assert(b>a) : "Missing field 8: "+new String(line);
if(b==a+1 && line[a]=='.'){attributes=DOTS;}
else{attributes=parseAttributes ? new String(line, a, b-a) : null;}
b++;
a=b;
// assert(strand>=0) : "\n"+this.toString()+"\n"+new String(line);
}
public GffLine(VCFLine vcf){
seqid=vcf.scaf;
source=DOTS;
type="sequence_variant_obs";
start=vcf.start()+1;
stop=vcf.stop()+1;
score=(float)vcf.qual;
strand=PLUS;
phase=-1;
final int vtype=vcf.type();
ByteBuilder bb=new ByteBuilder(16);
bb.append("ID=").append(Var.typeArray[vtype]).append(' ');
if(vtype==Var.SUB){
bb.append(vcf.ref).append('>').append(vcf.alt);
}else if(vtype==Var.DEL){
bb.append("length ").append(vcf.reflen()-vcf.readlen());
}else if(vtype==Var.INS){
int offset=vcf.reflen();
int length=vcf.readlen()-offset;
bb.append(vcf.alt, offset, length);
}else if(vtype==Var.NOCALL){
bb.append("length ").append(vcf.reflen());
}
attributes=bb.toString();
bb.clear();
}
public GffLine(Var v, double properPairRate, double totalQualityAvg, double totalMapqAvg, double readLengthAvg, double rarity, int ploidy, ScafMap map){
seqid=v.scafName();
source=DOTS;
type="sequence_variant_obs";
start=v.start+1;
stop=Tools.max(v.start+1, v.stop);
score=(float)v.score(properPairRate, totalQualityAvg, totalMapqAvg, readLengthAvg, rarity, ploidy, map);
strand=PLUS;
phase=-1;
final int vtype=v.type();
ByteBuilder bb=new ByteBuilder(16);
bb.append("ID=").append(Var.typeArray[vtype]);
if(vtype==Var.SUB || vtype==Var.INS){
bb.append(' ').append(v.allele);
}else if(vtype==Var.DEL || vtype==Var.NOCALL){
bb.append(" length ").append(v.reflen());
}else{assert(false) : vtype+"\n"+v;}
attributes=bb.toString();
bb.clear();
}
public GffLine(Var v){
seqid=v.scafName();
source="BBTools";
type="sequence_variant_obs";
start=v.start+1;
stop=Tools.max(v.start+1, v.stop);
score=-1;
strand=PLUS;
phase=-1;
final int vtype=v.type();
ByteBuilder bb=new ByteBuilder(16);
bb.append("ID=").append(Var.typeArray[vtype]);
if(vtype==Var.SUB || vtype==Var.INS){
bb.append(' ').append(v.allele);
}else if(vtype==Var.DEL || vtype==Var.NOCALL){
bb.append(" length ").append(v.reflen());
}else{assert(false) : vtype+"\n"+v;}
attributes=bb.toString();
bb.clear();
}
public static ArrayList<GffLine> loadGffFile(String fname, String types, boolean banUnprocessed){
FileFormat ff=FileFormat.testInput(fname, FileFormat.GFF, null, false, false);
return loadGffFile(ff, types, banUnprocessed);
}
public static ArrayList<GffLine>[] loadGffFileByType(FileFormat ff, String types, boolean banUnprocessed){
ArrayList<GffLine> list=loadGffFile(ff, types, banUnprocessed);
String[] typeArray=types.split(",");
ArrayList<GffLine>[] lists=new ArrayList[typeArray.length];
for(int i=0; i<typeArray.length; i++){
String type=typeArray[i];
lists[i]=new ArrayList<GffLine>();
for(GffLine gline : list){
if(gline.type.equals(type)){
lists[i].add(gline);
}
}
}
return lists;
}
public static ArrayList<GffLine> loadGffFile(FileFormat ff, String types, boolean banUnprocessed){
HashSet<String> set=null;
if(types!=null){
String[] split=types.split(",");
set=new HashSet<String>(split.length*2);
for(String s : split){
set.add(s);
}
}
ArrayList<GffLine> list=new ArrayList<GffLine>();
ByteFile bf=ByteFile.makeByteFile(ff);
for(byte[] line=bf.nextLine(); line!=null; line=bf.nextLine()){
if(line[0]=='#'){
//skip
}else{
GffLine gline=new GffLine(line);
assert(gline.strand>=0) : "\n"+gline.toString()+"\n"+new String(line)+"\n";
if(set==null || (gline.type!=null && set.contains(gline.type))){
if(!banUnprocessed || ProkObject.processType(gline.prokType())){
list.add(gline);
}
}
}
}
boolean error=bf.close();
assert(!error) : "Problem with file "+ff.name();
return list;
}
public static void toText(ByteBuilder bb, Var v, double properPairRate, double totalQualityAvg,
double totalMapqAvg, double readLengthAvg, double rarity, int ploidy, ScafMap map){
// assert(false);
bb.append(v.scafName(map)).append('\t');
bb.append('.').append('\t');
bb.append("sequence_variant_obs").append('\t');
bb.append(v.start+1).append('\t');
bb.append(Tools.max(v.start+1, v.stop)).append('\t');
bb.append(v.score(properPairRate, totalQualityAvg, totalMapqAvg, readLengthAvg, rarity, ploidy, map), 2).append('\t');
bb.append('+').append('\t');
bb.append('.').append('\t');
// System.err.println(v.typeString()+", "+v.start+", "+v.stop);
final int vtype=v.type();
bb.append("ID=").append(Var.typeArray[vtype]);
if(vtype==Var.SUB || vtype==Var.INS){
bb.append(' ').append(v.allele);
}else if(vtype==Var.DEL || vtype==Var.NOCALL){
bb.append(" length ").append(v.reflen());
}else{assert(false) : vtype+"\n"+v;}
}
public static String toHeader(double properPairRate, double totalQualityAvg, double mapqAvg, double rarity, double minAlleleFraction, int ploidy,
long reads, long pairs, long properPairs, long bases, String ref){
StringBuilder sb=new StringBuilder();
final double readLengthAvg=bases/Tools.max(1.0, reads);
sb.append("##gff-version 3\n");
sb.append("#BBMapVersion\t"+Shared.BBMAP_VERSION_STRING+"\n");
sb.append("#ploidy\t"+ploidy+"\n");
sb.append(String.format(Locale.ROOT, "#rarity\t%.5f\n", rarity));
sb.append(String.format(Locale.ROOT, "#minAlleleFraction\t%.4f\n", minAlleleFraction));
sb.append("#reads\t"+reads+"\n");
sb.append("#pairedReads\t"+pairs+"\n");
sb.append("#properlyPairedReads\t"+properPairs+"\n");
sb.append(String.format(Locale.ROOT, "#readLengthAvg\t%.2f\n", readLengthAvg));
sb.append(String.format(Locale.ROOT, "#properPairRate\t%.4f\n", properPairRate));
sb.append(String.format(Locale.ROOT, "#totalQualityAvg\t%.4f\n", totalQualityAvg));
sb.append(String.format(Locale.ROOT, "#mapqAvg\t%.2f\n", mapqAvg));
if(ref!=null){sb.append("#reference\t"+ref+"\n");}
sb.append("#seqid source type start end score strand phase attributes");
return sb.toString();
}
@Override
public String toString(){
ByteBuilder bb=new ByteBuilder();
appendTo(bb);
return bb.toString();
}
public ByteBuilder appendTo(ByteBuilder bb){
bb.append(seqid==null ? "." : seqid).append('\t');
bb.append(source==null ? "." : source).append('\t');
bb.append(type==null ? "." : type).append('\t');
bb.append(start).append('\t');
bb.append(stop).append('\t');
if(score<0){bb.append('.').append('\t');}
else{bb.append(score, 2).append('\t');}
bb.append((strand>=0 ? STRANDS[strand] : (byte)'.')).append('\t');
if(phase<0){bb.append('.').append('\t');}
else{bb.append(phase).append('\t');}
bb.append(attributes==null ? "." : attributes);
return bb;
}
public int length() {
return stop-start+1;
}
private static int find(byte a, byte[] array){
for(int i=0; i<array.length; i++){
if(array[i]==a){return i;}
}
return -1;
}
private static String intern(String s){
return Data.forceIntern(s);
}
@Override
public int hashCode(){
return trueStop()^seqid.hashCode();
}
@Override
public boolean equals(Object o){
GffLine b=(GffLine)o;
if(start!=b.start){return false;}
if(stop!=b.stop){return false;}
if(strand!=b.strand){return false;}
if(!seqid.equals(b.seqid)){return false;}
if(!type.equals(b.type)){return false;}
return true;
}
public int trueStart(){
return strand==0 ? start : stop;
}
public int trueStop(){
return strand==0 ? stop : start;
}
public final int prokType(){
if(type.equals("CDS")){
return ProkObject.CDS;
}else if(type.equals("tRNA")){
return ProkObject.tRNA;
}else if(type.equals("rRNA")){
if(attributes.contains("16S")){
return ProkObject.r16S;
}else if(attributes.contains("23S")){
return ProkObject.r23S;
}else if(attributes.contains("18S")){
return ProkObject.r18S;
}else if(attributes.contains("5S") && length()<300){
return ProkObject.r5S;
}
}
return -1;
}
public final boolean partial(){return attributes!=null && attributes.contains("partial=true");}
public final boolean inbounds(int scaflen){return start>=0 && stop<scaflen;}
public String seqid;
public String source;
public String type;
public int start;
public int stop;
public float score;
public int strand;
public int phase;
public String attributes;
private static final byte[] STRANDS=new byte[] {'+', '-', '?', '.'};
public static final int PLUS=0, MINUS=1, QMARK=2, DOT=3;
public static final String DOTS=".";
public static boolean parseSeqid=true;
public static boolean paseSource=false;
public static boolean parseType=true;
public static boolean parseScore=false;
public static boolean parseAttributes=true;
// public static boolean parseSeqid=true;
// public static boolean paseSource=true;
// public static boolean parseType=true;
// public static boolean parseScore=true;
// public static boolean parseAttributes=true;
}
|