Hello again Biostars community!

I am working on a project for my internship regarding the efficacy of different clustering techniques on RNA-seq data. I have used many different common clustering techniques in R and compared their results and the clusters they produce. Now, I have gotten to a point in the project where I want to analyze and explicitly show how my clustering is performing compared to "random" clustering. (Where I redistribute the "cluster" label across different groups of genes.)

To this point, I have identified k-means clustering as the most helpful for my dataset and identified some positive control groups that I know are biologically meaningful and should cluster together. For these positive controls, I have calculated a p-value and this is where my "random" groups come in!

For each of my clusters, I want to redistribute my genes as randomly as possible (within the limits of reproducibility) and examine how my p-values change for the positive control groups. Ideally I would like to see the p-values for my clusters showing much higher significance than the random clusters, but who knows? (I mean... I'm pretty sure I know, but that's why I'm doing this!)

I would like each new random cluster to contain the same number of genes as the clusters that I have already generated. (e.g. Cluster_1 had 60 genes, so Random_Cluster_1 should also have 60 genes etc.). Of course, I also need to make sure any one gene is only assigned to a single cluster.

Would anyone be able to recommend a robust method of "randomly" reassigning my genes to new clusters so that I can check my clustering performance?

Any feedback would be appreciated as I don't have much experience in this area (pretty new to bioinformatics!)

Thank you!
So you need to divide the genes (N=20000) into x clusters of different sizes. Each gene needs to be in one cluster.

Here some R solution (though not very optimal I guess - maybe there is already a function implemented in package that does that)

Example with N=20 genes and 4 clusters of size 2, 3, 5 and 10 genes.

    assign_genes_randomly <- function(genes,cluster_sizes){
      random_assignment <- sample(unlist(sapply(cluster_sizes,function(x){rep(x,x)})))
      random_clusters <- sapply(cluster_sizes,function(x){genes[random_assignment==x]})
      return(random_clusters)
    }
  
    genes <- 1:20
    cluster_sizes <- c(2,3,5,10)
       
    set.seed(123)
    random_clusters <- assign_genes_randomly(genes,cluster_sizes)


Results :

    random_clusters
    [[1]]
    [1]  6 20
    
    [[2]]
    [1] 12 18 19
    
    [[3]]
    [1]  1  3  9 11 14
    
    [[4]]
     [1]  2  4  5  7  8 10 13 15 16 17



   