10 months ago by

Netherlands

You can do it with base R. One way of doing it is first transposing the TPM count matrix (assuming you want to run PCA on the samples rather than the genes), centering it, then doing an SVD and subsequently plotting the first and second columns of the u matrix (assuming you are interested in the first and second principal components. Alternatively, use the prcomp() function instead of SVD and plot the first and second column of the x matrix. Both should yield the same pattern (note that the values will not be the same, but the pattern will).

Example:

tpm_centered <- t(tpm-rowMeans(tpm))
#SVD
tpm_svd <- svd(tpm_centered)
plot(tpm$u[,1], tpm$u[,2])
#prcomp
tpm_prcomp <- prcomp(tpm_centered)
plot(tpm_prcomp$x[,1], tpm$x[,2])

For more information, you might want to check out this tutorial: http://genomicsclass.github.io/book/pages/pca_svd.html