Unsupervised machine learning methods such as hierarchical clustering allow us to discover the trends and patterns of similarity within the data. Here, I demonstrate by using a test data, how to apply the Hierarchical clustering on columns of a test data matrix. Note that as my main focus is Bioinformatics application, I assume that the columns of the matrix represent individual samples and the rows represent the genes or transcripts or some other biological feature. However, as the application of clustering algorithms are not restricted to biology the rows or the column of the matrix may represent other things based on the field of research ! For the distance metric, I will use the Spearman correlation based distance supported by the Dist function of amap package. For a skewed data, it is  a good idea to check the similarity of the orders of the values rather than their linear relationship (i.e. Pearson correlation) or how geometrically close the values are (i.e. Euclidean distance). For more info, you can see an example that I provided in one of my previous posts on how Spearman correlation may discover associations more efficiently for a skewed data. Furthermore, check the "Details" in the manual for the various methods supported by the hclust function.


values<- matrix(rnorm(1000),ncol=20)
colnames(values)<- paste("col",1:20,sep="")
library(amap)
hRes<- hclust(Dist(t(values), method="spearman"))
plot(hRes)



 

After running Hierarchical clustering we can cut the result binary tree at a certain depth or request that it be cut in a manner that would result a certain number of clusters. Here, I request that the resulted binary tree be cut in away that would result to 2 sample clusters. Furthermore, I convert the resulted tree to a "dendogram" object and colour the branches and the labels of the tree to visualize the 2 clusters. One can use color_branches and color_labels functions to cut and colour the trees.

library(dendextend)

# Cut and colour
hResDen<- as.dendrogram(hRes)
hResCut<- cutree(hResDen,2)
hResDen <- color_branches(hResDen, k= 2)
hResDen <- color_labels(hResDen, k= 2)
plot(hResDen)


 

Alternatively, one can use color_branches and color_labels functions to manually define the colours of the labels and the branches of the tree.

# manual colouring based on cut results
colours<- c(2,3)
hResDen<- as.dendrogram(hRes)
colOrder<- hRes$order
hResDen <- color_branches(hResDen,clusters=hResCut[colOrder],col=colours)
lableCol<- colours
names(lableCol)<- unique(hResCut[colOrder])
hResDen <- color_labels(hResDen,col=lableCol[as.character(hResCut[colOrder])])
plot(hResDen)




But what if we want to colour the branches and the labels of the tree based on a predefined grouping of the samples ? Here, we colour the labels and the edges leading to them to visualize the position of "class1", "class2" and "class3" samples in the tree.

# Manual colouring based on some predefined classes

sampleClass<- c(rep("class1",5), rep("class2",6), rep("class3",9))
colours<- c("lightblue","green", "red")
hResDen<- as.dendrogram(hRes)
colOrder<- hRes$order
hResDen <- color_branches(hResDen,clusters=as.numeric(as.factor(sampleClass[colOrder])),col=colours)
lableCol<- colours
names(lableCol)<- unique(sampleClass[colOrder])
hResDen <- color_labels(hResDen,col=lableCol[as.character(sampleClass[colOrder])])
plot(hResDen)


0

Add a comment

In this post I show how groupScatterPlot(), function of the rnatoolbox R package can be used for plotting the individual values in several groups together with their mean (or other statistics). I think this is a useful function for plotting grouped data when some groups (or all groups) have few data points ! You may be wondering why to include such function in the rnatoolbox package ?! Well ! I happen to use it quit a bit for plotting expression values of different groups of genes/transcripts in a sample or expression levels of a specific gene/transcript in several sample groups. These expression value are either FPKM, TPM, LCPM, or PSI values (Maybe I should go through these different normalizations later in a different post 😐!). But of course its application is not restricted to gene expression or RNAseq data analysis.

In this post I show how classifySex(), function of the rnatoolbox R package can be used for inferring the sex of  the studied subjects from their binary alignment bam files. The sex can be a source of unwanted variation within the data, for which you may want to adjust your differential gene expression or splicing analysis. However, complete  metadata are unfortunately not always available. Furthermore, sometimes details within metadata are incorrect or have been misplaced due to manual error. Therefore, it is a good practice to quickly double check some details within the data to either complete the missing metadata information or to make sure that the prior stages have been performed without any accidental mix-ups. For muscle tissues, this showed to be useful on our ribo-depleted RNAseq data. NOTE! Earlier the function referred to in this post was named differently(i.e. getGender). Since version 0.2.1 classifySex() is used.
2

Recently I have started to organize my commonly used functions related to quality assessment and analyzing RNAseq data into an R package. It is called rnatoolbox and it is available here. In this post I introduce getMappedReadsCount(), i.e. a function that can be used for checking the number of aligned/mapped fragments in several bam files and detecting the outliers. The outliers are the bam files with oddly high (i.e. exceeding1.5 times the interquartile) and oddly low (i.e. lower than 1.5 times the interquartile) number of mapped fragments.

The adhan package is available here !

The prayer times cannot always be estimated accurately in some places such as countries located in higher latitudes (e.g. the Nordic countries) . as for instance during midsummer time the Fajr may be impossible to estimate or in other words it may simply not exist ! Some Muslim residents of those countries follow Prayer times of other places such as Mecca and Medina.

Unsupervised machine learning methods such as hierarchical clustering allow us to discover the trends and patterns of similarity within the data. Here, I demonstrate by using a test data, how to apply the Hierarchical clustering on columns of a test data matrix. Note that as my main focus is Bioinformatics application, I assume that the columns of the matrix represent individual samples and the rows represent the genes or transcripts or some other biological feature.

Note ! the & sign is to run the command in background.

Getting MD5 sum for all files and writing it to a txt file in Linux.

md5sum * > myChecklist.txt &

Getting MD5 sum for all files and subfolders and writing it to a txt file in Linux.

find ./ -type f -exec md5sum {} + > myChecklist.txt &

Getting MD5 sum for all files and writing it to a txt file in Mac.

md5 -r * > myChecklist.txt &

Getting MD5 sum for all files and subfolders and writing it to a txt file in Mac.

Many times, in our projects, we may need to compare different measured factors in our samples to one another, and study whether they are linearly dependent. These information can also help us to detect covariates and factors that affect our studies but we would like to adjust for/remove their effects (more on this at sometime later). Here, I mention several functions that can be used to perform correlation tests. All of these functions do support both Pearson and ranked (Spearman) methods. Note that in the end of this post I will focus on these two different methods (i.e. Pearson vs Spearman) and show their differences in application.
2

Occasionally when indexing data frames the format is converted, leading to confusing consequences. As for instance, when indexing to select a single column the result is a 'numeric' or 'integer' vector. The following  demonstrates this :

When analyzing a data constructed of individuals (or samples from individuals) of both male and female of a species (e.g. humans), often it is a good idea to compare the distribution of the various studied parameters for the males to those for the females. As for instance in RNAseq analysis, it is the measured expression of many genes may differ significantly between the studied males and females. In other words the gender may exhibit 'batch effect' in the gene expression data.

Here is an example of plotting 4 venn diagrams in a single screen with a 2*2 layout.

library(VennDiagram)

#defining vectors

av<- 1:10

bv<- 12:20

cv<-  7:15

# Building venndiagram grid objects (i.e.
1
Labels
Blog Archive
About Me
About Me
My Photo
I am a Postdoc researcher at the Neuromuscular Disorders Research lab and Genetic Determinants of Osteoporosis Research lab, in University of Helsinki and Folkhälsan RC. I specialize in Bioinformatics. I am interested in Machine learning and multi-omics data analysis. My go-to programming language is R.
My Blog List
My Blog List
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.