-
Notifications
You must be signed in to change notification settings - Fork 728
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #221 from nf-core/salmon
[Feature Addition: Salmon]
Showing
20 changed files
with
1,117 additions
and
636 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,75 @@ | ||
#!/usr/bin/env python | ||
from __future__ import print_function | ||
from collections import OrderedDict, defaultdict, Counter | ||
import logging | ||
import argparse | ||
import glob | ||
import os | ||
|
||
# Create a logger | ||
logging.basicConfig(format='%(name)s - %(asctime)s %(levelname)s: %(message)s') | ||
logger = logging.getLogger(__file__) | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
def read_top_transcript(salmon): | ||
txs = set() | ||
fn = glob.glob(os.path.join(salmon, "*", "quant.sf"))[0] | ||
with open(fn) as inh: | ||
for line in inh: | ||
if line.startswith("Name"): | ||
continue | ||
txs.add(line.split()[0]) | ||
if len(txs) > 100: | ||
break | ||
logger.info("Transcripts found in FASTA: %s" % txs) | ||
return txs | ||
|
||
|
||
def tx2gene(gtf, salmon, gene_id, extra, out): | ||
txs = read_top_transcript(salmon) | ||
votes = Counter() | ||
gene_dict = defaultdict(list) | ||
with open(gtf) as inh: | ||
for line in inh: | ||
if line.startswith("#"): | ||
continue | ||
cols = line.split("\t") | ||
attr_dict = OrderedDict() | ||
for gff_item in cols[8].split(";"): | ||
item_pair = gff_item.strip().split(" ") | ||
if len(item_pair) > 1: | ||
value = item_pair[1].strip().replace("\"", "") | ||
if value in txs: | ||
votes[item_pair[0].strip()] += 1 | ||
|
||
attr_dict[item_pair[0].strip()] = value | ||
gene_dict[attr_dict[gene_id]].append(attr_dict) | ||
|
||
if not votes: | ||
logger.warning("No attribute in GTF matching transcripts") | ||
return None | ||
|
||
txid = votes.most_common(1)[0][0] | ||
logger.info("Attributed found to be transcript: %s" % txid) | ||
seen = set() | ||
with open(out, 'w') as outh: | ||
for gene in gene_dict: | ||
for row in gene_dict[gene]: | ||
if txid not in row: | ||
continue | ||
if (gene, row[txid]) not in seen: | ||
seen.add((gene, row[txid])) | ||
print("%s,%s,%s" % (row[txid], gene, row[extra]), file=outh) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="""Get tx to gene names for tximport""") | ||
parser.add_argument("--gtf", type=str, help="GTF file") | ||
parser.add_argument("--salmon", type=str, help="output of salmon") | ||
parser.add_argument("--id", type=str, help="gene id in the gtf file") | ||
parser.add_argument("--extra", type=str, help="extra id in the gtf file") | ||
parser.add_argument("-o", "--output", dest='output', default='tx2gene.csv', type=str, help="file with output") | ||
|
||
args = parser.parse_args() | ||
tx2gene(args.gtf, args.salmon, args.id, args.extra, args.output) |
This file contains 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 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,81 @@ | ||
#!/usr/bin/env Rscript | ||
|
||
args = commandArgs(trailingOnly=TRUE) | ||
if (length(args) < 2) { | ||
stop("Usage: tximeta.r <coldata> <salmon_out>", call.=FALSE) | ||
} | ||
|
||
path = args[2] | ||
coldata = args[1] | ||
|
||
sample_name = args[3] | ||
|
||
prefix = paste(c(sample_name, "salmon"), sep="_") | ||
|
||
tx2gene = "tx2gene.csv" | ||
info = file.info(tx2gene) | ||
if (info$size == 0){ | ||
tx2gene = NULL | ||
}else{ | ||
rowdata = read.csv(tx2gene, header = FALSE) | ||
colnames(rowdata) = c("tx", "gene_id", "gene_name") | ||
tx2gene = rowdata[,1:2] | ||
} | ||
|
||
fns = list.files(path, pattern = "quant.sf", recursive = T, full.names = T) | ||
names = basename(dirname(fns)) | ||
names(fns) = names | ||
|
||
if (file.exists(coldata)){ | ||
coldata = read.csv(coldata) | ||
coldata = coldata[match(names, coldata[,1]),] | ||
coldata = cbind(files = fns, coldata) | ||
}else{ | ||
message("ColData not avaliable ", coldata) | ||
coldata = data.frame(files = fns, names = names) | ||
} | ||
|
||
library(SummarizedExperiment) | ||
library(tximport) | ||
|
||
txi = tximport(fns, type = "salmon", txOut = TRUE) | ||
rownames(coldata) = coldata[["names"]] | ||
extra = setdiff(rownames(txi[[1]]), as.character(rowdata[["tx"]])) | ||
if (length(extra) > 0){ | ||
rowdata = rbind(rowdata, | ||
data.frame(tx=extra, | ||
gene_id=extra, | ||
gene_name=extra)) | ||
} | ||
rowdata = rowdata[match(rownames(txi[[1]]), as.character(rowdata[["tx"]])),] | ||
rownames(rowdata) = rowdata[["tx"]] | ||
se = SummarizedExperiment(assays = list(counts = txi[["counts"]], | ||
abundance = txi[["abundance"]], | ||
length = txi[["length"]]), | ||
colData = DataFrame(coldata), | ||
rowData = rowdata) | ||
if (!is.null(tx2gene)){ | ||
gi = summarizeToGene(txi, tx2gene = tx2gene) | ||
growdata = unique(rowdata[,2:3]) | ||
growdata = growdata[match(rownames(gi[[1]]), growdata[["gene_id"]]),] | ||
rownames(growdata) = growdata[["tx"]] | ||
gse = SummarizedExperiment(assays = list(counts = gi[["counts"]], | ||
abundance = gi[["abundance"]], | ||
length = gi[["length"]]), | ||
colData = DataFrame(coldata), | ||
rowData = growdata) | ||
} | ||
|
||
if(exists("gse")){ | ||
saveRDS(gse, file = "gse.rds") | ||
write.csv(assays(gse)[["abundance"]], paste(c(prefix, "gene_tpm.csv"), collapse="_"), quote=FALSE) | ||
write.csv(assays(gse)[["counts"]], paste(c(prefix, "gene_counts.csv"), collapse="_"), quote=FALSE) | ||
} | ||
|
||
saveRDS(se, file = "se.rds") | ||
write.csv(assays(se)[["abundance"]], paste(c(prefix, "transcript_tpm.csv"), collapse="_"), quote=FALSE) | ||
write.csv(assays(se)[["counts"]], paste(c(prefix, "transcript_counts.csv"), collapse="_"), quote=FALSE) | ||
|
||
# Print sessioninfo to standard out | ||
citation("tximeta") | ||
sessionInfo() |
Oops, something went wrong.