library(ASRgenomics)
library(dplyr)

#### Load the marker data #####
m <- read.table("...", stringsAsFactors = F)

marker_names <- m[,1]
head(marker_names)

##### Transpose dataset #####
m.t <- as.data.frame(t(as.matrix(m)),stringsAsFactors = F)

##### Take the first row as column names #####
names(m.t) <- marker_names

##### Rename the first column to entryname #####
names(m.t)[1] <- "EntryName"

#### Remove the first row #####
m.t <- m.t[-1,]

##### Reset rownames #####
rownames(m.t) <- NULL 

##### Make the entryname as rownames ####
m.t$EntryName <- factor(m.t$EntryName)
row.names(m.t) <- m.t[,1]

##### Remove first column ####
m.t.2 <- m.t[,-1]


#### Print first five rows and columns for example (showing the marker format) #####
m.t.2[1:5,1:5]

## The marker format ##
############################################# 
# Geno snp_1  snp_2  snp_3  snp_4  snp_5    #
# G1      CC     CG     AA     AA     AG    #
# G2      AA     GG     AA     AA     AA    #
# G3    <NA>     GG     AA   <NA>   <NA>    #
# G4      CC     GG     AA     AA     AG    #
# G5      AA     CG     AA     AA     AG    #
#############################################


#### Convert to matrix ######
geno.sp <- as.matrix(m.t.2)

#### Marker cleaning with ASRgenomics #####
marker_filter <- qc.filtering(M = geno.sp , base = T, ref = NULL,
                              maf = 0.05, marker.callrate = 0.1, ind.callrate = 1, impute = T,
                              na.string = NA, plots = TRUE)

dim(marker_filter$M.clean)

##### These functions to check the markers after filtering ######
##### The ASRgenomics explain the details for each functions #####
marker_filter$plot.heteroz
marker_filter$plot.missing.ind
marker_filter$plot.missing.SNP
marker_filter$plot.maf

Marker_PP_Clean <- marker_filter$M.clean

#### Print 5 columns and 5 rows of the Marker_PP_Clean
Marker_PP_Clean[1:5, 1:5]

############################################
# Geno snp_1  snp_2  snp_3  snp_4  snp_5   # 
# G1       1      0      2      2      2   #
# G2       0      2      0      2      2   #
# G3       0      0      2      0      2   #
# G4       0      1      2      2      0   #
# G5       1      1      0      2      2   #
############################################


# Save marker as an RDS file
saveRDS(Marker_PP_Clean,file = "Marker_PP_Clean.rds")