Skip to content

Commit

Permalink
update for csv tag
Browse files Browse the repository at this point in the history
  • Loading branch information
ajitjohnson committed Jan 26, 2025
1 parent e05bcab commit 89ccce4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]

name = "SCIMAP"
version = "2.2.10"
version = "2.2.11"
description = "Spatial Single-Cell Analysis Toolkit"

license = "MIT"
Expand Down
93 changes: 85 additions & 8 deletions scimap/cli/_scimap_mcmicro.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pathlib
import textwrap
from joblib import Parallel, delayed
import time


# Actual mcmicro code
Expand All @@ -18,7 +19,7 @@ def mcmicro_wrap(argv=sys.argv):
parser = argparse.ArgumentParser()

parser.add_argument(
'csv',
'input_csv',
help='single-cell quantification table (CSV file) generated by mcmicro',
)
parser.add_argument('-o', '--output', default='.', help='output directory')
Expand All @@ -29,6 +30,11 @@ def mcmicro_wrap(argv=sys.argv):
nargs='+',
help='specify one or more clustering methods to run',
)
parser.add_argument(
'--csv',
action='store_true',
help='output csv version of the h5ad file',
)

args = parser.parse_args(argv[1:])

Expand All @@ -41,16 +47,27 @@ def mcmicro_wrap(argv=sys.argv):

results = []
for m in methods:
# Pass arguments as a list to clustering function
result = clustering(
[None, args.csv, '-o', args.output, '--clustering-method', m]
[
'clustering', # Program name as first arg
args.input_csv, # The input CSV path
'-o',
args.output,
'--clustering-method',
m,
]
)
results.append(result)

print(results)

# Run merge if we have more than one method
if len(methods) > 1:
merge([None, args.output, '-o', args.output, '-d', '--csv'])
merge_args = [None, args.output, '-o', args.output]
if args.csv:
merge_args.append('--csv') # Pass the csv flag to merge
merge(merge_args)

# move pdf files to output / plots
output_dir = pathlib.Path(args.output)
Expand All @@ -67,6 +84,35 @@ def mcmicro_wrap(argv=sys.argv):
move_to.parent.mkdir(exist_ok=True, parents=True)
p.replace(move_to)
print()

# Generate CSV if requested, regardless of number of methods
if args.csv:
output_dir = pathlib.Path(args.output)
try:
# First check method directories
csv_generated = False
for method in methods:
method_dir = output_dir / method
h5ad_files = list(method_dir.glob('*.h5ad'))
for h5ad_file in h5ad_files:
if h5ad_file.exists():
print(f"Generating CSV for {h5ad_file}")
hl.scimap_to_csv(adata=str(h5ad_file), output_dir=method_dir)
csv_generated = True

# Then check for merged file
merged_file = output_dir / f'{pathlib.Path(args.input_csv).stem}.h5ad'
if merged_file.exists():
print(f"Generating CSV for merged file: {merged_file}")
hl.scimap_to_csv(adata=str(merged_file), output_dir=output_dir)
csv_generated = True

if not csv_generated:
print("Warning: No h5ad files found to convert to CSV")

except Exception as e:
print(f"Error generating CSV: {str(e)}")

return 0


Expand Down Expand Up @@ -163,12 +209,15 @@ def merge(argv=sys.argv):
)

args = parser.parse_args(argv[1:])
print(f"Merge received args: {args}") # Debug print

input_dir = pathlib.Path(args.directory)
output_dir = pathlib.Path(args.output)
delete_after = args.delete_merged
output_csv = args.csv

print(f"output_csv flag value: {output_csv}") # Debug print

input_files = sorted(input_dir.rglob('*.h5ad'))

if len(input_files) == 0:
Expand Down Expand Up @@ -200,14 +249,39 @@ def merge(argv=sys.argv):

try:
# Merge data
print(f"Starting merge with csv flag: {output_csv}")

# Create output directory if it doesn't exist
output_dir.mkdir(parents=True, exist_ok=True)

# Ensure all input files exist and are readable
for f in input_files:
if not f.exists():
print(f"Error: Input file not found: {f}")
return 1

# Perform merge operation
print("Merging h5ad files...")
hl.merge_adata_obs(adata=[str(p) for p in input_files], output_dir=output_dir)

# Wait a moment and verify the merged file exists
time.sleep(1) # Give filesystem time to sync

if not output_file.exists():
print(f"Error: Failed to create merged file: {output_file}")
return 1

print(f"Successfully created merged file: {output_file}")

# Generate CSV if requested
if output_csv:
print(f"Generating CSV file for: {output_file}")
hl.scimap_to_csv(adata=str(output_file), output_dir=output_dir)
print(
textwrap.indent(
str(output_file.parent / f'{output_file.stem}.csv\n'), ' '
)
)
csv_file = output_file.parent / f'{output_file.stem}.csv'
if csv_file.exists():
print(f"Successfully created CSV file: {csv_file}")
else:
print(f"Warning: CSV file was not created: {csv_file}")

if delete_after:
if output_file in input_files:
Expand All @@ -224,6 +298,9 @@ def merge(argv=sys.argv):
return 0
except Exception as e:
print(f"Error during merge: {str(e)}")
import traceback

traceback.print_exc()
return 1


Expand Down

0 comments on commit 89ccce4

Please sign in to comment.