예제 #1
0
  /**
   * Create an index for an alignment or feature file The output index will have the same base name
   * is the input file, although it may be in a different directory. An appropriate index extension
   * (.sai, .idx, etc.) will be appended.
   *
   * @param ifile
   * @param typeString
   * @param outputDir
   * @param indexType
   * @param binSize
   * @throws IOException
   */
  public String doIndex(
      String ifile, String typeString, String outputDir, int indexType, int binSize)
      throws IOException {
    File inputFile = new File(ifile);

    if (outputDir == null) {
      outputDir = inputFile.getParent();
    }
    String outputFileName = (new File(outputDir, inputFile.getName())).getAbsolutePath();

    if (typeString.endsWith("gz")) {
      System.out.println("Cannot index a gzipped file");
      throw new PreprocessingException("Cannot index a gzipped file");
    }

    if (typeString.endsWith("bam")) {
      String msg =
          "Cannot index a BAM file. Use the samtools package for sorting and indexing BAM files.";
      System.out.println(msg);
      throw new PreprocessingException(msg);
    }

    // We have different naming conventions for different index files
    if (typeString.endsWith("sam") && !outputFileName.endsWith(".sai")) {
      outputFileName += ".sai";
    } else if (typeString.endsWith("bam") && !outputFileName.endsWith(".bai")) {
      outputFileName += ".bai";
    } else if (typeString.endsWith("fa") && !outputFileName.endsWith(".fai")) {
      outputFileName += ".fai";
    } else if (typeString.endsWith("fasta") && !outputFileName.endsWith(".fai")) {
      outputFileName += ".fai";
    } else if (!typeString.endsWith("sam")
        && !typeString.endsWith("bam")
        && !outputFileName.endsWith(".idx")) {
      outputFileName += ".idx";
    }

    File outputFile = new File(outputFileName);

    // Sam/FASTA files are special
    try {
      if (typeString.endsWith("sam")) {
        AlignmentIndexer indexer = AlignmentIndexer.getInstance(inputFile, null, null);
        indexer.createSamIndex(outputFile);
        return outputFileName;
      } else if (typeString.equals(".fa") || typeString.equals(".fasta")) {
        FastaUtils.createIndexFile(inputFile.getAbsolutePath(), outputFileName);
        return outputFileName;
      }
    } catch (Exception e) {
      e.printStackTrace();
      // Delete output file as it is probably corrupt
      if (outputFile.exists()) {
        outputFile.delete();
      }
    }

    Genome genome = null; // <= don't do chromosome conversion
    FeatureCodec codec = CodecFactory.getCodec(ifile, genome);
    if (codec != null) {
      try {
        createTribbleIndex(ifile, outputFile, indexType, binSize, codec);
      } catch (TribbleException.MalformedFeatureFile e) {
        StringBuffer buf = new StringBuffer();
        buf.append("<html>Files must be sorted by start position prior to indexing.<br>");
        buf.append(e.getMessage());
        buf.append(
            "<br><br>Note: igvtools can be used to sort the file, select \"File > Run igvtools...\".");
        MessageUtils.showMessage(buf.toString());
      }
    } else {
      throw new DataLoadException("Unknown File Type", ifile);
    }
    System.out.flush();
    return outputFileName;
  }
예제 #2
0
 @Override
 protected FeatureIndex doInBackground() throws Exception {
   AlignmentIndexer indexer = AlignmentIndexer.getInstance(file, progressBar, this);
   return indexer.createSamIndex(idxFile, 16000);
 }