# HG changeset patch
# User goeckslab
# Date 1662657773 0
# Node ID 6df8d6e421520275b0622c1ebb7bf36c5574ed1a
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/vitessce commit 9b2dc921e692af8045773013d9f87d4d790e2ea1
diff -r 000000000000 -r 6df8d6e42152 gate_finder.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/gate_finder.py Thu Sep 08 17:22:53 2022 +0000
@@ -0,0 +1,156 @@
+import argparse
+import json
+import warnings
+from pathlib import Path
+
+import numpy as np
+import pandas as pd
+from anndata import read_h5ad
+from sklearn.mixture import GaussianMixture
+from sklearn.preprocessing import MinMaxScaler
+from vitessce import (
+ AnnDataWrapper,
+ Component as cm,
+ MultiImageWrapper,
+ OmeTiffWrapper,
+ VitessceConfig,
+)
+
+
+# Generate binarized phenotype for a gate
+def get_gate_phenotype(g, d):
+ dd = d.copy()
+ dd = np.where(dd < g, 0, dd)
+ np.warnings.filterwarnings('ignore')
+ dd = np.where(dd >= g, 1, dd)
+ return dd
+
+
+def get_gmm_phenotype(data):
+ low = np.percentile(data, 0.01)
+ high = np.percentile(data, 99.99)
+ data = np.clip(data, low, high)
+
+ sum = np.sum(data)
+ median = np.median(data)
+ data_med = data / sum * median
+
+ data_log = np.log1p(data_med)
+ data_log = data_log.reshape(-1, 1)
+
+ scaler = MinMaxScaler(feature_range=(0, 1))
+ data_norm = scaler.fit_transform(data_log)
+
+ gmm = GaussianMixture(n_components=2)
+ gmm.fit(data_norm)
+ gate = np.mean(gmm.means_)
+
+ return get_gate_phenotype(gate, np.ravel(data_norm))
+
+
+def main(inputs, output, image, anndata, masks=None):
+ """
+ Parameter
+ ---------
+ inputs : str
+ File path to galaxy tool parameter.
+ output : str
+ Output folder for saving web content.
+ image : str
+ File path to the OME Tiff image.
+ anndata : str
+ File path to anndata containing phenotyping info.
+ masks : str
+ File path to the image masks.
+ """
+ warnings.simplefilter('ignore')
+
+ with open(inputs, 'r') as param_handler:
+ params = json.load(param_handler)
+
+ marker = params['marker'].strip()
+ from_gate = params['from_gate']
+ to_gate = params['to_gate']
+ increment = params['increment']
+ x_coordinate = params['x_coordinate'].strip() or 'X_centroid'
+ y_coordinate = params['y_coordinate'].strip() or 'Y_centroid'
+
+ adata = read_h5ad(anndata)
+
+ # If no raw data is available make a copy
+ if adata.raw is None:
+ adata.raw = adata
+
+ # Copy of the raw data if it exisits
+ if adata.raw is not None:
+ adata.X = adata.raw.X
+
+ data = pd.DataFrame(
+ adata.X,
+ columns=adata.var.index,
+ index=adata.obs.index
+ )
+ marker_values = data[[marker]].values
+ marker_values_log = np.log1p(marker_values)
+
+ # Identify the list of increments
+ gate_names = []
+ for num in np.arange(from_gate, to_gate, increment):
+ num = round(num, 3)
+ key = marker + '--' + str(num)
+ adata.obs[key] = get_gate_phenotype(num, marker_values_log)
+ gate_names.append(key)
+
+ adata.obs['GMM_auto'] = get_gmm_phenotype(marker_values)
+ gate_names.append('GMM_auto')
+
+ adata.obsm['XY_coordinate'] = adata.obs[[x_coordinate, y_coordinate]].values
+
+ vc = VitessceConfig(name=None, description=None)
+ dataset = vc.add_dataset()
+ image_wrappers = [OmeTiffWrapper(img_path=image, name='OMETIFF')]
+ if masks:
+ image_wrappers.append(
+ OmeTiffWrapper(img_path=masks, name='MASKS', is_bitmask=True)
+ )
+ dataset.add_object(MultiImageWrapper(image_wrappers))
+
+ dataset.add_object(
+ AnnDataWrapper(
+ adata,
+ spatial_centroid_obsm='XY_coordinate',
+ cell_set_obs=gate_names,
+ cell_set_obs_names=[obj[0].upper() + obj[1:] for obj in gate_names],
+ expression_matrix="X"
+ )
+ )
+ spatial = vc.add_view(dataset, cm.SPATIAL)
+ cellsets = vc.add_view(dataset, cm.CELL_SETS)
+ status = vc.add_view(dataset, cm.STATUS)
+ lc = vc.add_view(dataset, cm.LAYER_CONTROLLER)
+ genes = vc.add_view(dataset, cm.GENES)
+ cell_set_sizes = vc.add_view(dataset, cm.CELL_SET_SIZES)
+ cell_set_expression = vc.add_view(dataset, cm.CELL_SET_EXPRESSION)
+
+ vc.layout(
+ (status / genes / cell_set_expression)
+ | (cellsets / cell_set_sizes / lc)
+ | (spatial)
+ )
+ config_dict = vc.export(to='files', base_url='http://localhost', out_dir=output)
+
+ with open(Path(output).joinpath('config.json'), 'w') as f:
+ json.dump(config_dict, f, indent=4)
+
+
+if __name__ == '__main__':
+ aparser = argparse.ArgumentParser()
+ aparser.add_argument("-i", "--inputs", dest="inputs", required=True)
+ aparser.add_argument("-e", "--output", dest="output", required=True)
+ aparser.add_argument("-g", "--image", dest="image", required=True)
+ aparser.add_argument("-a", "--anndata", dest="anndata", required=True)
+ aparser.add_argument("-m", "--masks", dest="masks", required=False)
+
+ args = aparser.parse_args()
+
+ main(args.inputs, args.output, args.image, args.anndata, args.masks)
diff -r 000000000000 -r 6df8d6e42152 gate_finder.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/gate_finder.xml Thu Sep 08 17:22:53 2022 +0000
@@ -0,0 +1,63 @@
+
>4===0?this.huffmanTablesDC[15&Q]=Ct(y,b):this.huffmanTablesAC[15&Q]=Ct(y,b)}break;case 65501:A(),this.resetInterval=A();break;case 65498:A();for(var S=e[t++],F=[],R=this.frames[0],D=0;D >>=p,I-=p),I<15&&(l+=m[A++]<>>=p=B>>>24,I-=p,!(16&(p=B>>>16&255))){if(0===(64&p)){B=h[(65535&B)+(l&(1< a){e.msg="invalid distance too far back",w.mode=30;break e}if(l>>>=p,I-=p,Q>(p=r-i)){if((p=Q-p)>g&&w.sane){e.msg="invalid distance too far back",w.mode=30;break e}if(y=0,v=u,0===c){if(y+=s-p,p >>=p,I-=p),I<15&&(l+=m[A++]<>>=p=B>>>24,I-=p,!(16&(p=B>>>16&255))){if(0===(64&p)){B=h[(65535&B)+(l&(1< a){e.msg="invalid distance too far back",w.mode=30;break e}if(l>>>=p,I-=p,Q>(p=r-i)){if((p=Q-p)>g&&w.sane){e.msg="invalid distance too far back",w.mode=30;break e}if(y=0,v=u,0===c){if(y+=s-p,p>4],G.huffmanTableAC=this.huffmanTablesAC[15&k],F.push(G)}var x=e[t++],N=e[t++],U=e[t++],_=ht(e,t,R,F,this.resetInterval,x,N,U>>4,15&U);t+=_;break;case 65535:255!==e[t]&&t--;break;default:if(255===e[t-3]&&e[t-2]>=192&&e[t-2]<=254){t-=3;break}throw new Error("unknown JPEG marker ".concat(i.toString(16)))}i=A()}}},{key:"getResult",value:function(){var e=this.frames;if(0===this.frames.length)throw new Error("no frames were decoded");this.frames.length>1&&console.warn("more than one frame is not supported");for(var t=0;t>=7;n<30;n++)for(kt[n]=r<<7,e=0;e<1<0&&(0===e||1!==Q))return-1;for(G[1]=0,B=1;B<15;B++)G[B+1]=G[B]+D[B];for(p=0;p
852||2===e&&w>592)return 1;r[c=S&u]=y<<24|v<<16|l-i|0}}return 0!==S&&(r[l+S]=B-m<<24|64<<16|0),a.bits=y,0},En=AA.Z_FINISH,Qn=AA.Z_BLOCK,yn=AA.Z_TREES,vn=AA.Z_OK,mn=AA.Z_STREAM_END,bn=AA.Z_NEED_DICT,wn=AA.Z_STREAM_ERROR,Sn=AA.Z_DATA_ERROR,Fn=AA.Z_MEM_ERROR,Rn=AA.Z_BUF_ERROR,Dn=AA.Z_DEFLATED,Gn=function(e){return(e>>>24&255)+(e>>>8&65280)+((65280&e)<<8)+((255&e)<<24)};function kn(){this.mode=0,this.last=!1,this.wrap=0,this.havedict=!1,this.flags=0,this.dmax=0,this.check=0,this.total=0,this.head=null,this.wbits=0,this.wsize=0,this.whave=0,this.wnext=0,this.window=null,this.hold=0,this.bits=0,this.length=0,this.offset=0,this.extra=0,this.lencode=null,this.distcode=null,this.lenbits=0,this.distbits=0,this.ncode=0,this.nlen=0,this.ndist=0,this.have=0,this.next=null,this.lens=new Uint16Array(320),this.work=new Uint16Array(288),this.lendyn=null,this.distdyn=null,this.sane=0,this.back=0,this.was=0}var xn,Nn,Un=function(e){if(!e||!e.state)return wn;var t=e.state;return e.total_in=e.total_out=t.total=0,e.msg="",t.wrap&&(e.adler=1&t.wrap),t.mode=1,t.last=0,t.havedict=0,t.dmax=32768,t.head=null,t.hold=0,t.bits=0,t.lencode=t.lendyn=new Int32Array(852),t.distcode=t.distdyn=new Int32Array(592),t.sane=1,t.back=-1,vn},_n=function(e){if(!e||!e.state)return wn;var t=e.state;return t.wsize=0,t.whave=0,t.wnext=0,Un(e)},Mn=function(e,t){var A;if(!e||!e.state)return wn;var n=e.state;return t<0?(A=0,t=-t):(A=1+(t>>4),t<48&&(t&=15)),t&&(t<8||t>15)?wn:(null!==n.window&&n.wbits!==t&&(n.window=null),n.wrap=A,n.wbits=t,_n(e))},On=function(e,t){if(!e)return wn;var A=new kn;e.state=A,A.window=null;var n=Mn(e,t);return n!==vn&&(e.state=null),n},Ln=!0,Tn=function(e){if(Ln){xn=new Int32Array(512),Nn=new Int32Array(32);for(var t=0;t<144;)e.lens[t++]=8;for(;t<256;)e.lens[t++]=9;for(;t<280;)e.lens[t++]=7;for(;t<288;)e.lens[t++]=8;for(pn(1,e.lens,0,288,xn,0,e.work,{bits:9}),t=0;t<32;)e.lens[t++]=5;pn(2,e.lens,0,32,Nn,0,e.work,{bits:5}),Ln=!1}e.lencode=xn,e.lenbits=9,e.distcode=Nn,e.distbits=5},Zn=function(e,t,A,n){var r,i=e.state;return null===i.window&&(i.wsize=1<
>=7;n<30;n++)for(m[n]=r<<7,e=0;e<1<0?(2===e.strm.data_type&&(e.strm.data_type=function(e){var t,A=4093624447;for(t=0;t<=31;t++,A>>>=1)if(1&A&&0!==e.dyn_ltree[2*t])return 0;if(0!==e.dyn_ltree[18]||0!==e.dyn_ltree[20]||0!==e.dyn_ltree[26])return 1;for(t=32;t<256;t++)if(0!==e.dyn_ltree[2*t])return 1;return 0}(e)),O(e,e.l_desc),O(e,e.d_desc),o=function(e){var t;for(L(e,e.dyn_ltree,e.l_desc.max_code),L(e,e.dyn_dtree,e.d_desc.max_code),O(e,e.bl_desc),t=18;t>=3&&0===e.bl_tree[2*h[t]+1];t--);return e.opt_len+=3*(t+1)+5+5+4,t}(e),r=e.opt_len+3+7>>>3,(i=e.static_len+3+7>>>3)<=r&&(r=i)):r=i=A+5,A+4<=r&&-1!==t?H(e,t,A,n):4===e.strategy||i===r?(R(e,2+(n?1:0),3),M(e,f,d)):(R(e,4+(n?1:0),3),function(e,t,A,n){var r;for(R(e,t-257,5),R(e,A-1,5),R(e,n-4,4),r=0;r0&&(0===e||1!==Q))return-1;for(G[1]=0,B=1;B<15;B++)G[B+1]=G[B]+D[B];for(p=0;p
852||2===e&&w>592)return 1;r[c=S&u]=y<<24|v<<16|l-i|0}}return 0!==S&&(r[l+S]=B-m<<24|64<<16|0),a.bits=y,0},It=K.Z_FINISH,Ct=K.Z_BLOCK,ht=K.Z_TREES,ft=K.Z_OK,dt=K.Z_STREAM_END,Bt=K.Z_NEED_DICT,pt=K.Z_STREAM_ERROR,Et=K.Z_DATA_ERROR,Qt=K.Z_MEM_ERROR,yt=K.Z_BUF_ERROR,vt=K.Z_DEFLATED,mt=function(e){return(e>>>24&255)+(e>>>8&65280)+((65280&e)<<8)+((255&e)<<24)};function bt(){this.mode=0,this.last=!1,this.wrap=0,this.havedict=!1,this.flags=0,this.dmax=0,this.check=0,this.total=0,this.head=null,this.wbits=0,this.wsize=0,this.whave=0,this.wnext=0,this.window=null,this.hold=0,this.bits=0,this.length=0,this.offset=0,this.extra=0,this.lencode=null,this.distcode=null,this.lenbits=0,this.distbits=0,this.ncode=0,this.nlen=0,this.ndist=0,this.have=0,this.next=null,this.lens=new Uint16Array(320),this.work=new Uint16Array(288),this.lendyn=null,this.distdyn=null,this.sane=0,this.back=0,this.was=0}var wt,St,Ft=function(e){if(!e||!e.state)return pt;var t=e.state;return e.total_in=e.total_out=t.total=0,e.msg="",t.wrap&&(e.adler=1&t.wrap),t.mode=1,t.last=0,t.havedict=0,t.dmax=32768,t.head=null,t.hold=0,t.bits=0,t.lencode=t.lendyn=new Int32Array(852),t.distcode=t.distdyn=new Int32Array(592),t.sane=1,t.back=-1,ft},Rt=function(e){if(!e||!e.state)return pt;var t=e.state;return t.wsize=0,t.whave=0,t.wnext=0,Ft(e)},Dt=function(e,t){var A;if(!e||!e.state)return pt;var n=e.state;return t<0?(A=0,t=-t):(A=1+(t>>4),t<48&&(t&=15)),t&&(t<8||t>15)?pt:(null!==n.window&&n.wbits!==t&&(n.window=null),n.wrap=A,n.wbits=t,Rt(e))},Gt=function(e,t){if(!e)return pt;var A=new bt;e.state=A,A.window=null;var n=Dt(e,t);return n!==ft&&(e.state=null),n},kt=!0,xt=function(e){if(kt){wt=new Int32Array(512),St=new Int32Array(32);for(var t=0;t<144;)e.lens[t++]=8;for(;t<256;)e.lens[t++]=9;for(;t<280;)e.lens[t++]=7;for(;t<288;)e.lens[t++]=8;for(lt(1,e.lens,0,288,wt,0,e.work,{bits:9}),t=0;t<32;)e.lens[t++]=5;lt(2,e.lens,0,32,St,0,e.work,{bits:5}),kt=!1}e.lencode=wt,e.lenbits=9,e.distcode=St,e.distbits=5},Nt=function(e,t,A,n){var r,i=e.state;return null===i.window&&(i.wsize=1<