Search in sources :

Example 16 with FilePattern

use of loci.formats.FilePattern in project bioformats by openmicroscopy.

the class FilePatternDialog method constructDialog.

@Override
protected GenericDialog constructDialog() {
    // CTR - CHECK
    Location idLoc = new Location(options.getId());
    String id = FilePattern.findPattern(idLoc);
    if (id == null) {
        if (!options.isQuiet()) {
            IJ.showMessage("Bio-Formats", "Warning: Bio-Formats was unable to determine a grouping that\n" + "includes the file you chose. The most common reason for this\n" + "situation is that the folder contains extraneous files with\n" + "similar names and numbers that confuse the detection algorithm.\n" + " \n" + "For example, if you have multiple datasets in the same folder\n" + "named series1_z*_c*.tif, series2_z*_c*.tif, etc., Bio-Formats\n" + "may try to group all such files into a single series.\n" + " \n" + "For best results, put each image series's files in their own\n" + "folder, or type in a file pattern manually.\n");
        }
        id = idLoc.getAbsolutePath();
    }
    // construct dialog
    GenericDialog gd = new GenericDialog("Bio-Formats File Stitching") {

        @Override
        public void itemStateChanged(ItemEvent e) {
            super.itemStateChanged(e);
            Object source = e.getSource();
            if (!(source instanceof Checkbox)) {
                return;
            }
            boolean selected = e.getStateChange() == ItemEvent.SELECTED;
            Vector checkboxes = getCheckboxes();
            for (Object checkbox : checkboxes) {
                if (!checkbox.equals(source)) {
                    if (selected) {
                        ((Checkbox) checkbox).setState(false);
                    }
                } else if (!selected && checkbox.equals(source)) {
                    ((Checkbox) checkbox).setState(true);
                }
            }
        }
    };
    gd.addMessage("The list of files to be grouped can be specified in one of the following ways:");
    // option one
    int len = id.length() + 1;
    if (len > 80)
        len = 80;
    originalID = id;
    fp = new FilePattern(id);
    String[] prefixes = fp.getPrefixes();
    if (prefixes.length > 0) {
        gd.addCheckbox("Dimensions", true);
        int[] counts = fp.getCount();
        paddingZeros = new int[counts.length];
        String[][] elements = fp.getElements();
        BigInteger[] first = fp.getFirst();
        BigInteger[] step = fp.getStep();
        for (int i = 0; i < prefixes.length; i++) {
            String prefix = "Axis_" + (i + 1);
            gd.addStringField(prefix + "_number_of_images", "" + counts[i]);
            gd.addStringField(prefix + "_axis_first_image", first[i].toString());
            gd.addStringField(prefix + "_axis_increment", step[i].toString());
            try {
                paddingZeros[i] = elements[i][0].length() - String.valueOf(Integer.parseInt(elements[i][0])).length();
            } catch (NumberFormatException e) {
            }
        }
    }
    // option two
    gd.addCheckbox("File_name", false);
    gd.addStringField("contains", "");
    // option three
    gd.addCheckbox("Pattern", false);
    gd.addStringField("name", id, len);
    rebuild(gd);
    return gd;
}
Also used : ItemEvent(java.awt.event.ItemEvent) Checkbox(java.awt.Checkbox) GenericDialog(ij.gui.GenericDialog) BigInteger(java.math.BigInteger) FilePattern(loci.formats.FilePattern) Vector(java.util.Vector) Location(loci.common.Location)

Example 17 with FilePattern

use of loci.formats.FilePattern in project bioformats by openmicroscopy.

the class ImportProcess method createBaseReader.

/**
 * Initializes an {@link loci.formats.IFormatReader}
 * according to the current configuration.
 */
private void createBaseReader() throws FormatException, IOException {
    if (options.isLocal() || options.isHTTP()) {
        BF.status(options.isQuiet(), "Identifying " + idName);
        imageReader = LociPrefs.makeImageReader();
        baseReader = imageReader.getReader(options.isUsingPatternIds() ? new FilePattern(options.getId()).getFiles()[0] : options.getId());
    } else if (options.isOMERO()) {
        BF.status(options.isQuiet(), "Establishing server connection");
        try {
            ReflectedUniverse r = new ReflectedUniverse();
            r.exec("import loci.ome.io.OmeroReader");
            r.exec("baseReader = new OmeroReader()");
            ClassList<IFormatReader> classes = new ClassList<IFormatReader>(IFormatReader.class);
            r.setVar("classes", classes);
            r.exec("class = baseReader.getClass()");
            r.exec("classes.addClass(class)");
            imageReader = new ImageReader(classes);
            baseReader = imageReader.getReader(options.getId());
        } catch (Exception exc) {
            WindowTools.reportException(exc, options.isQuiet(), "Sorry, there was a problem communicating with the server.");
            cancel();
            return;
        }
    } else {
        WindowTools.reportException(null, options.isQuiet(), "Sorry, there has been an internal error: unknown data source");
        cancel();
        return;
    }
    // attach OME-XML metadata store
    Exception exc = null;
    try {
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        meta = service.createOMEXMLMetadata();
        omeXML = null;
    } catch (DependencyException de) {
        exc = de;
    } catch (ServiceException se) {
        exc = se;
    }
    if (exc != null) {
        WindowTools.reportException(exc, options.isQuiet(), "Sorry, there was a problem constructing the OME-XML metadata store");
        throw new FormatException(exc);
    }
    baseReader.setMetadataStore(meta);
    BF.status(options.isQuiet(), "");
    DebugTools.enableIJLogging(IJ.debugMode);
}
Also used : IFormatReader(loci.formats.IFormatReader) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) FilePattern(loci.formats.FilePattern) ReflectedUniverse(loci.common.ReflectedUniverse) ClassList(loci.formats.ClassList) ImageReader(loci.formats.ImageReader) DependencyException(loci.common.services.DependencyException) EnumerationException(ome.xml.model.enums.EnumerationException) ServiceException(loci.common.services.ServiceException) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException)

Example 18 with FilePattern

use of loci.formats.FilePattern in project bioformats by openmicroscopy.

the class MetamorphTiffReader method findTIFFs.

private void findTIFFs() throws IOException {
    Location baseFile = new Location(currentId).getAbsoluteFile();
    Location parent = baseFile.getParentFile();
    FilePattern pattern = new FilePattern(baseFile);
    String[] tiffs = pattern.getFiles();
    NumericComparator comparator = new NumericComparator();
    Arrays.sort(tiffs, comparator);
    final List<String> validTIFFs = new ArrayList<String>();
    for (String tiff : tiffs) {
        if (!new Location(tiff).exists()) {
            String base = tiff.substring(tiff.lastIndexOf(File.separator) + 1);
            base = base.substring(0, base.indexOf('.'));
            String suffix = tiff.substring(tiff.lastIndexOf("."));
            while (base.length() < 3) {
                base = "0" + base;
            }
            Location test = new Location(parent, base + suffix);
            if (test.exists()) {
                tiff = test.getAbsolutePath();
            } else
                continue;
        }
        if (File.separator.equals("\\")) {
            tiff = tiff.replaceAll("\\\\\\\\", "\\\\");
        }
        validTIFFs.add(tiff);
    }
    files = validTIFFs.toArray(new String[validTIFFs.size()]);
}
Also used : ArrayList(java.util.ArrayList) FilePattern(loci.formats.FilePattern) Location(loci.common.Location)

Example 19 with FilePattern

use of loci.formats.FilePattern in project bioformats by openmicroscopy.

the class SewTiffs method main.

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        System.out.println("Usage: java SewTiffs base_name channel_num [time_count]");
        System.exit(1);
    }
    String base = args[0];
    int c = Integer.parseInt(args[1]);
    int num;
    if (args.length < 3) {
        FilePattern fp = new FilePattern(new Location(base + "_C" + c + "_TP1.tiff"));
        int[] count = fp.getCount();
        num = count[count.length - 1];
    } else
        num = Integer.parseInt(args[2]);
    System.out.println("Fixing " + base + "_C" + c + "_TP<1-" + num + ">.tiff");
    TiffReader in = new TiffReader();
    TiffWriter out = new TiffWriter();
    String outId = base + "_C" + c + ".tiff";
    System.out.println("Writing " + outId);
    out.setId(outId);
    System.out.print("   ");
    boolean comment = false;
    for (int t = 0; t < num; t++) {
        String inId = base + "_C" + c + "_TP" + (t + 1) + ".tiff";
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        IMetadata meta = service.createOMEXMLMetadata();
        in.setMetadataStore(meta);
        in.setId(inId);
        out.setMetadataRetrieve(meta);
        // read first image plane
        byte[] image = in.openBytes(0);
        in.close();
        if (t == 0) {
            // read first IFD
            RandomAccessInputStream ras = new RandomAccessInputStream(inId);
            TiffParser parser = new TiffParser(ras);
            IFD ifd = parser.getFirstIFD();
            ras.close();
            // preserve TIFF comment
            String desc = ifd.getComment();
            if (desc != null) {
                ifd = new IFD();
                ifd.putIFDValue(IFD.IMAGE_DESCRIPTION, desc);
                comment = true;
                out.saveBytes(t, image, ifd);
                System.out.print(".");
                continue;
            }
        }
        // write image plane
        out.saveBytes(t, image);
        // update status
        System.out.print(".");
        if (t % DOTS == DOTS - 1) {
            System.out.println(" " + (t + 1));
            System.out.print("   ");
        }
    }
    System.out.println();
    if (comment)
        System.out.println("OME-TIFF comment saved.");
    else
        System.out.println("No OME-TIFF comment found.");
}
Also used : TiffWriter(loci.formats.out.TiffWriter) ServiceFactory(loci.common.services.ServiceFactory) IFD(loci.formats.tiff.IFD) OMEXMLService(loci.formats.services.OMEXMLService) TiffReader(loci.formats.in.TiffReader) IMetadata(loci.formats.meta.IMetadata) TiffParser(loci.formats.tiff.TiffParser) FilePattern(loci.formats.FilePattern) RandomAccessInputStream(loci.common.RandomAccessInputStream) Location(loci.common.Location)

Aggregations

FilePattern (loci.formats.FilePattern)19 Location (loci.common.Location)10 ArrayList (java.util.ArrayList)6 Test (org.testng.annotations.Test)6 IOException (java.io.IOException)4 CoreMetadata (loci.formats.CoreMetadata)4 FormatException (loci.formats.FormatException)4 File (java.io.File)3 RandomAccessInputStream (loci.common.RandomAccessInputStream)3 AxisGuesser (loci.formats.AxisGuesser)3 MetadataStore (loci.formats.meta.MetadataStore)3 BigInteger (java.math.BigInteger)2 Path (java.nio.file.Path)2 ServiceFactory (loci.common.services.ServiceFactory)2 ImageReader (loci.formats.ImageReader)2 OMEXMLService (loci.formats.services.OMEXMLService)2 TiffParser (loci.formats.tiff.TiffParser)2 GenericDialog (ij.gui.GenericDialog)1 YesNoCancelDialog (ij.gui.YesNoCancelDialog)1 DirectoryChooser (ij.io.DirectoryChooser)1