예제 #1
0
  /* @see loci.formats.FormatReader#initFile(String) */
  protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    CoreMetadata m = core.get(0);

    m.littleEndian = true;
    in.order(isLittleEndian());

    while (in.getFilePointer() < MAX_HEADER_SIZE) {
      readVariable();
    }

    in.seek(pixelsOffset);
    m.sizeX = in.readShort();
    pixelsOffset += 2;

    m.sizeY = getSizeX();
    m.pixelType = FormatTools.UINT16;

    m.sizeZ = 1;
    m.sizeC = 1;
    m.sizeT = 1;
    m.imageCount = 1;
    m.dimensionOrder = "XYZCT";

    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    if (date != null) {
      date = DateTools.formatDate(date, "MMM dd yyyy HH:mm:ssSSS");
      if (date != null) {
        store.setImageAcquisitionDate(new Timestamp(date), 0);
      }
    }

    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
      store.setImageDescription(comment, 0);

      PositiveFloat sizeX = FormatTools.getPhysicalSizeX((double) xSize / getSizeX());
      PositiveFloat sizeY = FormatTools.getPhysicalSizeY((double) xSize / getSizeY());

      if (sizeX != null) {
        store.setPixelsPhysicalSizeX(sizeX, 0);
      }
      if (sizeY != null) {
        store.setPixelsPhysicalSizeY(sizeY, 0);
      }
    }
  }
예제 #2
0
  /**
   * Populates the metadata store using the data parsed in {@link #initStandardMetadata()} along
   * with some further parsing done in the method itself.
   *
   * <p>All calls to the active <code>MetadataStore</code> should be made in this method and
   * <b>only</b> in this method. This is especially important for sub-classes that override the
   * getters for pixel set array size, etc.
   */
  protected void initMetadataStore() throws FormatException {
    LOGGER.info("Populating OME metadata");

    // the metadata store we're working with
    MetadataStore store = makeFilterMetadata();

    IFD firstIFD = ifds.get(0);
    IFD exif = null;

    if (ifds.get(0).containsKey(IFD.EXIF)) {
      try {
        IFDList exifIFDs = tiffParser.getExifIFDs();
        if (exifIFDs.size() > 0) {
          exif = exifIFDs.get(0);
        }
        tiffParser.fillInIFD(exif);
      } catch (IOException e) {
        LOGGER.debug("Could not read EXIF IFDs", e);
      }
    }

    MetadataTools.populatePixels(store, this, exif != null);

    // format the creation date to ISO 8601

    String creationDate = getImageCreationDate();
    String date = DateTools.formatDate(creationDate, DATE_FORMATS);
    if (creationDate != null && date == null) {
      LOGGER.warn("unknown creation date format: {}", creationDate);
    }
    creationDate = date;

    // populate Image

    if (creationDate != null) {
      store.setImageAcquisitionDate(new Timestamp(creationDate), 0);
    }

    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
      // populate Experimenter
      String artist = firstIFD.getIFDTextValue(IFD.ARTIST);

      if (artist != null) {
        String firstName = null, lastName = null;
        int ndx = artist.indexOf(" ");
        if (ndx < 0) lastName = artist;
        else {
          firstName = artist.substring(0, ndx);
          lastName = artist.substring(ndx + 1);
        }
        String email = firstIFD.getIFDStringValue(IFD.HOST_COMPUTER);
        store.setExperimenterFirstName(firstName, 0);
        store.setExperimenterLastName(lastName, 0);
        store.setExperimenterEmail(email, 0);
        store.setExperimenterID(MetadataTools.createLSID("Experimenter", 0), 0);
      }

      store.setImageDescription(firstIFD.getComment(), 0);

      // set the X and Y pixel dimensions

      double pixX = firstIFD.getXResolution();
      double pixY = firstIFD.getYResolution();

      PositiveFloat sizeX = FormatTools.getPhysicalSizeX(pixX);
      PositiveFloat sizeY = FormatTools.getPhysicalSizeY(pixY);

      if (sizeX != null) {
        store.setPixelsPhysicalSizeX(sizeX, 0);
      }
      if (sizeY != null) {
        store.setPixelsPhysicalSizeY(sizeY, 0);
      }
      store.setPixelsPhysicalSizeZ(null, 0);

      if (exif != null) {
        if (exif.containsKey(IFD.EXPOSURE_TIME)) {
          Object exp = exif.get(IFD.EXPOSURE_TIME);
          if (exp instanceof TiffRational) {
            Double exposure = ((TiffRational) exp).doubleValue();
            for (int i = 0; i < getImageCount(); i++) {
              store.setPlaneExposureTime(exposure, 0, i);
            }
          }
        }
      }
    }
  }
예제 #3
0
  /* @see loci.formats.FormatReader#initFile(String) */
  protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);

    Vector<String> uniqueChannels = new Vector<String>();
    Vector<Double> uniqueZs = new Vector<Double>();
    Vector<Double> stageX = new Vector<Double>();
    Vector<Double> stageY = new Vector<Double>();

    CoreMetadata m = core.get(0);

    String filename = id.substring(id.lastIndexOf(File.separator) + 1);
    filename = filename.substring(0, filename.indexOf("."));
    boolean integerFilename = true;
    try {
      Integer.parseInt(filename);
    } catch (NumberFormatException e) {
      integerFilename = false;
    }

    if (integerFilename
        && ifds.size() == 1
        && ifds.get(0).getIFDIntValue(IFD.NEW_SUBFILE_TYPE) == 2) {
      // look for other files in the dataset

      findTIFFs();

      String stageLabel = null;

      for (String tiff : files) {
        MetamorphHandler handler = new MetamorphHandler();
        parseFile(tiff, handler);

        String label = handler.getStageLabel();
        if (stageLabel == null) {
          stageLabel = label;
        } else if (!label.equals(stageLabel)) {
          break;
        }

        if (!uniqueChannels.contains(handler.getChannelName())) {
          uniqueChannels.add(handler.getChannelName());
        }

        Vector<Double> zPositions = handler.getZPositions();
        Double pos = Math.rint(zPositions.get(0));

        if (!uniqueZs.contains(pos)) {
          uniqueZs.add(pos);
        }
      }

      MetamorphHandler handler = new MetamorphHandler();
      parseFile(files[files.length - 1], handler);

      String lastStageLabel = handler.getStageLabel();
      int lastField = getField(lastStageLabel);
      int lastWellRow = getWellRow(lastStageLabel);
      int lastWellColumn = getWellColumn(lastStageLabel);

      int field = getField(stageLabel);
      int fieldRow = getWellRow(stageLabel);
      int fieldColumn = getWellColumn(stageLabel);

      wellCount = lastField - field + 1;
      fieldRowCount = lastWellRow - fieldRow + 1;
      fieldColumnCount = lastWellColumn - fieldColumn + 1;
      m.sizeC = uniqueChannels.size();
      m.sizeZ = uniqueZs.size();
    } else {
      files = new String[] {id};
      wellCount = 1;
      fieldRowCount = 1;
      fieldColumnCount = 1;
      m.sizeC = 0;
    }

    // parse XML comment

    MetamorphHandler handler = new MetamorphHandler(getGlobalMetadata());
    Vector<Double> xPositions = new Vector<Double>();
    Vector<Double> yPositions = new Vector<Double>();

    for (IFD ifd : ifds) {
      String xml = XMLTools.sanitizeXML(ifd.getComment());
      XMLTools.parseXML(xml, handler);

      double x = handler.getStagePositionX();
      double y = handler.getStagePositionY();

      if (xPositions.size() == 0) {
        xPositions.add(x);
        yPositions.add(y);
      } else {
        double previousX = xPositions.get(xPositions.size() - 1);
        double previousY = yPositions.get(yPositions.size() - 1);

        if (Math.abs(previousX - x) > 0.21 || Math.abs(previousY - y) > 0.21) {
          xPositions.add(x);
          yPositions.add(y);
        }
      }
    }

    if (xPositions.size() > 1) {
      fieldRowCount = xPositions.size();
    }

    Vector<Integer> wavelengths = handler.getWavelengths();
    Vector<Double> zPositions = handler.getZPositions();

    // calculate axis sizes

    Vector<Integer> uniqueC = new Vector<Integer>();
    for (Integer c : wavelengths) {
      if (!uniqueC.contains(c)) {
        uniqueC.add(c);
      }
    }
    int effectiveC = uniqueC.size();
    if (effectiveC == 0) effectiveC = 1;
    if (getSizeC() == 0) m.sizeC = 1;
    int samples = ifds.get(0).getSamplesPerPixel();
    m.sizeC *= effectiveC * samples;

    Vector<Double> uniqueZ = new Vector<Double>();
    for (Double z : zPositions) {
      if (!uniqueZ.contains(z)) uniqueZ.add(z);
    }
    if (getSizeZ() == 0) m.sizeZ = 1;
    m.sizeZ *= uniqueZ.size();

    Double zRange = zPositions.get(zPositions.size() - 1) - zPositions.get(0);
    Double physicalSizeZ = Math.abs(zRange);
    if (m.sizeZ > 1) {
      physicalSizeZ /= (m.sizeZ - 1);
    }

    int totalPlanes = files.length * ifds.size();
    effectiveC = getSizeC() / samples;
    m.sizeT =
        totalPlanes / (wellCount * fieldRowCount * fieldColumnCount * getSizeZ() * effectiveC);
    if (getSizeT() == 0) m.sizeT = 1;

    int seriesCount = wellCount * fieldRowCount * fieldColumnCount;

    if (seriesCount > 1 && getSizeZ() > totalPlanes / seriesCount) {
      m.sizeZ = 1;
      m.sizeT = totalPlanes / (seriesCount * getSizeT() * effectiveC);
    }

    m.imageCount = getSizeZ() * getSizeT() * effectiveC;

    if (seriesCount > 1) {
      core.clear();
      for (int i = 0; i < seriesCount; i++) {
        core.add(m);
      }
    }

    for (int s = 0; s < wellCount * fieldRowCount * fieldColumnCount; s++) {
      if (files.length > 1) {
        int[] lengths =
            new int[] {
              getSizeZ(),
              getEffectiveSizeC(),
              fieldColumnCount,
              fieldRowCount,
              wellCount,
              getSizeT()
            };

        Well well = getWell(s);
        int[] position = new int[] {0, 0, well.fieldCol, well.fieldRow, well.well, 0};

        int fileIndex = FormatTools.positionToRaster(lengths, position);
        parseFile(files[fileIndex], handler);

        stageX.add(handler.getStagePositionX());
        stageY.add(handler.getStagePositionY());
      } else {
        stageX.add(xPositions.get(s));
        stageY.add(yPositions.get(s));
      }
    }

    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);

    // Only populate Plate/Well metadata if there are multiple wells.
    // The presence of just one well suggests that we don't actually have
    // an HCS dataset (and even if we did, the Plate/Well metadata would be
    // effectively useless).
    if (wellCount > 1) {
      store.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
      store.setPlateRowNamingConvention(NamingConvention.LETTER, 0);
      store.setPlateColumnNamingConvention(NamingConvention.NUMBER, 0);

      for (int well = 0; well < wellCount; well++) {
        store.setWellID(MetadataTools.createLSID("Well", 0, well), 0, well);
        store.setWellRow(new NonNegativeInteger(0), 0, well);
        store.setWellColumn(new NonNegativeInteger(well), 0, well);

        for (int row = 0; row < fieldRowCount; row++) {
          for (int col = 0; col < fieldColumnCount; col++) {
            int field = row * fieldColumnCount + col;
            String wellSampleID = MetadataTools.createLSID("WellSample", 0, well, field);
            store.setWellSampleID(wellSampleID, 0, well, field);

            int seriesIndex = getSeriesIndex(row, col, well);
            String imageID = MetadataTools.createLSID("Image", seriesIndex);
            store.setImageID(imageID, seriesIndex);
            store.setWellSampleImageRef(imageID, 0, well, field);
            store.setWellSampleIndex(new NonNegativeInteger(seriesIndex), 0, well, field);
          }
        }
      }
    }

    for (int s = 0; s < seriesCount; s++) {
      setSeries(s);
      Well well = getWell(s);

      String name = handler.getImageName();
      if (seriesCount > 1) {
        name =
            "Field "
                + (char) (well.fieldRow + 'A')
                + (well.fieldCol + 1)
                + ", Well "
                + (well.well + 1)
                + ": "
                + name;
      }

      store.setImageName(name, s);

      String date = DateTools.formatDate(handler.getDate(), DateTools.ISO8601_FORMAT);
      if (date != null) {
        store.setImageAcquisitionDate(new Timestamp(date), s);
      }

      if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        Vector<String> timestamps = handler.getTimestamps();
        Vector<Double> exposures = handler.getExposures();

        for (int i = 0; i < timestamps.size(); i++) {
          long timestamp = DateTools.getTime(timestamps.get(i), DATE_FORMAT);
          addSeriesMetaList("timestamp", timestamp);
        }
        for (int i = 0; i < exposures.size(); i++) {
          addSeriesMetaList("exposure time (ms)", exposures.get(i).floatValue() * 1000);
        }

        long startDate = 0;
        if (timestamps.size() > 0) {
          startDate = DateTools.getTime(timestamps.get(0), DATE_FORMAT);
        }

        store.setImageDescription("", s);

        int image = 0;
        for (int c = 0; c < getEffectiveSizeC(); c++) {
          for (int t = 0; t < getSizeT(); t++) {
            store.setPlaneTheZ(new NonNegativeInteger(0), s, image);
            store.setPlaneTheC(new NonNegativeInteger(c), s, image);
            store.setPlaneTheT(new NonNegativeInteger(t), s, image);

            if (t < timestamps.size()) {
              String stamp = timestamps.get(t);
              long ms = DateTools.getTime(stamp, DATE_FORMAT);
              store.setPlaneDeltaT((ms - startDate) / 1000.0, s, image);
            }
            if (image < exposures.size()) {
              store.setPlaneExposureTime(exposures.get(image), s, image);
            }

            if (s < stageX.size()) {
              store.setPlanePositionX(stageX.get(s), s, image);
            }
            if (s < stageY.size()) {
              store.setPlanePositionY(stageY.get(s), s, image);
            }
            image++;
          }
        }

        store.setImagingEnvironmentTemperature(handler.getTemperature(), s);

        PositiveFloat sizeX = FormatTools.getPhysicalSizeX(handler.getPixelSizeX());
        PositiveFloat sizeY = FormatTools.getPhysicalSizeY(handler.getPixelSizeY());
        PositiveFloat sizeZ = FormatTools.getPhysicalSizeZ(physicalSizeZ);

        if (sizeX != null) {
          store.setPixelsPhysicalSizeX(sizeX, s);
        }
        if (sizeY != null) {
          store.setPixelsPhysicalSizeY(sizeY, s);
        }
        if (sizeZ != null) {
          store.setPixelsPhysicalSizeZ(sizeZ, s);
        }

        for (int c = 0; c < getEffectiveSizeC(); c++) {
          if (uniqueChannels.size() > c) {
            store.setChannelName(uniqueChannels.get(c), s, c);
          } else store.setChannelName(handler.getChannelName(), s, c);
        }
      }
    }
  }
예제 #4
0
  /* @see BaseTiffReader#initMetadataStore() */
  @Override
  protected void initMetadataStore() throws FormatException {
    super.initMetadataStore();
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);

    if (date != null) {
      date = DateTools.formatDate(date, DATE_FORMAT);
      if (date != null) {
        store.setImageAcquisitionDate(new Timestamp(date), 0);
      }
    }

    if (imageName != null) {
      store.setImageName(imageName, 0);
    }

    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
      if (imageDescription != null) {
        store.setImageDescription(imageDescription, 0);
      }
      if (userName != null) {
        store.setExperimenterID(MetadataTools.createLSID("Experimenter", 0), 0);
        store.setExperimenterLastName(userName, 0);
      }
      if (microscopeModel != null) {
        String instrument = MetadataTools.createLSID("Instrument", 0);
        store.setInstrumentID(instrument, 0);
        store.setImageInstrumentRef(instrument, 0);
        store.setMicroscopeModel(microscopeModel, 0);
      }
      if (detectors != null && detectors.size() > 0) {
        String instrument = MetadataTools.createLSID("Instrument", 0);
        store.setInstrumentID(instrument, 0);
        store.setImageInstrumentRef(instrument, 0);

        for (int i = 0; i < detectors.size(); i++) {
          String detectorID = MetadataTools.createLSID("Detector", 0, i);
          store.setDetectorID(detectorID, 0, i);
          store.setDetectorModel(detectors.get(i), 0, i);
          store.setDetectorType(getDetectorType("Other"), 0, i);
        }
      }
      if (magnification != null) {
        store.setObjectiveID(MetadataTools.createLSID("Objective", 0, 0), 0, 0);
        store.setObjectiveNominalMagnification(magnification, 0, 0);
        store.setObjectiveCorrection(getCorrection("Other"), 0, 0);
        store.setObjectiveImmersion(getImmersion("Other"), 0, 0);
      }

      store.setStageLabelX(stageX, 0);
      store.setStageLabelY(stageY, 0);
      store.setStageLabelZ(stageZ, 0);
      store.setStageLabelName("", 0);

      Length physicalSizeX = FormatTools.getPhysicalSizeX(sizeX);
      Length physicalSizeY = FormatTools.getPhysicalSizeY(sizeY);

      if (physicalSizeX != null) {
        store.setPixelsPhysicalSizeX(physicalSizeX, 0);
      }
      if (physicalSizeY != null) {
        store.setPixelsPhysicalSizeY(physicalSizeY, 0);
      }
      if (timeIncrement != null) {
        store.setPixelsTimeIncrement(new Time(timeIncrement, UNITS.S), 0);
      }
    }
  }