Changeset 1787


Ignore:
Timestamp:
11/13/06 15:58:06 (13 years ago)
Author:
curtis
Message:

Add support to file pattern for pattern detection from an arbitrary list
of filenames, in preparation for omebf multi-file import support.

Location:
trunk/loci/formats
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/FilePattern.java

    r1747 r1787  
    278278  // -- Utility methods -- 
    279279 
    280   /** Identifies the group pattern from a given file within that group. */ 
     280  /** 
     281   * Identifies the group pattern from a given file within that group. 
     282   * @param file The file to use as a template for the match. 
     283   */ 
    281284  public static String findPattern(File file) { 
    282285    return findPattern(file.getName(), file.getAbsoluteFile().getParent()); 
    283286  } 
    284287 
    285   /** Identifies the group pattern from a given file within that group. */ 
     288  /** 
     289   * Identifies the group pattern from a given file within that group. 
     290   * @param name The filename to use as a template for the match. 
     291   * @param dir The directory in which to search for matching files. 
     292   */ 
    286293  public static String findPattern(String name, String dir) { 
    287294    if (dir == null) dir = ""; // current directory 
     
    290297    } 
    291298    File dirFile = new File(dir.equals("") ? "." : dir); 
     299 
     300    // list files in the given directory 
     301    File[] f = dirFile.listFiles(); 
     302    if (f == null) return null; 
     303    String[] nameList = new String[f.length]; 
     304    for (int i=0; i<nameList.length; i++) nameList[i] = f[i].getName(); 
     305  
     306    return findPattern(name, dir, nameList); 
     307  } 
     308 
     309  /** 
     310   * Identifies the group pattern from a given file within that group. 
     311   * @param name The filename to use as a template for the match. 
     312   * @param dir The directory prefix to use for matching files. 
     313   * @param nameList The names through which to search for matching files. 
     314   */ 
     315  public static String findPattern(String name, String dir, String[] nameList) { 
     316    if (dir == null) dir = ""; // current directory 
     317    else if (!dir.equals("") && !dir.endsWith(File.separator)) { 
     318      dir += File.separator; 
     319    } 
    292320 
    293321    // compile list of numerical blocks 
     
    332360 
    333361      NumberFilter filter = new NumberFilter(pre, post); 
    334       File[] list = dirFile.listFiles(filter); 
     362      String[] list = matchFiles(nameList, filter); 
    335363      if (list == null || list.length == 0) return null; 
    336364      if (list.length == 1) { 
     
    341369      boolean fix = true; 
    342370      for (int j=0; j<list.length; j++) { 
    343         if (list[j].getName().length() != len) { 
     371        if (list[j].length() != len) { 
    344372          fix = false; 
    345373          break; 
     
    357385          char c = name.charAt(jx); 
    358386          for (int k=0; k<list.length; k++) { 
    359             if (list[k].getName().charAt(jx) != c) { 
     387            if (list[k].charAt(jx) != c) { 
    360388              same[j] = false; 
    361389              break; 
     
    374402          else { 
    375403            while (j < width && !same[j]) j++; 
    376             String p = findPattern(name, dirFile, jx, indexList[i] + j, ""); 
     404            String p = findPattern(name, nameList, jx, indexList[i] + j, ""); 
    377405            if (p == null) { 
    378406              // unable to find an appropriate breakdown of numerical blocks 
     
    387415        BigInteger[] numbers = new BigInteger[list.length]; 
    388416        for (int j=0; j<list.length; j++) { 
    389           numbers[j] = filter.getNumber(list[j].getName()); 
     417          numbers[j] = filter.getNumber(list[j]); 
    390418        } 
    391419        Arrays.sort(numbers); 
     
    403431  /** Recursive method for parsing a fixed-width numerical block. */ 
    404432  private static String findPattern(String name, 
    405     File dirFile, int ndx, int end, String p) 
     433    String[] nameList, int ndx, int end, String p) 
    406434  { 
    407435    if (ndx == end) return p; 
     
    409437      NumberFilter filter = new NumberFilter( 
    410438        name.substring(0, ndx), name.substring(ndx + i)); 
    411       File[] list = dirFile.listFiles(filter); 
     439      String[] list = matchFiles(nameList, filter); 
    412440      BigInteger[] numbers = new BigInteger[list.length]; 
    413441      for (int j=0; j<list.length; j++) { 
    414         numbers[j] = new BigInteger(list[j].getName().substring(ndx, ndx + i)); 
     442        numbers[j] = new BigInteger(list[j].substring(ndx, ndx + i)); 
    415443      } 
    416444      Arrays.sort(numbers); 
    417445      String bounds = getBounds(numbers, true); 
    418446      if (bounds == null) continue; 
    419       String pat = findPattern(name, dirFile, ndx + i, end, p + bounds); 
     447      String pat = findPattern(name, nameList, ndx + i, end, p + bounds); 
    420448      if (pat != null) return pat; 
    421449    } 
     
    461489  } 
    462490 
     491  /** Filters the given list of filenames according to the specified filter. */ 
     492  private static String[] matchFiles(String[] inFiles, NumberFilter filter) { 
     493    Vector v = new Vector(); 
     494    for (int i=0; i<inFiles.length; i++) { 
     495      if (filter.accept(inFiles[i])) v.add(inFiles[i]); 
     496    } 
     497    String[] s = new String[v.size()]; 
     498    v.copyInto(s); 
     499    return s; 
     500  } 
     501 
    463502  // -- Helper methods -- 
    464503 
     
    490529  /** Method for testing file pattern logic. */ 
    491530  public static void main(String[] args) { 
    492     File file = args.length < 1 ? 
    493       new File(System.getProperty("user.dir")).listFiles()[0] : 
    494       new File(args[0]); 
    495     System.out.println("File = " + file.getAbsoluteFile()); 
    496     String pat = findPattern(file); 
     531    String pat = null; 
     532    if (args.length > 0) { 
     533      // test file pattern detection based on the given file on disk 
     534      File file = new File(args[0]); 
     535      System.out.println("File = " + file.getAbsoluteFile()); 
     536      pat = findPattern(file); 
     537    } 
     538    else { 
     539      // test file pattern detection from a virtual file list 
     540      String[] nameList = new String[2 * 4 * 3 * 12 + 1]; 
     541      nameList[0] = "outlier.ext"; 
     542      int count = 1; 
     543      for (int i=1; i<=2; i++) { 
     544        for (int j=1; j<=4; j++) { 
     545          for (int k=0; k<=2; k++) { 
     546            for (int l=1; l<=12; l++) { 
     547              String sl = (l < 10 ? "0" : "") + l; 
     548              nameList[count++] = 
     549                "hypothetical" + sl + k + j + "c" + i + ".ext"; 
     550            } 
     551          } 
     552        } 
     553      } 
     554      pat = findPattern(nameList[1], null, nameList); 
     555    } 
    497556    if (pat == null) System.out.println("No pattern found."); 
    498557    else { 
  • trunk/loci/formats/NumberFilter.java

    r1629 r1787  
    6262  } 
    6363 
     64  /** Tests if a specified file should be included in a file list. */ 
     65  public boolean accept(String name) { 
     66    return getNumber(name) != null; 
     67  } 
     68 
    6469  // -- FileFilter API methods -- 
    6570 
    6671  /** Tests if a specified file should be included in a file list. */ 
    6772  public boolean accept(File pathname) { 
    68     return getNumber(pathname.getName()) != null; 
     73    return accept(pathname.getName()); 
    6974  } 
    7075 
Note: See TracChangeset for help on using the changeset viewer.