natverse / neuprintr

R client utilities for interacting with the neuPrint connectome analysis service
http://natverse.org/neuprintr
3 stars 3 forks source link

Neuron mesh download #129

Closed CesiumChloride closed 3 years ago

CesiumChloride commented 4 years ago

Hello!

Is there a way to download neuron mesh from FlyEM database with neuprintr? Thanks!

jefferis commented 4 years ago

Thanks for your interest and sorry for slow response. We do not have a way to do this in neuprintr (in fact neuprint does not contain the meshes). This functionality will probably appear soon in another package, fafbseg. See https://github.com/natverse/fafbseg/issues/18.

You can already download the hemibrain meshes using https://github.com/seung-lab/cloud-volume, a python tool, thanks to recent work from @perlman. I have pasted below (snippet 2) a python script lightly edited from something he gave me that will do the job, saving as wavefront obj format . If you want to do further analysis in R/natverse, you can read them in using snippet 3 below.

1. get ids for DA2 and DM6 PNs

> dput(neuprintr::neuprint_ids("/(DM6|DA2).*PN"))
c("1796817841", "1827516355", "1797505019", "818983130", "1796818119", 
"1764091241", "788306624", "5813070872", "5813039235")

3. read/plot neurons

# if required ...
# install.packages("readobj")
library(nat)
meshpaths=dir("/path/to/save/my/meshes", pattern = "obj$", full.names = T)
# extract bodyid from path
names(meshpaths)=tools::file_path_sans_ext(basename(meshpaths))
# read in the meshes in rgl::mesh3d format
# [[1]] extracts the first (and in this case only) object saved in obj file
meshes=nlapply(meshpaths, function(p) readobj::read.obj(p, convert.rgl = T)[[1]])
meshes[,]=neuprintr::neuprint_get_meta(names(meshes))
# clear the 3d screen
nclear3d()
# select a subset of neurons to plot
# NB you need add=T
plot3d(meshes, grepl("DM6", type), add=TRUE)
nclear3d()
# plot wireframes rather than solid mesh
plot3d(meshes, grepl("DM6", type), add=TRUE, type='wire')
# colour by type, speed up by not redrawing the screen after every neuron
plot3d(meshes, col=type, add=T, skipRedraw = T)

2. fetch meshes


import os
import logging
import cloudvolume
import numpy as np

# DM6 PNs
# see MBONIstvanDN.Rmd
segids = ["1764091241", "788306624", "5813070872", "5813039235"]

os.chdir("/path/to/save/my/meshes")
chosenlod=2

def main():
    logging.basicConfig(level=logging.DEBUG) 

    vol = cloudvolume.CloudVolume('https://storage.googleapis.com/neuroglancer-janelia-flyem-hemibrain/v1.0/segmentation', mip=0, cache=False)

    for segid in segids:
        fragdir= "%d" % (chosenlod)
        os.makedirs(fragdir, exist_ok=True)
        outfile=os.path.join(fragdir, "%s.obj" % segid)
        # don't overwrite by default
        if os.path.exists(outfile) :
            print("Path %s already exists" %outfile)
            continue
        meshes = vol.mesh.get(int(segid), lod=chosenlod)
        mesh = meshes[int(segid)]
        print(mesh)
        with open(outfile, "wb") as f:
            f.write(mesh.to_obj())

if __name__ == "__main__":
    main()
jefferis commented 4 years ago

note also the same question and similar answer at https://github.com/connectome-neuprint/neuprint-python/issues/17

jefferis commented 3 years ago

With the current version of nat from github, the plotting in section 3 is simplified

library(nat)
# pattern is optional but may be helpful if you have different things in the folder
meshes=read.neurons("/path/to/save/my/meshes", pattern = "obj$")
nclear3d()
# select a subset of neurons to plot
plot3d(meshes, grepl("DM6", type))
nclear3d()
# plot wireframes rather than solid mesh
plot3d(meshes, grepl("DM6", type), type='wire')
# colour by type, speed up by not redrawing the screen after every neuron
plot3d(meshes, col=type, add=T)
jefferis commented 3 years ago

@SridharJagannathan

jefferis commented 3 years ago

Closing since hemibrainr can read meshes, see https://natverse.org/hemibrainr/reference/hemibrain_neuron_meshes.html