@Override protected Fasta advance() { Fasta f = null; for (; ; ) { if (!in.hasNext()) return null; String line = in.next(); if (line.trim().isEmpty()) continue; if (!line.startsWith(">")) throw new RuntimeException("Expected '>'. Bad fasta line :" + line); f = new Fasta(); f.name = line.substring(1); break; } StringBuilder sb = new StringBuilder(); while (in.hasNext()) { String line = in.peek(); if (line.trim().isEmpty()) { in.next(); continue; } if (line.startsWith(">")) break; // new sequence sb.append(in.next().trim()); } f.sequence = sb.toString(); return f; }
/** * Returns a list of VariantContext records from a VCF file * * @param vcfFile VCF file * @throws IOException if the file does not exist or can not be opened * @return list of VariantContext records */ private static List<VariantContext> getVariantContexts(final File vcfFile) throws IOException { final VCFCodec codec = new VCFCodec(); final FileInputStream s = new FileInputStream(vcfFile); final LineIterator lineIteratorVCF = codec.makeSourceFromStream(new PositionalBufferedStream(s)); codec.readHeader(lineIteratorVCF); final List<VariantContext> VCs = new ArrayList<>(); while (lineIteratorVCF.hasNext()) { final String line = lineIteratorVCF.next(); Assert.assertFalse(line == null); VCs.add(codec.decode(line)); } return VCs; }
/** * Returns a list of attribute values from a VCF file * * @param vcfFile VCF file * @param attributeName attribute name * @throws IOException if the file does not exist or can not be opened * @return list of attribute values */ private List<String> getAttributeValues(final File vcfFile, final String attributeName) throws IOException { final VCFCodec codec = new VCFCodec(); final FileInputStream s = new FileInputStream(vcfFile); final LineIterator lineIteratorVCF = codec.makeSourceFromStream(new PositionalBufferedStream(s)); codec.readHeader(lineIteratorVCF); List<String> attributeValues = new ArrayList<String>(); while (lineIteratorVCF.hasNext()) { final String line = lineIteratorVCF.next(); Assert.assertFalse(line == null); final VariantContext vc = codec.decode(line); for (final Genotype g : vc.getGenotypes()) { if (g.hasExtendedAttribute(attributeName)) { attributeValues.add((String) g.getExtendedAttribute(attributeName)); } } } return attributeValues; }
private void scan(Graphics2D g, InputStream input) throws IOException { Set<String> unknownC = new HashSet<String>(); Pattern tab = Pattern.compile("[\t]"); LineIterator in = new LineIteratorImpl(LineReaderUtil.fromBufferedStream(input)); while (in.hasNext()) { String line = in.next(); String tokens[] = tab.split(line, 5); if (tokens.length < 4) { warning("Ignoring " + line); continue; } SAMSequenceRecord rec = this.context.getDictionary().getSequence(tokens[0]); if (rec == null) { warning("unknown chromosome " + tokens[0]); continue; } String country = tokens[3].toLowerCase().replaceAll("[ ]", ""); Shape shape = this.country2shape.get(country); if (shape == null) { if (!unknownC.contains(country)) { unknownC.add(country); warning("unknown country " + country); } continue; } seen.incr(country); int midpos = (Integer.parseInt(tokens[1]) + Integer.parseInt(tokens[2])) / 2; // country center Point2D.Double pt1 = new Point2D.Double(shape.getBounds2D().getCenterX(), shape.getBounds2D().getCenterY()); // circle point Point2D pt3 = this.context.convertPositionToPoint(tokens[0], midpos, getRadiusInt()); double angle = this.context.convertPositionToRadian(rec, midpos); double angle2 = angle -= Math.PI / 10.0; double distance13 = context .getCenter() .distance( new Point2D.Double( (pt1.getX() + pt3.getX()) / 2.0, (pt1.getY() + pt3.getY()) / 2.0)); // mid point Point2D pt2 = new Point2D.Double( context.getCenterX() + distance13 * Math.cos(angle2), context.getCenterX() + distance13 * Math.sin(angle2)); Composite old = g.getComposite(); Stroke olds = g.getStroke(); g.setStroke(new BasicStroke(0.8f)); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.02f)); g.setColor(Color.DARK_GRAY); GeneralPath p = new GeneralPath(); p.moveTo(pt1.getX(), pt1.getY()); p.quadTo(pt2.getX(), pt2.getY(), pt3.getX(), pt3.getY()); p.closePath(); g.draw(p); g.setComposite(old); g.setStroke(olds); } CloserUtil.close(in); }