def map_singlecell_inputs(cellName, cellTypeName):
    if not (cellTypeName in exTypes) and not (cellTypeName in inhTypes):
        errstr = 'Unknown cell type %s!'
        raise TypeError(errstr)

    startTime = time.time()

    print 'Loading cell morphology...'
    parser = sim.CellParser(cellName)
    parser.spatialgraph_to_cell()
    singleCell = parser.get_cell()

    print 'Loading spreadsheets and bouton/PST densities...'
    print '    Loading numberOfCells spreadsheet %s' % numberOfCellsSpreadsheetName
    numberOfCellsSpreadsheet = sim.read_celltype_numbers_spreadsheet(
        numberOfCellsSpreadsheetName)
    print '    Loading connections spreadsheet %s' % connectionsSpreadsheetName
    connectionsSpreadsheet = sim.read_connections_spreadsheet(
        connectionsSpreadsheetName)
    print '    Loading PST density %s' % ExPSTDensityName
    ExPSTDensity = sim.read_scalar_field(ExPSTDensityName)
    ExPSTDensity.resize_mesh()
    print '    Loading PST density %s' % InhPSTDensityName
    InhPSTDensity = sim.read_scalar_field(InhPSTDensityName)
    InhPSTDensity.resize_mesh()
    boutonDensities = {}
    columns = numberOfCellsSpreadsheet.keys()
    preCellTypes = numberOfCellsSpreadsheet[columns[0]]

    for col in columns:
        boutonDensities[col] = {}
        for preCellType in preCellTypes:
            boutonDensities[col][preCellType] = []
            boutonDensityFolder = os.path.join(prefix, boutonDensityFolderName,
                                               col, preCellType)
            boutonDensityNames = glob.glob(
                os.path.join(boutonDensityFolder, '*'))
            print '    Loading %d bouton densities from %s' % (
                len(boutonDensityNames), boutonDensityFolder)
            for densityName in boutonDensityNames:
                boutonDensity = sim.read_scalar_field(densityName)
                boutonDensity.resize_mesh()
                boutonDensities[col][preCellType].append(boutonDensity)

    inputMapper = sim.NetworkMapper(singleCell, cellTypeName,
                                    numberOfCellsSpreadsheet,
                                    connectionsSpreadsheet, ExPSTDensity,
                                    InhPSTDensity)
    inputMapper.exCellTypes = exTypes
    inputMapper.inhCellTypes = inhTypes
    inputMapper.create_network_embedding(cellName, boutonDensities)

    endTime = time.time()
    duration = (endTime - startTime) / 60.0
    print 'Runtime: %.1f minutes' % duration
def main(whisker, boutonFolder, outName):
    fnames = []
    scan_directory(boutonFolder, fnames, 'Boutons.am')
    #4. go through all cell types
    for cellType in cellTypes:
        cellTypeDensities = []
        cellTypeBounds = [1e6,-1e6,1e6,-1e6,1e6,-1e6] # min, max
        cellTypeSpacing = None
        activeColumns = []
    #5. go through all surrounding columns
        SuCs = surroundColumns[whisker].keys()
        for column in SuCs:
            spikeProb = cellTypePSTHs[cellType][surroundPSTHLookup[surroundColumns[whisker][column]]]
            if not spikeProb:
                continue
            activeColumns.append(column)
    #6. load bouton density
            fileName = None
            for fname in fnames:
                if cellType in fname and column in fname:
                    fileName = fname
                    break
            if fileName is None:
                errstr = 'Bouton density of cell type %s in column %s not found' % (cellType, column)
                raise IOError(errstr)
            tmpDensity = sm.read_scalar_field(fileName)
    #7. scale bouton density by PSTH
            tmpDensity.mesh *= spikeProb # without pGlut for now; does not really change anything
            cellTypeDensities.append(tmpDensity)
    #8. add to cell type bouton density
        for i in range(len(cellTypeDensities)):
            if not i:
                cellTypeSpacing = cellTypeDensities[i].spacing
            tmpBounds = cellTypeDensities[i].boundingBox
            for j in range(3):
                if tmpBounds[2*j] < cellTypeBounds[2*j]:
                    cellTypeBounds[2*j] = tmpBounds[2*j]
                if tmpBounds[2*j+1] > cellTypeBounds[2*j+1]:
                    cellTypeBounds[2*j+1] = tmpBounds[2*j+1]
        dims, extent, origin = [], [], []
        for i in range(3):
            dims.append(int((cellTypeBounds[2*i+1]-cellTypeBounds[2*i])/cellTypeSpacing[i]+1.001))
            extent.append(0)
            extent.append(dims[i]-1)
            origin.append(cellTypeBounds[2*i])
        cellTypeMesh = np.zeros(shape=dims)
        for i in range(len(cellTypeDensities)):
            tmpBounds = cellTypeDensities[i].boundingBox
            offset = []
            for j in range(3):
                minOffset = int((tmpBounds[2*j]-cellTypeBounds[2*j])/cellTypeSpacing[j]+0.001)
                #maxOffset = extent[2*j+1] - int((cellTypeBounds[2*j+1]-tmpBounds[2*j+1])/cellTypeSpacing[j]+0.001)
                maxOffset = minOffset + cellTypeDensities[i].extent[2*j+1]
                diff = (maxOffset - minOffset) - (cellTypeDensities[i].extent[2*j+1])
                if diff:
                    errstr = 'Offset difference does not match size of cell type density: maxOffset = %d - minOffset = %d - dim = %d' % (maxOffset, minOffset, cellTypeDensities[i].extent[2*j+1])
                    raise RuntimeError(errstr)
                offset.append(minOffset)
                offset.append(maxOffset)
            cellTypeMesh[offset[0]:offset[1]+1,offset[2]:offset[3]+1,offset[4]:offset[5]+1] += cellTypeDensities[i].mesh
    #9. write cell type-specific bouton density
        cellTypeScalarField = sm.ScalarField(cellTypeMesh, origin, extent, cellTypeSpacing, cellTypeBounds)
        cellTypeOutName = outName
        cellTypeOutName += '_'
        cellTypeOutName += whisker
        cellTypeOutName += '_'
        cellTypeOutName += cellType
        cellTypeOutName += '_active_synapses'
        sm.write_scalar_field(cellTypeOutName, cellTypeScalarField)
Exemple #3
0
def computeInnervation(dendriteListPath, dendriteFolderName, cellTypeName,
                       boutonListPath, boutonFolderName, outputCSVName):

    if not (cellTypeName in exTypes) and not (cellTypeName in inhTypes):
        errstr = 'Unknown cell type %s!' % cellTypeName
        print 'ERROR! %s' % errstr
        raise TypeError(errstr)

    startTime = time.time()

    # Main Path
    if sys.platform == 'win32':
        prefix = '//nas1/nas1/Data_daniel/Network/L5/L5_InVitro/'
    else:
        prefix = '/nas1/Data_daniel/Network/L5/L5_InVitro/'

    # Required data for calculation synapse density
    connectionsSpreadsheetName = os.path.join(
        prefix, 'data/CellTypeConnectionSpreadsheet.csv')
    ExPSTDensityName = os.path.join(prefix, 'data/EXNormalizationPSTs.am')
    connectionsSpreadsheet = sim.read_connections_spreadsheet(
        connectionsSpreadsheetName)
    ExPSTDensity = sim.read_scalar_field(ExPSTDensityName)
    ExPSTDensity.resize_mesh()
    InhPSTDensity = []
    boutonDensityList = []
    boutonDensityListName = []

    # Load each bouton density and store it
    with open(boutonListPath, 'r') as spreadsheet:
        for line in spreadsheet:

            # Load Bouton Density
            tmpLine = line.strip('\n\r')
            densityName = os.path.join(boutonFolderName, tmpLine)
            boutonDensity = sim.read_scalar_field(densityName)
            boutonDensity.resize_mesh()
            boutonDensityList.append(boutonDensity)
            boutonDensityListName.append(tmpLine)

    if len(boutonDensityList) == 0:
        errstr = 'No Bouton Densities were found'
        print 'ERROR! %s' % errstr
        raise TypeError(errstr)

    outputstr = 'Presynapse,Postsynapse,synapsesBasal,synapsesApical\n'

    # Go through all dendrites
    with open(dendriteListPath, 'r') as spreadsheet:
        for line in spreadsheet:

            # Read in Dendrite
            tmpLine = line.strip('\n\r')
            cellNameDend = os.path.join(dendriteFolderName, tmpLine)
            if cellNameDend[-4:] != '.hoc':
                errstr = 'Can only read in hoc morphologies: %s!' % cellNameDend
                print 'ERROR! %s' % errstr
                raise TypeError(errstr)

            # Load Postsynaptic hoc Morphology
            print 'Loading cell morphology... %s' % tmpLine
            parser = sim.CellParser(cellNameDend)
            parser.spatialgraph_to_cell()
            singleCell = parser.get_cell()

            synapseDensityComputation = sim.SynapseDensity(singleCell, cellTypeName, connectionsSpreadsheet,\
                                               exTypes, inhTypes, ExPSTDensity, InhPSTDensity)

            # Compute Innervation
            for b in range(len(boutonDensityList)):

                syn = synapseDensityComputation.compute_synapse_density(
                    boutonDensityList[b], cellTypeName)

                # Extract Innervation, if tmp == None, no innervation
                synApical = 0
                synBasal = 0

                if syn is not None:
                    if 'Dendrite' in syn.keys():
                        synBasal = np.sum(syn['Dendrite'].mesh)
                    if 'ApicalDendrite' in syn.keys():
                        synApical = np.sum(syn['ApicalDendrite'].mesh)

                # Name of Pre- and Postsynapse
                dendName = cellNameDend.split('/')[-1]
                dendName = dendName.replace(".am", "")
                dendName = dendName.replace(".hoc", "")
                boutonName = boutonDensityListName[b].replace(".am", "")
                tmpStr = '%s,%s,%f,%f\n' % (boutonName, dendName, synBasal,
                                            synApical)
                outputstr += tmpStr

    # Display runtime
    endTime = time.time()
    duration = (endTime - startTime) / 60.0
    print 'Runtime: %.1f minutes' % duration

    # Store Innervation as .csv file
    with open(outputCSVName, 'w') as csvOutput:
        csvOutput.write(outputstr)

        print 'Saved data as csv in %s' % outputCSVName
Exemple #4
0
def map_singlecell_inputs_invitro(cellName, cellTypeName):

    if not (cellTypeName in exTypes) and not (cellTypeName in inhTypes):
        errstr = 'Unknown cell type %s!' % cellTypeName
        print 'ERROR! %s' % errstr
        raise TypeError(errstr)

    if cellName[-4:] != '.hoc':
        errstr = 'Can only read in hoc morphologies: %s!' % cellName
        print 'ERROR! %s' % errstr
        raise TypeError(errstr)

    startTime = time.time()

    # Main Path
    if sys.platform == 'win32':
        prefix = '//nas1/nas1/Data_daniel/Network/L5/L5_InVitro/'
    else:
        prefix = '/nas1/Data_daniel/Network/L5/L5_InVitro/'

    # Load Postsynaptic hoc Morphology
    print 'Loading cell morphology...'
    parser = sim.CellParser(cellName)
    parser.spatialgraph_to_cell()
    singleCell = parser.get_cell()

    # Position of Postsynaptic Morphology and get path to correct bouton densities
    strtmp = cellName.replace('_invivo', '')
    s = strtmp.split('_')
    postsynapse_y = s[-3]
    postsynapse_x = s[-4]
    if postsynapse_y == '0':
        postsynapse_y = s[-1][:-4].strip('.am')
        boutonDensityFolderName = os.path.join(prefix, 'BoutonDensity_y0')
    elif postsynapse_x == '0':
        postsynapse_x = s[-1][:-4].strip('.am')
        boutonDensityFolderName = os.path.join(prefix, 'BoutonDensity_x0')
    else:
        errstr = 'Postsynaptic morphology lacks information about sliced dimension ( %s _ %s )!' % (
            postsynapse_x, postsynapse_y)
        print 'ERROR! %s' % errstr
        raise TypeError(errstr)

    # Required data for calculation synapse density
    connectionsSpreadsheetName = os.path.join(
        prefix, 'data/CellTypeConnectionSpreadsheet.csv')
    ExPSTDensityName = os.path.join(prefix, 'data/EXNormalizationPSTs.am')
    connectionsSpreadsheet = sim.read_connections_spreadsheet(
        connectionsSpreadsheetName)
    ExPSTDensity = sim.read_scalar_field(ExPSTDensityName)
    ExPSTDensity.resize_mesh()
    InhPSTDensity = []
    outputfname = cellName.replace('.hoc', '.csv')

    synapseDensityComputation = sim.SynapseDensity(singleCell, cellTypeName, connectionsSpreadsheet,\
                                               exTypes, inhTypes, ExPSTDensity, InhPSTDensity)

    boutonDensityList = os.path.join(boutonDensityFolderName, 'list.txt')
    outputstr = 'Presynapse,Postsynapse,Presynapse_x,Presynapse_y,Postsynapse_x,Postsynapse_y,synapsesBasal,synapsesApical\n'

    # Load each bouton density and compute Innervation
    with open(boutonDensityList, 'r') as spreadsheet:
        for line in spreadsheet:

            # Load Bouton Density
            tmpLine = line.strip('\n\r')
            densityName = os.path.join(boutonDensityFolderName, tmpLine)
            boutonDensity = sim.read_scalar_field(densityName)
            boutonDensity.resize_mesh()

            # Compute Innervation
            syn = synapseDensityComputation.compute_synapse_density(
                boutonDensity, cellTypeName)

            # Extract Innervation, if tmp == None, no innervation
            synApical = 0
            synBasal = 0

            if syn is not None:
                if 'Dendrite' in syn.keys():
                    synBasal = np.sum(syn['Dendrite'].mesh)
                if 'ApicalDendrite' in syn.keys():
                    synApical = np.sum(syn['ApicalDendrite'].mesh)

            # Position of Presynapse/BoutonDensity
            tmpLine = tmpLine.replace('_invivo', '')
            s = tmpLine.split('_')
            presynapse_y = s[-2]
            presynapse_x = s[-3]

            tmpStr = '%s,%s,%s,%s,%s,%s,%f,%f\n' % (
                tmpLine, cellName.split('/')[-1], presynapse_x, presynapse_y,
                postsynapse_x, postsynapse_y, synBasal, synApical)
            outputstr += tmpStr

    endTime = time.time()
    duration = (endTime - startTime) / 60.0
    print 'Runtime: %.1f minutes' % duration

    with open(outputfname, 'w') as csvOutput:
        csvOutput.write(outputstr)

        print 'Saved data as csv in %s' % outputfname