use of org.broad.igv.bbfile.BedFeature in project jvarkit by lindenb.
the class VcfEnsemblReg method annotate.
private void annotate(Track track, File inf, File outf) throws IOException {
boolean contained = false;
LOG.info("Processing " + track.id + " (" + track.shortLabel + ") " + track.url);
VcfIterator in = VCFUtils.createVcfIteratorFromFile(inf);
VCFHeader header = in.getHeader();
VCFInfoHeaderLine info = null;
SeekableStream sstream = SeekableStreamFactory.getInstance().getStreamFor(track.url);
BBFileReader bigFile = new BBFileReader(track.url.toString(), new SeekableStreamAdaptor(sstream));
VariantContextWriter w1 = VCFUtils.createVariantContextWriter(outf);
if (bigFile.isBigWigFile()) {
info = new VCFInfoHeaderLine(track.id, 1, VCFHeaderLineType.Float, String.valueOf(track.longLabel) + " " + track.url);
} else {
info = new VCFInfoHeaderLine(track.id, 1, VCFHeaderLineType.String, String.valueOf(track.longLabel) + " " + track.url);
}
header.addMetaDataLine(info);
w1.writeHeader(in.getHeader());
while (in.hasNext()) {
VariantContext ctx = in.next();
String chrom = ctx.getContig();
if (!chrom.startsWith("chr"))
chrom = "chr" + chrom;
if (!chrom.matches("(chrX|chrY|chr[0-9]|chr1[0-9]|chr2[12])")) {
w1.add(ctx);
} else if (bigFile.isBigWigFile()) {
BigWigIterator iter = bigFile.getBigWigIterator(chrom, ctx.getStart() - 1, chrom, ctx.getStart(), contained);
Float wigValue = null;
while (iter != null && iter.hasNext() && wigValue == null) {
WigItem item = iter.next();
wigValue = item.getWigValue();
}
if (wigValue == null) {
w1.add(ctx);
continue;
}
VariantContextBuilder vcb = new VariantContextBuilder(ctx);
vcb.attribute(track.id, wigValue);
w1.add(vcb.make());
} else {
BigBedIterator iter = bigFile.getBigBedIterator(chrom, ctx.getStart() - 1, chrom, ctx.getStart(), contained);
Set<String> bedValues = new HashSet<String>();
while (iter != null && iter.hasNext()) {
BedFeature item = iter.next();
String[] rest = item.getRestOfFields();
if (rest == null || rest.length != 6) {
System.err.println(track.id + " " + Arrays.toString(item.getRestOfFields()));
continue;
}
String color = null;
if (track.parent != null) {
if (track.parent.startsWith("Segway_17SegmentationSummaries")) {
color = segway_17SegmentationSummaries(rest[5]);
} else if (track.parent.startsWith("ProjectedSegments")) {
color = projectedSegments(rest[5]);
} else if (track.parent.startsWith("RegBuildOverview")) {
color = regBuildOverview(rest[5]);
} else if (track.parent.startsWith("Segway_17CellSegments")) {
color = segway_17CellSegments(rest[5]);
} else {
System.err.println("Unknown parent:" + track.parent);
}
}
if (color == null)
continue;
bedValues.add(rest[0] + "|" + color);
}
if (bedValues.isEmpty()) {
w1.add(ctx);
continue;
}
StringBuilder sb = new StringBuilder();
for (String s : bedValues) {
if (sb.length() != 0)
sb.append(",");
sb.append(s);
}
VariantContextBuilder vcb = new VariantContextBuilder(ctx);
vcb.attribute(track.id, sb.toString());
w1.add(vcb.make());
}
}
sstream.close();
in.close();
w1.close();
}
use of org.broad.igv.bbfile.BedFeature in project ASCIIGenome by dariober.
the class Utils method initRegionFromBigBed.
private static String initRegionFromBigBed(String bigBedFile) throws IOException {
BBFileReader reader = new BBFileReader(bigBedFile);
if (!reader.isBigBedFile()) {
System.err.println("File " + bigBedFile + " is not bigBed.");
throw new RuntimeException();
}
// Just get chrom to start with
String region = reader.getChromosomeNames().get(0);
for (String chrom : reader.getChromosomeNames()) {
BigBedIterator iter = reader.getBigBedIterator(chrom, 0, chrom, Integer.MAX_VALUE, false);
if (iter.hasNext()) {
BedFeature x = (BedFeature) iter.next();
region = x.getChromosome() + ":" + (x.getStartBase() + 1);
reader.close();
return region;
}
}
reader.close();
return region;
}
Aggregations