# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .r
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: R
#     language: R
#     name: ir
# ---

# Load Packages
library(ggplot2)
library(dplyr)
library(plyr)

# Load data 
filename <- "./data/Encode_HMM_data.txt"
data <- read.csv(filename, sep = '\t', header = FALSE)

# Examine Data 
head(data)

# Rename the columns for visualization
names(data)[1:4] <- c("chrom", "start", "stop", "type")

# Take a look at dataset
head(data)

# ## Create a Bar Plot

# Bar Plot 
ggplot(data, aes(x=chrom, fill=type))+
    geom_bar()

# Save plot in png format 
png("figs/plot.png")
ggplot(data, aes(x=chrom, fill=type))+
    geom_bar()
dev.off()

# Save plot in pdf format 
pdf("figs/plot.pdf")
ggplot(data, aes(x=chrom, fill=type))+
    geom_bar()
dev.off()

# Save plot in jpeg format 
jpeg("figs/plot.jpeg")
ggplot(data, aes(x=chrom, fill=type))+
    geom_bar()
dev.off()

# Save plot in tiff format 
tiff("figs/plot.tiff")
ggplot(data, aes(x=chrom, fill=type))+
    geom_bar()
dev.off()

# Save plot in png format in high resulation
png("figs/plot.png", 1000, 1000)
ggplot(data, aes(x=chrom, fill=type))+
    geom_bar()
dev.off()

# ## Basic Statistics

# Check data matrix 
dim(data)

# Summary statistics 
summary(data)

# Data structure
str(data)

# Summary for chromosomes 
summary(data$chrom)

# Summary for type 
summary(data$type)

# Summary for start position
summary(data$start)

# Summary for stop position
summary(data$stop)

# Create a new column is called size 
data$size = data$stop - data$start

# Now, take a look at dataset
head(data)

# Summary for size 
summary(data$size)

# Mean size 
mean(data$size)

# Meadian size 
median(data$size)

# Standard deviation
sd(data$size)

# Max size
max(data$size)

# Min size 
min(data$size)

# ## Customize Plot

# Bar Plot 
ggplot(data, aes(x=chrom, fill=type))+
    geom_bar()

# Remove `chr` prefix 
data$chrom <- factor(gsub("chr", "", data$chrom))

# See the result with `chr` removed 
summary(data$chrom)

# Now plot the data 
ggplot(data, aes(x=chrom, fill=type))+
    geom_bar()

# Reorder the chromosomes numerically
data$chrom <- factor(data$chrom, levels = c(seq(1,22), "X", "Y"))

# See the ordered chromosomes 
summary(data$chrom)

# Ordering
ggplot(data, aes(x=chrom, fill=type))+
    geom_bar()

data$type <- revalue(data$type, c("1_Active_Promoter"="Promoter", "4_Strong_Enhancer"="Enhancer", "8_Insulator"="Insulator"))

data 

StarcoderdataJupyterScriptsDedupFiltered