<commit_before>//
// OmeisImporter.java
//

/*
LOCI Bio-Formats package for reading and converting biological file formats.
Copyright (C) 2005-@year@ <NAME>, <NAME>, <NAME>
and <NAME>.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.formats;

import java.io.*;
import java.util.*;

/**
 * A command line utility used by OMEIS to interface with Bio-Formats.
 *
 * @author <NAME> ctrueden at wisc.edu
 */
public class OmeisImporter {

  /** Reader for handling file formats. */
  private IFormatReader reader;

  /** Metadata store, for gathering OME-XML metadata. */
  private OMEXMLMetadataStore store;

  // -- Constructor --

  public OmeisImporter() {
    reader = new ChannelSeparator();
    store = new OMEXMLMetadataStore();
    reader.setMetadataStore(store);
  }

  // -- OmeisImporter API methods - main functionality --

  /**
   * Tests whether Bio-Formats is potentially capable of importing the given
   * file IDs. Outputs the IDs it can potentially import, one per line.
   */
  public void testIds(int[] fileIds) throws OmeisException {
    String[] ids = new String[fileIds.length];
    String[] paths = new String[fileIds.length];
    for (int i=0; i<fileIds.length; i++) {
      Hashtable fileInfo = getFileInfo(fileIds[i]);
      ids[i] = (String) fileInfo.get("Name");
      paths[i] = getLocalFilePath(fileIds[i]);
      reader.mapId(ids[i], paths[i]);
    }
    for (int i=0; i<fileIds.length; i++) {
      if (ids[i] == null && reader.isThisType(ids[i])) {
        System.out.println(fileIds[i]);
      }
    }
  }

  /**
   * Attempts to import the given file IDs using Bio-Formats. Pixels are saved
   * to the pixels files designated by OMEIS, and an OME-XML metadata block
   * describing all successfully imported data is dumped to standard output.
   */
  public void importIds(int[] fileIds) throws OmeisException {
    boolean doLittle = isLittleEndian();

    for (int i=0; i<fileIds.length; i++) {
      String id = getLocalFilePath(fileIds[i]);
      Hashtable fileInfo = getFileInfo(fileIds[i]);
      String oid = (String) fileInfo.get("Name");

      try {
        int seriesCount = reader.getSeriesCount(id);
        for (int s=0; s<seriesCount; s++) {
          reader.setSeries(id, s);

          // gather pixels information for this series
          int sizeX = reader.getSizeX(id);
          int sizeY = reader.getSizeY(id);
          int sizeZ = reader.getSizeZ(id);
          int sizeC = reader.getSizeC(id);
          int sizeT = reader.getSizeT(id);
          int pixelType = reader.getPixelType(id);
          int bytesPerPixel;
          boolean isSigned, isFloat;
          switch (pixelType) {
            case FormatReader.INT8:
              bytesPerPixel = 1;
              isSigned = true;
              isFloat = false;
              break;
            case FormatReader.UINT8:
              bytesPerPixel = 1;
              isSigned = false;
              isFloat = false;
              break;
            case FormatReader.INT16:
              bytesPerPixel = 2;
              isSigned = true;
              isFloat = false;
              break;
            case FormatReader.UINT16:
              bytesPerPixel = 2;
              isSigned = false;
              isFloat = false;
              break;
            case FormatReader.INT32:
              bytesPerPixel = 4;
              isSigned = true;
              isFloat = false;
              break;
            case FormatReader.UINT32:
              bytesPerPixel = 4;
              isSigned = false;
              isFloat = false;
              break;
            case FormatReader.FLOAT:
              bytesPerPixel = 4;
              isSigned = true;
              isFloat = true;
              break;
            case FormatReader.DOUBLE:
              bytesPerPixel = 8;
              isSigned = true;
              isFloat = true;
              break;
            default:
              System.err.println("Error: unknown pixel type for '" +
                oid + "' series #" + s + ": " + pixelType);
              continue;
          }
          boolean little = reader.isLittleEndian(id);
          boolean swap = doLittle != little;

          // ask OMEIS to allocate new pixels file
          int pixelsId = newPixels(sizeX, sizeY, sizeZ, sizeC, sizeT,
            bytesPerPixel, isSigned, isFloat);
          String pixelsPath = getLocalPixelsPath(pixelsId);

          // write pixels to file
          FileOutputStream out = new FileOutputStream(pixelsPath);
          int imageCount = reader.getImageCount(id);
          for (int j=0; i<imageCount; j++) {
            byte[] plane = reader.openBytes(id, j);
            if (swap && bytesPerPixel > 1 && !isFloat) { // swap endianness
              for (int b=0; b<plane.length; b+=bytesPerPixel) {
                for (int k=0; k<bytesPerPixel/2; k++) {
                  int i1 = b + k;
                  int i2 = b + bytesPerPixel - k - 1;
                  byte b1 = plane[i1];
                  byte b2 = plane[i2];
                  plane[i1] = b2;
                  plane[i2] = b1;
                }
              }
            }
            out.write(plane);
          }
          out.close();
          reader.close();

          // tell OMEIS we're done
          pixelsId = finishPixels(pixelsId);

          // inject pixels ID into proper Pixels element
          // CTR TODO
        }

        // output OME-XML to standard output
        System.out.println(store.dumpXML());
        // CTR TODO - needs to be merged into one big OME block
      }
      catch (FormatException exc) {
        System.err.println(
          "Error: an exception occurred reading '" + oid + "':");
        exc.printStackTrace();
      }
      catch (IOException exc) {
        System.err.println(
          "Error: an exception occurred reading '" + oid + "':");
        exc.printStackTrace();
      }
    }
  }

  // -- OmeisImporter API methods - OMEIS method calls --

  /** Gets path to original file corresponding to the given file ID. */
  public String getLocalFilePath(int fileId) throws OmeisException {
    // ./omeis Method=GetLocalPath FileID=fid
    String[] s;
    try { s = omeis("GetLocalPath", "FileID=" + fileId); }
    catch (IOException exc) { throw new OmeisException(exc); }
    catch (InterruptedException exc) { throw new OmeisException(exc); }
    if (s.length > 1) {
      System.err.println("Warning: ignoring " + (s.length - 1) +
        " extraneous lines in OMEIS GetLocalPath call");
    }
    else if (s.length < 1) {
      throw new OmeisException(
        "Failed to obtain local path for file ID " + fileId);
    }
    return s[0];
  }

  /**
   * Gets information about the file corresponding to the given file ID.
   * @return hashtable containing the information as key/value pairs
   */
  public Hashtable getFileInfo(int fileId) throws OmeisException {<commit_msg>Use mapping API for OmeisImporter.importIds.<commit_after>//
// OmeisImporter.java
//

/*
LOCI Bio-Formats package for reading and converting biological file formats.
Copyright (C) 2005-@year@ <NAME>, <NAME>, <NAME>
and <NAME>.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.formats;

import java.io.*;
import java.util.*;

/**
 * A command line utility used by OMEIS to interface with Bio-Formats.
 *
 * @author <NAME> ctrueden at wisc.edu
 */
public class OmeisImporter {

  /** Reader for handling file formats. */
  private IFormatReader reader;

  /** Metadata store, for gathering OME-XML metadata. */
  private OMEXMLMetadataStore store;

  // -- Constructor --

  public OmeisImporter() {
    reader = new ChannelSeparator();
    store = new OMEXMLMetadataStore();
    reader.setMetadataStore(store);
  }

  // -- OmeisImporter API methods - main functionality --

  /**
   * Tests whether Bio-Formats is potentially capable of importing the given
   * file IDs. Outputs the IDs it can potentially import, one per line.
   */
  public void testIds(int[] fileIds) throws OmeisException {
    // set up file path mappings
    String[] ids = new String[fileIds.length];
    for (int i=0; i<fileIds.length; i++) {
      Hashtable fileInfo = getFileInfo(fileIds[i]);
      ids[i] = (String) fileInfo.get("Name");
      String path = getLocalFilePath(fileIds[i]);
      reader.mapId(ids[i], path);
    }
    // check types
    for (int i=0; i<fileIds.length; i++) {
      if (ids[i] != null && reader.isThisType(ids[i])) {
        System.out.println(fileIds[i]);
      }
    }
  }

  /**
   * Attempts to import the given file IDs using Bio-Formats. Pixels are saved
   * to the pixels files designated by OMEIS, and an OME-XML metadata block
   * describing all successfully imported data is dumped to standard output.
   */
  public void importIds(int[] fileIds) throws OmeisException {
    boolean doLittle = isLittleEndian();

    // set up file path mappings
    String[] ids = new String[fileIds.length];
    for (int i=0; i<fileIds.length; i++) {
      Hashtable fileInfo = getFileInfo(fileIds[i]);
      ids[i] = (String) fileInfo.get("Name");
      String path = getLocalFilePath(fileIds[i]);
      reader.mapId(ids[i], path);
    }

    // read files
    for (int i=0; i<fileIds.length; i++) {
      String id = ids[i];
      String path = reader.getMappedId(ids[i]);
      try {
        int seriesCount = reader.getSeriesCount(id);
        for (int s=0; s<seriesCount; s++) {
          reader.setSeries(id, s);

          // gather pixels information for this series
          int sizeX = reader.getSizeX(id);
          int sizeY = reader.getSizeY(id);
          int sizeZ = reader.getSizeZ(id);
          int sizeC = reader.getSizeC(id);
          int sizeT = reader.getSizeT(id);
          int pixelType = reader.getPixelType(id);
          int bytesPerPixel;
          boolean isSigned, isFloat;
          switch (pixelType) {
            case FormatReader.INT8:
              bytesPerPixel = 1;
              isSigned = true;
              isFloat = false;
              break;
            case FormatReader.UINT8:
              bytesPerPixel = 1;
              isSigned = false;
              isFloat = false;
              break;
            case FormatReader.INT16:
              bytesPerPixel = 2;
              isSigned = true;
              isFloat = false;
              break;
            case FormatReader.UINT16:
              bytesPerPixel = 2;
              isSigned = false;
              isFloat = false;
              break;
            case FormatReader.INT32:
              bytesPerPixel = 4;
              isSigned = true;
              isFloat = false;
              break;
            case FormatReader.UINT32:
              bytesPerPixel = 4;
              isSigned = false;
              isFloat = false;
              break;
            case FormatReader.FLOAT:
              bytesPerPixel = 4;
              isSigned = true;
              isFloat = true;
              break;
            case FormatReader.DOUBLE:
              bytesPerPixel = 8;
              isSigned = true;
              isFloat = true;
              break;
            default:
              System.err.println("Error: unknown pixel type for '" +
                path + "' series #" + s + ": " + pixelType);
              continue;
          }
          boolean little = reader.isLittleEndian(id);
          boolean swap = doLittle != little;

          // ask OMEIS to allocate new pixels file
          int pixelsId = newPixels(sizeX, sizeY, sizeZ, sizeC, sizeT,
            bytesPerPixel, isSigned, isFloat);
          String pixelsPath = getLocalPixelsPath(pixelsId);

          // write pixels to file
          FileOutputStream out = new FileOutputStream(pixelsPath);
          int imageCount = reader.getImageCount(id);
          for (int j=0; i<imageCount; j++) {
            byte[] plane = reader.openBytes(id, j);
            if (swap && bytesPerPixel > 1 && !isFloat) { // swap endianness
              for (int b=0; b<plane.length; b+=bytesPerPixel) {
                for (int k=0; k<bytesPerPixel/2; k++) {
                  int i1 = b + k;
                  int i2 = b + bytesPerPixel - k - 1;
                  byte b1 = plane[i1];
                  byte b2 = plane[i2];
                  plane[i1] = b2;
                  plane[i2] = b1;
                }
              }
            }
            out.write(plane);
          }
          out.close();
          reader.close();

          // tell OMEIS we're done
          pixelsId = finishPixels(pixelsId);

          // inject pixels ID into proper Pixels element
          // CTR TODO
        }

        // output OME-XML to standard output
        System.out.println(store.dumpXML());
        // CTR TODO - needs to be merged into one big OME block
      }
      catch (FormatException exc) {
        System.err.println(
          "Error: an exception occurred reading '" + path + "':");
        exc.printStackTrace();
      }
      catch (IOException exc) {
        System.err.println(
          "Error: an exception occurred reading '" + path + "':");
        exc.printStackTrace();
      }
    }
  }

  // -- OmeisImporter API methods - OMEIS method calls --

  /** Gets path to original file corresponding to the given file ID. */
  public String getLocalFilePath(int fileId) throws OmeisException {
    // ./omeis Method=GetLocalPath FileID=fid
    String[] s;
    try { s = omeis("GetLocalPath", "FileID=" + fileId); }
    catch (IOException exc) { throw new OmeisException(exc); }
    catch (InterruptedException exc) { throw new OmeisException(exc); }
    if (s.length > 1) {
      System.err.println("Warning: ignoring " + (s.length - 1) +
        " extraneous lines in OMEIS GetLocalPath call");
    }
    else if (s.length < 1) {
      throw new OmeisException(
        "Failed to obtain local path for file ID " + fileId);
    }
    return s[0];
  }

  /**
   * Gets information about the file corresponding to the given file ID.
   * @return hashtable containing the information as key/value pairs
   */
  public Hashtable getFileInfo(int fileId) throws OmeisException {
{'fraction_non_alphanumeric': 0.09873253168670783, 'fraction_numerical': 0.00799480012999675, 'mean_word_length': 1.9662618083670715, 'pattern_counts': {'":': 2, '<': 30, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5808221', 'n_tokens_mistral': 4412, 'n_tokens_neox': 4258, 'n_words': 1389}