use of htsjdk.tribble.readers.LineIteratorImpl in project jvarkit by lindenb.
the class GenomicJaspar method doWork.
@Override
public int doWork(List<String> args) {
if (jasparUri == null) {
LOG.error("Undefined jaspar-uri");
return -1;
}
PrintWriter out = null;
try {
out = super.openFileOrStdoutAsPrintWriter(OUT);
LOG.info("Reading " + jasparUri);
LineReader lr = IOUtils.openURIForLineReader(jasparUri);
LineIterator liter = new LineIteratorImpl(lr);
Iterator<Matrix> miter = Matrix.iterator(liter);
while (miter.hasNext()) {
Matrix matrix = miter.next();
this.jasparDb.add(matrix.convertToPWM());
}
lr.close();
LOG.info("JASPAR size: " + this.jasparDb.size());
if (args.isEmpty()) {
LOG.info("Reading from stdin");
run(out, new InputStreamReader(stdin()));
} else {
for (final String fname : args) {
LOG.info("Opening " + fname);
Reader in = IOUtils.openURIForBufferedReading(fname);
run(out, in);
in.close();
}
}
out.flush();
out.close();
out = null;
return 0;
} catch (Throwable err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(out);
}
}
use of htsjdk.tribble.readers.LineIteratorImpl in project jvarkit by lindenb.
the class VcfRegistryCGI method doWork.
private void doWork(XMLStreamWriter w, final GroupFile gf) throws XMLStreamException {
Position pos = parsePosition();
if (pos == null)
return;
w.writeStartElement("div");
w.writeStartElement("h2");
w.writeCharacters(pos.chrom + ":" + pos.pos);
w.writeEndElement();
w.writeStartElement("table");
w.writeStartElement("thead");
w.writeStartElement("tr");
for (String header : new String[] { "CHROM", "POS", "ID", "REF", "QUAL", "Sample", "Alleles", "DP", "GQ", "File" }) {
w.writeStartElement("th");
w.writeCharacters(header);
// td
w.writeEndElement();
}
// tr
w.writeEndElement();
// thead
w.writeEndElement();
w.writeStartElement("tbody");
Set<String> samplesWithGenotypes = new HashSet<String>();
Set<String> allSamples = new HashSet<String>();
for (VcfFile f : getVcfFiles(gf)) {
TabixReader tabixReader = null;
TabixReader.Iterator iter = null;
BlockCompressedInputStream bgzin = null;
VCFHeader header = null;
AbstractVCFCodec vcfCodec = VCFUtils.createDefaultVCFCodec();
LineIterator lineIterator = null;
for (int i = 0; i < 2; i++) {
try {
if (i == 0) {
bgzin = new BlockCompressedInputStream(f.file);
lineIterator = new LineIteratorImpl(new SynchronousLineReader(bgzin));
header = (VCFHeader) vcfCodec.readActualHeader(lineIterator);
allSamples.addAll(header.getGenotypeSamples());
} else {
tabixReader = new TabixReader(f.file.getPath());
String line;
int[] x = tabixReader.parseReg(pos.chrom + ":" + pos.pos + "-" + (pos.pos));
if (x != null && x.length > 2 && x[0] != -1) {
iter = tabixReader.query(x[0], x[1], x[2]);
} else {
}
while (iter != null && (line = iter.next()) != null) {
VariantContext var = vcfCodec.decode(line);
for (String sample : header.getSampleNamesInOrder()) {
final Genotype genotype = var.getGenotype(sample);
if (genotype == null || !genotype.isCalled())
continue;
if (!genotype.isAvailable())
continue;
samplesWithGenotypes.add(sample);
w.writeStartElement("tr");
w.writeStartElement("td");
w.writeCharacters(var.getContig());
w.writeEndElement();
w.writeStartElement("td");
w.writeCharacters(String.valueOf(var.getStart()));
w.writeEndElement();
if (var.hasID()) {
w.writeStartElement("td");
if (var.getID().matches("rs[0-9]+")) {
w.writeStartElement("a");
w.writeAttribute("href", "http://www.ncbi.nlm.nih.gov/snp/" + var.getID().substring(2));
w.writeCharacters(var.getID());
// a
w.writeEndElement();
} else {
w.writeCharacters(var.getID());
}
// td
w.writeEndElement();
} else {
w.writeEmptyElement("td");
}
if (var.getReference() != null) {
w.writeStartElement("td");
w.writeCharacters(var.getReference().getBaseString());
w.writeEndElement();
} else {
w.writeEmptyElement("td");
}
if (var.hasLog10PError()) {
w.writeStartElement("td");
w.writeCharacters(String.valueOf((int) var.getPhredScaledQual()));
w.writeEndElement();
} else {
w.writeEmptyElement("td");
}
w.writeStartElement("td");
w.writeCharacters(sample);
w.writeEndElement();
List<Allele> alleles = genotype.getAlleles();
w.writeStartElement("td");
w.writeStartElement("span");
if (genotype.isHomRef()) {
w.writeAttribute("style", "color:green;");
} else if (genotype.isHomVar()) {
w.writeAttribute("style", "color:red;");
} else if (genotype.isHet()) {
w.writeAttribute("style", "color:blue;");
}
for (int j = 0; j < alleles.size(); ++j) {
if (j > 0)
w.writeCharacters(genotype.isPhased() ? "|" : "/");
w.writeCharacters(alleles.get(j).getBaseString());
}
// span
w.writeEndElement();
w.writeEndElement();
if (genotype.hasDP()) {
w.writeStartElement("td");
w.writeCharacters(String.valueOf(genotype.getDP()));
w.writeEndElement();
} else {
w.writeEmptyElement("td");
}
if (genotype.hasGQ()) {
w.writeStartElement("td");
w.writeCharacters(String.valueOf(genotype.getGQ()));
w.writeEndElement();
} else {
w.writeEmptyElement("td");
}
w.writeStartElement("td");
w.writeCharacters(f.file.getName());
w.writeEndElement();
// tr
w.writeEndElement();
w.flush();
}
}
}
} catch (Exception err) {
w.writeComment("BOUM " + err);
header = null;
lastException = err;
} finally {
CloserUtil.close(lineIterator);
CloserUtil.close(bgzin);
CloserUtil.close(tabixReader);
CloserUtil.close(iter);
}
if (i == 0 && header == null)
break;
}
w.flush();
}
// tbody
w.writeEndElement();
// table
w.writeEndElement();
allSamples.removeAll(samplesWithGenotypes);
if (!allSamples.isEmpty()) {
w.writeStartElement("h3");
w.writeCharacters("Samples not found");
w.writeEndElement();
w.writeStartElement("ol");
for (String sample : new TreeSet<String>(allSamples)) {
w.writeStartElement("li");
w.writeCharacters(sample);
w.writeEndElement();
}
w.writeEndElement();
}
writeHTMLException(w, this.lastException);
// div
w.writeEndElement();
}
use of htsjdk.tribble.readers.LineIteratorImpl in project jvarkit by lindenb.
the class ConvertBedChromosomes method doWork.
@SuppressWarnings("resource")
protected int doWork(InputStream in, PrintStream out) throws IOException {
final int chromColumn0 = chromColumn1 - 1;
Pattern tab = Pattern.compile("[\t]");
LineIterator lr = new LineIteratorImpl(new AsciiLineReader(in));
while (lr.hasNext()) {
String line = lr.next();
if (BedLine.isBedHeader(line)) {
out.println(line);
continue;
}
final String[] tokens = tab.split(line, (chromColumn0 + 2));
if (chromColumn0 >= tokens.length)
throw new IOException("Bad BED line : " + line + " extected at least " + (chromColumn0 + 2) + " columns");
final String chrom = convertName(tokens[chromColumn0]);
if (chrom == null)
continue;
for (int i = 0; i < tokens.length; ++i) {
if (i > 0)
out.print("\t");
out.print(i == chromColumn0 ? chrom : tokens[i]);
}
out.println();
}
out.flush();
return 0;
}
use of htsjdk.tribble.readers.LineIteratorImpl in project gatk by broadinstitute.
the class SAMPileupCodecUnitTest method testDecodeLoc.
@Test(dataProvider = "stringFeature")
private void testDecodeLoc(final String pileupString, final SAMPileupFeature expected) throws Exception {
final Feature feature = CODEC.decodeLoc(new LineIteratorImpl(makeReader(pileupString)));
Assert.assertEquals(feature.getContig(), expected.getContig());
Assert.assertEquals(feature.getStart(), expected.getStart());
Assert.assertEquals(feature.getEnd(), expected.getEnd());
}
use of htsjdk.tribble.readers.LineIteratorImpl in project gatk by broadinstitute.
the class TableCodecUnitTest method testDecodeComment.
@Test
public void testDecodeComment() {
TableCodec tc = new TableCodec();
LineReader reader = makeReader(asList("#HEADER a b c", "HEADER d e f"));
LineIterator li = new LineIteratorImpl(reader);
List<String> hd = tc.readActualHeader(li);
Assert.assertEquals(hd, asList("HEADER", "d", "e", "f"));
}
Aggregations