-
Notifications
You must be signed in to change notification settings - Fork 5
Constructing spatial neighborhood with custom parameters #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7d98cd2
Constructing spatial neighborhood with custom parameters
bdac0af
Separate each function into individual file
Qirongmao97 3f68c32
Delete preprocessing/neighbors/neighborhood_construction.py
Qirongmao97 3890170
Delete preprocessing/neighbors/neighborhood_construction.yml
Qirongmao97 4130adf
Update delaunay_triangulation.py
Qirongmao97 36367e9
Update coord_type.py
Qirongmao97 6e77564
Update n_neighs.py
Qirongmao97 d30d4ac
Update n_rings.py
Qirongmao97 ee5533f
Update radius.py
Qirongmao97 235a298
Update and rename n_neighs.py to n_neighbourhood.py
Qirongmao97 ea486da
Update delaunay_triangulation.py
Qirongmao97 0625fdb
Delete preprocessing/neighbors/coord_type.py
Qirongmao97 d87dcf7
Delete preprocessing/neighbors/coord_type.yml
Qirongmao97 e76411a
Update n_neighbourhood.py
Qirongmao97 f8e317b
Update delaunay_triangulation.py
Qirongmao97 db8597d
Update n_rings.py
Qirongmao97 fb659d7
Update radius.py
Qirongmao97 6c551de
Committing changes based on the comments
94528f3
Putting each function to subdirectory
97c6bd2
Removing config file of Delaunay Traingulation
dc59a5b
remove config reading
niklasmueboe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Author_and_contribution: Niklas Mueller-Boetticher; created script, | ||
| # Author_and_contribution: Qirong Mao; implemented method | ||
|
|
||
| import argparse | ||
|
|
||
| # TODO adjust description | ||
| parser = argparse.ArgumentParser( | ||
| description="Constructing spatial neighborhood with custom parameters" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-c", "--coordinates", help="Path to coordinates (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-m", "--matrix", help="Path to (transformed) counts (as mtx).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-f", "--features", help="Path to features (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-o", "--observations", help="Path to observations (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "--coord_type", default=None, choices=['grid', 'generic', None],help="Type of coordinate system.", required=False | ||
| ) | ||
|
|
||
| parser.add_argument("-d", "--out_dir", help="Output directory.", required=True) | ||
|
|
||
| parser.add_argument( | ||
| "--config", | ||
| help="Optional config file (json) used to pass additional parameters.", | ||
| required=False, | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # Output files | ||
| from pathlib import Path | ||
|
|
||
| out_dir = Path(args.out_dir) | ||
|
|
||
| spatial_connectivities_file = out_dir / "spatial_connectivities.mtx" | ||
| spatial_distances_file = out_dir / "spatial_distances.mtx" | ||
|
|
||
| # Use these filepaths and inputs ... | ||
| coord_file = args.coordinates | ||
| matrix_file = args.matrix | ||
| feature_file = args.features | ||
| observation_file = args.observations | ||
|
|
||
| ## Custom parameters | ||
| coord_type = args.coord_type | ||
|
|
||
|
|
||
| if args.config is not None: | ||
| config_file = args.config | ||
|
|
||
|
|
||
| # ... or AnnData if you want | ||
| def get_anndata(args): | ||
| # Untested template | ||
| import anndata as ad | ||
| import pandas as pd | ||
| import scipy as sp | ||
|
|
||
| X = sp.io.mmread(args.matrix) | ||
| if sp.sparse.issparse(X): | ||
| X = X.tocsr() | ||
| observations = pd.read_table(args.observations, index_col=0) | ||
| features = pd.read_table(args.features, index_col=0) | ||
| coordinates = ( | ||
| pd.read_table(args.coordinates, index_col=0) | ||
| .loc[observations.index, :] | ||
| .to_numpy() | ||
| ) | ||
|
|
||
| adata = ad.AnnData( | ||
| X=X, obs=observations, var=features, obsm={"spatial": coordinates} | ||
| ) | ||
|
|
||
| return adata | ||
|
|
||
|
|
||
| adata = get_anndata(args) | ||
|
|
||
| ## Your code goes here | ||
| import squidpy as sq | ||
|
|
||
| sq.gr.spatial_neighbors(adata, coord_type=coord_type) | ||
|
|
||
| neighbors = adata.obsp["spatial_connectivities"].astype(int) | ||
| distance = adata.obsp["spatial_distances"].astype(int) | ||
|
|
||
| ## Write output | ||
| import scipy as sp | ||
|
|
||
| out_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| sp.io.mmwrite(spatial_connectivities_file, neighbors) | ||
| sp.io.mmwrite(spatial_distances_file, distance) | ||
|
|
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| channels: | ||
| - conda-forge | ||
| dependencies: | ||
| - python=3.9.18 | ||
| - pip | ||
| - pip: | ||
| - squidpy==1.3.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Author_and_contribution: Niklas Mueller-Boetticher; created script, | ||
| # Author_and_contribution: Qirong Mao; implemented method | ||
|
|
||
| import argparse | ||
|
|
||
| # TODO adjust description | ||
| parser = argparse.ArgumentParser( | ||
| description="Constructing spatial neighborhood with custom parameters" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-c", "--coordinates", help="Path to coordinates (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-m", "--matrix", help="Path to (transformed) counts (as mtx).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-f", "--features", help="Path to features (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-o", "--observations", help="Path to observations (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "--coord_type", default=None, choices=['grid', 'generic', None],help="Type of coordinate system.", required=False | ||
niklasmueboe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "--n_neighs", type=int, default=6,help="Number of neighboring tiles (grid data) or neighborhoods (non-grid data)", required=False | ||
| ) | ||
|
|
||
| parser.add_argument("-d", "--out_dir", help="Output directory.", required=True) | ||
|
|
||
| parser.add_argument( | ||
| "--config", | ||
| help="Optional config file (json) used to pass additional parameters.", | ||
| required=False, | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # Output files | ||
| from pathlib import Path | ||
|
|
||
| out_dir = Path(args.out_dir) | ||
|
|
||
| spatial_connectivities_file = out_dir / "spatial_connectivities.mtx" | ||
| spatial_distances_file = out_dir / "spatial_distances.mtx" | ||
|
|
||
| # Use these filepaths and inputs ... | ||
| coord_file = args.coordinates | ||
| matrix_file = args.matrix | ||
| feature_file = args.features | ||
| observation_file = args.observations | ||
|
|
||
| ## Custom parameters | ||
| coord_type = args.coord_type | ||
| n_neighs= args.n_neighs | ||
|
|
||
| if args.config is not None: | ||
| config_file = args.config | ||
|
|
||
|
|
||
| # ... or AnnData if you want | ||
| def get_anndata(args): | ||
| # Untested template | ||
| import anndata as ad | ||
| import pandas as pd | ||
| import scipy as sp | ||
|
|
||
| X = sp.io.mmread(args.matrix) | ||
| if sp.sparse.issparse(X): | ||
| X = X.tocsr() | ||
| observations = pd.read_table(args.observations, index_col=0) | ||
| features = pd.read_table(args.features, index_col=0) | ||
| coordinates = ( | ||
| pd.read_table(args.coordinates, index_col=0) | ||
| .loc[observations.index, :] | ||
| .to_numpy() | ||
| ) | ||
|
|
||
| adata = ad.AnnData( | ||
| X=X, obs=observations, var=features, obsm={"spatial": coordinates} | ||
| ) | ||
|
|
||
| return adata | ||
|
|
||
|
|
||
| adata = get_anndata(args) | ||
|
|
||
| ## Your code goes here | ||
| import squidpy as sq | ||
|
|
||
| sq.gr.spatial_neighbors(adata, coord_type=coord_type, n_neighs=n_neighs) | ||
|
|
||
| neighbors = adata.obsp["spatial_connectivities"].astype(int) | ||
| distance = adata.obsp["spatial_distances"].astype(int) | ||
niklasmueboe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Write output | ||
| import scipy as sp | ||
|
|
||
| out_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| sp.io.mmwrite(spatial_connectivities_file, neighbors) | ||
| sp.io.mmwrite(spatial_distances_file, distance) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| channels: | ||
| - conda-forge | ||
| dependencies: | ||
| - python=3.9.18 | ||
| - pip | ||
| - pip: | ||
| - squidpy==1.3.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Author_and_contribution: Niklas Mueller-Boetticher; created script, | ||
| # Author_and_contribution: Qirong Mao; implemented method | ||
|
|
||
| ### Only used when coord_type = 'grid' | ||
|
|
||
|
|
||
| import argparse | ||
|
|
||
| # TODO adjust description | ||
| parser = argparse.ArgumentParser( | ||
| description="Constructing spatial neighborhood with custom parameters" | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-c", "--coordinates", help="Path to coordinates (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-m", "--matrix", help="Path to (transformed) counts (as mtx).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-f", "--features", help="Path to features (as tsv).", required=True | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "-o", "--observations", help="Path to observations (as tsv).", required=True | ||
| ) | ||
|
|
||
|
|
||
| parser.add_argument( | ||
| "--n_rings", type=int,default=1,help="Number of rings of neighbors for grid data. Only used when coord_type = 'grid'", required=False | ||
| ) | ||
|
|
||
| parser.add_argument("-d", "--out_dir", help="Output directory.", required=True) | ||
|
|
||
| parser.add_argument( | ||
| "--config", | ||
| help="Optional config file (json) used to pass additional parameters.", | ||
| required=False, | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # Output files | ||
| from pathlib import Path | ||
|
|
||
| out_dir = Path(args.out_dir) | ||
|
|
||
| spatial_connectivities_file = out_dir / "spatial_connectivities.mtx" | ||
| spatial_distances_file = out_dir / "spatial_distances.mtx" | ||
|
|
||
| # Use these filepaths and inputs ... | ||
| coord_file = args.coordinates | ||
| matrix_file = args.matrix | ||
| feature_file = args.features | ||
| observation_file = args.observations | ||
|
|
||
| ## Custom parameters | ||
| n_rings = args.n_rings | ||
|
|
||
| if args.config is not None: | ||
| config_file = args.config | ||
|
|
||
|
|
||
| # ... or AnnData if you want | ||
| def get_anndata(args): | ||
| # Untested template | ||
| import anndata as ad | ||
| import pandas as pd | ||
| import scipy as sp | ||
|
|
||
| X = sp.io.mmread(args.matrix) | ||
| if sp.sparse.issparse(X): | ||
| X = X.tocsr() | ||
| observations = pd.read_table(args.observations, index_col=0) | ||
| features = pd.read_table(args.features, index_col=0) | ||
| coordinates = ( | ||
| pd.read_table(args.coordinates, index_col=0) | ||
| .loc[observations.index, :] | ||
| .to_numpy() | ||
| ) | ||
|
|
||
| adata = ad.AnnData( | ||
| X=X, obs=observations, var=features, obsm={"spatial": coordinates} | ||
| ) | ||
|
|
||
| return adata | ||
|
|
||
|
|
||
| adata = get_anndata(args) | ||
|
|
||
| ## Your code goes here | ||
| import squidpy as sq | ||
|
|
||
| sq.gr.spatial_neighbors(adata, coord_type="grid",n_rings=n_rings) | ||
|
|
||
| neighbors = adata.obsp["spatial_connectivities"].astype(int) | ||
| distance = adata.obsp["spatial_distances"].astype(int) | ||
niklasmueboe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Write output | ||
| import scipy as sp | ||
|
|
||
| out_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| sp.io.mmwrite(spatial_connectivities_file, neighbors) | ||
| sp.io.mmwrite(spatial_distances_file, distance) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.