We're Hiring!

How to edit channel names?

General and open developer discussion about using OMERO APIs from C++, Java, Python, Matlab and more! Please new questions at https://forum.image.sc/tags/omero
Please note:
Historical discussions about OMERO. Please look for and ask new questions at https://forum.image.sc/tags/omero

If you are having trouble with custom code, please provide a link to a public repository, ideally GitHub.

How to edit channel names?

Postby Kouichi_C_Nakamura » Fri Jun 29, 2018 9:37 pm

Hi,

There are Channels field under Image Details. You can keep information about what does the channel represent. It's easy to edit via GUI, but is there a way to edit it programmatically via Java or Python? I ultimately want to use it in MATLAB, but Java and Python code can run in MATLAB if necessary.

Closest information I found is this one. But this about retrieving channel names rather than editing.
https://docs.openmicroscopy.org/omero/5 ... guide.html

I don't think it's an Annotation object. Then what is it?

Cheers,
Kouichi
Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to edit channel names?

Postby jmoore » Sat Jun 30, 2018 8:25 am

Hi Kouichi,

Channels are objects contained by the Image object. See https://github.com/openmicroscopy/openmicroscopy/blob/develop/examples/Training/matlab/LoadMetadataAdvanced.m#L44 for an example of loading them:

Code: Select all
    fprintf(1, 'Loading channels for image %g\n', imageId');
    channels = loadChannels(session, image);
    for i = 1 : numel(channels),
        channel = channels(i);
        channelId = channel.getId().getValue();
        channelName = channel.getLogicalChannel().getName();


If you modify the objects, you can then save them with saveAndReturnObject (which if you were interested in porting to MATLAB might become updateImage or updateChannel).

Cheers,
~Josh
User avatar
jmoore
Site Admin
 
Posts: 1591
Joined: Fri May 22, 2009 1:29 pm
Location: Germany

Re: How to edit channel names?

Postby jmoore » Sat Jun 30, 2018 8:28 am

P.S. I should also mention that some of this logic will already exist in the Java Gateway, e.g. https://github.com/openmicroscopy/openmicroscopy/blob/develop/components/blitz/src/omero/gateway/facility/MetadataFacility.java#L115. Longer-term, we'd like to update the OMERO.matlab code to make use of the Gateway as a higher-level wrapper around the session object.

~Josh
User avatar
jmoore
Site Admin
 
Posts: 1591
Joined: Fri May 22, 2009 1:29 pm
Location: Germany

Re: How to edit channel names?

Postby Kouichi_C_Nakamura » Sun Jul 01, 2018 8:42 am

Thanks. Updating doesn't seem that easy.

Code: Select all
session.getUpdateService().saveAndReturnObject(channels);


Does this mean I should update ImageI object?

Code: Select all
session.getUpdateService().saveAndReturnObject(image);


If yes, I need to update the image object first.

Associating the modified channels to the parent image object isn't that simple either. Judging from the content of loadChannels.m, I might need to use pixelsService, but pixelsService doesn't have a method to save new values.

Code: Select all
context = java.util.HashMap;
group = image.getDetails().getGroup().getId().getValue();
context.put('omero.group', java.lang.String(num2str(group)));
pixelsService = session.getPixelsService();
Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to edit channel names?

Postby Dominik » Mon Jul 02, 2018 12:42 pm

Hi Kouichi,

you don't need to update/save the Image, just the modified Channel object.
Here's an example using the Java gateway: https://gist.github.com/dominikl/70a890 ... fde6691acc

If you don't want to use the gateway, you'd have to use the updateDataObject method of the ContainerService, see
https://downloads.openmicroscopy.org/om ... arameters-

Regards,
Dominik
User avatar
Dominik
Team Member
 
Posts: 149
Joined: Mon Feb 10, 2014 11:26 am

Re: How to edit channel names?

Postby Kouichi_C_Nakamura » Tue Jul 10, 2018 5:01 am

Gateway seems to require additional MATLAB toolbox, so I opted for ContainerService.

Neither cs.updateDataObjects nor cs.updateDataObject worked in the example below. Any suggestions?


Code: Select all
>> channels % containing new channel names
channels =

  omero.model.ChannelI[]:

    [omero.model.ChannelI]
    [omero.model.ChannelI]
    [omero.model.ChannelI]
    [omero.model.ChannelI]

>> cs = session.getContainerService();
>> cs.updateDataObjects(channels);
>> cs.updateDataObject(channels(1));


Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to edit channel names?

Postby Dominik » Tue Jul 10, 2018 10:00 am

Unfortunately I can't test the Matlab code, but I created another example which only uses the Java API directly without the Gateway, hope that helps:

Code: Select all
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import omero.api.IContainerPrx;
import omero.api.IPixelsPrx;
import omero.api.ServiceFactoryPrx;
import omero.model.Channel;
import omero.model.IObject;
import omero.model.Image;
import omero.model.Pixels;

public class EditChannelNames {

    public static void main(String[] args) throws Exception {
        String hostname = "localhost";
        String username = "root";
        String password = "omero";

        client cl = new client(hostname, 4064);
        ServiceFactoryPrx sf = cl.createSession(username, password);

        long imageid = 12345l;

        IContainerPrx cp = sf.getContainerService();
        Image img = cp
                .getImages("Image", Collections.singletonList(imageid), null)
                .iterator().next();

        IPixelsPrx pp = sf.getPixelsService();
        Pixels pix = pp
                .retrievePixDescription(img.getPrimaryPixels().getId().val);

        List<Channel> channels = pix.copyChannels();
        List<IObject> toSave = new ArrayList<IObject>();
        for (int i = 0; i < channels.size(); i++) {
            Channel channel = channels.get(i);
            channel.getLogicalChannel().setName(
                    omero.rtypes.rstring("Channel " + i));
            toSave.add(channel);
        }

        cp.updateDataObjects(toSave, null);
       
        cl.closeSession();
    }

}
User avatar
Dominik
Team Member
 
Posts: 149
Joined: Mon Feb 10, 2014 11:26 am

Re: How to edit channel names?

Postby Kouichi_C_Nakamura » Tue Jul 10, 2018 1:21 pm

Thanks a lot.

Although I could not really understand why it has to be ArrayList rather than an array of ChannelI object from the documentation (I thought T was supposed to be IObject, but may I was confused), I managed to make my code working.

https://downloads.openmicroscopy.org/om ... arameters-

Do you think this is also worth adding as a Pull Request? Given the time I spent on writing this small function, it's not an easy job for many people and it probably is worthwhile, I think.

Code: Select all
function T = editChannelNames(session,img,varargin)
% editChannelNames allows you to change Channel Names for an image in OMERO
% server.
%
% SYNTAX
% T = editChannelNames(session,img)
% T = editChannelNames(session,img,newchanNames)
% T = editChannelNames(____,'Param',value)
%
% T = editChannelNames(session,img) returns the current channel names. If
% not specified yet, they will be empty characters, although it may look
% like 0, 1, or 2 etc on OMERO GUI.
%
% T = editChannelNames(session,img,newchanNames) will set the channel names
% to newchanNames.
%
% INPUT ARGUMENTS
% session     omero.api.ServiceFactoryPrxHelper object
%
% img         positive integer | omero.model.ImageI object
%             An Image ID or an omero.model.ImageI object for OMERO.
%
% newchanNames
%             cell vector of character vectors | string vector
%             The new channel names. If you specify 'channelIDs', the
%             length of channelIDs and that of newchanNames must tally.
%
% OPTIONAL PARAMETER/VALUE PAIRS
% 'channelIDs'
%             [] | vector of positive integers
%             (Optional) Channel IDs for specific editing. If you specify
%             'channelIDs', the length of channelIDs and that of
%             newchanNames must tally.
%
% OUTPUT ARGUMENTS
% T           table array
%             With variables, 'Id', 'Name', and, if newchanNames is
%             specified, 'NewName'.
%
% Written by Kouichi C. Nakamura Ph.D.
% MRC Brain Network Dynamics Unit
% University of Oxford
% kouichi.c.nakamura@gmail.com
% 10-Jul-2018 12:04:46
%
% See also
% loadChannels, getImages

p = inputParser;
p.addRequired('session',@(x) isscalar(x));
p.addRequired('img',@(x) isscalar(x));
p.addOptional('newchanNames',[],@(x) isvector(x) && iscellstr(x) || isstring(x));
p.addParameter('ChannelIDs',[],@(x) isvector(x) && all(fix(x) == x & x > 0));

p.parse(session,img,varargin{:});

newchanNames = p.Results.newchanNames;
channelIDs = p.Results.ChannelIDs;

if ~isempty(channelIDs)
   
    assert(length(channelIDs) == length(newchanNames),...
        'When you specify channelIDs, the length of channelIDs and newchanNames must tally.')
   
end

if isstring(newchanNames)
    newchanNames = cellstr(newchanNames);
end


if isnumeric(img)

    img_ = img;

    img = getImages(session,img_);

end


channels = loadChannels(session, img);

channelId = zeros(numel(channels),1);
channelName = cell(numel(channels),1);
channelName_ = cell(numel(channels),1);


import java.util.ArrayList
li = ArrayList;

j = 0;
for i = 1:numel(channels)
    ch = channels(i);
    channelId(i,1) = double(ch.getId().getValue());
   
    if ~isempty(ch.getLogicalChannel().getName())
   
        channelName{i,1} = char(ch.getLogicalChannel().getName().getValue()); %java.lang.String

    else
       
        channelName{i,1} ='';
       
    end
   
    if ~isempty(newchanNames)
        if isempty(channelIDs) || ismember(channelId(i,1),channelIDs)
            j = j + 1;

            channels(i).getLogicalChannel().setName(rstring(newchanNames{j})); % overwrite
            channelName_{i,1} = char(ch.getLogicalChannel().getName().getValue());
        else
            channelName_{i,1} = channelName{i,1};
           
        end
    end
   
    li.add(channels(i));
end

if ~isempty(newchanNames)
    T = table(channelId,channelName,channelName_,'VariableNames',{'Id','Name','NewName'});

    cs = session.getContainerService();
    cs.updateDataObjects(li,[]);% tricky to find the right type. see http://www.openmicroscopy.org/community/viewtopic.php?f=6&t=8536
   
else
    T = table(channelId,channelName,'VariableNames',{'Id','Name'});
end


end

Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm

Re: How to edit channel names?

Postby Dominik » Tue Jul 10, 2018 1:59 pm

That's great! Yes, please go ahead, definitely worth opening a pull request.

Oh, the extra List<IObject> was just necessary, because the updateDataObjects method of the Java API wants a List<IObject> and a List<Channel> cannot be casted into a List<IObject> directly.

Regards,
Dominik
User avatar
Dominik
Team Member
 
Posts: 149
Joined: Mon Feb 10, 2014 11:26 am

Re: How to edit channel names?

Postby Kouichi_C_Nakamura » Tue Jul 10, 2018 2:30 pm

Kouichi_C_Nakamura
 
Posts: 165
Joined: Thu Oct 19, 2017 1:35 pm


Return to Developer Discussion

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

cron