bnprks / BPCells

Scaling Single Cell Analysis to Millions of Cells
https://bnprks.github.io/BPCells
Other
150 stars 17 forks source link

inconsistent results between `rank_transform` and `colRanks` #66

Closed Yunuuuu closed 7 months ago

Yunuuuu commented 8 months ago
mock_matrix <- function(ngenes, ncells) {
    cell.means <- 2^stats::runif(ngenes, 2, 10)
    cell.disp <- 100 / cell.means + 0.5
    cell.data <- matrix(stats::rnbinom(ngenes * ncells,
        mu = cell.means,
        size = 1 / cell.disp
    ), ncol = ncells)
    rownames(cell.data) <- sprintf("Gene_%s", formatC(seq_len(ngenes),
        width = 4, flag = 0
    ))
    colnames(cell.data) <- sprintf("Cell_%s", formatC(seq_len(ncells),
        width = 3, flag = 0
    ))
    cell.data
}
mat <- mock_matrix(2000, 200)
path <- normalizePath(tempfile(tmpdir = tempdir()), mustWork = FALSE)
obj <- BPCells::write_matrix_dir(mat = as(mat, "dgCMatrix"), dir = path)
#> Warning: Matrix compression performs poorly with non-integers.
#> • Consider calling convert_matrix_type if a compressed integer matrix is intended.
#> This message is displayed once every 8 hours.
obj <- BPCells:::rank_transform(obj, "col")
mat <- matrixStats::colRanks(mat, ties.method = "average", preserveShape = TRUE)
waldo::compare(as.matrix(obj), mat)
#> Warning: Converting to a dense matrix may use excessive memory
#> This message is displayed once every 8 hours.
#> old vs new
#>               [,  1] [,  2] [,  3] [,  4] [,  5] [,  6] [,  7] [,  8] [,  9] [, 10] [, 11] [, 12] [, 13] [, 14] [, 15] [, 16] [, 17] [, 18] [, 19] [, 20] [, 21] [, 22] [, 23] [, 24] [, 25] [, 26] [, 27] [, 28] [, 29] [, 30] [, 31] [, 32] [, 33] [, 34] [, 35] [, 36] [, 37] [, 38] [, 39] [, 40] [, 41] [, 42] [, 43] [, 44] [, 45] [, 46] [, 47] [, 48] [, 49] [, 50] [, 51] [, 52] [, 53] [, 54] [, 55] [, 56] [, 57] [, 58] [, 59] [, 60] [, 61] [, 62] [, 63] [, 64] [, 65] [, 66] [, 67] [, 68] [, 69] [, 70] [, 71] [, 72] [, 73] [, 74] [, 75] [, 76] [, 77] [, 78] [, 79] [, 80] [, 81] [, 82] [, 83] [, 84] [, 85] [, 86] [, 87] [, 88] [, 89] [, 90] [, 91] [, 92] [, 93] [, 94] [, 95] [, 96] [, 97] [, 98] [, 99] [,100] [,101] [,102] [,103] [,104] [,105] [,106] [,107] [,108] [,109] [,110] [,111] [,112] [,113] [,114] [,115] [,116] [,117] [,118] [,119] [,120] [,121] [,122] [,123] [,124] [,125] [,126] [,127] [,128] [,129] [,130] [,131] [,132] [,133] [,134] [,135] [,136] [,137] [,138] [,139] [,140] [,141] [,142] [,143] [,144] [,145] [,146] [,147] [,148] [,149] [,150] [,151] [,152] [,153] [,154] [,155] [,156] [,157] [,158] [,159] [,160] [,161] [,162] [,163] [,164] [,165] [,166] [,167] [,168] [,169] [,170] [,171] [,172] [,173] [,174] [,175] [,176] [,177] [,178] [,179] [,180] [,181] [,182] [,183] [,184] [,185] [,186] [,187] [,188] [,189] [,190] [,191] [,192] [,193] [,194] [,195] [,196] [,197] [,198] [,199] [,200]
#> - old[1, ]    1200.0    0.0  974.5 1037.5 1131.0  988.0  621.0  571.5  812.5  702.0    0.0  591.0 1105.0 1103.5  926.5  454.0  645.5 1095.0  540.5  823.0  832.5  721.5  984.0  908.0  879.0  749.0 1005.5    0.0 1478.5 1139.0  458.0 1372.5  520.0  504.0    0.0  675.0  775.5  411.5  852.5    0.0  866.5 1224.0  558.0  562.5 1007.5  851.5  614.0  902.0 1307.5  555.0 1540.0  487.0  839.0  945.0 1324.5  838.0 1133.5 1195.0  820.5  784.5  614.0  345.5  377.0  759.0  702.0  981.0 1074.5  691.0  997.5  859.0 1490.0 1291.0    0.0 1217.5  691.5    0.0    0.0  279.0  443.5  386.0  822.0  843.5 1150.5  366.0  768.5  755.5  512.5  732.5    0.0 1423.0  436.5 1050.5  714.5 1331.5  780.0    0.0  359.0 1337.0  964.0 1136.0  785.5 1543.0 1089.5 1075.0  611.5 1308.0 1307.5 1550.0  984.5  470.5  463.5 1511.0    0.0  749.0  643.5  607.0  536.0  643.0  817.5    0.0  962.0  631.0  671.5  494.5  512.5  914.5  946.5  703.0  579.5    0.0  658.0  525.0  416.0  383.0  607.0  296.0 1425.5    0.0  814.0  358.5 1023.5  291.0  687.0 1018.5 1217.5  917.0  810.0    0.0  670.0 1272.5 1064.0 1315.5  560.5  305.0  743.5  914.5    0.0  833.5  951.5 1344.5 1077.0  971.5  989.0  946.5  299.0  413.5  650.5    0.0    0.0 1339.5  348.0  281.5  596.0 1264.0  740.5  466.5  530.0 1503.5 1191.0 1243.0    0.0 1196.0  958.5  878.5  625.0 1153.5 1255.0  955.0  856.5  480.5  609.0  346.0  752.0  885.0 1046.0  863.5    0.0  485.5  579.0  589.5
#> + new[1, ]    1449.5  262.0 1225.5 1296.5 1404.5 1257.0  877.5  837.5 1060.5  956.0  248.0  835.5 1359.5 1355.0 1173.0  711.5  902.5 1364.0  806.5 1086.0 1082.0  985.0 1240.5 1163.0 1132.0 1012.5 1259.5  257.5 1728.0 1394.5  706.0 1606.0  785.5  762.0  258.0  932.0 1029.0  665.0 1097.5  260.0 1125.0 1468.5  809.0  831.5 1240.0 1108.0  876.0 1149.0 1569.5  795.5 1799.5  750.0 1098.0 1194.0 1570.0 1092.5 1389.5 1452.5 1075.0 1030.0  880.5  602.5  625.5 1007.5  961.0 1236.0 1317.5  942.0 1266.0 1125.0 1745.0 1549.5  253.5 1485.0  937.5  250.5  257.5  528.0  691.5  639.5 1078.0 1098.0 1407.5  612.5 1029.0  998.5  774.5  972.0  270.0 1669.0  687.0 1310.0  967.5 1593.5 1033.5  255.0  623.5 1587.5 1218.5 1389.5 1038.0 1802.0 1342.0 1348.0  871.0 1561.5 1573.0 1810.0 1239.0  718.5  713.5 1770.5  262.0  997.0  896.5  856.0  791.5  888.5 1075.5  251.5 1207.5  892.5  934.0  745.5  755.0 1171.0 1197.5  970.0  828.5  255.5  924.5  783.5  675.5  638.0  864.0  557.5 1677.5  264.0 1066.0  604.5 1287.0  543.0  947.5 1282.0 1484.5 1170.0 1060.0  249.0  924.0 1522.5 1318.0 1588.5  800.5  574.0 1005.5 1171.0  265.0 1096.5 1206.5 1598.0 1343.5 1231.0 1240.0 1206.0  569.0  671.5  903.0  252.0  260.5 1590.5  598.5  527.5  861.0 1523.5  999.5  717.0  772.5 1753.5 1438.5 1487.5  272.0 1453.5 1214.5 1137.0  870.5 1413.5 1505.5 1217.0 1122.5  745.5  872.0  599.5 1006.5 1138.5 1298.5 1110.5  249.5  738.5  823.5  831.0
#> - old[2, ]    1423.5  597.0    0.0  617.0  903.0    0.0  672.0    0.0  752.5 1187.0  718.5  948.0 1171.5  871.5  939.0 1336.0  550.5  436.5  540.5    0.0  795.0  429.0  804.5  704.0 1010.5  723.0  708.0 1362.0  430.0 1277.0  824.0 1287.0 1538.5  867.5 1495.5 1040.0  287.5  445.0  426.0  515.0  928.0 1090.0 1291.0 1264.5 1101.5 1174.0 1023.5  681.0  909.0  634.5  795.5  735.5  545.5  927.0  975.5  646.0  869.5 1335.0  697.0  937.0  667.5  385.0 1211.5 1080.0  627.0    0.0  848.5  700.0  798.5  661.5 1203.5 1134.0 1062.0  569.5  878.0  973.5  953.0  984.0  402.0  815.0    0.0 1322.5  287.0  818.0  789.5  732.0  775.5 1015.0 1053.0  680.0 1519.0  851.0  877.0  342.0  833.5  947.0 1394.5 1501.5  765.0  587.5  672.5 1398.5  931.0  822.0  496.5  961.0 1273.5  673.5 1461.5 1313.0  907.5  550.5 1205.0 1098.5  699.0 1131.0 1327.5  577.5  636.5  796.5  279.0  824.0  797.0  624.0 1207.0  582.0 1351.0  471.5  917.0 1260.0  810.0  350.0 1057.0  647.5 1010.5 1062.5  605.0 1129.5  849.5  816.0  782.5  582.5 1058.0  851.0 1024.5  704.0  661.5 1297.5  788.5  941.5 1112.5    0.0 1263.5 1297.0 1307.5  692.0 1250.0  923.5 1154.0  496.5  966.5 1307.0  531.0  670.5  718.5 1451.5  285.5  627.0 1127.0  800.5  823.5  512.5  980.0 1306.5 1085.5 1311.0 1204.5 1088.0  291.0  708.0  939.5  737.0    0.0  764.0  983.5  291.5  847.5  435.0    0.0 1404.5  877.0 1309.5  761.5  640.5  668.0 1082.5  879.5  821.0  981.0  834.0
#> + new[2, ]    1673.0  859.0  251.0  876.0 1176.5  269.0  928.5  266.0 1000.5 1441.0  966.5 1192.5 1426.0 1123.0 1185.5 1593.5  807.5  705.5  806.5  263.0 1044.5  692.5 1061.0  959.0 1263.5  986.5  962.0 1619.5  679.5 1532.5 1072.0 1520.5 1804.0 1125.5 1753.5 1297.0  541.0  698.5  671.0  775.0 1186.5 1334.5 1542.0 1533.5 1334.0 1430.5 1285.5  928.0 1171.0  875.0 1055.0  998.5  804.5 1176.0 1221.0  900.5 1125.5 1592.5  951.5 1182.5  934.0  642.0 1460.0 1328.5  886.0  255.0 1091.5  951.0 1067.0  927.5 1458.5 1392.5 1315.5  837.0 1124.0 1224.0 1210.5 1233.0  650.0 1068.5  256.0 1577.0  544.0 1064.5 1050.0  975.0 1037.5 1254.5 1323.0  926.0 1769.5 1110.5 1130.0  604.0 1087.0 1202.0 1659.0 1752.0 1019.5  841.0  925.0 1657.5 1183.5 1095.0  756.0 1214.5 1539.0  933.5 1716.0 1561.0 1157.5  810.0 1467.0 1346.5  952.0 1380.0 1583.0  823.0  894.5 1048.0  524.5 1085.5 1059.5  875.0 1449.5  838.5 1602.0  738.5 1166.0 1515.5 1076.5  608.5 1316.5  902.5 1267.5 1324.0  857.0 1393.5 1101.5 1062.0 1046.0  834.5 1318.5 1114.5 1291.5  957.0  911.5 1546.5 1042.5 1191.5 1366.5  273.0 1503.5 1566.0 1569.5  948.5 1515.0 1186.5 1409.0  750.0 1233.0 1566.5  782.0  930.0  988.5 1709.5  538.0  879.0 1387.5 1051.5 1074.0  758.5 1245.0 1566.0 1344.5 1561.5 1447.0 1338.0  538.5  952.5 1211.5  994.5  256.0 1022.5 1229.0  551.5 1098.0  697.0  266.0 1669.5 1140.0 1563.0 1016.0  894.0  920.5 1329.5 1129.0 1074.0 1225.5 1075.5
#> - old[3, ]       0.0  766.5    0.0  771.5    0.0  359.0    0.0  357.0  284.5  298.5  339.5    0.0 1144.0  826.5    0.0  580.0  460.5  296.0    0.0  528.5  624.5    0.0  913.5  665.0  485.0  508.0  937.0  994.0  430.0  770.0  497.5  500.5    0.0  439.0  537.5 1024.0 1214.0  610.5  956.5  822.5  642.5  823.0  710.5  953.0    0.0  450.5  516.0 1061.5 1095.5  361.5  346.0  787.0  296.0  537.5    0.0 1027.5  842.5 1260.0  390.0  345.0    0.0 1040.0  341.5    0.0  491.5  526.0  273.5  508.5    0.0 1177.5  706.0  466.0  341.5  438.5  285.0  624.0  555.5 1059.5 1048.5  630.0    0.0  543.5  287.0  366.0    0.0  341.0    0.0  273.0  760.0    0.0    0.0    0.0  510.0  750.0  970.0  295.5  711.5  362.5    0.0  284.5  735.0 1133.0  431.0  425.5    0.0  424.0  546.5    0.0  388.5  343.5  836.0  347.5    0.0  372.5 1205.0  460.5  940.0  818.0  698.5  386.0  713.0  691.0 1007.5  678.5    0.0    0.0  784.5    0.0  417.0  532.0    0.0  844.5    0.0  434.5 1129.0    0.0  469.0  353.0  344.0 1003.0  988.5  767.5    0.0  976.5    0.0 1157.5  916.5 1159.5 1148.5  723.0  591.5  523.0 1073.5  875.5    0.0  472.5  677.0    0.0 1101.5    0.0  790.5  649.5    0.0 1092.0    0.0    0.0  821.5    0.0  845.5  285.5  389.0  605.5    0.0    0.0    0.0  535.0  814.0  482.0  639.0  913.0    0.0  664.0  924.5    0.0  566.0    0.0    0.0  295.5  477.5    0.0  539.5 1020.0  865.5  293.5  778.0    0.0  391.0    0.0    0.0  335.5
#> + new[3, ]     249.5 1028.5  251.0 1030.5  273.5  628.0  256.5  623.0  532.5  552.5  587.5  244.5 1398.5 1078.0  246.5  837.5  717.5  565.0  266.0  791.5  874.0  263.5 1170.0  920.0  738.0  771.5 1191.0 1251.5  679.5 1025.5  745.5  734.0  265.5  697.0  795.5 1281.0 1467.5  864.0 1201.5 1082.5  901.0 1067.5  961.5 1222.0  232.5  707.0  778.0 1308.5 1357.5  602.0  605.5 1050.0  555.0  786.5  245.5 1282.0 1098.5 1517.5  644.5  590.5  266.5 1297.0  590.0  248.5  750.5  781.0  516.5  759.5  268.5 1443.5  961.0  724.5  595.0  706.0  531.0  874.5  813.0 1308.5 1296.5  883.5  256.0  798.0  544.0  612.5  260.5  584.0  262.0  512.5 1030.0  246.0  250.5  259.5  763.0 1012.0 1223.5  550.5  976.0  613.0  254.5  538.0  987.5 1392.0  683.5  698.5  259.5  677.5  812.0  260.0  643.0  591.5 1086.0  607.0  262.0  620.5 1458.0  709.5 1195.5 1063.5  956.5  637.5  958.5  952.5 1270.0  929.5  242.5  256.5 1035.5  267.0  666.0  787.5  266.5 1103.0  259.5  689.5 1386.0  261.5  721.0  617.0  596.0 1249.0 1252.0 1019.5  260.5 1240.0  267.0 1410.5 1166.5 1408.5 1402.5  973.0  845.5  796.0 1313.5 1144.5  262.0  729.0  942.0  263.0 1356.5  253.5 1057.0  909.0  251.0 1351.5  270.0  258.0 1074.0  252.0 1106.0  536.5  639.5  851.5  265.0  259.5  259.0  785.5 1056.5  732.0  886.5 1157.5  272.0  921.5 1180.5  258.5  811.5  260.0  250.5  557.5  743.5  265.0  802.5 1273.5 1120.0  547.0 1030.5  247.0  640.5  253.0  244.5  577.0
#> - old[4, ]     818.5  959.5  928.5  960.0 1052.0 1218.5 1230.5  662.0 1144.0  723.5  909.0  283.0 1175.5 1200.5  628.0 1040.5  923.5  555.5  738.5 1042.0  799.0  406.0 1087.5  971.0 1340.0 1209.0  463.5  668.0 1567.0 1230.5  951.5  678.5  971.0  719.5 1152.5 1468.0  608.5 1212.5    0.0  434.0 1266.5  433.5  346.0 1473.0    0.0  620.5  960.0  865.0 1211.5  859.5 1103.0  755.5 1103.5  859.0 1112.0  393.0 1386.0 1335.0  707.0  479.5 1411.0 1608.0  716.0  735.5  333.5  450.5 1191.5 1038.0  809.5 1545.0 1015.5 1142.0 1145.5  894.0 1168.5  487.0  603.0  872.5    0.0 1399.0 1080.5  493.0 1228.5  805.0 1144.5  497.5 1490.5 1582.0 1133.0  416.5 1238.0 1252.0  631.0  887.5  570.5  520.0  871.5    0.0  746.0 1522.5 1116.0  625.0 1181.0  830.5  432.5 1010.5 1395.5 1202.0  418.0 1108.0 1569.0 1138.0  688.5  468.5 1134.5 1495.0 1132.0 1127.5 1216.0  455.0 1569.5  955.5 1229.5 1132.0  581.5 1220.5  866.0  703.0  851.0  786.5  596.5  902.0  870.0 1155.5  820.5 1162.0  593.5 1139.5 1008.5 1190.5  761.5  632.5  697.0  573.0  461.5 1391.0  677.5 1455.5 1059.5  732.0 1168.5 1136.5  899.5 1285.5  590.5 1226.0 1177.5    0.0 1251.0    0.0  639.5 1136.5  730.0 1046.5  629.0  977.0 1141.0 1194.0 1145.5  662.0  423.5  696.0  383.0 1225.0 1478.0 1003.0  582.5 1080.5 1099.0  801.0  960.5  482.0  968.5 1336.0 1020.0  630.0  774.5  608.0 1047.5 1033.5 1238.0  843.0 1082.0 1168.5 1046.0 1141.0  817.5 1325.5  926.0  633.5
#> + new[4, ]    1068.0 1221.5 1179.5 1219.0 1325.5 1487.5 1487.0  928.0 1392.0  977.5 1157.0  527.5 1430.0 1452.0  874.5 1298.0 1180.5  824.5 1004.5 1305.0 1048.5  669.5 1344.0 1226.0 1593.0 1472.5  717.5  925.5 1816.5 1486.0 1199.5  912.0 1236.5  977.5 1410.5 1725.0  862.0 1466.0  245.0  694.0 1525.0  678.0  597.0 1742.0  232.5  877.0 1222.0 1112.0 1473.5 1100.0 1362.5 1018.5 1362.5 1108.0 1357.5  647.5 1642.0 1592.5  961.5  725.0 1677.5 1865.0  964.5  984.0  592.5  705.5 1434.5 1289.0 1078.0 1811.0 1270.5 1400.5 1399.0 1161.5 1414.5  737.5  860.5 1121.5  248.0 1652.5 1336.5  747.5 1485.5 1051.5 1405.0  740.5 1752.5 1821.5 1403.0  662.5 1488.5 1511.5  884.0 1149.5  824.0  775.0 1136.0  250.5 1000.5 1776.0 1368.5  884.0 1433.5 1103.5  692.0 1264.0 1661.0 1462.0  672.5 1356.0 1819.0 1397.5  950.5  716.5 1387.5 1744.0 1387.5 1373.0 1474.0  706.5 1815.0 1217.0 1492.0 1383.0  824.0 1477.0 1117.0  970.0 1100.0 1042.0  863.0 1160.5 1129.5 1410.5 1077.5 1423.5  845.5 1403.5 1260.5 1436.5 1025.0  884.5  957.5  836.5  728.5 1644.0  927.5 1704.5 1313.5  982.0 1422.5 1409.5 1139.5 1554.5  852.5 1482.5 1442.5  263.0 1506.0  253.5  906.0 1396.0  981.0 1306.0  899.0 1235.0 1393.5 1446.0 1406.0  913.0  674.0  942.0  648.0 1484.5 1737.0 1253.5  825.0 1330.5 1346.5 1045.5 1232.5  739.5 1224.5 1594.5 1265.5  890.0 1025.0  870.0 1313.5 1298.5 1501.0 1096.5 1336.5 1422.0 1298.5 1388.0 1067.0 1578.5 1170.5  875.0
#> - old[5, ]       0.0  523.5    0.0  511.0  745.5    0.0    0.0    0.0  284.5    0.0    0.0    0.0  481.0    0.0  280.0  293.5    0.0    0.0  370.0  633.5    0.0  406.0  660.0    0.0  291.5  417.0    0.0  659.0 1302.0    0.0  671.0    0.0    0.0    0.0    0.0    0.0  789.5    0.0    0.0    0.0  964.5  379.0    0.0  746.5    0.0  641.0    0.0 1366.0  754.5    0.0  544.5  577.5 1071.5  739.5  526.0    0.0    0.0    0.0 1112.0    0.0  308.0    0.0  547.0    0.0    0.0    0.0  328.5    0.0    0.0  549.0    0.0    0.0  529.5  366.5    0.0  355.0    0.0    0.0    0.0  386.0  283.5    0.0  580.0  279.0    0.0    0.0    0.0    0.0    0.0  279.0    0.0 1323.5    0.0  709.0  554.0  462.5  359.0    0.0    0.0  870.0  410.5 1078.5    0.0    0.0  455.5    0.0  498.0    0.0 1124.0  343.5  386.5    0.0  626.0 1089.5    0.0    0.0  294.0  956.0  706.0    0.0  423.5    0.0    0.0    0.0    0.0    0.0    0.0    0.0  632.5    0.0  753.5  391.5    0.0  434.5    0.0    0.0    0.0  296.5    0.0    0.0  468.5    0.0  348.0    0.0    0.0    0.0    0.0  289.5    0.0    0.0    0.0    0.0  335.5  707.5  602.5    0.0    0.0  976.0 1124.5    0.0  408.5  717.0    0.0    0.0  429.0    0.0    0.0  543.5    0.0    0.0    0.0    0.0    0.0  964.5    0.0  412.5    0.0  768.0    0.0  467.0    0.0    0.0  289.0    0.0 1195.5 1189.0    0.0    0.0    0.0    0.0  721.5  290.5  552.0    0.0    0.0  378.0    0.0 1106.0 1149.0    0.0
#> + new[5, ]     249.5  785.5  251.0  770.0 1019.0  269.0  256.5  266.0  532.5  254.0  248.0  244.5  735.5  251.5  526.5  551.0  257.0  269.0  636.0  896.5  249.5  669.5  916.5  255.0  544.5  680.5  254.0  916.5 1551.5  255.5  919.0  233.5  265.5  258.0  258.0  257.0 1043.0  253.5  245.0  260.0 1223.0  623.5  251.0 1015.5  232.5  897.5  262.0 1613.0 1016.5  240.5  804.0  840.5 1330.5  988.5  771.5  254.5  256.0  257.5 1366.5  245.5  574.5  257.0  795.5  248.5  259.0  255.0  571.5  251.0  268.5  815.0  255.0  258.5  783.0  634.0  246.0  605.5  257.5  249.0  248.0  639.5  539.5  254.5  837.0  525.5  260.5  243.0  262.0  239.5  270.0  525.0  250.5 1583.0  253.0  971.0  807.5  717.5  623.5  250.5  254.5 1123.5  663.0 1337.5  252.5  273.0  715.0  253.5  763.5  260.0 1378.5  591.5  636.5  259.5  888.0 1337.5  253.0  249.0  549.5 1201.5  964.0  251.5  669.0  261.5  262.5  251.0  242.5  256.5  251.0  267.0  881.5  255.5 1020.0  650.0  259.5  689.5  257.0  261.5  252.0  560.5  252.0  246.0  732.0  252.0  608.5  263.5  267.0  253.0  250.0  538.5  254.0  250.0  254.0  273.0  575.5  976.5  864.5  256.5  265.0 1239.0 1379.5  253.5  675.0  976.5  251.0  259.5  699.0  258.0  252.5  795.5  260.5  251.0  250.5  246.0  265.0 1224.0  259.0  663.0  242.5 1018.0  247.5  711.5  272.0  257.5  545.0  258.5 1441.0 1449.0  250.5  262.0  266.0  265.0  984.5  544.0  806.5  253.5  252.5  625.0  249.5 1359.0 1393.5  241.5
#> - old[6, ]       0.0  349.5  281.0  491.5  513.5  359.0    0.0    0.0    0.0  530.0 1100.0  521.0    0.0  976.0    0.0    0.0  528.0  296.0  691.0  486.0    0.0 1167.0  378.0  493.5  963.5  384.0  691.0 1440.0    0.0  398.5    0.0  671.0    0.0  766.0  290.0 1171.5    0.0    0.0  920.0    0.0    0.0 1289.0  617.0    0.0    0.0  620.5  601.0  833.5  422.0    0.0    0.0    0.0  581.0  804.0  388.0    0.0  433.5    0.0 1050.5    0.0    0.0    0.0    0.0  390.0  781.0  657.0    0.0  911.0    0.0    0.0  295.5    0.0    0.0  775.0    0.0  355.0    0.0    0.0 1009.0  684.5    0.0  994.5    0.0  649.5  937.0  540.0 1283.5  975.0    0.0    0.0    0.0  693.5    0.0  994.0    0.0  757.5    0.0  533.0  634.5    0.0    0.0    0.0  651.5  690.5  622.5    0.0  498.0  409.5    0.0  903.5  346.0  417.0    0.0    0.0    0.0    0.0    0.0  935.5    0.0    0.0  739.5    0.0    0.0  476.5 1170.0    0.0    0.0    0.0    0.0  451.0    0.0  350.0  357.5  564.0  685.0  350.5    0.0    0.0 1088.0  288.5  911.5 1197.0    0.0  307.0 1120.5  502.5  464.0  803.0  512.0    0.0  474.0    0.0  439.0    0.0    0.0  472.5    0.0  460.5  846.0    0.0    0.0  430.5  626.5    0.0    0.0    0.0  566.5  285.0    0.0    0.0    0.0  281.5  744.5 1111.0  507.5 1306.5    0.0 1006.0  825.5 1332.0    0.0    0.0    0.0  508.0    0.0  534.5  546.5    0.0    0.0    0.0    0.0    0.0    0.0    0.0  548.0    0.0    0.0    0.0  275.0    0.0
#> + new[6, ]     249.5  611.5  532.0  750.5  787.0  628.0  256.5  266.0  248.0  784.0 1348.0  765.5  254.5 1227.5  246.5  257.5  785.0  565.0  957.0  749.0  249.5 1430.5  634.5  748.5 1216.5  647.5  945.0 1697.5  249.5  654.0  248.0  904.5  265.5 1024.0  548.0 1428.5  253.5  253.5 1165.0  260.0  258.5 1533.5  868.0  269.0  232.5  877.0  863.0 1080.5  684.0  240.5  259.5  263.0  840.0 1053.0  633.5  254.5  689.5  257.5 1305.0  245.5  266.5  257.0  248.5  638.5 1040.0  912.0  243.0 1162.0  268.5  266.0  550.5  258.5  253.5 1042.5  246.0  605.5  257.5  249.0 1257.0  938.0  256.0 1249.0  257.0  896.0 1197.5  783.0 1545.5 1214.5  270.0  246.0  250.5  953.0  253.0 1256.0  253.5 1012.5  264.5  783.5  889.0  253.5  252.5  259.0  904.0  963.5  882.0  253.5  763.5  669.5  254.5 1151.5  596.0  676.5  262.0  248.0  253.0  249.0  255.5 1181.0  258.0  251.5  985.0  261.5  262.5  727.5 1412.5  256.5  251.0  267.0  249.0  706.5  266.5  608.5  617.0  819.0  942.0  612.0  252.0  264.0 1340.0  534.5 1175.0 1449.0  260.5  570.5 1387.5  755.5  714.0 1052.0  766.0  250.0  728.0  273.0  679.0  269.0  262.0  729.0  265.0  723.5 1101.0  253.5  266.5  690.0  877.5  259.5  270.0  258.0  819.0  537.0  260.5  251.0  250.5  527.5 1009.5 1370.5  766.5 1557.0  242.5 1256.0 1073.0 1576.5  272.0  257.5  256.0  766.5  245.5  794.5  797.0  262.0  266.0  265.0  263.0  253.5  254.5  253.5  800.5  247.0  249.5  253.0  519.5  241.5
#> - old[7, ]     969.0  349.5  493.0 1208.0 1189.0  573.5  705.0 1069.0  685.0  400.5  857.5 1007.0 1012.0  554.5    0.0  946.0  791.0  412.0 1304.5  558.5  871.0  452.0  750.0  606.5 1171.0 1132.0 1342.0  894.0 1425.5  792.5  773.5  867.5 1389.0  781.0  592.5 1292.0  889.0  290.0  998.5 1316.0  448.5 1398.0  656.5  892.5  829.5  807.0 1131.5  628.5  784.0 1568.5  605.0 1234.5    0.0  762.5  581.5  587.0  626.5 1024.0  351.5 1097.0  861.5  436.5 1243.0  671.5 1518.5 1048.5 1216.5  938.0  651.0 1396.0  942.0  786.5  970.0  904.5 1089.5  692.0  472.0 1103.5 1057.0  607.0  671.5  641.0  829.5 1248.0 1027.0 1167.5  656.5  607.5 1109.0  384.0 1327.0  492.5  539.0  770.5  682.5  647.5  931.5  512.0  343.0  877.5 1325.5  744.5 1118.0    0.0  572.5  879.5  956.0 1526.0 1120.5 1080.0 1232.5 1483.5 1074.0  898.0 1288.5 1157.5  536.0  674.0  626.5  706.5 1133.0  631.0  615.5 1362.5  646.0 1345.0  983.0  529.0 1058.5    0.0  876.0 1236.0  974.0 1062.0 1548.5 1147.0 1208.0  393.0 1266.5  964.0  698.0 1090.5  612.5 1246.0  544.0  913.0 1142.0 1155.0  774.5  843.0 1073.5  749.0 1357.0  436.5 1385.0  506.5 1000.0  739.5 1029.0  875.0 1325.5 1342.0  821.0  874.5  349.5 1169.0  944.5  396.5 1179.5 1289.5  994.0  971.0 1398.5  602.0  915.0 1150.0 1173.0  836.0  808.5  967.0 1305.0 1346.5    0.0 1148.5  598.5  745.5  847.5 1241.5 1301.5 1132.5 1145.0  551.5  731.5 1021.5  785.5  975.0  854.5  716.0    0.0  727.5
#> + new[7, ]    1218.5  611.5  744.0 1467.0 1462.5  842.5  961.5 1335.0  933.0  654.5 1105.5 1251.5 1266.5  806.0  246.5 1203.5 1048.0  681.0 1570.5  821.5 1120.5  715.5 1006.5  861.5 1424.0 1395.5 1596.0 1151.5 1675.0 1048.0 1021.5 1101.0 1654.5 1039.0  850.5 1549.0 1142.5  543.5 1243.5 1576.0  707.0 1642.5  907.5 1161.5 1062.0 1063.5 1393.5  875.5 1046.0 1809.0  864.5 1497.5  259.0 1011.5  827.0  841.5  882.5 1281.5  606.0 1342.5 1128.0  693.5 1491.5  920.0 1777.5 1303.5 1459.5 1189.0  919.5 1662.0 1197.0 1045.0 1223.5 1172.0 1335.5  942.5  729.5 1352.5 1305.0  860.5  927.5  895.5 1086.5 1494.5 1287.5 1410.5  918.5  847.0 1379.0  630.0 1577.5  752.0  792.0 1032.5  936.0  902.5 1196.0  762.5  597.5 1131.0 1578.0 1003.5 1370.5  273.0  832.0 1133.0 1221.5 1786.0 1375.0 1328.0 1482.5 1743.0 1336.0 1146.0 1541.5 1406.5  791.5  919.5  884.5  958.0 1378.5  892.5  878.0 1613.5  888.5 1601.5 1234.0  796.0 1307.5  255.5 1142.5 1494.5 1233.5 1317.0 1805.5 1408.5 1460.0  657.0 1518.5 1210.0  961.5 1342.5  873.0 1509.5  811.0 1166.0 1392.0 1404.0 1028.5 1093.0 1327.5 1022.0 1597.0  705.5 1647.0  763.0 1265.0 1002.5 1284.0 1128.5 1592.0 1601.5 1072.0 1134.0  619.5 1427.0 1197.0  648.5 1440.0 1540.5 1244.5 1217.0 1663.5  861.5 1174.0 1400.5 1415.5 1086.0 1056.0 1211.5 1577.0 1604.0  256.0 1407.0  844.0 1005.5 1098.0 1503.5 1567.5 1397.5 1408.0  805.0  986.0 1275.0 1038.0 1222.0 1104.0  969.0  244.5  969.0
#> - old[8, ]     813.0    0.0  341.0 1258.5    0.0  753.0    0.0 1106.5    0.0  602.0  758.0  752.0  905.5  679.0  334.5  293.5  966.0    0.0 1247.5  375.0    0.0  608.5  804.5  548.5 1313.5  896.5  283.5 1159.5  931.5  434.0    0.0  829.5  838.5  472.5    0.0    0.0 1114.5  763.0  366.5  475.0  390.0  605.0  728.0  956.5  644.5  765.0  458.5    0.0 1174.0  361.5  346.0  871.0  386.5 1223.5    0.0    0.0  473.5    0.0  390.0    0.0  927.0  499.5  480.5  805.0 1063.5  539.0  628.0    0.0  845.0    0.0  517.0  802.0    0.0    0.0  967.0  392.0  292.5  865.5    0.0    0.0 1044.5  375.5  605.0  639.5  297.5  573.5    0.0 1406.5    0.0  791.5  730.5  800.5    0.0    0.0  441.0    0.0  532.5  362.5  560.0    0.0  929.0  422.0  325.5 1145.0  599.0 1224.5 1025.0    0.0  293.5  391.0  514.0    0.0  743.0    0.0  284.0 1275.5    0.0  674.0  734.5  513.0 1031.0  355.5  420.0  608.0  445.0  743.0  895.5  510.5    0.0  425.5    0.0  350.0  890.5  296.0  293.0  584.0  797.5  632.0 1131.0 1157.5  792.5  973.5  687.0  528.5    0.0  630.5  661.5  662.0  774.5 1150.0  748.0  307.5  772.0  940.0    0.0 1031.0  487.5 1048.5  535.0 1049.5    0.0  430.5  289.5  295.5  580.5 1298.0  604.5    0.0    0.0 1406.0  836.5  466.0  439.5 1276.5    0.0 1028.5  280.5  655.0  713.5  441.5  700.5  506.0  796.0  508.0 1126.5  679.5  290.0  355.5  961.5 1004.0    0.0    0.0 1341.0  356.0  489.0  341.0 1003.0  443.5  974.5  408.5
#> + new[8, ]    1062.5  262.0  592.0 1517.5  273.5 1022.0  256.5 1372.5  248.0  856.0 1006.0  996.5 1160.0  930.5  581.0  551.0 1223.0  269.0 1513.5  638.0  249.5  872.0 1061.0  803.5 1566.5 1160.0  537.5 1417.0 1181.0  689.5  248.0 1063.0 1104.0  730.5  258.0  257.0 1368.0 1016.5  611.5  735.0  648.5  849.5  979.0 1225.5  877.0 1021.5  720.5  247.0 1436.0  602.0  605.5 1134.0  645.5 1472.5  245.5  254.5  729.5  257.5  644.5  245.5 1193.5  756.5  729.0 1053.5 1322.5  794.0  871.0  251.0 1113.5  266.0  772.0 1060.5  253.5  267.5 1213.0  642.5  550.0 1114.5  248.0  253.5 1300.5  630.0  862.0  886.0  558.0  816.5  262.0 1646.0  270.0 1037.5  981.0 1060.0  253.0  262.0  694.5  255.0  797.0  613.0  814.5  253.5 1181.5  681.0  578.0 1418.0  858.5 1478.0 1290.5  260.0  548.0  639.0  764.0  259.5 1005.0  248.0  537.0 1524.5  255.5  919.5  992.5  764.5 1276.5  617.0  682.5  859.0  687.5  999.5 1146.5  777.5  249.0  681.0  266.5  608.5 1150.0  551.0  550.0  845.5 1049.5  896.0 1383.0 1403.5 1056.0 1225.5  947.5  792.0  267.0  883.5  911.5  911.0 1028.5 1400.0 1002.0  580.5 1012.0 1209.0  262.0 1287.5  752.5 1311.5  790.0 1303.0  266.5  690.0  540.5  555.0  850.5 1556.0  857.0  252.0  260.5 1657.0 1087.0  712.0  704.5 1536.0  259.0 1279.0  523.0  905.0  961.0  686.0  972.5  763.5 1052.0  766.5 1372.0  939.5  540.5  617.5 1227.5 1269.0  263.0  253.5 1595.5  609.5  741.5  588.0 1252.5  696.5 1219.0  650.0
#> - old[9, ]    1242.0  390.5    0.0  376.5 1116.0    0.0    0.0  357.0  719.0    0.0  765.5  685.5  642.0  714.0  334.5  889.0  885.0 1107.5  990.5    0.0  283.0    0.0  857.5  330.5  741.0    0.0  595.5    0.0    0.0    0.0    0.0  275.0    0.0  567.5    0.0  595.0    0.0  445.0  909.5  355.5  390.0  433.5  415.5    0.0 1343.5  894.5 1365.5    0.0  422.0  361.5  908.5    0.0  630.0    0.0  560.5    0.0  348.5  343.0    0.0    0.0 1029.0  721.5  798.5  951.0    0.0  356.5    0.0  402.0    0.0    0.0  295.5  291.0  497.5    0.0  347.0  538.0  393.0  279.0  736.0  796.0  512.5  781.5    0.0  493.0    0.0  639.0    0.0  504.0  562.0    0.0  514.5    0.0  918.0    0.0    0.0    0.0  446.0    0.0    0.0    0.0  641.5  679.0  804.5    0.0    0.0  771.5    0.0  520.0  443.0  889.5    0.0    0.0  591.0  280.5    0.0    0.0  294.0  969.5    0.0    0.0    0.0    0.0  304.5  293.0    0.0    0.0  459.0  915.0  563.0    0.0    0.0  621.5    0.0  473.0    0.0    0.0    0.0    0.0  344.0  559.0  346.5  767.5  687.0    0.0    0.0    0.0 1178.0 1193.0  929.0  958.0  662.0  307.5  593.5  500.0  343.5  613.0    0.0  665.5  570.0  456.0  560.0    0.0    0.0  654.0  674.0    0.0  592.0    0.0  607.5  951.0  606.0    0.0  484.5  912.5  446.0  790.5    0.0  998.0  639.0  600.5    0.0  721.0    0.0    0.0  371.5  350.0    0.0  641.0  952.0    0.0    0.0  290.5    0.0  555.0  527.5  992.0    0.0  283.5 1204.0    0.0
#> + new[9, ]    1491.5  652.5  251.0  635.5 1389.5  269.0  256.5  623.0  967.0  254.0 1013.5  930.0  896.5  965.5  581.0 1146.5 1142.0 1376.5 1256.5  263.0  532.5  263.5 1114.0  585.5  994.0  263.5  849.5  257.5  249.5  255.5  248.0  508.5  265.5  825.5  258.0  852.0  253.5  698.5 1154.5  615.5  648.5  678.0  666.5  269.0 1576.0 1151.0 1627.5  247.0  684.0  602.0 1168.0  263.0  889.0  249.0  806.0  254.5  604.5  600.5  254.5  245.5 1295.5  978.5 1047.0 1199.5  259.0  611.5  243.0  653.0  268.5  266.0  550.5  549.5  751.0  267.5  593.0  788.5  650.5  528.0  984.0 1049.5  768.5 1036.0  257.0  739.5  260.5  882.0  262.0  743.5  832.0  246.0  765.0  259.5 1171.0  262.0  253.5  255.0  710.5  250.5  254.5  253.5  894.0  938.0 1057.0  273.0  259.5 1025.0  265.5  780.0  697.5 1137.5  250.0  259.5  853.0  528.5  253.0  249.0  549.5 1215.0  258.0  251.5  245.5  261.5  567.0  544.0  242.5  256.5  710.0 1182.0  812.0  255.5  266.5  880.0  259.5  728.0  257.0  261.5  252.0  264.0  596.0  805.0  610.0 1019.5  947.5  263.5  267.0  253.0 1428.0 1442.0 1183.0 1208.0  916.0  580.5  833.5  769.0  605.5  869.5  265.0  928.5  825.0  709.5  826.5  259.5  251.0  913.5  944.0  258.0  844.5  252.0  868.0 1202.0  856.5  246.0  749.5 1172.0  705.0 1041.0  242.5 1248.0  886.5  845.0  272.0  978.5  256.0  258.5  617.0  610.0  250.5  903.0 1218.0  265.0  263.0  544.0  254.5  808.5  780.0 1239.0  249.5  536.5 1448.5  241.5
#> - old[10, ]    717.0 1077.0 1658.0 1232.0 1322.0 1513.5 1430.5 1328.5 1128.0 1455.5 1163.5 1353.0 1351.5  913.5 1531.0  919.5 1267.0 1261.0 1240.5 1401.5 1545.5 1297.5 1085.0 1590.0 1584.5  652.5 1703.0  996.5 1418.5 1641.5 1673.0 1313.5 1568.5 1425.0 1525.0 1556.0  642.0 1681.5  514.5 1472.5 1535.0  860.5  997.5 1610.0  791.5    0.0 1370.0 1594.0  562.0 1719.5 1323.5 1554.0 1651.0  593.0 1330.0  721.0 1668.5 1587.5 1519.5 1216.5 1535.5 1521.5 1515.0 1237.5 1220.0 1632.5 1314.5 1659.5 1606.5  998.5 1366.5 1287.5 1411.0  953.0 1202.5 1210.5  995.0 1206.5 1315.5  782.5  813.0 1598.5 1103.5 1666.5 1185.5 1358.5  917.0  779.5 1467.0 1578.5 1659.0 1216.5  912.0  608.5 1140.5 1164.0 1673.5 1267.0 1157.0 1459.5 1355.0 1357.5 1043.5 1190.5 1603.5 1450.0 1116.5 1566.0 1567.0 1711.0 1600.0 1568.5 1407.5  566.5 1689.0  765.5 1477.5 1588.5 1503.0 1430.0 1552.0 1119.5 1095.0 1458.5 1491.5 1299.5 1322.5 1598.0 1620.0  736.0 1519.5 1014.0 1631.5 1550.5 1515.0 1528.0 1140.0 1356.5 1729.0  925.5 1605.5 1431.5 1222.0 1510.5 1213.5 1242.5 1615.0 1354.5  493.5 1608.0 1515.5  767.5 1384.0 1396.0  817.0  700.0 1562.5 1522.0 1503.0 1303.0 1447.0 1098.5 1049.5  854.5 1259.0 1100.5 1493.5 1568.0 1378.0 1678.0 1422.0 1086.5 1595.0  958.0 1038.0 1337.5 1735.5  630.5 1530.5  868.0 1630.0 1610.0 1300.0 1250.0 1652.5 1473.5  801.0 1344.0 1676.0 1693.0  856.5 1548.5 1723.5 1530.0 1116.0 1404.5 1505.5 1450.5  591.5 1451.0
#> + new[10, ]    966.5 1339.0 1909.0 1491.0 1595.5 1782.5 1687.0 1594.5 1376.0 1709.5 1411.5 1597.5 1606.0 1165.0 1777.5 1177.0 1524.0 1530.0 1506.5 1664.5 1795.0 1561.0 1341.5 1845.0 1837.5  916.0 1957.0 1254.0 1668.0 1897.0 1921.0 1547.0 1834.0 1683.0 1783.0 1813.0  895.5 1935.0  759.5 1732.5 1793.5 1105.0 1248.5 1879.0 1024.0  256.5 1632.0 1841.0  824.0 1960.0 1583.0 1817.0 1910.0  842.0 1575.5  975.5 1924.5 1845.0 1774.0 1462.0 1802.0 1778.5 1763.5 1486.0 1479.0 1887.5 1557.5 1910.5 1875.0 1264.5 1621.5 1546.0 1664.5 1220.5 1448.5 1461.0 1252.5 1455.5 1563.5 1036.0 1069.0 1853.0 1360.5 1913.0 1446.0 1601.5 1179.0 1019.0 1737.0 1824.5 1909.5 1476.0 1165.0  870.5 1394.0 1419.0 1938.0 1517.5 1411.5 1713.0 1607.5 1616.5 1296.0 1463.5 1863.0 1703.5 1382.0 1826.0 1821.5 1959.0 1850.0 1828.0 1669.5  814.5 1942.0 1014.5 1733.0 1834.0 1761.0 1681.5 1797.5 1381.0 1357.5 1709.5 1734.0 1556.0 1573.5 1865.0 1869.0  991.5 1786.0 1272.5 1891.0 1805.5 1772.0 1789.5 1392.0 1620.5 1981.0 1171.5 1869.0 1683.5 1482.5 1774.0 1480.5 1495.5 1865.0 1603.5  747.5 1858.0 1769.5 1040.5 1624.0 1665.0 1079.0  956.5 1827.5 1785.0 1758.0 1556.5 1713.5 1358.0 1300.5 1114.0 1529.0 1358.5 1746.0 1820.0 1638.5 1929.0 1672.5 1332.5 1860.0 1217.5 1297.0 1588.0 1978.0  880.5 1778.0 1112.5 1902.0 1867.5 1556.0 1508.5 1898.0 1733.5 1051.5 1606.0 1942.0 1958.0 1119.5 1802.0 1978.0 1783.5 1368.5 1651.5 1755.0 1703.5  836.0 1692.5
#> and 1990 more ...

Created on 2024-01-28 with reprex v2.0.2

Yunuuuu commented 8 months ago

And the differences were large.

bnprks commented 8 months ago

The rank_transform function has documented differences from the colRanks function:

Rank values are offset such that the rank of a 0 value is 0, and ties are handled by averaging ranks.

If you account for this in your test, you should find that all the values match up:

zero_rank <- matrixStats::colMins(mat)
obj <- as.matrix(obj)
dimnames(obj) <- NULL
waldo::compare(BPCells::add_cols(as.matrix(obj), zero_rank), mat)

Note that the method I showed for finding the zero_rank assumes that you don't have negative values in the input matrix, but the BPCells function should still work properly even in the presence of negative input values.

Edit: to clarify the rationale here -- maintaining sparse matrices in BPCells is important for efficiency, which is why rank_transform performs the offset so that the rank of 0 values stays at 0. This doesn't affect most downstream use-cases like spearman correlation or Wilcoxon rank-sum tests. But also, rank_transform is not itself an exported function from BPCells due to this somewhat unusual behavior.

Yunuuuu commented 8 months ago

Thanks for the explanations, is it possible to provide an argument to control the behaviour, so we can implement a rowRanks and colRanks function easily

Yunuuuu commented 8 months ago

Or implement a colMins function, so can combine both function to create rowRanks and colRanks function

bnprks commented 8 months ago

Hey @Yunuuuu, thanks for taking a look this and the other issues/pull requests you've opened this weekend -- I appreciate the help! It will take me a little bit of time to review each of the issues and pull requests, probably Tuesday/Wednesday for most of them since I'm a bit busy for the start of this week.

Do you happen to know any C++? I think implementing a colMins function might be the cleanest way to get at your use-case. It's not on my immediate roadmap, but it could make a good first contribution to the C++ side of the code base and would help you solve your use-case here. I can of course offer pointers if you're interested, or even arrange a short video call in Pacific Time to give an overview of all the steps.

Brief pointers to examples of similar functionality if you're impatient Basically what you want here is a function that will read through each element in an IterableMatrix and return some vector to R. The Wilcoxon rank sum code demonstrates all the necessary elements of this albeit with a more complicated function, and `matrix_value_histogram_cpp` shows the most bare-bones version, albeit missing functionality to check for ctrl-C from the user in the middle of the function running.
Yunuuuu commented 8 months ago

Thanks for your great work in this package, I can only write some small C++ codes. I have tried to understand the function matrix_value_histogram_cpp and wilcoxon_rank_sum, they are too complicated for me. I'm not famaliar many basic cpp function.

These issues were all found when I tested the BPCells basic function,I think I can make a pull request to add more unit tests

bnprks commented 8 months ago

Hi @Yunuuuu, I think I've come up with a way you can do this fully with the existing BPCells functionality. I still think that the performance of an exact colRanks equivalent in BPCells is likely to be quite poor since the matrix will be dense thus requiring 10-20x more computation and storage for downstream operations than the sparse version that BPCells provides.

However, if you really want to implement this operation for compatibility reasons, then I think this should do the trick for you:

dense_col_rank <- function(m) {
    zeros <- nrow(m) - matrix_stats(m, col_stats="nonzero")$col_stats["nonzero",]
    negatives <- nrow(m) - colSums(m > 0) - zeros

    rank_offsets <- ifelse(zeros != 0, negatives + (1+zeros)/2, 0)

    add_cols(rank_transform(m, "col"), rank_offsets)
}
Yunuuuu commented 7 months ago

Sorry for the delayed reply, by following the code snippet you provided, I successfully created the rowRanks and colRanks function. For me, it's okay to close this thread or you would lik to keep it open for others who might have similar questions.

I just added the modified rank_transform for others who may want to use it:

rank_transform <- function(object, axis = NULL, offset = TRUE, ...) {
    main_axis <- BPCells::storage_order(object)
    if (is.null(axis)) {
        axis <- main_axis
        cli::cli_inform("Doing {.val {axis}} rank transformation")
    } else {
        axis <- match.arg(axis, c("row", "col"))
        if (axis != main_axis) {
            cli::cli_warn(c(
                "transposing the storage order for {.arg object}",
                i = "{.arg axis} specified is different from the the storage axis of {.arg object} ({.val {main_axis}})" # nolint
            ))
            object <- BPCells::transpose_storage_order(
                matrix = object, ...
            )
        }
    }
    matrix <- BPCells:::rank_transform(mat = object, axis = axis)
    if (!offset) {
        if (axis == "row") {
            nonzero <- BPCells::matrix_stats(object,
                row_stats = "nonzero"
            )$row_stats["nonzero", , drop = TRUE]
            zeros <- ncol(object) - nonzero
            negatives <- ncol(object) - rowSums(object > 0) - zeros
            rank_offsets <- ifelse(zeros != 0,
                negatives + (1 + zeros) / 2, 0
            )
            matrix <- matrix + rank_offsets
        } else {
            nonzero <- BPCells::matrix_stats(object,
                col_stats = "nonzero"
            )$col_stats["nonzero", , drop = TRUE]
            zeros <- nrow(object) - nonzero
            negatives <- nrow(object) - colSums(object > 0) - zeros
            rank_offsets <- ifelse(zeros != 0,
                negatives + (1 + zeros) / 2, 0
            )
            matrix <- t(t(matrix) + rank_offsets)
        }
    }
    matrix
}
bnprks commented 7 months ago

Glad it worked for you, and thanks for sharing that code snippet