示例#1
0
def generateRandomMorphology():

    morphology = Morphology()

    p = Point3DWithDiam(x=0, y=0, z=0, diameter=soma_diam)
    d = Point3DWithDiam(x=soma_len, y=0, z=0, diameter=soma_diam)
    soma = Segment(proximal=p, distal=d, name='Soma', id=0)

    morphology.segments.append(soma)
    parent_seg = soma

    for dend_id in range(0, dend_num):

        p = Point3DWithDiam(x=d.x, y=d.y, z=d.z, diameter=dend_diam)
        d = Point3DWithDiam(x=p.x, y=p.y + dend_len, z=p.z, diameter=dend_diam)
        dend = Segment(proximal=p,
                       distal=d,
                       name='Dend_%i' % dend_id,
                       id=1 + dend_id)
        dend.parent = SegmentParent(segments=parent_seg.id)
        parent_seg = dend

        morphology.segments.append(dend)

    morphology.id = "TestMorphology"

    return morphology
def createMorphoMlFile(fileName, cell):
    '''
    Convert to new neuroml structures and write
    '''

    if not muscle_dict.has_key(cell.name):
        neuroMlwriter = NeuroMlWriter(fileName, cell.name)
        neuroMlwriter.addCell(cell)
        neuroMlwriter.writeDocumentToFile()
        return

    #
    # Incomplete code to use the neuroml interface to write the file,
    # used for muscles, doesn't produce good enough result on neurons yet.
    #

    seg0 = cell.segments[0].position
    soma = Segment(proximal=cvt_pt(seg0.proximal_point),
                   distal=cvt_pt(seg0.distal_point))
    soma.name = 'Soma'
    soma.id = 0

    axon_segments = []
    for seg1 in cell.segments[1:]:

        parent = SegmentParent(segments=seg1.parent)
        if seg1.position.proximal_point is None:
            p = None
        else:
            p = cvt_pt(seg1.position.proximal_point)

        axon_segment = Segment(proximal=p,
                               distal=cvt_pt(seg1.position.distal_point),
                               parent=parent)
        axon_segment.id = seg1.id
        axon_segment.name = seg1.name
        axon_segments.append(axon_segment)

    morphology = Morphology()
    morphology.segments.append(soma)
    morphology.segments += axon_segments
    morphology.id = 'morphology_' + cell.name

    nml_cell = neuroml_Cell()
    nml_cell.id = cell.name
    nml_cell.morphology = morphology

    doc = NeuroMLDocument()
    doc.cells.append(nml_cell)
    #addCell(doc, cell)
    doc.id = "TestNeuroMLDocument"
    writers.NeuroMLWriter.write(doc, "Output/%s.nml" % fileName)
def add_segment(obj, p0, p1, name=None):

    p = Point3DWithDiam(x=p0[0], y=p0[1], z=p0[2], diameter=p0[3])
    d = Point3DWithDiam(x=p1[0], y=p1[1], z=p1[2], diameter=p1[3])

    segid = len(obj.morphology.segments)

    parent = None if segid == 0 else SegmentParent(segments=segid - 1)

    segment = Segment(id=segid, proximal=p, distal=d, parent=parent)
    if name:
        segment.name = name

    obj.morphology.segment_groups[0].members.append(Member(segments=segid))

    obj.morphology.segments.append(segment)

    return segment
def createMorphoMlFile(fileName, cell):
    '''
    Convert to new neuroml structures and write
    '''

    seg0 = cell.segments[0].position
    soma = Segment(proximal=cvt_pt(seg0.proximal_point),
                   distal=cvt_pt(seg0.distal_point))
    soma.name = 'Soma'
    soma.id = 0

    axon_segments = []
    for seg1 in cell.segments[1:]:

        parent = SegmentParent(segments=seg1.parent)
        if seg1.position.distal_point is None:
            p = None
        else:
            p = cvt_pt(seg1.position.distal_point)
        axon_segment = Segment(proximal=p,
                               distal=cvt_pt(seg1.position.distal_point),
                               parent=parent)
        axon_segment.id = seg1.id
        axon_segment.name = seg1.name
        axon_segments.append(axon_segment)

    morphology = Morphology()
    morphology.segments.append(soma)
    morphology.segments += axon_segments
    morphology.id = 'morphology_' + cell.name

    nml_cell = neuroml_Cell()
    nml_cell.id = cell.name
    nml_cell.morphology = morphology

    doc = NeuroMLDocument()
    #doc.name = "Test neuroML document"
    doc.cells.append(nml_cell)
    doc.id = fileName
    writers.NeuroMLWriter.write(doc, "Output/%s.nml" % fileName)
示例#5
0
def neuroml_single_cell(skeleton_id, nodes, pre, post):
    """ Encapsulate a single skeleton into a NeuroML Cell instance.
        
        skeleton_id: the ID of the skeleton to which all nodes belong.
        nodes: a dictionary of node ID vs tuple of node parent ID, location as a tuple of 3 floats, and radius. In nanometers.
        pre: a dictionary of node ID vs list of connector ID
        post: a dictionary of node ID vs list of connector ID

        Returns a Cell with id=skeleton_id.
    """

    # Collect the children of every node
    successors = defaultdict(
        list)  # parent node ID vs list of children node IDs
    rootID = None
    for nodeID, props in nodes.iteritems():
        parentID = props[0]
        if not parentID:
            rootID = nodeID
            continue
        successors[parentID].append(nodeID)

    # Cache of Point3DWithDiam
    points = {}

    def asPoint(nodeID):
        """ Return the node as a Point3DWithDiam, in micrometers. """
        p = points.get(nodeID)
        if not p:
            props = nodes[nodeID]
            radius = props[2]
            if radius < 0:
                radius = 0.1  # FUTURE Will have to change
            loc = props[1]
            # Point in micrometers
            p = Point3DWithDiam(loc[0] / 1000.0, loc[1] / 1000.0,
                                loc[2] / 1000.0, radius)
            points[nodeID] = p
        return p

    # Starting from the root node, iterate towards the end nodes, adding a segment
    # for each parent-child pair.

    segments = []
    segment_id = 1
    todo = [rootID]

    # VERY CONFUSINGLY, the Segment.parent is a SegmentParent with the same id as the parent Segment. An unseemly overheady way to reference the parent Segment.

    while todo:
        nodeID = todo.pop()
        children = successors[nodeID]
        if not children:
            continue
        p1 = asPoint(nodeID)
        parent = segments[-1] if segments else None
        segment_parent = SegmentParent(segments=parent.id) if parent else None
        for childID in children:
            p2 = asPoint(childID)
            segment_id += 1
            segment = Segment(proximal=p1, distal=p2, parent=segment_parent)
            segment.id = segment_id
            segment.name = "%s-%s" % (nodeID, childID)
            segments.append(segment)
            todo.append(childID)

    # Pack the segments into a Cell
    morphology = Morphology()
    morphology.segments.extend(segments)
    morphology.id = "Skeleton #%s" % skeleton_id

    # Synapses: TODO requires input from Padraig Gleeson

    cell = Cell()
    cell.name = 'Cell'
    cell.id = skeleton_id
    cell.morphology = morphology

    return cell
示例#6
0
def add_segment(cell,
                prox,
                dist,
                name=None,
                parent=None,
                fraction_along=1.0,
                group=None):
    # type: (Cell, List[float], List[float], str, SegmentParent, float, SegmentGroup) -> Segment
    """Add a segment to the cell.

    Convention: use axon_, soma_, dend_ prefixes for axon, soma, and dendrite
    segments respectivey. This will allow this function to add the correct
    neurolex IDs to the group.

    The created segment is also added to the default segment groups that were
    created by the `create_cell` function: "all", "dendrite_group",
    "soma_group", "axon_group" if the convention is followed.

    :param cell: cell to add segment to
    :type cell: Cell
    :param prox: proximal segment information
    :type prox: list with 4 float entries: [x, y, z, diameter]
    :param dist: dist segment information
    :type dist: list with 4 float entries: [x, y, z, diameter]
    :param name: name of segment
    :type name: str
    :param parent: parent segment
    :type parent: SegmentParent
    :param fraction_along: where the new segment is connected to the parent (0: distal point, 1: proximal point)
    :type fraction_along: float
    :param group: segment group to add the segment to
    :type group: SegmentGroup
    :returns: the created segment

    """
    try:
        if prox:
            p = Point3DWithDiam(x=prox[0],
                                y=prox[1],
                                z=prox[2],
                                diameter=prox[3])
        else:
            p = None
    except IndexError as e:
        print_function("{}: prox must be a list of 4 elements".format(e))
    try:
        d = Point3DWithDiam(x=dist[0], y=dist[1], z=dist[2], diameter=dist[3])
    except IndexError as e:
        print_function("{}: dist must be a list of 4 elements".format(e))

    segid = len(cell.morphology.segments)

    sp = SegmentParent(segments=parent.id,
                       fraction_along=fraction_along) if parent else None
    segment = Segment(id=segid, proximal=p, distal=d, parent=sp)

    if name:
        segment.name = name

    if group:
        seg_group = None
        seg_group = get_seg_group_by_id(group, cell)
        seg_group_all = get_seg_group_by_id("all", cell)
        seg_group_default = None
        neuro_lex_id = None

        if "axon_" in group:
            neuro_lex_id = neuro_lex_ids[
                "axon"]  # See http://amigo.geneontology.org/amigo/term/GO:0030424
            seg_group_default = get_seg_group_by_id("axon_group", cell)
        if "soma_" in group:
            neuro_lex_id = neuro_lex_ids["soma"]
            seg_group_default = get_seg_group_by_id("soma_group", cell)
        if "dend_" in group:
            neuro_lex_id = neuro_lex_ids["dend"]
            seg_group_default = get_seg_group_by_id("dendrite_group", cell)

        if seg_group is None:
            seg_group = SegmentGroup(id=group, neuro_lex_id=neuro_lex_id)
            cell.morphology.segment_groups.append(seg_group)

        seg_group.members.append(Member(segments=segment.id))
        # Ideally, these higher level segment groups should just include other
        # segment groups using Include, which would result in smaller NML
        # files. However, because these default segment groups are defined
        # first, they are printed in the NML file before the new segments and their
        # groups. The jnml validator does not like this.
        # TODO: clarify if the order of definition is important, or if the jnml
        # validator needs to be updated to manage this use case.
        if seg_group_default:
            seg_group_default.members.append(Member(segments=segment.id))

    seg_group_all.members.append(Member(segments=segment.id))

    cell.morphology.segments.append(segment)
    return segment