function [idx_done] = disp_qc(emesh, conn, im_path, idx, count, idx_genes, idx_exclude) % disp_mcomposites(emesh, m_ref, m_res, panels, conn, m_res_mesh, m_res_mesh_idx) % displays composites of reference (m_ref) with others (m_res) % emesh is standard emesh struct % m_ref and m_res are indices for emesh % m_ref must have size of 1, m_res is an array % m_ref can be also logical, then a synthetic mesh is constructed % conn is optional, if given will query the gene symbols, can be empty % m_res_mesh contains the result meshes if emesh should not be used % m_res_mesh_idx contains the indices of result meshes in case there is no % 1:1 correspondence between m_res and m_res_mesh % Constants counter = 1; panels = 2; ysize = 200; choices = {'accurate', 'accurate stripes', 'inaccurate stripes', 'mesh_reject', 'segmentation inaccurate', 'TI lacks detail', 'single cells missing'}; gpri_choices = [ 1, 2, 3, 0, 0, 3, 3]; hsel_u = cell(length(choices), 1); if exist('idx_genes'), if length(idx_genes) ~= length(idx), error('Number of genes doesn''t match number of meshes\n'); end gene_selection = []; % Add to gene selection until the desired count is exceeded while length(gene_selection) < count, % get random selection rand_gene_selection = unique(idx_genes(randi(length(unique(idx_genes)),count,1))); % exclude previously scored genes if exist('idx_exclude'), rand_gene_selection = setdiff(rand_gene_selection, idx_exclude); end % exclude previous gene_selection rand_gene_selection = setdiff(rand_gene_selection, gene_selection); gene_selection = [gene_selection; rand_gene_selection]; end gene_selection = gene_selection(1:count); if exist('idx_exclude'), idx_done = [idx_exclude; gene_selection]; else idx_done = gene_selection; end selection = []; for i=1:length(gene_selection) selection = [selection; idx(find(idx_genes == gene_selection(i)))]; end else gene_selection = []; selection = unique(idx(randi(length(idx),count,1))); end count = length(selection); selection_choice = zeros(count, 1); selection_check = zeros(count, 1); resmesh = zeros(size(emesh.stain,1), count); resfiles = cell(count,1); ressym = cell(count,1); resdisp = cell(count,1); for i=1:count resmesh(:,i) = emesh.stain(:, selection(i)); if ~isempty(conn), resfiles{i} = emesh.files{selection(i)}; sym = pg_query(conn,0, my_insitus('symbol'),resfiles{i}); % resfiles{i} = sprintf('%s (%s)', sym{1}, fn); ressym{i} = sym{1}; resdisp{i} = sprintf('%s (%s)\n%d of %d', sym{1}, resfiles{i}, i, count); end end % Create and then hide the GUI as it is being constructed. % minimum [360,500,600,300] w/o list f = figure('Visible','off','Position',[360,500,700,260+ysize]); % Construct the components hforw = uicontrol('Style','pushbutton','String','Forward >',... 'Position',[380,15,70,25],... 'Callback',{@forwardbutton_Callback}); hback = uicontrol('Style','pushbutton','String','< Back',... 'Position',[50,15,70,25],... 'Callback',{@backbutton_Callback}); hprint = uicontrol('Style','pushbutton','String','Print stats',... 'Position',[225,15,70,25], ... 'Callback',{@printbutton_Callback}); hsel = uibuttongroup('visible','off','Units', 'pixels', 'Position',[500, 150, 150, 100+ysize]); for i=1:length(choices), hsel_u{i} = uicontrol('Style','Radio','String',choices{i},... 'pos',[10 50+ysize-(i-1)*30 130 20],'parent',hsel, 'HandleVisibility','off'); end set(hsel,'SelectionChangeFcn',{@rbutton_Callback}); set(hsel,'SelectedObject',[]); % No selection set(hsel,'Visible','on'); hsel_chk = uicontrol('Style','checkbox','String','Staining layer problem',... 'Position',[510 100 130 20], 'Callback',{@checkbox_Callback}); hsel_chk_min = get(hsel_chk, 'Min'); hsel_chk_max = get(hsel_chk, 'Max'); htext = uicontrol('Style','text','String', resdisp{counter},... 'Position',[500,15,150,35]); hp = uipanel('Units','Pixels','Position',[50,50,400,200+ysize]); ha = axes('Parent', hp, 'Units','Pixels','Position',[0,0,400,200+ysize]); % align([hforw,hback,htext],'Center','None'); % Initialize the GUI. %Change units to normalized so components resize automatically. set([f,ha,hp,hforw, hback, htext, hsel, hprint, hsel_chk],... 'Units','normalized'); %Create a plot in the axes. displaypanels(panels, im_path, resfiles{counter}, resmesh(:, counter), emesh.p_scale, emesh.t); % Assign the GUI a name to appear in the window title. set(f,'Name','Insitu Quality Control') % Move the GUI to the center of the screen. movegui(f,'center') % Make the GUI visible. set(f,'Visible','on'); % Callbacks for simple_gui. These callbacks automatically have % access to component handles and initialized data because % they are nested at a lower level. function rbutton_Callback(source, eventdata) r_choice = get(get(source,'SelectedObject'),'String'); selection_choice(counter) = strmatch(r_choice, choices, 'exact'); end function checkbox_Callback(hObject, eventdata, handles) if (get(hObject,'Value') == get(hObject,'Max')) % Checkbox is checked-take approriate action selection_check(counter) = 1; else % Checkbox is not checked-take approriate action selection_check(counter) = 0; end end % Push button callbacks. Each callback plots current_data in the % specified plot type. function forwardbutton_Callback(source,eventdata) % Display surf plot of the currently selected data. if counter < count, counter = counter + 1; set(htext, 'String', resdisp{counter}); displaypanels(panels, im_path, resfiles{counter}, resmesh(:, counter), emesh.p_scale, emesh.t); setChoices(); end end function backbutton_Callback(source,eventdata) % Display mesh plot of the currently selected data. if counter > 1, counter = counter - 1; set(htext, 'String', resdisp{counter}); displaypanels(panels, im_path, resfiles{counter}, resmesh(:, counter), emesh.p_scale, emesh.t); setChoices(); end end function printbutton_Callback(source,eventdata) for j=1:length(resfiles), fprintf('%s\t%d\t%d\n', resfiles{j}, selection_choice(j), selection_check(j)); end fprintf('Meshes (%d):\n', length(selection_choice)); fprintf('======\n'); n_ct = histc(selection_choice, 1:length(choices)); for j=1:length(choices), fprintf('%s\t%d\n', choices{j}, n_ct(j)); end fprintf('------\nTotal\t%d\n', sum(n_ct)); fprintf('Other layers\t%d\n', sum(selection_check)); % Calculate the summary for the genes if ~isempty(gene_selection) gene_choice = zeros(length(gene_selection), 1); gene_check = zeros(length(gene_selection), 1); sel_ptr = 1; for j=1:length(gene_selection) sel_len = length(idx(find(idx_genes == gene_selection(j)))); for jj=sel_ptr:sel_ptr+sel_len-1, if selection_choice(jj) == 0, continue; end if gene_choice(j) == 0, gene_choice(j) = selection_choice(jj); elseif gpri_choices(gene_choice(j)) < gpri_choices(selection_choice(jj)), gene_choice(j) = selection_choice(jj); end if selection_check(jj) == 1, gene_check(j) = 1; end end sel_ptr = sel_ptr + sel_len; end fprintf('\nGenes (%d):\n', length(gene_selection)); fprintf('=======\n'); n_ct = histc(gene_choice, 1:length(choices)); for j=1:length(choices), fprintf('%s\t%d\n', choices{j}, n_ct(j)); end fprintf('------\nTotal\t%d\n', sum(n_ct)); fprintf('Other layers\t%d\n', sum(gene_check)); end end function setChoices() if selection_choice(counter) == 0 set(hsel,'SelectedObject',[]); else set(hsel,'SelectedObject', hsel_u{selection_choice(counter)}); end if selection_check(counter) == 0, set(hsel_chk, 'Value', hsel_chk_min); else set(hsel_chk, 'Value', hsel_chk_max); end end function setGene(old_counter, new_counter) if isempty(gene), return; end if idx_genes(selection(old_counter)) ~= idx_genes(selection(new_counter)), gene = idx_genes(selection(new_counter)); gene_counter = find(gene_selection == gene); end end end function displaypanels(panels, im_path, imagefile, mesh, p_scale,t) subplot(panels,1,1); imshow(insitu_image(im_path, imagefile)); axis off; subplot(panels,1,2); displaymesh(mesh, p_scale, t); end function [im2] = insitu_image(image_path, filename) fnum = regexpi(filename, '\d+', 'match'); flen = length(fnum{1}) - 3; fdir = fnum{1}(1:flen); fdig = fnum{1}(flen+1:end); if fdig == 0, fdir = fdir - 1; end fullname = strcat(image_path,'/img_dir_',fdir,'/',filename); im = imread(fullname); im2 = imresize(im, 0.5); end function displaydmesh(refmesh, compmesh, p_scale,t) axis ij; axis off; hold on; cmesh = zeros(length(refmesh), 3); cmesh(:,1) = (255 - refmesh) ./ 255; cmesh(:,2) = (255 - compmesh) ./ 255; for i = 1:size(t,1) roic = fix(p_scale(t(i,:),1)'); roir = fix(p_scale(t(i,:),2)'); fill(roic,roir,cmesh(i,:), 'EdgeColor', cmesh(i,:)); end; end function displaycmesh(stain, p_scale, t, green) axis ij; axis off; hold on; cmesh = zeros(length(stain), 3); if green == 0, cmesh(:,1) = (255 - stain) ./ 255; else cmesh(:,2) = (255 - stain) ./ 255; end for i = 1:size(t,1) roic = fix(p_scale(t(i,:),1)'); roir = fix(p_scale(t(i,:),2)'); fill(roic,roir,cmesh(i,:),'EdgeColor', cmesh(i,:)); end; hold off; end