Snakemake pipeline for single-cell long-read sequencing

public public 1yr ago 0 bookmarks

This documentation - with additional info - will be hosted here at some point

Clone the repository

#clone
git clone --recursive https://github.com/davidebolo1993/smk_sc_lr
cd smk_sc_lr

Create a dedicated conda environment

Setting up

config/config.yaml and config/samples.tsv manually, then:

#print help
./workflow/scripts/prepare.sh

Running individual rules on slurm cluster

Code Snippets

19
20
21
22
23
24
25
26
27
28
29
shell:
	'''
	cd results/ill/cellranger_count \
	&& rm -rf {params.sample_id} \
	&& cellranger count \
		--id {params.sample_id} \
		--transcriptome {input.ref} \
		--fastqs {params.fq_folder} \
		--localcores {threads} \
		--localmem {resources.mem_mb}
	'''
20
21
22
23
24
25
26
27
28
29
30
31
shell:
	'''
	cd results/ill/cellranger_vdj \
	&& rm -rf {params.sample_id} \
	&& cellranger vdj \
		--id {params.sample_id} \
		--reference {input.ref} \
		--fastqs {params.fq_folder} \
		--localcores {threads} \
		--localmem {resources.mem_mb} \
		--sample {params.sample_id_2}
	'''
SnakeMake From line 20 of rules/vdj.smk
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
shell:
	'''
	nextflow run resources/wf-single-cell \
		-w {params.out_folder}/workspace \
		-profile local \
		-c resources/single-cell-resources/wf-single-cell.config \
		--fastq {params.fq_folder} \
		--single_cell_sample_sheet {params.sample_sheet} \
		--ref_genome_dir {input.ref} \
		--out_dir {params.out_folder} \
		--matrix_min_genes 1 \
		--matrix_min_cells 1 \
		--matrix_max_mito 100 \
		--max_threads {threads} \
		--umi_cluster_max_threads {threads} \
		--resources_mm2_max_threads {threads} \
		--merge_bam
	'''
59
60
61
62
shell:
	'''
	Rscript workflow/scripts/tsvtomtx.r -c {input.tsv} -g {input.gtf} -b -o {params.out_folder}
	'''
76
77
78
79
shell:
	'''
	Rscript workflow/scripts/tsvtomtx.r -c {input.tsv} -g {input.gtf} -t -b -o {params.out_folder}
	'''
  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
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
library(Matrix)
library(optparse)
library(UniProt.ws)
library(OmnipathR)

sessionInfo()

writeMMgz <- function(x, file) {
	mtype <- "real"
	if (is(x, "ngCMatrix")) {
		mtype <- "integer"
	}
	zz<-gzfile(file,"w")
	writeLines(
		c(
			sprintf("%%%%MatrixMarket matrix coordinate %s general", mtype),
			sprintf("%s %s %s", x@Dim[1], x@Dim[2], length(x@x))
		),
		zz
	)
	close(zz)
	data.table::fwrite(
		x = summary(x),
		file = file,
		append = TRUE,
		sep = " ",
		row.names = FALSE,
		col.names = FALSE
	)
}

writeGzFile <- function(x,file,has_colnames){
	data.table::fwrite(
		x = x,
		file = file,
		sep = "\t",
		row.names = FALSE,
		col.names = has_colnames
	)
}

options(warn = -1)

option_list = list(
  	make_option(c('-c', '--counts'), action='store', type='character', help='gene/transcript counts .tsv'),
	make_option(c('-g', '--gtf'), action='store', type='character', help='gene model in .gtf format'),
	make_option(c('-o', '--output'), action='store', type='character', help='output directory'),
	make_option(c('-t', '--transcript'), action='store_true', default=FALSE, help='use transcript matrix instead of gene matrix'),
	make_option(c('-b', '--biotype'), action='store_true', default=FALSE, help='additionally store a features-like file with biotypes')
)

opt = parse_args(OptionParser(option_list=option_list))

print(opt)

# create directory
dir.create(file.path(opt$output), showWarning=F)

# generate single-cell RNA seq data
now<-Sys.time()
message('[',now,'][Message] reading gene/transcript x cell .tsv') 

gbm<-as.matrix(data.table::fread(file.path(opt$counts)),header=T,rownames=1)

now<-Sys.time()
message('[',now,'][Message] done')
message('[',now,'][Message] converting to sparse matrix')

# save sparse matrix
sparse.gbm <- Matrix(gbm,sparse = T)

now<-Sys.time()
message('[',now,'][Message] done')
message('[',now,'][Message] storing to file')
## Market Exchange Format (MEX) format
writeMMgz(x=sparse.gbm, file=file.path(opt$output,"matrix.mtx.gz"))

now<-Sys.time()
message('[',now,'][Message] done')
message('[',now,'][Message] loading gene model')


#load gene model - gtf
gtf<-rtracklayer::import(file.path(opt$gtf))
gtf_df<-data.table::as.data.table(gtf)

now<-Sys.time()
message('[',now,'][Message] done')

#maybe there are better ways - but this is pretty fast

if (!opt$transcript) {

	#we have name -> we get id
	message('[',now,'][Message] translating gene names to ensembl gene ids')
	vals<-do.call(c,lapply(rownames(gbm),function(x) {unique(gtf_df[gene_name == x]$gene_id)[1]}))
	now<-Sys.time()
	message('[',now,'][Message] done')
	message('[',now,'][Message] storing to file')

	writeGzFile(x = data.frame(V1=rownames(gbm),V2=vals, V3="Gene Expression"), file =file.path(opt$output,"features.tsv.gz"),has_colnames=FALSE)

	if (opt$biotype) {

		now<-Sys.time()
		message('[',now,'][Message] extracting biotypes')
		bios<-do.call(c,lapply(rownames(gbm),function(x) {unique(gtf_df[gene_name == x]$gene_type)[1]}))
		now<-Sys.time()
		message('[',now,'][Message] done')
		message('[',now,'][Message] storing to file')
		gns_df<-data.frame(V1=rownames(gbm),V2=vals, V3=bios)
		colnames(gns_df)<-c("gene_name", "gene_id", "gene_type")	
		writeGzFile(x = gns_df, file =file.path(opt$output,"biotypes.tsv.gz"),has_colnames=TRUE)
	}


} else {

	#we have id-> we get name
	message('[',now,'][Message] translating transcript ids to ensembl transcript names')
	vals<-do.call(c,lapply(rownames(gbm),function(x) {unique(gtf_df[transcript_id == x]$transcript_name)[1]}))	

	now<-Sys.time()
	message('[',now,'][Message] done')
	message('[',now,'][Message] storing to file')

	writeGzFile(x = data.frame(V1=vals,V2=rownames(gbm), V3="Gene Expression"), file =file.path(opt$output,"features.tsv.gz"),has_colnames=FALSE)

	if (opt$biotype) {

		now<-Sys.time()
		message('[',now,'][Message] extracting biotypes')
		bios<-do.call(c,lapply(rownames(gbm),function(x) {unique(gtf_df[transcript_id == x]$transcript_type)[1]}))
		now<-Sys.time()
		message('[',now,'][Message] done')
		trns_df<-data.frame(V1=vals,V2=rownames(gbm), V3=bios)
		message('[',now,'][Message] retrieving uniprot ids')
		entrez_df<-as.data.frame(uniprot_id_mapping_table(trns_df$V2, from="enst", to="uniprot", chunk_size = NULL))
		trns_df$V4 <- entrez_df$To[match(trns_df$V2, entrez_df$From)]
		now<-Sys.time()
		message('[',now,'][Message] done')
		message('[',now,'][Message] querying uniprot db for features')

		#query routine
		up <- UniProt.ws(taxId=9606)
		kt<-"UniProtKB"
		columns<-c("cc_subcellular_location","ft_transmem","xref_ensembl")
		res<-select(up,trns_df$V4,columns,kt)
		res_df<-as.data.frame(res)
		now<-Sys.time()
		message('[',now,'][Message] done')
		message('[',now,'][Message] matching informations and storing to file')

		#subcellular location
		trns_df$V5<-res_df$Subcellular.location..CC.[match(trns_df$V4, res_df$From)]
		#transmembrane
		trns_df$V6<-res_df$Transmembrane[match(trns_df$V4, res_df$From)]
		#ensembl_ids
		trns_df$V7<-res_df$Ensembl[match(trns_df$V4, res_df$From)]

		#to None to facilitate downstream analysis in python
		trns_df[is.na(trns_df)] <- "None"
		colnames(trns_df)<-c("transcript_name", "transcript_id", "transcript_type", "protein_id", "cc_subcellular_location", "ft_transmem", "xref_ensembl")

		#write
		writeGzFile(x = trns_df, file =file.path(opt$output,"biotypes.tsv.gz"), has_colnames=TRUE)
	}

}

now<-Sys.time()
message('[',now,'][Message] done')
message('[',now,'][Message] storing cell barcodes to file')

#barcodes
writeGzFile(x = data.frame(V1=paste0(colnames(gbm), "-1")), file=file.path(opt$output,"barcodes.tsv.gz"), has_colnames=FALSE)

now<-Sys.time()
message('[',now,'][Message] done')
ShowHide 5 more snippets with no or duplicated tags.

Login to post a comment if you would like to share your experience with this workflow.

Do you know this workflow well? If so, you can request seller status , and start supporting this workflow.

Free

Created: 1yr ago
Updated: 1yr ago
Maitainers: public
URL: https://github.com/davidebolo1993/smk_sc_lr
Name: smk_sc_lr
Version: 1
Badge:
workflow icon

Insert copied code into your website to add a link to this workflow.

Accessed: 47
Downloaded: 0
Copyright: Public Domain
License: GNU General Public License v3.0
  • Future updates

Related Workflows

cellranger-snakemake-gke
snakemake workflow to run cellranger on a given bucket using gke.
A Snakemake workflow for running cellranger on a given bucket using Google Kubernetes Engine. The usage of this workflow ...