def flame(E, knn=10, threshold=-1, threshold2=-3.0, steps=500, **kwargs): with TemporaryDirectory() as tmpdir: with open(tmpdir + "/E.csv", "w") as outfile: outfile.write(str(E.shape[1]) + " " + str(E.shape[0]) + "\n") standardize(E).T.to_csv(outfile, index=False, header=False, sep=" ") binary = os.environ["PERSOFTWARELOCATION"] + "/flame/sample" command = "{binary} {tmpdir}/E.csv {knn} {threshold2} {steps} {threshold}".format( **locals()) process = sp.Popen(command, shell=True, stdout=sp.PIPE) out, err = process.communicate() modules = [] for row in out.decode().split("\n"): if row.startswith("Cluster") and "outliers" not in row: gids = row[row.index(":") + 1:].split(",") if gids[0] != "": module = Module([E.columns[int(gid)] for gid in gids]) modules.append(module) return modules
def click(E, homogeneity=0.5, **kwargs): with TemporaryDirectory() as tmpdir: with open(tmpdir + "/clickInput.orig", "w") as outfile: outfile.write("{nG} {nC}\n".format(nG = len(E.columns), nC=len(E.index))) E.T.to_csv(outfile, sep="\t", header=False) with open(tmpdir + "/clickParams.txt", "w") as outfile: outfile.write("""DATA_TYPE FP INPUT_FILES_PREFIX {tmpdir}/clickInput OUTPUT_FILE_PREFIX {tmpdir}/clickOutput SIMILARITY_TYPE CORRELATION HOMOGENEITY {homogeneity} """.format(tmpdir=tmpdir, homogeneity=homogeneity)) # PERSOFTWARELOCATION is the location in which the software is installed click_location = os.environ["PERSOFTWARELOCATION"] + "/Expander/click.exe" command = "{click_location} {tmpdir}/clickParams.txt".format(**locals()) sp.call(command, shell=True) labels = pd.read_csv(tmpdir + "/clickOutput.res.sol", sep="\t", index_col=0, header=None, squeeze=True) modules = convert_labels2modules(labels.tolist(), labels.index.tolist(), 0) return modules
def makeAtlases(target, trainingTable, outdir, mabs=False): outdir = local.path(outdir) from plumbum.cmd import mkdir mkdir('-p', outdir) logging.info( 'Create atlases: compute transforms from images to target and apply') for idx, r in trainingTable.iterrows(): warp = outdir / 'warp{idx}.nii.gz'.format(**locals()) atlas = outdir / 'atlas{idx}.nii.gz'.format(**locals()) computeWarp(r['image'], target, warp) applyWarp(r['image'], warp, target, atlas) for labelname, label in r.iloc[1:].iteritems(): atlaslabel = outdir / '{labelname}{idx}.nii.gz'.format(**locals()) applyWarp(label, warp, target, atlaslabel, interpolation='NearestNeighbor') if mabs: from plumbum.cmd import unu, ConvertBetweenFileFormats, AverageImages for labelname in list(trainingTable)[1:]: out = outdir / labelname + '.nrrd' labelmaps = outdir // labelname + '*' with TemporaryDirectory() as tmpdir: nii = tmpdir / 'mabs.nii.gz' AverageImages('3', nii, '0', *labelmaps) ConvertBetweenFileFormats(nii, out) unu['2op', 'gt', out, '0.5'] | \ unu['save', '-e', 'gzip', '-f', 'nrrd', '-o', out] & FG
def main(self): with TemporaryDirectory() as t: t = local.path(t) ukf = self.ukf fsindwi = self.fsindwi if '.gz' in self.ukf.suffix: ukf = t / 'ukf.vtk' from plumbum.cmd import gunzip (gunzip['-c', self.ukf] > ukf)() tract_querier = local['tract_querier'] tract_math = local['tract_math'] ukfpruned = t / 'ukfpruned.vtk' # tract_math(ukf, 'tract_remove_short_tracts', '2', ukfpruned) tract_math[ukf, 'tract_remove_short_tracts', '2', ukfpruned] & FG if not ukfpruned.exists(): raise Exception( "tract_math failed to make '{}'".format(ukfpruned)) self.out = local.path(self.out) if self.out.exists(): self.out.delete() self.out.mkdir() tract_querier['-t', ukfpruned, '-a', fsindwi, '-q', self.query, '-o', self.out / '_'] & FG logging.info('Convert vtk field data to tensor data') # use the following multi-processed loop pool = Pool(int(self.nproc)) pool.map_async(_activateTensors_py, self.out.glob('*.vtk')) pool.close() pool.join()
def opsm(E, l=2, **kwargs): with TemporaryDirectory() as tmpdir: pd.DataFrame(standardize(E)).T.to_csv(tmpdir + "E.csv", index=0, header=0, sep=" ") output_location = os.path.abspath(tmpdir + "/output.txt") # PERSOFTWARELOCATION is the location in which the software is installed binary = "java -XX:ParallelGCThreads=1 -Xmx1G -jar " + os.environ["PERSOFTWARELOCATION"] + "/OPSM/OPSM.jar" command = "{binary} {E_location} {nG} {nC} {output_location} {l}".format( binary=binary, E_location = os.path.abspath(tmpdir + "E.csv"), nG = str(len(E.columns)), nC = str(len(E.index)), output_location = output_location, l = str(l) ) print(command) sp.call(command, shell=True) bics = [] with open(os.path.abspath(output_location) , "r") as infile: lines = infile.readlines() for line1, line2, _ in zip(lines[::3], lines[1::3], lines[2::3]): if len(line1) > 0 and len(line2) > 0: gids = [int(gid) for gid in line1.strip().split(" ")] cids = [int(cid) for cid in line2.strip().split(" ")] bics.append(Bicluster(E.columns[gids].tolist(), E.index[cids].tolist())) return bics
def msbe(E, alpha=0.4, beta=0.5, gamma=1.2, refgene="random 500", refcond="random 20", **kwargs): with TemporaryDirectory() as tmpdir: standardize(E).to_csv(tmpdir + "/E.csv", sep="\t") binary = "sh " + os.environ[ "PERSOFTWARELOCATION"] + "/MSBE_linux_1.0.5/additiveBi" command = "{binary} {tmpdir}/E.csv {alpha} {beta} {gamma} {refgene} {refcond} {tmpdir}/results.txt".format( **locals()) sp.call(command, shell=True) bics = [] with open(tmpdir + "/results.txt", "r") as infile: lines = infile.readlines() for _, line1, line2 in zip(lines[::3], lines[1::3], lines[2::3]): if len(line1) > 0 and len(line2) > 0: print(line1, line2) cids = [int(cid) - 1 for cid in line1.strip().split(" ")] gids = [int(gid) - 1 for gid in line2.strip().split(" ")] bics.append( Bicluster(E.columns[gids].tolist(), E.index[cids].tolist())) return bics
def main(self): with TemporaryDirectory() as t: t = local.path(t) ukf = self.ukf fsindwi = self.fsindwi if nrrd(self.fsindwi): fsindwi = t / 'wmparcInDwi.nii.gz' ConvertBetweenFileFormats(self.fsindwi, fsindwi) if '.gz' in self.ukf.suffix: ukf = t / 'ukf.vtk' from plumbum.cmd import gunzip (gunzip['-c', self.ukf] > ukf)() tract_querier = local['tract_querier'] tract_math = local['tract_math'] ukfpruned = t / 'ukfpruned.vtk' # tract_math(ukf, 'tract_remove_short_tracts', '2', ukfpruned) tract_math[ukf, 'tract_remove_short_tracts', '2', ukfpruned] & FG if not ukfpruned.exists(): raise Exception( "tract_math failed to make '{}'".format(ukfpruned)) self.out.mkdir() tract_querier['-t', ukfpruned, '-a', fsindwi, '-q', self.query, '-o', self.out / '_'] & FG logging.info('Convert vtk field data to tensor data') for vtk in self.out.glob('*.vtk'): vtknew = vtk.dirname / (vtk.stem[2:] + ''.join(vtk.suffixes)) activateTensors_py(vtk, vtknew) vtk.delete()
def genomica(E, R, n=100, **kwargs): E_genomica = pd.DataFrame(np.vstack([np.array([["desc" for i in range(len(E.columns))], ["desc" for i in range(len(E.columns))]]), scale(E)]), index=["desc", "desc"] + E.index.tolist(), columns=E.columns).T E_genomica.index.name = "genes" with TemporaryDirectory() as tmpdir: E_genomica.to_csv(tmpdir + "/E.csv", sep="\t") R_genomica = {gid:g for gid, g in enumerate(E_genomica.index) if g in R and g in E.columns} with open(tmpdir + "/regulators.csv", "w") as outfile: outfile.write("\n".join([str(gid) for gid in R_genomica.keys()])) # PERSOFTWARELOCATION is the location in which the software is installed genomica_loc = os.environ["PERSOFTWARELOCATION"] + "/Genomica/new/" genomica_command = "cd {genomica_loc};java -XX:ParallelGCThreads=1 -Xmx12G -cp .:../Genomica.jar ExampleProgram ".format(**locals()) n = int(n) command = genomica_command + "{tmpdir}/E.csv {tmpdir}/regulators.csv {n} 10 > {tmpdir}/output.txt".format(**locals()) sp.call(command, shell=True) # postprocess output file state = "members" modules = [] modulenet = [] with open(tmpdir + "/output.txt") as infile: for line in infile.readlines(): line = line.rstrip() if line.startswith("Module members: "): moduleregulator_scores = defaultdict(float) state = "members" module = Module([E.columns[int(geneid)] for geneid in line[len("Module members: "):].split(" ")]) print("----") print(module) print(len(modules)) elif line.startswith("<<<<<PROGRAM"): state = "program" elif line.find("Regulator: ") > -1: line = line.split(",") regulatorid = int(line[0].split(" ")[-1]) #print(line[0].split(" ")[-1]) #print(R_genomica[regulatorid], regulatorid, float(line[-1].split(" ")[-1])) moduleregulator_scores[R_genomica[regulatorid]] += float(line[-1].split(" ")[-1]) elif line.startswith(">>>>>MODULE"): #print(moduleregulator_scores) modules.append(module) modulenet.append(moduleregulator_scores) modulenet = pd.DataFrame(modulenet, columns=R).fillna(0) return modules, modulenet
def computeWarp(image, target, out): from util.antspath import ComposeMultiTransform, antsRegistrationSyN_sh with TemporaryDirectory() as tmpdir: tmpdir = local.path(tmpdir) pre = tmpdir / 'ants' warp = pre + '1Warp.nii.gz' affine = pre + '0GenericAffine.mat' antsRegistrationSyN_sh['-m', image, '-f', target, '-o', pre, '-n', 32] & FG ComposeMultiTransform('3', out, '-R', target, warp, affine)
def main(self): with TemporaryDirectory() as tmpdir: tmpdir = local.path(tmpdir) pre = tmpdir / 'ants' rigidxfm = pre + '0GenericAffine.mat' antsRegistrationSyN_sh['-t', 'r', '-m', self.infile, '-f', self.target, '-o', pre, '-n', 32] & FG antsApplyTransforms('-d', '3', '-i', self.labelmap, '-t', rigidxfm, '-r', self.target, '-o', self.out, '--interpolation', 'NearestNeighbor')
def main(self): with TemporaryDirectory() as tmpdir: tmpdir = local.path(tmpdir) shortdwi = tmpdir / 'dwiShort.nii.gz' shortmask = tmpdir / 'maskShort.nii.gz' tmpdwi = tmpdir / 'dwi.nhdr' tmpdwimask = tmpdir / 'dwimask.nhdr' # TODO when UKFTractography supports float32, it should be removed # typecast to short short = load_nifti(self.dwi._path) save_nifti(shortdwi._path, short.get_data().astype('int16'), short.affine, short.header) short = load_nifti(self.dwimask._path) save_nifti(shortmask._path, short.get_data().astype('int16'), short.affine, short.header) # convert the dwi to NRRD nhdr_write(shortdwi._path, self.bvalFile._path, self.bvecFile._path, tmpdwi._path) # convert the mask to NRRD nhdr_write(shortmask._path, None, None, tmpdwimask._path) key_val_pair = [] if self.givenParams: key_val_pair = self.givenParams.split(',') for i in range( 0, len(ukfdefaults) - 1, 2 ): # -1 to pass --recordTensors which is always a default try: ind = key_val_pair.index(ukfdefaults[i]) ukfdefaults[i + 1] = key_val_pair[ind + 1] # since ukfdefault[i+1] has been already replaced by key_val_pair[ind+1] # remove key_val_pair[ind] and [ind+1] to prevent double specification key_val_pair[ind:ind + 2] = [] except ValueError: pass params = [ '--dwiFile', tmpdwi, '--maskFile', tmpdwimask, '--seedsFile', tmpdwimask, '--tracts', self.out ] + list(ukfdefaults) + key_val_pair logging.info('Peforming UKF tractography of {}'.format(tmpdwi)) UKFTractography[params] & FG
def main(self): with TemporaryDirectory() as tmpdir: tmpdir = local.path(tmpdir) bse = tmpdir / "maskedbse.nrrd" t2masked = tmpdir / "maskedt2.nrrd" t2inbse = tmpdir / "t2inbse.nrrd" epiwarp = tmpdir / "epiwarp.nii.gz" t2tobse_rigid = tmpdir / "t2tobse_rigid" logging.info('1. Extract and mask the DWI b0') bse_py('-m', self.dwimask ,'-i', self.dwi ,'-o', bse) logging.info("2. Mask the T2") unu("3op", "ifelse", self.t2mask, self.t2, "0", "-o", t2masked) logging.info( "3. Compute a rigid registration from the T2 to the DWI baseline") antsRegistrationSyN_sh("-d", "3" ,"-f", bse ,"-m", t2masked ,"-t", "r" ,"-o", tmpdir / "t2tobse_rigid") antsApplyTransforms("-d", "3" ,"-i", t2masked ,"-o", t2inbse ,"-r", bse ,"-t", tmpdir / "t2tobse_rigid0GenericAffine.mat") logging.info("4. Compute 1d nonlinear registration from the DWI to the T2 along the phase direction") moving = bse fixed = t2inbse pre = tmpdir / "epi" dwiepi = tmpdir / "dwiepi"+getext(self.out) antsRegistration("-d", "3" ,"-m", "cc["+fixed+","+moving+",1,2]" ,"-t", "SyN[0.25,3,0]" ,"-c", "50x50x10" ,"-f", "4x2x1" , "-s", "2x1x0" ,"--restrict-deformation", "0x1x0" ,"-v", "1" ,"-o", pre) local.path(pre+"0Warp.nii.gz").move(epiwarp) logging.info("5. Apply warp to the DWI") antsApplyTransformsDWI_sh(self.dwi, self.dwimask, epiwarp, dwiepi) if getext(dwiepi) == '.nhdr': unu("save","-e","gzip","-f","nrrd","-i",dwiepi,self.out) else: dwiepi.move(self.out) if self.debug: tmpdir.move("epidebug-"+getpid())
def main(self): fshome = local.path(os.getenv('FREESURFER_HOME')) if not fshome: logging.error('Set FREESURFER_HOME first.') sys.exit(1) with TemporaryDirectory() as tmpdir: tmpdir = local.path(tmpdir) b0masked = tmpdir / "b0masked.nrrd" b0masked1mm = tmpdir / "b0masked1mm.nrrd" brain = tmpdir / "brain.nii.gz" wmparc = tmpdir / "wmparc.nii.gz" brainmgz = self.parent.fsdir / 'mri/brain.mgz' wmparcmgz = self.parent.fsdir / 'mri/wmparc.mgz' wmparcindwi1mm = tmpdir / 'wmparcInDwi1mm.nii.gz' logging.info( "Make brain.nii.gz and wmparc.nii.gz from their mgz versions") vol2vol = local[fshome / 'bin/mri_vol2vol'] label2vol = local[fshome / 'bin/mri_label2vol'] with local.env(SUBJECTS_DIR=''): vol2vol('--mov', brainmgz, '--targ', brainmgz, '--regheader', '--o', brain) label2vol('--seg', wmparcmgz, '--temp', brainmgz, '--regheader', wmparcmgz, '--o', wmparc) logging.info('Extract B0 from DWI and mask') bse_py('-i', self.parent.dwi, '-m', self.parent.dwimask, '-o', b0masked) logging.info('Made masked B0') logging.info('Upsample masked baseline to 1x1x1') ResampleImageBySpacing('3', b0masked, b0masked1mm, '1', '1', '1') logging.info('Made 1x1x1 baseline') logging.info('Register wmparc to B0') pre = tmpdir / 'fsbrain_to_b0' affine = pre + '0GenericAffine.mat' warp = pre + '1Warp.nii.gz' antsRegistrationSyNMI_sh['-m', brain, '-f', b0masked1mm, '-o', pre, '-n', 32] & FG antsApplyTransforms('-d', '3', '-i', wmparc, '-t', warp, affine, '-r', b0masked1mm, '-o', wmparcindwi1mm, '--interpolation', 'NearestNeighbor') logging.info('Made ' + wmparcindwi1mm) logging.info('Make output directory') self.parent.out.mkdir() b0masked.copy(self.parent.out) b0masked1mm.copy(self.parent.out) wmparcindwi1mm.copy(self.parent.out)
def main(self): with TemporaryDirectory() as tmpdir, local.cwd(tmpdir): tmpdir = local.path(tmpdir) dicePrefix = 'dwi' logging.info("Dice DWI") (unu['convert', '-t', 'int16', '-i', self.dwi] | \ unu['dice','-a','3','-o',dicePrefix]) & FG logging.info("Apply warp to each DWI volume") vols = sorted(tmpdir // (dicePrefix + '*')) volsWarped = [] for vol in vols: if self.dwimask: unu('3op', 'ifelse', self.dwimask, vol, '0', '-o', vol) volwarped = vol.stem + '-warped.nrrd' WarpImageMultiTransform('3', vol, volwarped, '-R', vol, self.xfm) unu('convert', '-t', 'int16', '-i', volwarped, '-o', volwarped) volsWarped.append(volwarped) logging.info("Join warped volumes together") (unu['join', '-a', '3', '-i', volsWarped] | \ unu['save', '-e', 'gzip', '-f', 'nrrd'] | \ unu['data','-'] > 'tmpdwi.raw.gz') & FG logging.info( "Create new nrrd header pointing to the newly generated data file" ) unu('save', '-e', 'gzip', '-f', 'nrrd', '-i', self.dwi, '-o', 'dwi.nhdr') with open("dwi.nhdr", "r") as hdr: lines = hdr.readlines() with open("dwi.nhdr", "w") as hdr: for line in lines: hdr.write( re.sub(r'^data file:.*$', 'data file: tmpdwi.raw.gz', line)) logging.info('Make ' + str(self.out)) unu('save', '-e', 'gzip', '-f', 'nrrd', '-i', 'dwi.nhdr', '-o', self.out) logging.info('Made ' + str(self.out)) if self.debug: from os import getpid pid = str(getpid()) d = local.path(self.out.dirname / ('antsApplyTransformsDWi-' + pid)) tmpdir.copy(d)
def main(self): fshome = local.path(os.getenv('FREESURFER_HOME')) if not fshome: logging.error('Set FREESURFER_HOME first.') sys.exit(1) if not self.force and self.out.exists(): logging.error( 'Output directory exists, use -f/--force to force an overwrite.' ) sys.exit(1) with TemporaryDirectory() as tmpdir, local.env(SUBJECTS_DIR=tmpdir, FSFAST_HOME='', MNI_DIR=''): if self.t1mask: logging.info('Mask the t1') ImageMath('3', tmpdir / 't1masked.nii.gz', 'm', self.t1, self.t1mask) t1 = tmpdir / 't1masked.nii.gz' skullstrip = '-noskullstrip' else: skullstrip = '' if '.nrrd' in self.t1.suffixes or '.nhdr' in self.t1.suffixes: logging.info('t1 is in nrrd format, convert to nifti') t1 = tmpdir / 't1.nii.gz' ConvertBetweenFileFormats(self.t1, t1) logging.info("Run freesurfer on " + t1) subjid = t1.stem from plumbum.cmd import bash bash['-c', 'source ' + fshome + '/FreeSurferEnv.sh; recon-all -s ' + subjid + ' -i ' + t1 + ' -autorecon1 ' + skullstrip] & FG (tmpdir / subjid / 'mri/T1.mgz').copy(tmpdir / subjid / 'mri/brainmask.mgz') bash['-c', 'source ' + fshome + '/FreeSurferEnv.sh; recon-all -autorecon2 -subjid ' + subjid] & FG bash['-c', 'source ' + fshome + '/FreeSurferEnv.sh; recon-all -autorecon3 -subjid ' + subjid] & FG logging.info("Freesurfer done.") (tmpdir / subjid).copy(self.out, override=True) # overwrites existing logging.info("Made " + self.out)
def main(self): with TemporaryDirectory() as tmpdir: tmpdir = local.path(tmpdir) pre = tmpdir / 'ants' rigidxfm = pre + '0GenericAffine.mat' check_call((' ').join([ pjoin(FILEDIR, 'antsRegistrationSyNMI.sh'), '-f', self.target, '-m', self.infile, '-t', 'r', '-o', pre, '-n', ANTSREG_THREADS ]), shell=True) antsApplyTransforms['-d', '3', '-i', self.labelmap, '-t', rigidxfm, '-r', self.target, '-o', self.out, '--interpolation', 'NearestNeighbor'] & FG
def transitivity(E, threshold=0.1, simdist_function="pearson_correlation", cutoff=-1, **kwargs): similarities = simdist(E, simdist_function, **kwargs) with TemporaryDirectory() as tmpdir: #tmpdir = "../tmp/" # save similarity and cost files # similarity file is only required for fuzzy clustering with open(tmpdir + "/sim.tsv", "w") as outfile: for i, (g1, col) in enumerate(similarities.iteritems()): for j, (g2, value) in enumerate(col.iteritems()): outfile.write(g1 + "\t" + g2 + "\t" + str(value) + "\n") cost = similarities.copy() cost.values[cost.values < threshold] = threshold - 1 cost = cost - threshold with open(tmpdir + "/cost.tsv", "w") as outfile: outfile.write(str(cost.shape[0]) + "\n") outfile.write("\n".join(cost.index) + "\n") for i, (j, row) in zip(range(cost.shape[0], 1, -1), cost.iterrows()): outfile.write("\t".join(row.astype(str)[-i + 1:]) + "\n") if cutoff == -1: fuzzytext = "" resultsfile = "results.tsv" else: fuzzytext = " -fuzzy " + str(cutoff) resultsfile = "results.tsv_fuzzy" # run the transitivity clustering tool command = "java -jar " + os.environ[ "PERSOFTWARELOCATION"] + "/TransClust.jar -i {tmpdir}/cost.tsv -o {tmpdir}/results.tsv -verbose -sim {tmpdir}/sim.tsv {fuzzytext}".format( **locals()) sp.call(command, shell=True) results = pd.read_csv(tmpdir + "/" + resultsfile, sep="\t", squeeze=True, index_col=0) modules = [[] for i in range(results.max())] for g, moduleid in results.iteritems(): if moduleid > 0: modules[moduleid - 1].append(g) return modules
def main(self): if not self.force and self.out.exists(): logging.error("'{}' already exists, use '--force' to force overwrite.".format(self.out)) sys.exit(1) with TemporaryDirectory() as tmpdir: tmpdir = local.path(tmpdir) bse = tmpdir / "maskedbse.nrrd" t2masked = tmpdir / "maskedt2.nrrd" t2inbse = tmpdir / "t2inbse.nrrd" epiwarp = tmpdir / "epiwarp.nii.gz" t2tobse_rigid = tmpdir / "t2tobse_rigid" logging.info('1. Extract and mask the DWI b0') bse_py('-m', self.dwimask, '-i', self.dwi, '-o', bse) logging.info("2. Mask the T2") unu("3op", "ifelse", self.t2mask, self.t2, "0", "-o", t2masked) logging.info( "3. Compute a rigid registration from the T2 to the DWI baseline") antsRegistrationSyN_sh("-d", "3", "-f", bse, "-m", t2masked, "-t", "r", "-o", tmpdir / "t2tobse_rigid") antsApplyTransforms("-d", "3", "-i", t2masked, "-o", t2inbse, "-r", bse, "-t", tmpdir / "t2tobse_rigid0GenericAffine.mat") logging.info( "4. Compute 1d nonlinear registration from the DWI to the T2 along the phase direction") moving = bse fixed = t2inbse pre = tmpdir / "epi" dwiepi = tmpdir / ("dwiepi" + ''.join(self.out.suffixes)) antsRegistration("-d", "3", "-m", "cc[" + str(fixed) + "," + str(moving) + ",1,2]", "-t", "SyN[0.25,3,0]", "-c", "50x50x10", "-f", "4x2x1", "-s", "2x1x0", "--restrict-deformation", "0x1x0", "-v", "1", "-o", pre) local.path(str(pre) + "0Warp.nii.gz").move(epiwarp) logging.info("5. Apply warp to the DWI") antsApplyTransformsDWI_py('-i', self.dwi, '-m', self.dwimask, '-t', epiwarp, '-o', dwiepi) if '.nhdr' in dwiepi.suffixes: unu("save", "-e", "gzip", "-f", "nrrd", "-i", dwiepi, self.out) else: dwiepi.move(self.out) if self.debug: tmpdir.copy(self.out.dirname / ("epidebug-" + str(getpid())))
def computeWarp(image, target, out): with TemporaryDirectory() as tmpdir: tmpdir = local.path(tmpdir) pre = tmpdir / 'ants' warp = pre + '1Warp.nii.gz' affine = pre + '0GenericAffine.mat' # pre is the prefix (directory) for saving 1Warp.nii.gz and 0GenericAffine.mat antsReg(target, None, image, pre) # out is Warp{idx}.nii.gz, saved in the specified output direcotry # ComposeMultiTransform combines the 1Warp.nii.gz and 0GenericAffine.mat into a Warp{idx}.nii.gz file ComposeMultiTransform('3', out, '-R', target, warp, affine)
def fuseAvg(labels, out, target_header): from plumbum.cmd import AverageImages with TemporaryDirectory() as tmpdir: nii = local.path(tmpdir) / 'avg.nii.gz' AverageImages['3', nii, '0', labels] & FG img= load_nifti(nii._path) # Binary operation, if out>0.5, pipe the output and save as {labelname}.nii.gz # out is {labelname}.nii.gz save_nifti(out, ((img.get_data()>0.5)*1).astype('uint8'), target_header.get_best_affine(), target_header) print("Made labelmap: " + out)
def fuseAvg(labels, out): from plumbum.cmd import AverageImages with TemporaryDirectory() as tmpdir: nii = local.path(tmpdir) / 'avg.nii.gz' AverageImages['3', nii, '0', labels] & FG # out is {labelname}.nrrd ConvertBetweenFileFormats(nii, out) # Binary operation, if out>0.5, pipe the output and save as {labelname}.nrrd (unu['2op', 'gt', out, '0.5'] | \ unu['save', '-e', 'gzip', '-f', 'nrrd', '-o', out]) & FG print("Made labelmap: " + out)
def main(self): repo = 'https://github.com/demianw/tract_querier.git' with TemporaryDirectory() as tmpdir: clone = local.path(tmpdir) / "tract_querier" if not self.githash: git("clone", "--depth", "1", repo, clone) else: git("clone", repo, clone) clone_hash = git("rev-parse", "--short", "HEAD")[:-1] # remove trailing \n # save 70M of space rm('-r', clone / 'doc') rm('-r', clone / '.git') out = self.prefix / "tract_querier-" + clone_hash clone.move(out) chmod('-R', 'a-w', out)
def main(self): with TemporaryDirectory() as tmpdir, local.cwd(tmpdir): tmpdir = local.path(tmpdir) dicePrefix = 'vol' logging.info("Dice DWI") fslsplit[self.dwi] & FG logging.info("Apply warp to each DWI volume") vols = sorted(tmpdir // (dicePrefix + '*.nii.gz')) # use the following multi-processed loop pool= Pool(int(self.nproc)) res= [] for vol in vols: res.append(pool.apply_async(_WarpImage, (self.dwimask, vol, self.xfm))) volsWarped= [r.get() for r in res] pool.close() pool.join() # or use the following for loop # volsWarped = [] # for vol in vols: # if self.dwimask: # fslmaths[vol, '-mas', self.dwimask, vol] # volwarped = vol.stem + '-warped.nrrd' # WarpImageMultiTransform('3', vol, volwarped, '-R', vol, self.xfm) # volsWarped.append(volwarped) logging.info("Join warped volumes together") volsWarped.sort() fslmerge['-t', self.out, volsWarped] & FG logging.info('Made ' + str(self.out)) if self.debug: from os import getpid pid = str(getpid()) d = local.path(self.out.dirname / ('antsApplyTransformsDWi-' + pid)) tmpdir.copy(d)
def main(self): if self.out.exists() and not self.force: logging.error( "'{}' already exists, use --force to force overwrite") import sys sys.exit(1) with TemporaryDirectory() as tmpdir: #nii = tmpdir / 'dwi.nii.gz' if nrrd(self.dwi): bse = tmpdir / 'bse.nrrd' bse_py['-i', self.dwi, '-o', bse] & FG bsenii = tmpdir / 'bse.nii.gz' ConvertBetweenFileFormats[bse, bsenii] & FG bet[bsenii, tmpdir / 'dwi', '-m', '-f', self.threshold] & FG else: #nifti bet[self.dwi, tmpdir / 'dwi', '-m', '-f', self.threshold] & FG ConvertBetweenFileFormats[tmpdir / 'dwi_mask.nii.gz', self.out] & FG
def main(self): with TemporaryDirectory() as tmpdir, local.cwd(tmpdir): repo = downloadGithubRepo('demianw/tract_querier', self.parent.commit) sha, date = getCommitInfo(repo) # save space (repo / 'doc').delete() (repo / '.git').delete() outdir = local.path(self.parent.dest / 'tract_querier-' + sha) if outdir.exists(): logging.warning(outdir + ' already exists, quitting.') sys.exit(0) logging.info("Make '{outdir}'".format(**locals())) repo.move(outdir) chmod('-R', 'a-w', outdir) chmod('a-w', outdir) date_symlink = self.parent.dest / 'tract_querier-' + date outdir.symlink(date_symlink)
def biforce(E, m="o", t=0, **kwargs): with TemporaryDirectory() as tmpdir: E_location = tmpdir + "/E.csv" output_location = tmpdir + "/output.txt" standardize(E).T.to_csv(tmpdir + "/E.csv", index=False, header=False, sep="\t") binary = "java -XX:ParallelGCThreads=1 -Xmx12G -jar mbiforce.jar" command = "{binary} -i={i} -o={o} -m={m} -h=false -t={t}".format( binary=binary, i=os.path.abspath(E_location), o=os.path.abspath(output_location), m=m, t=str(t)) original_wd = os.getcwd() try: os.chdir( os.environ["PERSOFTWARELOCATION"] + "/BiForceV2/" ) # change working directory because biclue only looks for the parameter.ini file in the current working directory (...) sp.call(command, shell=True) except BaseException as e: raise e finally: os.chdir(original_wd) bics = [] with open(output_location) as infile: for line in infile.readlines()[1:-1]: line = line.strip().split(",") genes = [] conditions = [] for rowcol in line: if rowcol[0] == "R": genes.append(E.columns[int(rowcol[1:]) - 1]) elif rowcol[0] == "C": conditions.append(E.index[int(rowcol[1:]) - 1]) bics.append(Bicluster(genes, conditions)) return bics
def main(self): with TemporaryDirectory() as tmpdir: tmpdir = local.path(tmpdir) pre = tmpdir / 'ants' warp = pre + '1Warp.nii.gz' affine = pre + '0GenericAffine.mat' check_call((' ').join([ pjoin(FILEDIR, 'antsRegistrationSyNMI.sh'), '-f', self.target, '-m', self.infile, '-t r' if self.reg_method == 'rigid' else '', '-o', pre ]), shell=True) xfrms = f'-t {warp} -t {affine}' if self.reg_method == 'SyN' else f'-t {affine}' antsApplyTransforms['-d', '3', '-i', self.labelmap, xfrms.split(), '-r', self.target, '-o', self.out, '--interpolation', 'NearestNeighbor'] & FG
def merlin(E,R, h=0.6, p=5, r=4, initial_number=300, **kwargs): if E.min().min() <= 0: E = E + np.abs(E.min().min()) + 0.1 print(E.min().min()) R = sorted(list(set(R) & set(E.columns))) with TemporaryDirectory() as tmpdir: E.to_csv(tmpdir + "/E.csv", sep="\t", index=False) with open(tmpdir + "/R.csv", "w") as outfile: outfile.write("\n".join(R)) modules = agglom(E, number=initial_number) labels = convert_modules2labels(modules, E.columns) with open(tmpdir + "/clusterassign.csv", "w") as outfile: outfile.write("\n".join([g + "\t" + str(label) for g,label in labels.items()])) # PERSOFTWARELOCATION is the location in which the software is installed binary = os.environ["PERSOFTWARELOCATION"] + "/gpdream/modules/Merlin/src/merlin" command = "{binary} -d {tmpdir}/E.csv -o {tmpdir} -l {tmpdir}/R.csv -c {tmpdir}/clusterassign.csv -v 1 -h {h} -k 300 -p {p} -r {r}".format(**locals()) print(command) sp.call(command, shell=True) labels = pd.read_csv(tmpdir + "/fold0/modules.txt", sep="\t", squeeze=True, index_col=0, header=None) modules = convert_labels2modules(labels, labels.index) print(labels) netable = pd.read_csv(tmpdir + "/fold0/prediction_k300.txt", sep="\t", names=["regulator", "target", "score"]) wnet = pd.DataFrame(0, columns=R, index=E.columns) for i, (regulator, target, score) in netable.iterrows(): wnet.ix[target, regulator] = score return modules, wnet
def fuseWeightedAvg(labels, weights, out): # for each label, fuse warped labelmaps to compute output labelmap print("Apply weights to warped training {} et al., fuse, and threshold".format(labels[0])) init= True for label, w in zip(labels, weights): img= nib.load(str(label)) if init: data=img.get_data()*w affine= img.affine init= False else: data+=img.get_data()*w data= (data>0.5)*1 with TemporaryDirectory() as tmpdir: nii = local.path(tmpdir) / 'avg.nii.gz' Nifti1Image= nib.Nifti1Image(data, affine= affine, header= img.header) nib.save(Nifti1Image, str(nii)) ConvertBetweenFileFormats(nii, out) print("Made labelmap: " + out)
def main(self): if self.out.exists() and not self.force: logging.error( "'{}' already exists, use --force to force overwrite") sys.exit(1) with TemporaryDirectory() as tmpdir: if nrrd(self.dwi): bse = tmpdir / 'bse.nrrd' bse_py['-i', self.dwi, '-o', bse] & FG bsenii = tmpdir / 'bse.nii.gz' ConvertBetweenFileFormats[bse, bsenii] & FG bet[bsenii, tmpdir / 'dwi', '-m', '-f', self.threshold] & FG else: # nifti # FSL 6.0.1, unlike <=5.0.11, doesn't create only one mask corresponding to b0 extracted from given DWI # hence we need to provide baseline image to bet like we did for nrrd above bsenii = tmpdir / 'bse.nii.gz' bse_py['-i', self.dwi, '-o', bsenii] & FG bet[bsenii, tmpdir / 'dwi', '-m', '-f', self.threshold] & FG ConvertBetweenFileFormats[tmpdir / 'dwi_mask.nii.gz', self.out] & FG