Skip to content

mcmicro_to_scimap

Short Description

sm.pp.mcmicro_to_scimap: The function allows users to directly import the output from mcmicro into scimap.

Function

mcmicro_to_scimap(feature_table_path, remove_dna=True, remove_string_from_name=None, log=True, drop_markers=None, random_sample=None, unique_CellId=True, CellId='CellID', split='X_centroid', custom_imageid=None, min_cells=None, output_dir=None)

feature_table_path (list):    
    This is a list of paths that lead to the single-cell spatial feature tables. Each image should have a unique path assigned to it.

remove_dna (bool):    
    Remove the DNA channels from the final output. Looks for channels with the string 'dna' in it.

remove_string_from_name (string):    
    Used to clean channel names. The given string will be removed from all marker names.

log (bool):  
    Take Log of data (log1p transformation will be applied).

drop_markers (list):   
    List of markers to drop from the analysis. e.g. ["CD3D", "CD20"].

random_sample (int):  
    Randomly sub-sample data, new sample contains desired number of cells.

CellId (string):  
    Name of the column that contains the cell ID.

unique_CellId (bool):   
    By default, the function creates a unique name for each cell/row by combining the 
    `CellId` and `imageid`. If you wish not to perform this operation please pass `False`.
    The function will use whatever is under `CellId`. In which case, please be careful to pass unique `CellId`
    especially when loading multiple datasets togeather.

split (string):  
    To split the CSV into counts table and meta data, pass in the name of the column
    that immediately follows the marker quantification.

custom_imageid (string):    
    Pass a user defined Image ID. By default the name of the CSV file is used.

min_cells (int):   
    Images with less cells than int will be dropped
    Particulary useful when importing multiple images.

output_dir (string):    
    Path to output directory.

Returns:

AnnData Object

Example:

1
2
3
feature_table_path = ['/Users/aj/whole_sections/PTCL1_450.csv',
                  '/Users/aj/whole_sections/PTCL2_552.csv']
adata = sm.pp.mcmicro_to_scimap (feature_table_path, drop_markers= ['CD21', 'ACTIN'], random_sample=5000)

Source code in scimap/preprocessing/_mcmicro_to_scimap.py
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
def mcmicro_to_scimap (feature_table_path,
                       remove_dna=True,
                       remove_string_from_name=None,
                       log=True,
                       drop_markers=None,
                       random_sample=None, 
                       unique_CellId=True,
                       CellId='CellID',
                       split='X_centroid',
                       custom_imageid=None,
                       min_cells=None, 
                       output_dir=None):
    """
Parameters:

    feature_table_path (list):    
        This is a list of paths that lead to the single-cell spatial feature tables. Each image should have a unique path assigned to it.

    remove_dna (bool):    
        Remove the DNA channels from the final output. Looks for channels with the string 'dna' in it.

    remove_string_from_name (string):    
        Used to clean channel names. The given string will be removed from all marker names.

    log (bool):  
        Take Log of data (log1p transformation will be applied).

    drop_markers (list):   
        List of markers to drop from the analysis. e.g. ["CD3D", "CD20"].

    random_sample (int):  
        Randomly sub-sample data, new sample contains desired number of cells.

    CellId (string):  
        Name of the column that contains the cell ID.

    unique_CellId (bool):   
        By default, the function creates a unique name for each cell/row by combining the 
        `CellId` and `imageid`. If you wish not to perform this operation please pass `False`.
        The function will use whatever is under `CellId`. In which case, please be careful to pass unique `CellId`
        especially when loading multiple datasets togeather.  

    split (string):  
        To split the CSV into counts table and meta data, pass in the name of the column
        that immediately follows the marker quantification.

    custom_imageid (string):    
        Pass a user defined Image ID. By default the name of the CSV file is used.

    min_cells (int):   
        Images with less cells than int will be dropped
        Particulary useful when importing multiple images.

    output_dir (string):    
        Path to output directory. 

Returns:

    AnnData Object


Example:
```python
feature_table_path = ['/Users/aj/whole_sections/PTCL1_450.csv',
                  '/Users/aj/whole_sections/PTCL2_552.csv']
adata = sm.pp.mcmicro_to_scimap (feature_table_path, drop_markers= ['CD21', 'ACTIN'], random_sample=5000)
```
    """

    # feature_table_path list or string
    if isinstance(feature_table_path, str):
        feature_table_path = [feature_table_path]
    feature_table_path = [pathlib.Path(p) for p in feature_table_path]

    # Import data based on the location provided
    def load_process_data (image):
        # Print the data that is being processed
        print(f"Loading {image.name}")
        d = pd.read_csv(image)
        # If the data does not have a unique image ID column, add one.
        if 'imageid' not in d.columns:
            if custom_imageid is not None:
                imid = custom_imageid
            else:
                #imid = random.randint(1000000,9999999)
                imid = image.stem
            d['imageid'] = imid
        # Unique name for the data
        if unique_CellId is True:
            d.index = d['imageid'].astype(str)+'_'+d[CellId].astype(str)
        else:
            d.index = d[CellId]

        # move image id and cellID column to end
        cellid_col = [col for col in d.columns if col != CellId] + [CellId]; d = d[cellid_col]
        imageid_col = [col for col in d.columns if col != 'imageid'] + ['imageid']; d = d[imageid_col]
        # If there is INF replace with zero
        d = d.replace([np.inf, -np.inf], 0)
        # Return data
        return d
    # Apply function to all images and create a master dataframe
    r_load_process_data = lambda x: load_process_data(image=x) # Create lamda function
    all_data = list(map(r_load_process_data, list(feature_table_path))) # Apply function

    # Merge all the data into a single large dataframe
    for i in range(len(all_data)):
        all_data[i].columns = all_data[0].columns
    entire_data = pd.concat(all_data, axis=0, sort=False)

    # Randomly sample the data
    if random_sample is not None:
        entire_data = entire_data.sample(n=random_sample,replace=False)

    #Remove the images that contain less than a defined threshold of cells (min_cells)
    if min_cells is not None:
        to_drop = entire_data['imageid'].value_counts()[entire_data['imageid'].value_counts() < min_cells].index
        entire_data = entire_data[~entire_data['imageid'].isin(to_drop)]
        print('Removed Images that contained less than '+str(min_cells)+' cells: '+ str(to_drop.values))

    # Split the data into expression data and meta data
    # Step-1 (Find the index of the column with name Area)
    split_idx = entire_data.columns.get_loc(split)
    meta = entire_data.iloc [:,split_idx:]
    # Step-2 (select only the expression values)
    entire_data = entire_data.iloc [:,:split_idx]

    # Rename the columns of the data
    if remove_string_from_name is not None:
        entire_data.columns = entire_data.columns.str.replace(remove_string_from_name, '')

    # Save a copy of the column names in the uns space of ANNDATA
    markers = list(entire_data.columns)

    # Remove DNA channels
    if remove_dna is True:
        entire_data = entire_data.loc[:,~entire_data.columns.str.contains('dna', case=False)]

    # Drop unnecessary markers
    if drop_markers is not None:
        if isinstance(drop_markers, str):
            drop_markers = [drop_markers]
        entire_data = entire_data.drop(columns=drop_markers)

    # Create an anndata object
    adata = ad.AnnData(entire_data)
    adata.obs = meta
    adata.uns['all_markers'] = markers

    # Add log data
    if log is True:
        adata.raw = adata
        adata.X = np.log1p(adata.X)

    # Save data if requested
    if output_dir is not None:
        output_dir = pathlib.Path(output_dir)
        output_dir.mkdir(exist_ok=True, parents=True)
        imid = feature_table_path[0].stem
        adata.write(output_dir / f'{imid}.h5ad')
        #adata.write(str(output_dir) + '/' + imid + '.h5ad')
    else:    
        # Return data
        return adata