def merge(adata, ldata, copy=True):
if '<STR_LIT>' in ldata.layers.keys() and '<STR_LIT>' not in ldata.obs.keys(): set_initial_size(ldata)<EOL>elif '<STR_LIT>' in adata.layers.keys() and '<STR_LIT>' not in adata.obs.keys(): set_initial_size(adata)<EOL>common_obs = adata.obs_names.intersection(ldata.obs_names)<EOL>common_vars = adata.var_names.intersection(ldata.var_names)<EOL>if len(common_obs) == <NUM_LIT:0>:<EOL><INDENT>clean_obs_names(adata)<EOL>clean_obs_names(ldata)<EOL>common_obs = adata.obs_names.intersection(ldata.obs_names)<EOL><DEDENT>if copy:<EOL><INDENT>_adata = adata[common_obs].copy() if adata.shape[<NUM_LIT:1>] >= ldata.shape[<NUM_LIT:1>] else ldata[common_obs].copy()<EOL>_ldata = ldata[common_obs].copy() if adata.shape[<NUM_LIT:1>] >= ldata.shape[<NUM_LIT:1>] else adata[common_obs].copy()<EOL><DEDENT>else:<EOL><INDENT>adata._inplace_subset_obs(common_obs)<EOL>_adata, _ldata = adata, ldata[common_obs]<EOL><DEDENT>same_vars = (len(_adata.var_names) == len(_ldata.var_names) and np.all(_adata.var_names == _ldata.var_names))<EOL>if len(common_vars) > <NUM_LIT:0> and not same_vars:<EOL><INDENT>_adata._inplace_subset_var(common_vars)<EOL>_ldata._inplace_subset_var(common_vars)<EOL><DEDENT>for attr in _ldata.obs.keys():<EOL><INDENT>_adata.obs[attr] = _ldata.obs[attr]<EOL><DEDENT>for attr in _ldata.obsm.keys():<EOL><INDENT>_adata.obsm[attr] = _ldata.obsm[attr]<EOL><DEDENT>for attr in _ldata.uns.keys():<EOL><INDENT>_adata.uns[attr] = _ldata.uns[attr]<EOL><DEDENT>for attr in _ldata.layers.keys():<EOL><INDENT>_adata.layers[attr] = _ldata.layers[attr]<EOL><DEDENT>if _adata.shape[<NUM_LIT:1>] == _ldata.shape[<NUM_LIT:1>]:<EOL><INDENT>same_vars = (len(_adata.var_names) == len(_ldata.var_names) and np.all(_adata.var_names == _ldata.var_names))<EOL>if same_vars:<EOL><INDENT>for attr in _ldata.var.keys():<EOL><INDENT>_adata.var[attr] = _ldata.var[attr]<EOL><DEDENT>for attr in _ldata.varm.keys():<EOL><INDENT>_adata.varm[attr] = _ldata.varm[attr]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>return _adata if copy else None<EOL>
Merges two annotated data matrices.

    Arguments
    ---------
    adata: :class:`~anndata.AnnData`
        Annotated data matrix (reference data set).
    ldata: :class:`~anndata.AnnData`
        Annotated data matrix (to be merged into adata).

    Returns
    -------
    Returns a :class:`~anndata.AnnData` object