예제 #1
0
def fetch_aligned_morphology(specimen_id=None, swc_file=None):
    if specimen_id:
        swc_file, m0 = fetch_cell(specimen_id)
        morphology = swc.read_swc(swc_file)

        t0 = np.eye(4)
        t0[:, 3] = -np.dot(m0, [
            morphology.root['x'], morphology.root['y'], morphology.root['z'], 1
        ])
        r = xform.rotate3x(math.radians(-90))
    else:
        swc_file = swc_file
        morphology = swc.read_swc(swc_file)
        m0 = np.eye(4)
        t0 = xform.translate3(-morphology.root['x'], -morphology.root['y'],
                              -morphology.root['z'])

        r = xform.rotate3x(math.radians(90))

    sz = xform.scale3(1, 1, 3)
    sm = xform.scale3(.001, .001, .001)

    m = np.dot(sm, np.dot(np.dot(r, np.dot(sz, t0)), m0))

    morphology = make_transformed_morphology(morphology, m)

    # rotate the apical
    ra = align_apical_dendrite(morphology)

    morphology = make_transformed_morphology(morphology, ra, .002)

    return morphology
예제 #2
0
 def __init__(self, morph_file, cell_id=None):
     self.morph_file = morph_file
     self.morph = swc.read_swc(morph_file)
     self.soma_coord = np.array(
         [self.morph.soma["x"], self.morph.soma["y"], self.morph.soma["z"]])
     self.soma_rad = self.morph.soma['radius']
     self.cell_id = cell_id
예제 #3
0
    def get_reconstruction(self, specimen_id, file_name=None):
        """
        Download and open a reconstruction for a single cell in the database.

        Parameters
        ----------
        
        specimen_id: int
            The ID of a cell specimen to download.

        file_name: string
            File name to save/read the reconstruction SWC.  
            If file_name is None, the file_name will be pulled out of the 
            manifest.  If caching is disabled, no file will be saved. 
            Default is None.

        Returns
        -------
        Morphology
             A class instance with methods for accessing morphology compartments.
        """

        file_name = self.get_cache_path(file_name, self.RECONSTRUCTION_KEY, specimen_id)

        if file_name is None:
            raise Exception("Please enable caching (CellTypes.cache = True) or specify a save_file_name.")

        if not os.path.exists(file_name):
            self.api.save_reconstruction(specimen_id, file_name)

        return swc.read_swc(file_name)
예제 #4
0
def _morph_layer_density(specimen_id, layer_list=None):
    layer_list = layer_list or ["1", "2/3", "4", "5", "6a", "6b"]

    df = layer_counts.query_lims_for_layers(specimen_id)
    try:
        soma_coords, pia_coords, wm_coords, layer_coords = layer_counts.get_coords(
            df, layer_list)
    except Exception:
        return None

    if None in [soma_coords, pia_coords, wm_coords, layer_coords]:
        return None

    ld = layer_counts.layer_depths(layer_coords, soma_coords, pia_coords,
                                   wm_coords)

    swc_filename, swc_path = lims_utils.get_swc_from_lims(specimen_id)

    morph = swc.read_swc(swc_path)

    soma_layer = soma_layer_from_morph(morph, layer_coords, soma_coords,
                                       layer_list)

    ax, dend, apic = layer_counts.compartment_counts_by_layer(
        morph, layer_coords, soma_coords, layer_list)

    all_dend = {k: dend[k] + apic[k] for k in dend}

    return {
        'soma_layer': soma_layer,
        'ax': ax,
        'dend': dend,
        'apic': apic,
        'all_dend': all_dend,
    }
예제 #5
0
def check_swc_for_apical(morph_path):
    morphology = swc.read_swc(morph_path)
    no_apical = True
    for n in morphology.compartment_list:
        if n['type'] == 4:
            no_apical = False
            break
    return no_apical
예제 #6
0
def validate_swc(swc_file):
    """ 
    To be compatible with NEURON, SWC files must have the following properties:
        1) a single root node with parent ID '-1'
        2) sequentially increasing ID numbers
        3) immediate children of the soma cannot branch
    """
    soma_id = swc.Morphology.SOMA
    morphology = swc.read_swc(swc_file)
    # verify that there is a single root node
    num_soma_nodes = sum([(int(c['type']) == soma_id)
                          for c in morphology.compartment_list])
    if num_soma_nodes != 1:
        raise Exception("SWC must have single soma compartment.  Found: %d" %
                        num_soma_nodes)
    # sanity check
    root = morphology.root
    if root is None:
        raise Exception("Morphology has no root node")
    # verify that children of the root have max one child
    for root_child_id in root['children']:
        root_child = morphology.compartment_index[root_child_id]
        num_grand_children = len(root_child['children'])
        if num_grand_children > 1:
            raise Exception("Child of root (%s) has more than one child (%d)" %
                            (root_child_id, num_grand_children))
    # get a list of all of the ids, make sure they are unique while we're at it
    all_ids = set()
    for compartment in morphology.compartment_list:
        iid = int(compartment["id"])
        if iid in all_ids:
            raise Exception("Compartment ID %s is not unique." %
                            compartment["id"])
        pid = int(compartment["parent"])
        if iid < pid:
            raise Exception(
                "Compartment (%d) has a smaller ID that its parent (%d)" %
                (iid, pid))
        all_ids.add(iid)

    # sort the ids and make sure there are no gaps
    sorted_ids = sorted(all_ids)
    for i in xrange(1, len(sorted_ids)):
        if sorted_ids[i] - sorted_ids[i - 1] != 1:
            raise Exception("Compartment IDs are not sequential")
    return True
예제 #7
0
 def get_reconstruction(self):
     '''
     Checks for a morphology .swc file associated with the biophysical model.
     If such a file is found, uses the AllenSDK swc helper functions to return a Morphology instance.
     
     Returns
     -------
     Morphology
         a morphology instance if a morphology file is found associated with the model. None otherwise.
     '''
     file_name = self.bp.ids['morphology'].values()[0]
     if len(file_name) > 0:
         file_name = os.path.join(os.getcwd(), self.model_dir, file_name)
         return swc.read_swc(file_name)
     else:
         print "Could not find morphology file for model ", self.model_id, ", please check directory structure and try again."
         return None
예제 #8
0
def validate_swc(swc_file):
    """
    To be compatible with NEURON, SWC files must have the following properties:
        1) a single root node with parent ID '-1'
        2) sequentially increasing ID numbers
        3) immediate children of the soma cannot branch
    """
    soma_id = swc.Morphology.SOMA
    morphology = swc.read_swc(swc_file)
    # verify that there is a single root node
    num_soma_nodes = sum([(int(c['type']) == soma_id)
                          for c in morphology.compartment_list])
    if num_soma_nodes != 1:
        raise Exception(
            "SWC must have single soma compartment.  Found: %d" % num_soma_nodes)
    # sanity check
    root = morphology.root
    if root is None:
        raise Exception("Morphology has no root node")
    # verify that children of the root have max one child
    for root_child_id in root['children']:
        root_child = morphology.compartment_index[root_child_id]
        num_grand_children = len(root_child['children'])
        if num_grand_children > 1:
            raise Exception("Child of root (%s) has more than one child (%d)" % (
                root_child_id, num_grand_children))
    # get a list of all of the ids, make sure they are unique while we're at it
    all_ids = set()
    for compartment in morphology.compartment_list:
        iid = int(compartment["id"])
        if iid in all_ids:
            raise Exception("Compartment ID %s is not unique." %
                            compartment["id"])
        pid = int(compartment["parent"])
        if iid < pid:
            raise Exception(
                "Compartment (%d) has a smaller ID that its parent (%d)" % (iid, pid))
        all_ids.add(iid)

    # sort the ids and make sure there are no gaps
    sorted_ids = sorted(all_ids)
    for i in xrange(1, len(sorted_ids)):
        if sorted_ids[i] - sorted_ids[i - 1] != 1:
            raise Exception("Compartment IDs are not sequential")
    return True
예제 #9
0
def write_morphology_vtk(vtk_file_name, cells, manifest):
    points = []

    pds = []
    for cell in cells:
        swc_path = manifest.get_path('MORPHOLOGY_%s' % cell['type'])
        morphology = swc.read_swc(swc_path)
        root = morphology.root
        rootp = {'x': root['x'], 'y': root['y'], 'z': root['z']}

        for compartment in morphology.compartment_list:
            compartment['x'] = compartment['x'] - rootp['x'] + float(cell['x'])
            compartment['y'] = compartment['y'] - rootp['y'] + float(cell['y'])
            compartment['z'] = compartment['z'] - rootp['z'] + float(cell['z'])

        pd = tube.generate_polydata(morphology.compartment_index,
                                    morphology.root)

        pds.append(pd)

    pd = tube.merge_polydata(pds)
    tube.save_polydata(pd, vtk_file_name)
예제 #10
0
def write_morphology_vtk(vtk_file_name, cells, manifest):
    points = []

    pds = []
    for cell in cells:
        swc_path = manifest.get_path('MORPHOLOGY_%s' % cell['type'])
        morphology = swc.read_swc(swc_path)
        root = morphology.root
        rootp = { 'x': root['x'], 'y': root['y'], 'z': root['z'] }

        for compartment in morphology.compartment_list:
            compartment['x'] = compartment['x'] - rootp['x'] + float(cell['x'])
            compartment['y'] = compartment['y'] - rootp['y'] + float(cell['y'])
            compartment['z'] = compartment['z'] - rootp['z'] + float(cell['z'])

        pd = tube.generate_polydata(morphology.compartment_index,
                                    morphology.root)

        pds.append(pd)

    pd = tube.merge_polydata(pds)
    tube.save_polydata(pd, vtk_file_name)
예제 #11
0
################################################################################
# Morphology
# Morphological neuron reconstructions are available for download as SWC files. The SWC file format is a white-space delimited text file with a standard set of headers. The file lists a set of 3D neuronal compartments, each of which has:
# Column	Data Type	Description
# id	string	compartment ID
# type	integer	compartment type
# x	float	3D compartment position (x)
# y	float	3D compartment position (y)
# z	float	3D compartment position (z)
# radius	float	compartment radius
# parent	string	parent compartment ID
# Comment lines begin with a ‘#’. Reconstructions in the Allen Cell Types Database can contain the following compartment types:
################################################################################
file_name = 's-1-1.swc'
morphology = swc.read_swc(file_name)

# the compartment list has all of the nodes in the file
# print soma compartment data
if True:
    import pprint
    pprint.pprint(morphology.compartment_list)

    fig, axes = plt.subplots(1, 2, sharey=True, sharex=True)
    # Make a line drawing of x-y and y-z views
    for n in morphology.compartment_list:
        for c in morphology.children_of(n):
            axes[0].plot([n['x'], c['x']], [n['y'], c['y']], color='black')
            axes[1].plot([n['z'], c['z']], [n['y'], c['y']], color='black')

    axes[0].set_ylabel('y')
예제 #12
0
# calculate features
cell_features = extract_cell_features(data_set,
                                      sweep_numbers['Ramp'],
                                      sweep_numbers['Short Square'],
                                      sweep_numbers['Long Square'])

#===============================================================================
# example 3
#===============================================================================

import allensdk.core.swc as swc

# if you ran the examples above, you will have a reconstruction here
file_name = 'cell_types/specimen_485909730/reconstruction.swc'
morphology = swc.read_swc(file_name)

# subsample the morphology 3x. root, soma, junctions, and the first child of the root are preserved.
sparse_morphology = morphology.sparsify(3)

# compartments in the order that they were specified in the file
compartment_list = sparse_morphology.compartment_list

# a dictionary of compartments indexed by compartment id
compartments_by_id = sparse_morphology.compartment_index

# the root soma compartment 
soma = morphology.soma

# all compartments are dictionaries of compartment properties
# compartments also keep track of ids of their children
예제 #13
0
    def parse_neuron_swc(self, filepath, neuron_number):
        """
		Given an swc file, render the neuron

		:param filepath: str with path to swc file
		:param neuron_number: numnber of neuron being rendered

		"""
        # details on swc files: http://www.neuronland.org/NLMorphologyConverter/MorphologyFormats/SWC/Spec.html
        _sample = namedtuple("sample", "sampleN structureID x y z r parent"
                             )  # sampleN structureID x y z r parent

        # in json {'allenId': 1021, 'parentNumber': 5, 'radius': 0.5, 'sampleNumber': 6,
        # 'structureIdentifier': 2, 'x': 6848.52419500001, 'y': 2631.9816355, 'z': 3364.3552898125}

        if not os.path.isfile(filepath) or not ".swc" in filepath.lower():
            raise ValueError("unrecognized file path: {}".format(filepath))

        try:
            morphology = allen_swc.read_swc(filepath)
            return self.parse_neurons_swc_allen(morphology, neuron_number)
        except:
            pass  #  the .swc file fas not generate with by allen

        f = open(filepath)
        content = f.readlines()
        f.close()

        # crate empty dicts for soma axon and dendrites
        data = {
            'soma':
            dict(allenId=[],
                 parentNumber=[],
                 radius=[],
                 sampleNumber=[],
                 x=[],
                 y=[],
                 z=[]),
            'axon':
            dict(allenId=[],
                 parentNumber=[],
                 radius=[],
                 sampleNumber=[],
                 x=[],
                 y=[],
                 z=[]),
            'dendrite':
            dict(allenId=[],
                 parentNumber=[],
                 radius=[],
                 sampleNumber=[],
                 x=[],
                 y=[],
                 z=[])
        }

        # start looping around samples
        for sample in content:
            if sample[0] == '#':
                continue  # skip comments
            s = _sample(
                *
                [float(samp.replace("\n", "")) for samp in sample.split("\t")])

            # what structure is this
            if s.structureID in [1., -1.]: key = "soma"
            elif s.structureID in [2.]: key = 'axon'
            elif s.structureID in [3., 4.]: key = 'dendrite'
            else:
                raise ValueError(
                    "unrecognised sample in SWC file: {}".format(s))

            # append data to dictionary
            data[key]['parentNumber'].append(int(s.parent))
            data[key]['radius'].append(s.r)
            data[key]['x'].append(s.x)
            data[key]['y'].append(s.y)
            data[key]['z'].append(s.z)
            data[key]['sampleNumber'].append(int(s.sampleN))
            data[key]['allenId'].append(-1)  # TODO get allen ID from coords

        return data
    for r in radii :
        header.append( str(r) )
    cwriter.writerow( header )

    for idx, cell in enumerate(cells) :

        #if cell['id'] != 397351623:
        #    continue

        print '%d: %d' % (idx,cell['id'])
        fname = os.path.join( 'swc', '%d_morphology.swc' % cell['id'] )
        
        if download_file :
            ct.save_reconstruction( cell['id'], fname )
            
        morphology = swc.read_swc(fname)
        compartment_list = morphology.compartment_list
        
        root = compartment_list[0]
        if root['type'] != 1 :
            print 'first node is not soma'
            continue
            
        sholl = np.zeros( radii.shape )
        
        for n in morphology.compartment_list :
        
            node_soma_diff = np.array([root['x'],root['y'],root['z']]) - np.array([n['x'],n['y'], n['z']])
            node_soma_diff_norm = np.linalg.norm( node_soma_diff )
            node_soma_diff_norm_square = node_soma_diff_norm ** 2
            #print node_soma_diff
예제 #15
0
    for r in radii:
        header.append(str(r))
    cwriter.writerow(header)

    for idx, cell in enumerate(cells):

        #if cell['id'] != 397351623:
        #    continue

        print '%d: %d' % (idx, cell['id'])
        fname = os.path.join('swc', '%d_morphology.swc' % cell['id'])

        if download_file:
            ct.save_reconstruction(cell['id'], fname)

        morphology = swc.read_swc(fname)
        compartment_list = morphology.compartment_list

        root = compartment_list[0]
        if root['type'] != 1:
            print 'first node is not soma'
            continue

        sholl = np.zeros(radii.shape)

        for n in morphology.compartment_list:

            node_soma_diff = np.array([root['x'], root['y'], root['z']
                                       ]) - np.array([n['x'], n['y'], n['z']])
            node_soma_diff_norm = np.linalg.norm(node_soma_diff)
            node_soma_diff_norm_square = node_soma_diff_norm**2