File size: 1,496 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
#
# Combine transcripts with the tximport package. 
# 
# https://bioconductor.org/packages/release/bioc/html/tximport.html
#
# Transcript level summarization.
#

# Load the packages
library(tximport)

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

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

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

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

# Inform the user.
print("# Tool: Combine transcripts")
print(paste("# Sample: ", desing_file))
print(paste("# Data dir: ", data_dir))

# Read the sample file
sample_data <- read.csv(desing_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")

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

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

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

# Create a new column for transcript ids.
df$ensembl_transcript_id = rownames(df)

# List the desired column order.
cols <- c("ensembl_transcript_id", 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))