-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.py
139 lines (110 loc) · 5.12 KB
/
converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
#!/usr/bin/python
"""
Program: converter.py
Author: Andrew Blair
Email: andrew.blair@gladstone.ucsf.edu
Description: Cross compatible single cell object converter for Seurat version 3.1.2, Scanpy version 1.4.4, and SingleCellExperiment (sce) version 1.8.0. The first and second arguments must be the absolute paths for the input and desired output file type.
Example:
# Seurat to Scanpy
python3 converter.py --input_seurat <path to rds> --output_scanpy <path to h5ad>
# Scanpy to Seurat
python3 converter.py --input_scanpy <path to h5ad> --output_seurat <path to rds>
# Seurat to SingleCellExperiment
python3 converter.py --input_seurat <path to rds> --output_sce <path to Rdata>
# SingleCellExperiment to Seurat
python3 converter.py --input_sce <path to Rdata> --output_seurat <path to rds>
# SingleCellExperiment to Scanpy
python3 converer.py --input_sce <path to Rdata> --output_scanpy <path to rds>
Notes:
* This script is the primary interface for the single cell file object converter but relies on utils/converter.R, which users can run independently.
* Using subprocess module because sourcing Seurat using rpy2 causes segmentation fault.
"""
import os
import argparse
import subprocess
import pandas as pd
import scanpy as sc
import anndata2ri
import rpy2.robjects as robjects
anndata2ri.activate()
def scanpy_to_seurat(input_scanpy, output_seurat):
'''
Convert a Seurat object to a Scanpy object
:param input_scanpy: str, Scanpy object file path
:param output_seurat: str, Seurat object file path
return: None
'''
subprocess.call('Rscript utils/converter.R --scanpy_to_seurat ' + input_scanpy + ' ' + output_seurat, shell=True)
def seurat_to_sce(input_seurat, output_sce, meta_export='no'):
'''
Convert a Seurat object to a SingleCellExperiment object
:param input_seurat: str, Seurat object file path
:param output_sce: str, SingleCellExperiment object file path
return: None
'''
subprocess.call('Rscript utils/converter.R --seurat_to_sce ' + input_seurat + ' ' + output_sce + ' --meta ' + meta_export, shell=True)
def seurat_to_scanpy(input_seurat, output_scanpy):
'''
Convert a Seurat object to a Scanpy object
:param input_seurat: str, Seurat object file path
:param output_scanpy: str, SingleCellExperiment object file path
return: None
'''
seurat_to_sce(input_seurat, 'sce.rds', meta_export='yes')
meta_df = pd.read_csv('meta.csv')
os.remove('meta.csv')
sce_to_scanpy('sce.rds', output_scanpy, meta_df = meta_df, remove_sce=True)
def sce_to_scanpy(input_sce, output_scanpy, meta_df=None, remove_sce=False):
'''
Convert a SingleCellExperiment object to a Scanpy object
:param input_sce: str, SingleCellExperiment object file path
:param output_scanpy: str, Scanpy object file path
return: None
'''
readRDS = robjects.r['readRDS']
adata = readRDS(input_sce)
if remove_sce:
os.remove('sce.rds')
if not meta_df.empty:
meta_df = meta_df.set_index('Unnamed: 0')
meta_df.index.name = 'index'
adata.obs = meta_df
adata.write(output_scanpy)
def main(input_scanpy, input_seurat, input_sce, output_scanpy, output_seurat, output_sce):
'''
Convert single cell object file type
return: None
'''
# Scanpy to Seurat
if None not in [input_scanpy, output_seurat]:
scanpy_to_seurat(input_scanpy, output_seurat)
# Seurat to Scanpy
if None not in [input_seurat, output_scanpy]:
seurat_to_scanpy(input_seurat, output_scanpy)
# Seurat to SingleCellExperiment
if None not in [input_seurat, output_sce]:
seurat_to_sce(input_seurat, output_sce)
# SingleCellExperiment to Seurat
if None not in [input_sce, output_seurat]:
sce_to_seurat(input_sce, output_seurat)
# SingleCellExperiment to Scanpy
if None not in [input_sce, output_scanpy]:
sce_to_scanpy(input_sce, output_scanpy)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Convert a single cell file format.')
# Inputs
parser.add_argument('--input_scanpy', metavar='path', required=False,
help='Input Scanpy object file path.')
parser.add_argument('--input_seurat', metavar='path', required=False,
help='Input Seurat object file path.')
parser.add_argument('--input_sce', metavar='path', required=False,
help='Input SingleCellExperiment object file path.')
# Output
parser.add_argument('--output_scanpy', metavar='path', required=False,
help='Desired output object file path.')
parser.add_argument('--output_seurat', metavar='path', required=False,
help='Desired output object file path.')
parser.add_argument('--output_sce', metavar='path', required=False,
help='Desired output object file path.')
args = parser.parse_args()
main(input_scanpy=args.input_scanpy, input_seurat=args.input_seurat, input_sce=args.input_sce, output_scanpy=args.output_scanpy, output_seurat=args.output_seurat, output_sce=args.output_sce)