File size: 2,052 Bytes
90f526e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#
# Combine transcripts into genes with the tximport package.
# 
# https://bioconductor.org/packages/release/bioc/html/tximport.html
#

# Load the packages
library(tximport)

# The directory where the counts for each sample are located.
data_dir <- 'salmon'

# The sample file is in CSV format and must have the headers "sample" and "condition".
design_file = "design.csv"

# What software created the mappings.
method <- "salmon"

# The ouput file name.
output_file = "counts.csv"

# The name of the file that contains transcript to gene mapping.
# See our guide on how to make a mapping file.
tx2gene_file = "tx2gene.csv"

# Inform the user.
print("# Tool: Combine genes")
print(paste("# Sample: ", design_file))
print(paste("# Data dir: ", data_dir))
print(paste("# Gene map: ", tx2gene_file))

# Read the sample file
sample_data <- read.csv(design_file, stringsAsFactors=F)

# Isolate the sample names.
sample_names <- sample_data$sample

# Generate the file names that contain the quantification data.
files <- file.path(data_dir,  sample_names, "quant.sf")

# Read the transcript to gene mapping file.
tx2gene = read.csv(tx2gene_file)

# Summarize over transcripts.
tx <- tximport(files, type=method, tx2gene=tx2gene)

# Transform into a dataframe.
df <- data.frame(tx$counts)

# Set the column names
colnames(df) <- sample_names

# Add a rowname by gene id
df$ensembl_gene_id = row.names(df)

# Create a smaller data frame that connects gene to name.
id2name = tx2gene[, c("ensembl_gene_id", "external_gene_name")]

# Need to de-duplicate so the merge won't create new entries.
id2name = id2name[!duplicated(id2name),]

# Add the gene names to the list
df = merge(df, id2name, by="ensembl_gene_id", all.x = TRUE, all.y = FALSE)

# This will be the new column order.
cols <- c("ensembl_gene_id", "external_gene_name", sample_names)

# Reorganize the columns.
df <- df[, cols]

# Save the  resulting summarized counts.
write.csv(df, file=output_file,  row.names = FALSE, quote = FALSE)

# Inform the user.
print(paste("# Results: ", output_file))