Ejemplo n.º 1
0
  /**
   * creates a new index, given the feature file and the codec
   *
   * @param featureFile the feature file (i.e. .vcf, .bed)
   * @param indexFile the index file; the location we should be writing the index to
   * @param codec the codec to read features with
   * @return an index instance
   */
  public static Index createAndWriteNewIndex(File featureFile, File indexFile, FeatureCodec codec) {
    try {
      Index index = IndexFactory.createLinearIndex(featureFile, codec);

      // try to write it to disk
      LittleEndianOutputStream stream =
          new LittleEndianOutputStream(new FileOutputStream(indexFile));
      index.write(stream);
      stream.close();

      return index;
    } catch (IOException e) {
      throw new RuntimeException("Unable to create index from file " + featureFile, e);
    }
  }
  /** attempt to close the VCF file */
  public void close() {
    try {
      // close the underlying output stream
      outputStream.close();

      // close the index stream (keep it separate to help debugging efforts)
      if (indexer != null) {
        final Index index = indexer.finalizeIndex(positionalOutputStream.getPosition());
        setIndexSequenceDictionary(index, refDict);
        index.write(idxStream);
        idxStream.close();
      }

    } catch (IOException e) {
      throw new RuntimeException("Unable to close index for " + getStreamName(), e);
    }
  }
 private static void setIndexSequenceDictionary(
     final Index index, final SAMSequenceDictionary dict) {
   for (final SAMSequenceRecord seq : dict.getSequences()) {
     final String contig = SequenceDictionaryPropertyPredicate + seq.getSequenceName();
     final String length = String.valueOf(seq.getSequenceLength());
     index.addProperty(contig, length);
   }
 }
Ejemplo n.º 4
0
  /** attempt to close the VCF file */
  public void close() {
    // try to close the vcf stream
    try {
      mWriter.flush();
      mWriter.close();
    } catch (IOException e) {
      throw new TribbleException(
          "Unable to close " + locationString() + " because of " + e.getMessage());
    }

    // try to close the index stream (keep it separate to help debugging efforts)
    if (indexer != null) {
      try {
        Index index = indexer.finalizeIndex(positionalStream.getPosition());
        index.write(idxStream);
        idxStream.close();
      } catch (IOException e) {
        throw new TribbleException(
            "Unable to close index for " + locationString() + " because of " + e.getMessage());
      }
    }
  }
Ejemplo n.º 5
0
 /**
  * Create a tribble style index.
  *
  * @param ifile
  * @param outputFile
  * @param indexType
  * @param binSize
  * @throws IOException
  */
 private void createTribbleIndex(
     String ifile, File outputFile, int indexType, int binSize, FeatureCodec codec)
     throws IOException {
   File inputFile = new File(ifile);
   Index idx = null;
   if (indexType == LINEAR_INDEX) {
     idx = IndexFactory.createLinearIndex(inputFile, codec, binSize);
   } else {
     idx = IndexFactory.createIntervalIndex(inputFile, codec, binSize);
   }
   if (idx != null) {
     String idxFile;
     if (outputFile != null) {
       idxFile = outputFile.getAbsolutePath();
     } else {
       idxFile = ifile + ".idx";
     }
     LittleEndianOutputStream stream = null;
     try {
       stream =
           new LittleEndianOutputStream(new BufferedOutputStream(new FileOutputStream(idxFile)));
       idx.write(stream);
     } catch (Exception e) {
       e.printStackTrace();
       // Delete output file as its probably corrupt
       File tmp = new File(idxFile);
       if (tmp.exists()) {
         tmp.delete();
       }
     } finally {
       if (stream != null) {
         stream.close();
       }
     }
   }
 }