Gather cells for each group according to specified factors, then randomly assign and aggregate cells to each pseudo-samples with randomized cell size. (min.cells <= size <= max.cells)

pseudo_samples(
  data,
  by,
  fun = c("sum", "mean"),
  scale = NULL,
  min.cells = 0,
  max.cells = Inf,
  slot = "counts"
)

# S4 method for matrix,data.frame
pseudo_samples(
  data,
  by,
  fun = c("sum", "mean"),
  scale = NULL,
  min.cells = 0,
  max.cells = Inf,
  slot = "counts"
)

# S4 method for matrix,vector
pseudo_samples(
  data,
  by,
  fun = c("sum", "mean"),
  scale = NULL,
  min.cells = 0,
  max.cells = Inf,
  slot = "counts"
)

# S4 method for Seurat,character
pseudo_samples(
  data,
  by,
  fun = c("sum", "mean"),
  scale = NULL,
  min.cells = 0,
  max.cells = Inf,
  slot = "counts"
)

# S4 method for SummarizedExperiment,character
pseudo_samples(
  data,
  by,
  fun = c("sum", "mean"),
  scale = NULL,
  min.cells = 0,
  max.cells = Inf,
  slot = "counts"
)

Arguments

data

a matrix or Seurat/SCE object containing expression and metadata

by

a vector of group names or dataframe for aggregation

fun

chr, methods used to aggregate cells, could be 'sum' or 'mean', default 'sum'

scale

a num or NULL, if to multiply a scale to the average expression

min.cells

num, default 300, the minimum size of cells aggregating to each pseudo-sample

max.cells

num, default 600, the maximum size of cells aggregating to each pseudo-sample

slot

chr, specify which slot of seurat object to aggregate, can be 'counts', 'data', 'scale.data'..., default is 'counts'

Value

An expression matrix after aggregating cells on specified factors

Examples

counts <- matrix(abs(rnorm(10000, 10, 10)), 100)
rownames(counts) <- 1:100
colnames(counts) <- 1:100
meta <- data.frame(
  subset = rep(c("A", "B"), 50),
  level = rep(1:4, each = 25)
)
rownames(meta) <- 1:100
scRNA <- SeuratObject::CreateSeuratObject(counts = counts, meta.data = meta)
#> Warning: Data is of class matrix. Coercing to dgCMatrix.
pseudo_samples(scRNA,
  by = c("subset", "level"),
  min.cells = 10, max.cells = 20
)
#>         A.1_1     B.1_1     A.2_1     B.2_1     A.3_1     B.3_1     A.4_1
#> 1   189.68722 121.98300 173.59774 132.37316 161.90622  82.83385  74.00843
#> 2   113.55731 143.13743 149.34681 135.02673 118.68681 105.62701 180.36954
#> 3   131.75680 120.33109  88.79949 136.70885 127.87282 113.85903 103.15364
#> 4   137.79398 123.99236 104.62808 170.11164 167.21097 133.73793  96.14825
#> 5   119.70122 155.69769  95.49198 158.68897 157.32530 164.67492 148.26891
#> 6   142.84524  91.72708 165.75720 153.66090 157.42112 141.95432 163.26024
#> 7   135.35171  63.31922 159.55743 165.07608 127.54185 126.04112 144.15204
#> 8   167.63501 113.18236 176.09361 148.38489 132.46825 131.80872 146.52483
#> 9   125.49418 174.31157 201.61424 132.20680 184.64838 171.16434 130.79296
#> 10  151.14669 130.48577  74.25771 170.67381 208.84564 109.02682 112.01205
#> 11  141.28966 105.18412 111.67373 153.81897 146.44124 109.66240 161.11279
#> 12  133.02632 112.49485 141.66453 133.54187 139.52859 102.35169 153.01657
#> 13  211.98352 103.69540 196.75709 111.52534 135.98126 181.67306 136.81369
#> 14  161.54498  95.64675 128.72895 185.88465 142.49943  88.60712 207.66124
#> 15  181.50221 145.34465 175.52367 155.39052 132.69363 125.83006 152.05919
#> 16  139.76634 170.28360 144.04753 115.58868  94.30047 139.03152 158.27809
#> 17  119.25600 122.25930 106.82541 210.47899 159.34706 172.30329 129.93391
#> 18   98.64513 173.93139 190.78519 152.87092 108.20865 140.90585  99.79503
#> 19  146.76182 121.90673 199.53307 174.65382 221.52282 135.58557 145.39171
#> 20  145.44988 155.39075 127.12557 205.12120 135.80232 140.19118 105.33722
#> 21  144.22909  88.42659 133.36863 136.20200 149.97343 123.58212 149.15485
#> 22   77.50024 148.31076 112.41375 120.31431 217.21700 131.85235 105.77658
#> 23  178.62705 138.03047 151.34307 176.86262 226.36086 222.72993 166.50805
#> 24  131.33487 168.75010 144.91455 114.17635 167.56778 132.12395 163.18861
#> 25  189.00121 145.51388  89.20254 162.34954 116.20897 131.21528 137.72366
#> 26  177.19058 149.49713 121.29631 131.20777 129.08716 149.75898 188.64200
#> 27  179.53207 161.00383 192.65908 111.39228  93.78502 126.23654 141.49655
#> 28  156.85976 138.08462 135.51942 132.57034 115.48046 151.02738 100.83030
#> 29  172.00178 155.47571 134.10873 193.89878 192.63929 170.97542 127.00404
#> 30  160.03235 123.36168 151.32547 120.19444 171.84692 126.43097 153.21790
#> 31  142.73124 121.22444 137.71340 139.14134 228.70061  98.38036 173.74738
#> 32  163.69546 151.60239 139.87557 162.97546 153.64335 137.89799  96.58310
#> 33  139.91584 113.82856 144.12502 168.00912 185.06124 159.30621 114.21858
#> 34  118.60255 111.02796 135.85433 160.21549 169.25268 114.40709 177.77873
#> 35  111.53624 135.75861 137.22564 120.41312 162.96794 165.65371 132.54810
#> 36  141.88905 136.44579 116.30800 149.45404 159.58985 216.87430 115.96152
#> 37  168.42692 120.73995 141.02927 149.65292 163.17299 184.35580 123.39997
#> 38  118.12692 120.95846 106.21947  95.24047 140.10679 110.23412 111.42971
#> 39  140.75078 171.87613 105.18287 146.85064 139.77149 140.08613 156.15351
#> 40  125.16554 127.51253 171.52505 142.95273 132.89474 157.96192 169.39175
#> 41  154.43246 138.62955 148.82826 174.46699 189.99985 139.92258 128.93020
#> 42  137.18535 132.01065 141.50503 197.09304 149.51984 187.30512 148.11743
#> 43  168.72966 167.45133 148.90523  98.08387 214.86655 128.81215 131.37275
#> 44  116.53132 102.78026 166.04307 135.90853 122.39914 149.76654 116.52017
#> 45  171.40576 114.31848 140.70313 131.83608  98.81261 155.82964 144.14875
#> 46  136.38422 159.71070 182.34255 167.93693 141.47571 162.44664 162.28427
#> 47   94.23191 190.30190 131.57428 158.13349 154.46660 117.45466  98.20392
#> 48  176.27532 131.56702 175.26901 106.71867 179.41642 132.25168 157.32601
#> 49  135.02601 143.02540 146.98142 113.73427 128.40506 130.66558 133.15190
#> 50  123.37749 137.65855 137.75333 159.09431 127.63693 135.09683 212.97384
#> 51  142.53113 154.48536 125.05302 117.71794 144.40922 175.00135 122.01067
#> 52  163.33794 176.82883 140.26340  86.95519 130.57713 122.44501 125.29554
#> 53  154.75637 152.41459 124.64661 156.00459 140.06693  97.71946 162.49488
#> 54  169.65302 163.25135 174.47116 103.68960 119.29346 149.80891 172.90797
#> 55  150.91978  98.49893 119.06761 135.69739 138.82382 102.77848 176.30407
#> 56  171.19151 181.98949 152.77896 109.45666 118.89750 143.41229 115.24077
#> 57  142.15999 126.70304 127.26604 144.30520 177.03485 178.81867 151.89968
#> 58  137.75963 133.56338 162.81770 132.86220 133.74097 170.42361  72.00191
#> 59  153.15005 139.97899 139.36732 105.29047 129.23231 136.95028 152.30742
#> 60  183.50655 172.93153 119.52994 179.62976 167.32639 169.85753 126.96885
#> 61  160.04242 110.68325 144.17584 203.14916 137.93523 133.38678 116.53730
#> 62  165.29590 151.88126 141.85100 128.24976 166.67573 147.31708 205.65401
#> 63  161.49065 111.88172 127.52463 153.93291 156.35382 172.57065 118.98497
#> 64  187.29301 180.77306 135.62019 168.89627 145.81305 114.09015 119.89152
#> 65  149.59176 104.67738 152.75293 167.97606 144.72368 129.27171 154.16107
#> 66  185.93447 166.74373 130.68865 133.63313 147.38035 113.62497 107.80571
#> 67  189.44294 128.63910 138.69346 153.10409 104.27821 118.63941 120.68062
#> 68  108.93392 129.82867 114.26113 173.44618 212.11985 177.30256 150.45316
#> 69  132.43397 126.83912 158.60956 159.34707 164.56646 161.90964 192.25618
#> 70  136.17406 116.95184 133.01425 128.07568 117.15701 140.44289 147.38681
#> 71  117.98421 139.66682 149.96809 178.65193 156.34516 121.33355 148.40827
#> 72  155.87204 127.89444 163.12975 175.73652 171.29815  95.59407 121.15379
#> 73  144.74701 182.38883 155.35096 160.75360 115.05246 149.66405 145.78086
#> 74  176.70679 162.04054 152.31783 190.18228 176.50492 182.01136 135.65073
#> 75  136.10517 100.34024 141.54730 176.31093 143.00608 143.98044 154.73191
#> 76  183.05370 103.41441 137.09786 168.49059 111.52272 101.26147 148.73211
#> 77  160.39397 112.34053 157.95759 116.11384 140.97881 129.25549 104.19151
#> 78  185.48698 136.39326 178.69966 112.97186 150.31052 117.93729 118.98760
#> 79  170.18142 164.28004  86.92166 191.90845 165.69606 127.99919 155.22401
#> 80  173.46872 132.03322 144.41601 124.10896 159.94914 191.11107 109.24537
#> 81  158.66655 189.18053 157.43184 127.25322 167.27427 145.21845 137.97146
#> 82  138.64645 171.95974 164.30303 155.46261 149.96127  83.00759 105.70354
#> 83  170.82594 208.01383 125.67612 128.18215 154.66352 122.73331 144.29716
#> 84  118.25818 141.06687 120.48415 135.38601 148.08735 178.28947 157.90375
#> 85  107.22170 174.68200 139.82389 163.51852 205.32098 152.68241 136.69555
#> 86  152.74897 204.76911 136.59917 167.95307 118.09719 132.90654 128.01009
#> 87  164.70989 171.47352 171.96567 191.61977 143.02314 110.99619 136.07745
#> 88  187.90291 108.72985 127.37064  86.24131 166.04394 136.56392 108.65707
#> 89  177.35862 114.88868 127.63264 114.03414 147.90756 150.36166 114.22137
#> 90  170.24155 122.87071 144.88045 151.73199 195.34020 124.52237 166.21762
#> 91  107.20805 102.01781 184.94572 197.98942 180.34172 133.53967 138.32017
#> 92  165.06989 156.40293 127.71484 104.11640 122.38703 157.77631 142.05474
#> 93  131.57759 165.06823 149.98789 224.29848 158.85693 187.83668 170.13196
#> 94  163.52462 131.92110 149.94273 173.44589 114.66168 115.26817 132.96696
#> 95  236.74991 143.67160 162.03612 123.55417 141.65654 129.33231 157.94876
#> 96  125.31123 138.77398 126.02873 181.64131 128.49287 116.08790  99.92635
#> 97  171.05130 202.74235 126.16718 135.54263 115.79488 108.57026 134.72792
#> 98  146.13239 122.93164 163.78158 240.11379 177.62143 127.60179 174.19292
#> 99  119.90107 176.67327 125.18370 200.54795 185.44578 158.42220  85.14491
#> 100 135.94622 161.40311 170.24227 180.48176  74.89803 154.04619 156.89536
#>         B.4_1
#> 1   158.95106
#> 2   127.33277
#> 3   182.26554
#> 4   138.40140
#> 5   216.51755
#> 6   161.83396
#> 7   123.86341
#> 8   114.14400
#> 9   182.42093
#> 10  119.99527
#> 11  147.64032
#> 12  137.50043
#> 13  167.73681
#> 14  173.81418
#> 15  160.67309
#> 16  168.53794
#> 17  157.21792
#> 18  144.62790
#> 19  162.39087
#> 20  179.98769
#> 21  145.71762
#> 22  133.58680
#> 23  194.42441
#> 24  146.13222
#> 25  133.56678
#> 26  125.03868
#> 27  213.18534
#> 28  123.71904
#> 29  167.38160
#> 30  129.22928
#> 31   95.34245
#> 32  146.52435
#> 33  170.99716
#> 34  167.11236
#> 35  124.54871
#> 36  143.35381
#> 37  149.94561
#> 38  173.73283
#> 39  129.32817
#> 40  119.67268
#> 41  131.94817
#> 42  143.36369
#> 43  179.79637
#> 44  181.85425
#> 45  188.49693
#> 46  168.17354
#> 47  166.69336
#> 48  179.46099
#> 49  132.49543
#> 50  153.14814
#> 51  132.83920
#> 52  133.22302
#> 53  186.27361
#> 54  162.26204
#> 55  187.39131
#> 56  152.50999
#> 57  137.66761
#> 58  167.49420
#> 59  107.58681
#> 60  156.92992
#> 61  134.35422
#> 62  113.06585
#> 63  171.76279
#> 64  153.53890
#> 65  157.63878
#> 66  134.65172
#> 67  110.97550
#> 68  121.16068
#> 69  133.36843
#> 70  151.81463
#> 71  160.27338
#> 72  150.66634
#> 73  122.86705
#> 74  119.11739
#> 75  148.76254
#> 76  163.92544
#> 77  183.84516
#> 78  150.66294
#> 79  212.21201
#> 80  173.08564
#> 81  215.85679
#> 82  137.21647
#> 83  165.78361
#> 84  166.69018
#> 85   85.58729
#> 86  152.27006
#> 87  118.98352
#> 88  179.13747
#> 89  146.58365
#> 90  111.55352
#> 91  201.46264
#> 92  152.85726
#> 93  125.30626
#> 94  103.72387
#> 95  148.60806
#> 96  167.21946
#> 97  157.32018
#> 98  214.25658
#> 99  119.25753
#> 100 161.82133