Esempio n. 1
0
  /**
   * @param reader the line reader to take header lines from
   * @return the number of header lines
   */
  public Object readHeader(LineReader reader) {
    List<String> headerStrings = new ArrayList<String>();

    String line;
    try {
      boolean foundHeaderVersion = false;
      while ((line = reader.readLine()) != null) {
        lineNo++;
        if (line.startsWith(VCFHeader.METADATA_INDICATOR)) {
          String[] lineFields = line.substring(2).split("=");
          if (lineFields.length == 2 && VCFHeaderVersion.isFormatString(lineFields[0])) {
            if (!VCFHeaderVersion.isVersionString(lineFields[1]))
              throw new TribbleException.InvalidHeader(
                  lineFields[1] + " is not a supported version");
            foundHeaderVersion = true;
            version = VCFHeaderVersion.toHeaderVersion(lineFields[1]);
            if (version != VCFHeaderVersion.VCF3_3 && version != VCFHeaderVersion.VCF3_2)
              throw new TribbleException.InvalidHeader(
                  "This codec is strictly for VCFv3 and does not support " + lineFields[1]);
          }
          headerStrings.add(line);
        } else if (line.startsWith(VCFHeader.HEADER_INDICATOR)) {
          if (!foundHeaderVersion) {
            throw new TribbleException.InvalidHeader(
                "We never saw a header line specifying VCF version");
          }
          return createHeader(headerStrings, line);
        } else {
          throw new TribbleException.InvalidHeader(
              "We never saw the required CHROM header line (starting with one #) for the input VCF file");
        }
      }
    } catch (IOException e) {
      throw new RuntimeException("IO Exception ", e);
    }
    throw new TribbleException.InvalidHeader(
        "We never saw the required CHROM header line (starting with one #) for the input VCF file");
  }
Esempio n. 2
0
  public void writeHeader(VCFHeader header) {
    mHeader = doNotWriteGenotypes ? new VCFHeader(header.getMetaData()) : header;

    try {
      // the file format field needs to be written first
      mWriter.write(
          VCFHeader.METADATA_INDICATOR
              + VCFHeaderVersion.VCF4_1.getFormatString()
              + "="
              + VCFHeaderVersion.VCF4_1.getVersionString()
              + "\n");

      for (VCFHeaderLine line : mHeader.getMetaData()) {
        if (VCFHeaderVersion.isFormatString(line.getKey())) continue;

        // are the records filtered (so we know what to put in the FILTER column of passing records)
        // ?
        if (line instanceof VCFFilterHeaderLine) filtersWereAppliedToContext = true;

        mWriter.write(VCFHeader.METADATA_INDICATOR);
        mWriter.write(line.toString());
        mWriter.write("\n");
      }

      // write out the column line
      mWriter.write(VCFHeader.HEADER_INDICATOR);
      for (VCFHeader.HEADER_FIELDS field : mHeader.getHeaderFields()) {
        mWriter.write(field.toString());
        mWriter.write(VCFConstants.FIELD_SEPARATOR);
      }

      if (mHeader.hasGenotypingData()) {
        mWriter.write("FORMAT");
        for (String sample : mHeader.getGenotypeSamples()) {
          mWriter.write(VCFConstants.FIELD_SEPARATOR);
          mWriter.write(sample);
        }
      }

      mWriter.write("\n");
      mWriter.flush(); // necessary so that writing to an output stream will work
    } catch (IOException e) {
      throw new TribbleException("IOException writing the VCF header to " + locationString(), e);
    }
  }