Changeset 7070


Ignore:
Timestamp:
10/13/10 13:09:53 (9 years ago)
Author:
melissa
Message:

Added support for non-numerical file pattern blocks, closes #395.

You can now specify patterns such as:

  • "<A-M>.tiff"
  • "<A-M:2>.tiff" (which would give you A.tiff, C.diff, E.tiff, etc.)
  • "<R,G,B>.tiff"
  • "<1,5,13>.tiff"
  • ".*.tiff"
Location:
trunk/components/bio-formats/src/loci/formats
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/components/bio-formats/src/loci/formats/AxisGuesser.java

    r7021 r7070  
    7979  protected static final String[] S = {"s", "series", "sp"}; 
    8080 
    81   protected static final BigInteger TWO = new BigInteger("2"); 
    82   protected static final BigInteger THREE = new BigInteger("3"); 
     81  protected static final String TWO = "2"; 
     82  protected static final String THREE = "3"; 
    8383 
    8484  // -- Fields -- 
     
    125125    String[] prefixes = fp.getPrefixes(); 
    126126    String suffix = fp.getSuffix(); 
    127     BigInteger[] first = fp.getFirst(); 
    128     BigInteger[] last = fp.getLast(); 
    129     BigInteger[] step = fp.getStep(); 
    130     int[] count = fp.getCount(); 
    131     axisTypes = new int[count.length]; 
     127    String[][] elements = fp.getElements(); 
     128    axisTypes = new int[elements.length]; 
    132129    boolean foundZ = false, foundT = false; 
    133130 
     
    190187 
    191188      // check special case: <2-3> (Bio-Rad PIC) 
    192       if (first[i].equals(TWO) && last[i].equals(THREE) && 
    193         step[i].equals(BigInteger.ONE) && suffix.equalsIgnoreCase(".pic")) 
     189      if (elements[i].length == 2 && elements[i][0].equals(TWO) && 
     190        elements[i][1].equals(THREE) && suffix.equalsIgnoreCase(".pic")) 
    194191      { 
    195192        axisTypes[i] = C_AXIS; 
    196193        break; 
     194      } 
     195      else if (elements[i].length == 2 || elements[i].length == 3) { 
     196        char first = elements[i][0].toLowerCase().charAt(0); 
     197        char second = elements[i][1].toLowerCase().charAt(0); 
     198        char third = elements[i].length == 2 ? 'b' : 
     199          elements[i][2].toLowerCase().charAt(0); 
     200 
     201        if ((first == 'r' || second == 'r' || third == 'r') && 
     202          (first == 'g' || second == 'g' || third == 'g') && 
     203          (first == 'b' || second == 'b' || third == 'b')) 
     204        { 
     205          axisTypes[i] = C_AXIS; 
     206          break; 
     207        } 
    197208      } 
    198209    } 
  • trunk/components/bio-formats/src/loci/formats/FilePattern.java

    r7022 r7070  
    2929import java.util.Arrays; 
    3030import java.util.List; 
     31import java.util.regex.Matcher; 
     32import java.util.regex.Pattern; 
     33import java.util.regex.PatternSyntaxException; 
    3134 
    3235import loci.common.DataTools; 
     
    7881  private int[] endIndex; 
    7982 
    80   /** First number of each numerical block. */ 
    81   private BigInteger[] begin; 
    82  
    83   /** Last number of each numerical block. */ 
    84   private BigInteger[] end; 
    85  
    86   /** Step size of each numerical block. */ 
    87   private BigInteger[] step; 
    88  
    89   /** Total numbers withins each numerical block. */ 
    90   private int[] count; 
    91  
    92   /** Whether each numerical block is fixed width. */ 
    93   private boolean[] fixed; 
    94  
    95   /** The number of leading zeroes for each numerical block. */ 
    96   private int[] zeroes; 
     83  /** List of pattern blocks for this file pattern. */ 
     84  private FilePatternBlock[] blocks; 
    9785 
    9886  /** File listing for this file pattern. */ 
     
    127115    int left = -1; 
    128116    while (true) { 
    129       left = pattern.indexOf("<", left + 1); 
     117      left = pattern.indexOf(FilePatternBlock.BLOCK_START, left + 1); 
    130118      if (left < 0) break; 
    131119      lt.add(new Integer(left)); 
     
    133121    int right = -1; 
    134122    while (true) { 
    135       right = pattern.indexOf(">", right + 1); 
     123      right = pattern.indexOf(FilePatternBlock.BLOCK_END, right + 1); 
    136124      if (right < 0) break; 
    137125      gt.add(new Integer(right)); 
     
    162150 
    163151    // parse numerical blocks 
    164     begin = new BigInteger[num]; 
    165     end = new BigInteger[num]; 
    166     step = new BigInteger[num]; 
    167     count = new int[num]; 
    168     fixed = new boolean[num]; 
    169     zeroes = new int[num]; 
     152    blocks = new FilePatternBlock[num]; 
    170153    for (int i=0; i<num; i++) { 
    171154      String block = pattern.substring(startIndex[i], endIndex[i]); 
    172       int dash = block.indexOf("-"); 
    173       String b, e, s; 
    174       if (dash < 0) { 
    175         // no range; assume entire block is a single number (e.g., <15>) 
    176         b = e = block.substring(1, block.length() - 1); 
    177         s = "1"; 
    178       } 
    179       else { 
    180         int colon = block.indexOf(":"); 
    181         b = block.substring(1, dash); 
    182         if (colon < 0) { 
    183           e = block.substring(dash + 1, block.length() - 1); 
    184           s = "1"; 
    185         } 
    186         else { 
    187           e = block.substring(dash + 1, colon); 
    188           s = block.substring(colon + 1, block.length() - 1); 
    189         } 
    190       } 
    191       try { 
    192         begin[i] = new BigInteger(b); 
    193         end[i] = new BigInteger(e); 
    194         if (begin[i].compareTo(end[i]) > 0) { 
    195           msg = "Begin value cannot be greater than ending value."; 
    196           return; 
    197         } 
    198         step[i] = new BigInteger(s); 
    199         if (step[i].compareTo(BigInteger.ONE) < 0) { 
    200           msg = "Step value must be at least one."; 
    201           return; 
    202         } 
    203         count[i] = end[i].subtract(begin[i]).divide(step[i]).intValue() + 1; 
    204         fixed[i] = b.length() == e.length(); 
    205         int z = 0; 
    206         for (z=0; z<e.length(); z++) { 
    207           if (e.charAt(z) != '0') break; 
    208         } 
    209         zeroes[i] = z; 
    210       } 
    211       catch (NumberFormatException exc) { 
    212         msg = "Invalid numerical range values."; 
    213         return; 
    214       } 
    215     } 
     155      blocks[i] = new FilePatternBlock(block); 
     156    } 
     157 
    216158 
    217159    // build file listing 
     
    234176  public String getErrorMessage() { return msg; } 
    235177 
    236   /** Gets the first number of each numerical block. */ 
    237   public BigInteger[] getFirst() { return begin; } 
    238  
    239   /** Gets the last number of each numerical block. */ 
    240   public BigInteger[] getLast() { return end; } 
    241  
    242   /** Gets the step increment of each numerical block. */ 
    243   public BigInteger[] getStep() { return step; } 
    244  
    245   /** Gets the total count of each numerical block. */ 
    246   public int[] getCount() { return count; } 
    247  
    248178  /** Gets a listing of all files matching the given file pattern. */ 
    249179  public String[] getFiles() { return files; } 
     180 
     181  public String[][] getElements() { 
     182    String[][] elements = new String[blocks.length][]; 
     183    for (int i=0; i<elements.length; i++) { 
     184      elements[i] = blocks[i].getElements(); 
     185    } 
     186    return elements; 
     187  } 
     188 
     189  public int[] getCount() { 
     190    int[] count = new int[blocks.length]; 
     191    for (int i=0; i<count.length; i++) { 
     192      count[i] = blocks[i].getElements().length; 
     193    } 
     194    return count; 
     195  } 
    250196 
    251197  /** Gets the specified numerical block. */ 
     
    599545  /** Recursive method for building filenames for the file listing. */ 
    600546  private void buildFiles(String prefix, int ndx, List<String> fileList) { 
    601     // compute bounds for constant (non-block) pattern fragment 
    602     int num = startIndex.length; 
    603     int n1 = ndx == 0 ? 0 : endIndex[ndx - 1]; 
    604     int n2 = ndx == num ? pattern.length() : startIndex[ndx]; 
    605     String pre = pattern.substring(n1, n2); 
    606  
    607     if (ndx == 0) fileList.add(pre + prefix); 
     547    if (blocks.length == 0) { 
     548      // regex pattern 
     549 
     550      String[] files = null; 
     551      int end = pattern.lastIndexOf(File.separator) + 1; 
     552      String dir = pattern.substring(0, end); 
     553      if (dir.equals("") || !new Location(dir).exists()) { 
     554        files = Location.getIdMap().keySet().toArray(new String[0]); 
     555        if (files.length == 0) { 
     556          dir = "."; 
     557          files = new Location(dir).list(true); 
     558        } 
     559      } 
     560      else { 
     561        files = new Location(dir).list(true); 
     562      } 
     563 
     564      Arrays.sort(files); 
     565 
     566      Pattern regex = Pattern.compile(pattern); 
     567 
     568      for (String f : files) { 
     569        if (regex.matcher(f).matches()) { 
     570          fileList.add(new Location(dir, f).getAbsolutePath()); 
     571        } 
     572      } 
     573    } 
    608574    else { 
    609       // for (int i=begin[ndx]; i<end[ndx]; i+=step[ndx]) 
    610       BigInteger bi = begin[--ndx]; 
    611       while (bi.compareTo(end[ndx]) <= 0) { 
    612         String s = bi.toString(); 
    613         int z = zeroes[ndx]; 
    614         if (fixed[ndx]) z += end[ndx].toString().length() - s.length(); 
    615         for (int j=0; j<z; j++) s = "0" + s; 
    616         buildFiles(s + pre + prefix, ndx, fileList); 
    617         bi = bi.add(step[ndx]); 
     575      // compute bounds for constant (non-block) pattern fragment 
     576      int num = startIndex.length; 
     577      int n1 = ndx == 0 ? 0 : endIndex[ndx - 1]; 
     578      int n2 = ndx == num ? pattern.length() : startIndex[ndx]; 
     579      String pre = pattern.substring(n1, n2); 
     580 
     581      if (ndx == 0) fileList.add(pre + prefix); 
     582      else { 
     583        FilePatternBlock block = blocks[--ndx]; 
     584        String[] blockElements = block.getElements(); 
     585        for (String element : blockElements) { 
     586          buildFiles(element + pre + prefix, ndx, fileList); 
     587        } 
    618588      } 
    619589    } 
  • trunk/components/bio-formats/src/loci/formats/FileStitcher.java

    r7021 r7070  
    226226    patternIds = false; 
    227227    String[] patterns = findPatterns(new FilePattern(id).getFiles()[0]); 
     228    if (patterns.length == 0) patterns = new String[] {id}; 
     229    else { 
     230      FilePattern test = new FilePattern(patterns[0]); 
     231      if (test.getFiles().length == 0) patterns = new String[] {id}; 
     232    } 
    228233    patternIds = true; 
    229234    return patterns; 
     
    880885    AxisGuesser ag = s.getAxisGuesser(); 
    881886    int[] axes = ag.getAxisTypes(); 
     887 
    882888    int numZ = ag.getAxisCountZ(); 
    883889    int numC = ag.getAxisCountC(); 
    884890    int numT = ag.getAxisCountT(); 
     891 
     892    if (axes.length == 0 && s.getFiles().length > 1) { 
     893      axes = new int[] {AxisGuesser.T_AXIS}; 
     894      count = new int[] {s.getFiles().length}; 
     895      numT++; 
     896    } 
    885897 
    886898    core[sno].sizeZ = sizeZ[sno]; 
  • trunk/components/bio-formats/src/loci/formats/in/MIASReader.java

    r6953 r7070  
    528528        firstTiff.getName(), firstTiff.getParentFile().getAbsolutePath()); 
    529529      String[] blocks = fp.getPrefixes(); 
    530       BigInteger[] firstNumber = fp.getFirst(); 
    531       BigInteger[] lastNumber = fp.getLast(); 
    532       BigInteger[] step = fp.getStep(); 
    533530 
    534531      order[j] = "XY"; 
     532 
     533      int[] count = fp.getCount(); 
    535534 
    536535      for (int block=blocks.length - 1; block>=0; block--) { 
     
    539538          blocks[block].substring(blocks[block].lastIndexOf("_") + 1); 
    540539 
    541         BigInteger tmp = lastNumber[block].subtract(firstNumber[block]); 
    542         tmp = tmp.add(BigInteger.ONE).divide(step[block]); 
    543         int count = tmp.intValue(); 
    544  
    545540        if (blocks[block].equals("z")) { 
    546           zCount[j] = count; 
     541          zCount[j] = count[block]; 
    547542          order[j] += "Z"; 
    548543        } 
    549544        else if (blocks[block].equals("t")) { 
    550           tCount[j] = count; 
     545          tCount[j] = count[block]; 
    551546          order[j] += "T"; 
    552547        } 
    553548        else if (blocks[block].equals("mode")) { 
    554           cCount[j] = count; 
     549          cCount[j] = count[block]; 
    555550          order[j] += "C"; 
    556551        } 
    557         else if (blocks[block].equals("im")) tileRows = count; 
    558         else if (blocks[block].equals("")) tileCols = count; 
     552        else if (blocks[block].equals("im")) tileRows = count[block]; 
     553        else if (blocks[block].equals("")) tileCols = count[block]; 
    559554        else if (blocks[block].replaceAll("\\d", "").length() == 0) { 
    560           if (block == 3) tileRows = count; 
    561           else if (block == 2) tileCols = count; 
     555          if (block == 3) tileRows = count[block]; 
     556          else if (block == 2) tileCols = count[block]; 
    562557          else if (block == 0) { 
    563             zCount[j] = count; 
     558            zCount[j] = count[block]; 
    564559            order[j] += "Z"; 
    565560          } 
    566561          else if (block == 1) { 
    567             tCount[j] = count; 
     562            tCount[j] = count[block]; 
    568563            order[j] += "T"; 
    569564          } 
  • trunk/components/bio-formats/src/loci/formats/in/TCSReader.java

    r6881 r7070  
    277277      AxisGuesser guesser = 
    278278        new AxisGuesser(fp, "XYTZC", 1, ifds.size(), 1, true); 
    279       BigInteger[] first = fp.getFirst(); 
    280       BigInteger[] last = fp.getLast(); 
    281       BigInteger[] step = fp.getStep(); 
    282279 
    283280      int[] axisTypes = guesser.getAxisTypes(); 
     281      int[] count = fp.getCount(); 
    284282 
    285283      for (int i=axisTypes.length-1; i>=0; i--) { 
    286         int size = last[i].subtract(first[i]).divide(step[i]).intValue() + 1; 
    287284        if (axisTypes[i] == AxisGuesser.Z_AXIS) { 
    288285          if (getDimensionOrder().indexOf("Z") == -1) { 
    289286            core[0].dimensionOrder += "Z"; 
    290287          } 
    291           core[0].sizeZ *= size; 
     288          core[0].sizeZ *= count[i]; 
    292289        } 
    293290        else if (axisTypes[i] == AxisGuesser.C_AXIS) { 
     
    295292            core[0].dimensionOrder += "C"; 
    296293          } 
    297           core[0].sizeC *= size; 
     294          core[0].sizeC *= count[i]; 
    298295        } 
    299296      } 
Note: See TracChangeset for help on using the changeset viewer.