## The TriComp Function: # 0 Input: # 0.1 Required: # df = input data.frame # var1, var2, var3 = Column names of the three input variables # VariablesAreLog = {TRUE/FALSE} denotes if the input values are already logarithmised or not # 0.2 Optional parameters # Prefix: Will attach the prefix to all output variables. Useful when performing multiple Tricomp algorithms on the same dataset # 1 Process # 1.1 if necessary converts the input variables to logarithmised format # 1.2 Calculates the logarithmic fold change for each pair # 1.3 Determines the Highest Absolute Fold Change difference between any pairs # 2 Calculates two cartesian plotting variables (a and b) # 3 Converts the two cartesian coordinates into polar coordinates # 3.1 The radius (r), which corresponds to the relative intensity of the difference between the three input values # 3.2 The Degree variable (theta, between 0 and 360 degrees) which corresponds to the relationship. # 3.3 Summarises Degree with a group variable (Group 1:6) # 4. The original dataframe is returned including the new variables with the chosen prefix attached # logFC2over1 ## Calculates relative change from Group 1 to Group 2 # logFC3over1 ## Calculates relative change from Group 1 to Group 3 # logFC3over2 ## Calculates relative change from Group 2 to Group 3 # HighestlogFC ## Calculates the absolute highest logFC between any of the three groups # a ##Coordinate for plotting # b ## Coordinate for plotting # Group ##Category # Distance ##Polar coordinate # Degree ##Polar coordinate TriComp <- function(df, var1, var2, var3, VariablesAreLog = TRUE, Prefix = "TriComp_"){ save = df #1.1 if (VariablesAreLog == FALSE){ df$logFC2over1=log(df[,var2],2)-log(df[,var1],2) df$logFC3over1=log(df[,var3],2)-log(df[,var1],2) df$logFC3over2=log(df[,var3],2)-log(df[,var2],2) } else { df$logFC2over1=df[,var2]-df[,var1] df$logFC3over1=df[,var3]-df[,var1] df$logFC3over2=df[,var3]-df[,var2] } #1.3 df$HighestlogFC= pmax(abs(df$logFC2over1),abs(df$logFC3over1),abs(df$logFC3over2)) #2 df$a= - df$logFC3over1 + sin(pi/6)*df$logFC2over1 df$b= - cos(pi/6)*df$logFC2over1 #3.1 df$Distance=sqrt(df$a^2 + df$b^2) #3.2 df$Degree <- 0 for (i in 1:nrow(df)){ if (is.na(df$a[i]) == TRUE) { df$Degree[i] <- NA } else if (df$a[i] >= 0 & df$b[i] > 0){ df$Degree[i] <- atan(abs(df$a[i]/df$b[i]))*180/pi } else if (df$a[i] >= 0 & df$b[i] < 0){ df$Degree[i] <- 180 - atan(abs(df$a[i]/df$b[i]))*180/pi } else if (df$a[i] < 0 & df$b[i] > 0){ df$Degree[i] <- 360 -atan(abs(df$a[i]/df$b[i]))*180/pi } else if (df$a[i] < 0 & df$b[i] < 0){ df$Degree[i] <- 180 + atan(abs(df$a[i]/df$b[i]))*180/pi } else if (df$a[i] > 0 & df$b[i] == 0){ df$Degree[i] <- 90 } else if (df$a[i] < 0 & df$b[i] == 0){ df$Degree[i] <- 270 } else if (df$a[i] == 0 & df$b[i] == 0){ df$Degree[i] <- NA }} #3.3 df$Group=floor(df$Degree/60) + 1 ##Category 1-6 based on Degree #4 keep <- c("logFC2over1", "logFC3over1", "logFC3over2", "HighestlogFC", "a", "b", "Degree", "Group") df <- cbind(save,df[,keep]) colnames(df)[match(keep,colnames(df))] <- paste0(Prefix, keep) return(df) }