Changeset 6759


Ignore:
Timestamp:
08/02/10 17:28:00 (9 years ago)
Author:
curtis
Message:

Update OmeroReader to use OMERO 4.2 API.

Location:
trunk
Files:
2 added
2 deleted
6 edited

Legend:

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

    r6751 r6759  
    115115  private DimensionSwapper dimSwapper; 
    116116  private BufferedImageReader biReader; 
    117  
    118   private String seriesLabel = null; 
    119117 
    120118  private Double[] preGlobalMin = null, preGlobalMax = null; 
     
    264262  } 
    265263 
     264  public void setReader(IFormatReader reader) { 
     265    this.reader = reader; 
     266  } 
     267 
    266268  public void createReader() { 
     269    if (reader != null) return; // reader was set programmatically 
    267270    if (format != null) { 
    268271      // create reader of a specific format type 
  • trunk/components/ome-io/.classpath

    r6517 r6759  
    88        <classpathentry combineaccessrules="false" kind="src" path="/common"/> 
    99        <classpathentry combineaccessrules="false" kind="src" path="/External libraries"/> 
     10        <classpathentry combineaccessrules="false" kind="src" path="/ome-xml"/> 
    1011        <classpathentry kind="output" path="build-eclipse"/> 
    1112</classpath> 
  • trunk/components/ome-io/build.properties

    r6749 r6759  
    1414                           ${lib.dir}/log4j-1.2.15.jar:\ 
    1515                           ${lib.dir}/ome-java.jar:\ 
    16                            ${lib.dir}/omero-client-4.1.1.jar:\ 
    17                            ${lib.dir}/omero-common-4.1.1.jar:\ 
     16                           ${lib.dir}/omero-client-4.2.0.jar:\ 
     17                           ${lib.dir}/omero-common-4.2.0.jar:\ 
    1818                           ${lib.dir}/slf4j-api-1.5.10.jar:\ 
    1919                           ${lib.dir}/testng-5.11-jdk15.jar 
    20 component.java-version   = 1.5 
     20component.java-version   = 1.6 
    2121component.deprecation    = true 
    2222 
     
    2424component.resources-text = 
    2525 
    26 component.main-class     = loci.ome.io.OMEWriter 
     26component.main-class     = loci.ome.io.OmeroReader 
    2727component.runtime-cp     = ${component.classpath} 
    2828 
    2929 
    30 # NB: do not include ${artifact.dir}/bio-formats.jar, as its' classpath 
    31 #     includes ome-io.jar 
     30# NB: do not include ${artifact.dir}/bio-formats.jar, 
     31#     as its classpath includes ome-io.jar 
    3232component.classpath-no-omeio  = ${root.dir}/components/bio-formats/build/classes:\ 
    3333                                ${artifact.dir}/loci-common.jar:\ 
  • trunk/components/ome-io/src/loci/ome/io/OMEWriter.java

    r6294 r6759  
    4242import loci.formats.FormatWriter; 
    4343import loci.formats.ImageTools; 
    44 import loci.formats.MetadataTools; 
    4544import loci.formats.meta.MetadataRetrieve; 
    4645import loci.formats.services.OMEXMLService; 
     
    377376 
    378377  /* @see loci.formats.IFormatWriter#canDoStacks(String) */ 
    379   public boolean canDoStacks() { return true; } 
     378  @Override 
     379        public boolean canDoStacks() { return true; } 
    380380 
    381381  // -- IFormatHandler API methods -- 
    382382 
    383383  /* @see loci.formats.IFormatHandler#close() */ 
    384   public void close() throws IOException { 
     384  @Override 
     385        public void close() throws IOException { 
    385386    try { 
    386387      r.exec("rc.logout()"); 
  • trunk/components/ome-io/src/loci/ome/io/OmeroReader.java

    r6234 r6759  
    2424package loci.ome.io; 
    2525 
     26import Glacier2.CannotCreateSessionException; 
     27import Glacier2.PermissionDeniedException; 
     28 
     29import java.io.Console; 
    2630import java.io.IOException; 
    2731 
     
    3135import loci.formats.MetadataTools; 
    3236import loci.formats.meta.MetadataStore; 
    33 import ome.api.RawPixelsStore; 
    34 import ome.system.Login; 
    35 import ome.system.Server; 
    36 import ome.system.ServiceFactory; 
     37import loci.formats.tools.ImageInfo; 
     38import omero.ServerError; 
     39import omero.api.GatewayPrx; 
    3740import omero.api.RawPixelsStorePrx; 
     41import omero.api.ServiceFactoryPrx; 
    3842import omero.model.Image; 
    3943import omero.model.Pixels; 
     
    4145/** 
    4246 * Implementation of {@link loci.formats.IFormatReader} 
    43  * for use in export from an OMERO Beta 4.0.x database. 
     47 * for use in export from an OMERO Beta 4.2.x database. 
    4448 * 
    4549 * <dl><dt><b>Source code:</b></dt> 
     
    4953public class OmeroReader extends FormatReader { 
    5054 
     55  // -- Constants -- 
     56 
     57  public static final int DEFAULT_PORT = 4064; 
     58 
    5159  // -- Fields -- 
    5260 
    53   private RawPixelsStore raw; 
    54   private RawPixelsStorePrx prx; 
     61  private String server; 
     62  private String username; 
     63  private String password; 
     64  private int thePort = DEFAULT_PORT; 
     65 
     66  private RawPixelsStorePrx store; 
    5567  private Pixels pix; 
    5668 
     
    6173  } 
    6274 
    63   public OmeroReader(RawPixelsStore raw, Pixels pix) { 
    64     this(pix, raw, null); 
    65   } 
    66  
    67   public OmeroReader(RawPixelsStorePrx prx, Pixels pix) { 
    68     this(pix, null, prx); 
    69   } 
    70  
    71   private OmeroReader(Pixels pix, RawPixelsStore raw, RawPixelsStorePrx prx) { 
    72     super("OMERO", "*"); 
    73     this.pix = pix; 
    74     this.prx = prx; 
    75     this.raw = raw; 
    76     if (this.raw == null && this.prx == null) { 
    77       throw new IllegalArgumentException( 
    78         "Must specify either RawPixelsStore or RawPixelsStorePrx"); 
    79     } 
    80     else if (this.raw != null && this.prx != null) { 
    81       throw new IllegalArgumentException( 
    82         "Must not specify both RawPixelsStore and RawPixelsStorePrx"); 
    83     } 
     75  // -- OmeroReader methods -- 
     76 
     77  public void setServer(String server) { 
     78    this.server = server; 
     79  } 
     80 
     81  public void setPort(int port) { 
     82    thePort = port; 
     83  } 
     84 
     85  public void setUsername(String username) { 
     86    this.username = username; 
     87  } 
     88 
     89  public void setPassword(String password) { 
     90    this.password = password; 
    8491  } 
    8592 
    8693  // -- IFormatReader methods -- 
    8794 
     95  @Override 
    8896  public boolean isThisType(String name, boolean open) { 
    8997    return name.startsWith("omero:"); 
    9098  } 
    9199 
     100  @Override 
    92101  public byte[] openBytes(int no, byte[] buf, int x1, int y1, int w1, int h1) 
    93102    throws FormatException, IOException 
     
    97106    FormatTools.checkBufferSize(this, buf.length); 
    98107 
    99     int[] zct = FormatTools.getZCTCoords(this, no); 
    100  
    101     byte[] plane = null; 
    102     if (raw != null) { 
    103       plane = raw.getPlane(zct[0], zct[1], zct[2]); 
    104     } 
    105     else if (prx != null) { 
    106       try { 
    107         plane = prx.getPlane(zct[0], zct[1], zct[2]); 
    108       } 
    109       catch (Exception e) { 
    110         throw new RuntimeException(e); 
    111       } 
    112     } 
    113     else throw new IllegalStateException("No raw pixels store available"); 
    114  
    115     int len = getSizeX() * getSizeY() * 
     108    final int[] zct = FormatTools.getZCTCoords(this, no); 
     109 
     110    final byte[] plane; 
     111                try { 
     112                        plane = store.getPlane(zct[0], zct[1], zct[2]); 
     113                } 
     114                catch (ServerError e) { 
     115                        throw new FormatException(e); 
     116                } 
     117 
     118    final int len = getSizeX() * getSizeY() * 
    116119      FormatTools.getBytesPerPixel(getPixelType()); 
    117     System.arraycopy((byte[]) plane, 0, buf, 0, len); 
     120    System.arraycopy(plane, 0, buf, 0, len); 
    118121 
    119122    return buf; 
    120123  } 
    121124 
     125  @Override 
    122126  public void close() throws IOException { 
    123127    super.close(); 
    124   } 
    125  
     128    // TODO close last session 
     129  } 
     130 
     131  @Override 
    126132  protected void initFile(String id) throws FormatException, IOException { 
    127133    LOGGER.debug("OmeroReader.initFile({})", id); 
     
    129135    super.initFile(id); 
    130136 
    131     // parse credentials from id string 
    132  
    133     LOGGER.info("Parsing credentials"); 
    134  
    135137    if (!id.startsWith("omero:")) { 
    136138      throw new IllegalArgumentException("Not an OMERO id: " + id); 
    137139    } 
    138140 
    139     String address = null, user = null, pass = null; 
    140     int port = 1099; 
     141    // parse credentials from id string 
     142 
     143    LOGGER.info("Parsing credentials"); 
     144 
     145    String address = server, user = username, pass = password; 
     146    int port = thePort; 
    141147    long pid = -1; 
    142148 
    143     String[] tokens = id.split("\n"); 
     149    final String[] tokens = id.substring(6).split("\n"); 
    144150    for (String token : tokens) { 
    145       int equals = token.indexOf("="); 
     151      final int equals = token.indexOf("="); 
    146152      if (equals < 0) continue; 
    147       String key = token.substring(0, equals); 
    148       String val = token.substring(equals + 1); 
     153      final String key = token.substring(0, equals); 
     154      final String val = token.substring(equals + 1); 
    149155      if (key.equals("server")) address = val; 
    150156      else if (key.equals("user")) user = val; 
     
    165171 
    166172    if (address == null) { 
    167       throw new IllegalArgumentException("No server address in id"); 
     173      throw new FormatException("Invalid server address"); 
    168174    } 
    169175    if (user == null) { 
    170       throw new IllegalArgumentException("No username in id"); 
     176      throw new FormatException("Invalid username"); 
    171177    } 
    172178    if (pass == null) { 
    173       throw new IllegalArgumentException("No password in id"); 
     179      throw new FormatException("Invalid password"); 
    174180    } 
    175181    if (pid < 0) { 
    176       throw new IllegalArgumentException("No pixels ID in id"); 
    177     } 
    178  
    179     System.out.println("Server: " + address);//TEMP 
    180     System.out.println("Port: " + port);//TEMP 
    181     System.out.println("User: " + user);//TEMP 
    182     System.out.println("Pixels ID: " + pid);//TEMP 
    183  
    184     // authenticate with OMERO server 
    185  
    186     LOGGER.info("Logging in"); 
    187  
    188     Login login = new Login(user, pass); 
    189     Server server = new Server(address, port); 
    190     ServiceFactory sf = new ServiceFactory(login); 
    191  
    192     // get raw pixels store and pixels 
    193  
    194     LOGGER.info("Getting raw pixels store"); 
    195  
    196     raw = sf.createRawPixelsStore(); 
    197     raw.setPixelsId(pid, false); 
    198  
    199 /* 
    200     CTR: Need to obtain omero.model.Pixels object from OMERO somehow. 
    201     Ideally, utilize higher-level client-side Gateway API in OMERO SVN: 
    202       components/tools/OmeroImageJ/Omero_ImageJ/src/ome/ij/data 
    203  
    204     IAdmin admin = sf.getAdminService(); 
    205     EventContext ec = admin.getEventContext(); 
    206     Long userId = ec.getCurrentUserId(); 
    207  
    208     Parameters params = new Parameters().addId(userId); 
    209  
    210     Query query = sf.getQueryService(); 
    211     String q = "select p from Pixels as p " + 
    212       "left outer join fetch p.pixelsType as pt " + 
    213       "left outer join fetch p.channels as c " + 
    214       "left outer join fetch p.pixelsDimensions " + 
    215       "left outer join fetch p.image " + 
    216       "left outer join fetch c.colorComponent " + 
    217       "left outer join fetch c.logicalChannel as lc " + 
    218       "left outer join fetch c.statsInfo " + 
    219       "left outer join fetch lc.photometricInterpretation " + 
    220       "where p.id = :id"; 
    221  
    222     params = new Parameters(); 
    223     params.addId(idObj); 
    224  
    225     Pixels results = query.findByQuery(q, params); 
    226     PixelsData pix = new PixelsData(results); 
    227 */ 
    228  
    229     // populate metadata 
    230  
    231     LOGGER.info("Populating metadata"); 
    232  
    233     int sizeX = pix.getSizeX().getValue(); 
    234     int sizeY = pix.getSizeY().getValue(); 
    235     int sizeZ = pix.getSizeZ().getValue(); 
    236     int sizeC = pix.getSizeC().getValue(); 
    237     int sizeT = pix.getSizeT().getValue(); 
    238     String pixelType = pix.getPixelsType().getValue().getValue(); 
    239  
    240     core[0].sizeX = sizeX; 
    241     core[0].sizeY = sizeY; 
    242     core[0].sizeZ = sizeZ; 
    243     core[0].sizeC = sizeC; 
    244     core[0].sizeT = sizeT; 
    245     core[0].rgb = false; 
    246     core[0].littleEndian = false; 
    247     core[0].dimensionOrder = "XYZCT"; 
    248     core[0].imageCount = sizeZ * sizeC * sizeT; 
    249     core[0].pixelType = FormatTools.pixelTypeFromString(pixelType); 
    250  
    251     // CTR TODO this is wrong 
    252     double px = pix.getSizeX().getValue(); 
    253     double py = pix.getSizeY().getValue(); 
    254     double pz = pix.getSizeZ().getValue(); 
    255  
    256     Image image = pix.getImage(); 
    257  
    258     String name = image.getName().getValue(); 
    259     String description = image.getDescription().getValue(); 
    260  
    261     MetadataStore store = getMetadataStore(); 
    262     store.setImageName(name, 0); 
    263     store.setImageDescription(description, 0); 
    264     MetadataTools.populatePixels(store, this); 
    265  
    266     store.setPixelsPhysicalSizeX(new Double(px), 0); 
    267     store.setPixelsPhysicalSizeY(new Double(py), 0); 
    268     store.setPixelsPhysicalSizeZ(new Double(pz), 0); 
     182      throw new FormatException("Invalid pixels ID"); 
     183    } 
     184 
     185                try { 
     186            // authenticate with OMERO server 
     187 
     188            LOGGER.info("Logging in"); 
     189 
     190            omero.client client = new omero.client(server, port); 
     191                        final ServiceFactoryPrx serviceFactory = client.createSession(user, pass); 
     192 
     193            // get raw pixels store and pixels 
     194 
     195            store = serviceFactory.createRawPixelsStore(); 
     196            store.setPixelsId(pid, false); 
     197 
     198            final GatewayPrx gateway = serviceFactory.createGateway(); 
     199            pix = gateway.getPixels(pid); 
     200            final int sizeX = pix.getSizeX().getValue(); 
     201            final int sizeY = pix.getSizeY().getValue(); 
     202            final int sizeZ = pix.getSizeZ().getValue(); 
     203            final int sizeC = pix.getSizeC().getValue(); 
     204            final int sizeT = pix.getSizeT().getValue(); 
     205            final String pixelType = pix.getPixelsType().getValue().getValue(); 
     206 
     207            // populate metadata 
     208 
     209            LOGGER.info("Populating metadata"); 
     210 
     211            core[0].sizeX = sizeX; 
     212            core[0].sizeY = sizeY; 
     213            core[0].sizeZ = sizeZ; 
     214            core[0].sizeC = sizeC; 
     215            core[0].sizeT = sizeT; 
     216            core[0].rgb = false; 
     217            core[0].littleEndian = false; 
     218            core[0].dimensionOrder = "XYZCT"; 
     219            core[0].imageCount = sizeZ * sizeC * sizeT; 
     220            core[0].pixelType = FormatTools.pixelTypeFromString(pixelType); 
     221 
     222            // CTR TODO this is wrong 
     223            double px = pix.getSizeX().getValue(); 
     224            double py = pix.getSizeY().getValue(); 
     225            double pz = pix.getSizeZ().getValue(); 
     226 
     227            Image image = pix.getImage(); 
     228 
     229            String name = image.getName().getValue(); 
     230            String description = image.getDescription().getValue(); 
     231 
     232            MetadataStore store = getMetadataStore(); 
     233            store.setImageName(name, 0); 
     234            store.setImageDescription(description, 0); 
     235            MetadataTools.populatePixels(store, this); 
     236 
     237            store.setPixelsPhysicalSizeX(new Double(px), 0); 
     238            store.setPixelsPhysicalSizeY(new Double(py), 0); 
     239            store.setPixelsPhysicalSizeZ(new Double(pz), 0); 
     240                } 
     241                catch (CannotCreateSessionException e) { 
     242                        throw new FormatException(e); 
     243                } 
     244                catch (PermissionDeniedException e) { 
     245                        throw new FormatException(e); 
     246                } 
     247                catch (ServerError e) { 
     248                        throw new FormatException(e); 
     249                } 
     250  } 
     251 
     252  /** A simple command line tool for downloading images from OMERO. */ 
     253  public static void main(String[] args) throws Exception { 
     254    // parse OMERO credentials 
     255    final Console con = System.console(); 
     256    final String server = con.readLine("Server? "); 
     257    final String portString = con.readLine("Port [%d]? ", DEFAULT_PORT); 
     258    final int port = portString.equals("") ? DEFAULT_PORT : 
     259      Integer.parseInt(portString); 
     260    final String user = con.readLine("Username? "); 
     261    final String pass = new String(con.readPassword("Password? ")); 
     262    final int pixelsId = Integer.parseInt(con.readLine("Pixels ID? ")); 
     263    con.writer().write("\n\n"); 
     264 
     265    // construct the OMERO reader 
     266    final OmeroReader omeroReader = new OmeroReader(); 
     267    omeroReader.setUsername(user); 
     268    omeroReader.setPassword(pass); 
     269    omeroReader.setServer(server); 
     270    omeroReader.setPort(port); 
     271    final String id = "omero:pid=" + pixelsId; 
     272    omeroReader.setId(id); 
     273    omeroReader.close(); 
     274 
     275    // delegate the heavy lifting to Bio-Formats ImageInfo utility 
     276    final ImageInfo imageInfo = new ImageInfo(); 
     277    imageInfo.setReader(omeroReader); // override default image reader 
     278    if (!imageInfo.testRead(args)) System.exit(1); 
    269279  } 
    270280 
  • trunk/jar/.classpath

    r6542 r6759  
    2121        <classpathentry exported="true" kind="lib" path="ome-java-deprecated.jar"/> 
    2222        <classpathentry exported="true" kind="lib" path="ome-java.jar"/> 
    23         <classpathentry exported="true" kind="lib" path="omero-client-4.1.1.jar"/> 
    24         <classpathentry exported="true" kind="lib" path="omero-common-4.1.1.jar"/> 
     23        <classpathentry exported="true" kind="lib" path="omero-client-4.2.0.jar"/> 
     24        <classpathentry exported="true" kind="lib" path="omero-common-4.2.0.jar"/> 
    2525        <classpathentry exported="true" kind="lib" path="serializer-2.7.1.jar"/> 
    2626        <classpathentry exported="true" kind="lib" path="skinlf.jar"/> 
Note: See TracChangeset for help on using the changeset viewer.