[R] Calculating all possible ratios
Rui Barradas
ruipbarradas at sapo.pt
Fri Oct 5 23:01:58 CEST 2012
And was it it?
Rui Barradas
Em 05-10-2012 21:44, genome1976 escreveu:
> Thanks Rui.I actually exactly did that.
>
>
> Date: Fri, 5 Oct 2012 05:49:20 -0700
> From: ml-node+s789695n4645154h35 at n4.nabble.com
> To: genome1976 at hotmail.com
> Subject: Re: Calculating all possible ratios
>
>
>
> Hello,
>
>
> Comment out the second apply and all following instructions using 'r2'.
>
> In the end return 'r1', not cbind.
>
>
> Hope this helps,
>
>
> Rui Barradas
>
> Em 04-10-2012 23:38, genome1976 escreveu:
>
>> Hi Rui,
>>
>> A while ago you helped me with calculaing all possible ratios from a dataset.
>>
>> This is the code I am using as suggested by you.
>>
>> data <- read.table("new_data.txt", header=T, row.names=1, sep="\t")
>> pairwise.ratios <- function(x, prefix="probeset", char=":"){
>> n <- ncol(x)
>> cn <- colnames(x)
>> if(length(cn) == 0){
>> cn <- gsub(" ", "0", formatC(seq.int(n), width=nchar(n)))
>> cn <- paste(prefix, cn, sep="")
>> }
>> cmb <- combn(n, 2)
>> r1 <- apply(cmb, 2, function(j) x[, j[1]]/x[, j[2]])
>> r2 <- apply(cmb, 2, function(j) x[, j[2]]/x[, j[1]])
>> colnames(r1) <- apply(cmb, 2, function(j) paste(cn[j], collapse=char))
>> colnames(r2) <- apply(cmb, 2, function(j) paste(cn[rev(j)], collapse=char))
>> cbind(r1, r2)[, order(c(colnames(r1), colnames(r2)))]
>> }
>> results <- pairwise.ratios(data.t)
>> write.table(t(results), "ratios_results.txt", sep="\t")
>>
>> It works perfectly fine only that it gives both pairs of ratios a:b and b:a for any two variables a and b.
>> Can you suggest me a way so that I get only one ratio and not both (Combination with caring for the order and not Permutation??)
>>
>> Thanks for any help.
>>
>> Best Regards,
>> Som.
>>
>> Date: Sat, 12 May 2012 15:20:52 -0700
>> From: [hidden email]
>> To: [hidden email]
>> Subject: RE: Calculating all possible ratios
>> Hello,
>> Nothing wrong with me, maybe your R session has some conflicting objects.
>> Running the function in the previous post on the first 4 rows and first 6 columns of your dataset the result was (copy&paste to your session)
>> result <- structure(c(8.74714923153198, 1.83094400392095, 9.92065138471113,
>> 1.77145415014708, 1.01515180575001, 0.167175438316099, 0.222321656865252,
>> 0.155576771874649, 3.09417748158541, 0.469647988505747, 1.29398633565582,
>> 0.524043736521509, 3.75969597954255, 0.422694576901317, 9.75471698113208,
>> 0.290397651827521, 4.9035575319622, 1.00105273231888, 1.01093964697178,
>> 0.26895145631068, 0.114322960947685, 0.546166347992352, 0.100799832714726,
>> 0.564507977763338, 0.11605516024473, 0.0913055986191245, 0.0224099858208782,
>> 0.0878243288779063, 0.353735531392494, 0.256505926724138, 0.130433606169248,
>> 0.295826869963301, 0.42981957664441, 0.230861553382365, 0.983273839877614,
>> 0.163931791180376, 0.56058921623124, 0.546741314958369, 0.10190254729944,
>> 0.151825242718447, 0.9850743448771, 5.98173996175908, 4.49798734905118,
>> 6.4276947512815, 8.61659229879359, 10.9522309159971, 44.6229644227777,
>> 11.3863665430362, 3.04799485560622, 2.8093121408046, 5.82033416762497,
>> 3.36839317468124, 3.70358005398494, 2.52844904226946, 43.8765935747068,
>> 1.86658746243623, 4.83036872336483, 5.98803713273998, 4.54719374273333,
>> 1.72873786407767, 0.323187666496628, 2.12925430210325, 0.772805687699305,
>> 1.90823767237023, 2.82697074863659, 3.89854539725884, 7.66673581578674,
>> 3.38035554418724, 0.328084543240185, 0.35595902124055, 0.1718114409242,
>> 0.296877457036954, 1.21508737036511, 0.900024246342843, 7.53850076491586,
>> 0.554147739185128, 1.58476931628683, 2.13149583692219, 0.781259909100518,
>> 0.513223300970874, 0.265978952936953, 2.36577437858509, 0.102514506769826,
>> 3.44355401535389, 2.32655759378615, 4.33160041310018, 1.01701068353905,
>> 6.10009805175427, 0.270009014365446, 0.395499368696959, 0.0227911949977918,
>> 0.535737017484743, 0.822986086753186, 1.11108117816092, 0.132652370966651,
>> 1.8045729131197, 1.30424309801742, 2.36826490573261, 0.103635979283374,
>> 0.926148867313916, 0.203933571388086, 0.998948374760994, 0.989178733859585,
>> 3.71814309436142, 1.78383738225087, 1.82901853699522, 9.81329737579089,
>> 6.58652001534723, 0.207023533247665, 0.166999632405824, 0.219915855047535,
>> 0.578456699988768, 0.631006664328306, 0.469154094827586, 1.27998376513563,
>> 1.9484696000908, 0.76672822844154, 0.422250060615857, 9.64915859255482,
>> 1.07974002376127), .Dim = c(4L, 30L), .Dimnames = list(c("S1",
>> "S2", "S3", "S4"), c("P1:P2", "P1:P3", "P1:P4", "P1:P5", "P1:P6",
>> "P2:P1", "P2:P3", "P2:P4", "P2:P5", "P2:P6", "P3:P1", "P3:P2",
>> "P3:P4", "P3:P5", "P3:P6", "P4:P1", "P4:P2", "P4:P3", "P4:P5",
>> "P4:P6", "P5:P1", "P5:P2", "P5:P3", "P5:P4", "P5:P6", "P6:P1",
>> "P6:P2", "P6:P3", "P6:P4", "P6:P5")))
>> Rui Barradas
>> genome1976 wrote
>> Hi Rui,
>> Thanks once again. I really appreciate it.
>> I tried using the code with the following dataset:
>>
>>
>> Sample
>> P1
>> P2
>> P3
>> P4
>> P5
>> P6
>> P7
>> P8
>> P9
>> P10
>>
>>
>> S1
>> 5292.9
>> 605.1
>> 5213.9
>> 1710.6
>> 1407.8
>> 1079.4
>> 1379.6
>> 9321.4
>> 6951
>> 1205.8
>>
>>
>> S2
>> 104.6
>> 57.129
>> 625.69
>> 222.72
>> 247.46
>> 104.49
>> 330.29
>> 1863.7
>> 389.67
>> 216.29
>>
>>
>> S3
>> 191.29
>> 19.282
>> 860.42
>> 147.83
>> 19.61
>> 189.22
>> 203.27
>> 1799
>> 369.9
>> 175.73
>>
>>
>> S4
>> 41.553
>> 23.457
>> 267.09
>> 79.293
>> 143.09
>> 154.5
>> 52.567
>> 613.54
>> 408.86
>> 61.715
>>
>>
>> S5
>> 671.33
>> 19.076
>> 1040.9
>> 319.04
>> 50.766
>> 57.445
>> 50.005
>> 1615.5
>> 1149.1
>> 163.99
>>
>>
>> S6
>> 125.9
>> 22.296
>> 563.83
>> 236.36
>> 112.38
>> 81.581
>> 48.406
>> 2073.6
>> 388.4
>> 62.575
>>
>>
>> S7
>> 78.485
>> 18.152
>> 248.18
>> 156.19
>> 322.4
>> 162.01
>> 38.379
>> 2786.8
>> 630.63
>> 71.163
>>
>>
>> S8
>> 1355.6
>> 51.534
>> 422.51
>> 134.89
>> 202.34
>> 48.368
>> 69.45
>> 231.11
>> 1875.9
>> 153.18
>>
>>
>> S9
>> 2167.6
>> 45.244
>> 430.73
>> 262.19
>> 365.71
>> 116.49
>> 65.663
>> 151.04
>> 3071.5
>> 210.55
>>
>>
>> S10
>> 575.7
>> 24.699
>> 170.09
>> 128.64
>> 42.58
>> 31.034
>> 55.256
>> 294.67
>> 448.05
>> 226.19
>>
>>
>> S11
>> 234.22
>> 22.594
>> 944.54
>> 118.91
>> 16.994
>> 102.67
>> 199.32
>> 2300
>> 192.38
>> 108.3
>>
>>
>> S12
>> 193.38
>> 25.374
>> 829.88
>> 74.872
>> 108.1
>> 116.49
>> 175.49
>> 1248
>> 340.33
>> 65.022
>>
>>
>> S13
>> 60.43
>> 15.907
>> 947.23
>> 49.12
>> 62.992
>> 189.11
>> 27.348
>> 134.46
>> 209.66
>> 67.638
>>
>>
>> S14
>> 94.563
>> 23.304
>> 303.25
>> 36.745
>> 220.11
>> 128.72
>> 33.242
>> 135.6
>> 263.31
>> 107.4
>>
>>
>> S15
>> 44.379
>> 17.794
>> 768
>> 26.144
>> 49.329
>> 103
>> 26.782
>> 290.04
>> 151.63
>> 66.928
>>
>>
>> S16
>> 42.333
>> 14.671
>> 224.49
>> 19.642
>> 100.96
>> 82.624
>> 42.736
>> 94.781
>> 182.99
>> 68.087
>>
>>
>> S17
>> 528.59
>> 49.81
>> 685.29
>> 24.981
>> 106.7
>> 72.219
>> 114.44
>> 610.74
>> 1779.9
>> 245.57
>>
>> When I run the code, this is what I get :
>> probeset1:probeset2 probeset1:probeset3 probeset1:probeset4
>> [1,] 0.2000000 0.1111111 0.07692308
>> [2,] 0.3333333 0.2000000 0.14285714
>> [3,] 0.4285714 0.2727273 0.20000000
>> [4,] 0.5000000 0.3333333 0.25000000
>> probeset1:probeset5 probeset1:probeset6 probeset2:probeset1
>> [1,] 0.05882353 0.04761905 5.000000
>> [2,] 0.11111111 0.09090909 3.000000
>> [3,] 0.15789474 0.13043478 2.333333
>> [4,] 0.20000000 0.16666667 2.000000
>> probeset2:probeset3 probeset2:probeset4 probeset2:probeset5
>> [1,] 0.5555556 0.3846154 0.2941176
>> [2,] 0.6000000 0.4285714 0.3333333
>> [3,] 0.6363636 0.4666667 0.3684211
>> [4,] 0.6666667 0.5000000 0.4000000
>> probeset2:probeset6 probeset3:probeset1 probeset3:probeset2
>> [1,] 0.2380952 9.000000 1.800000
>> [2,] 0.2727273 5.000000 1.666667
>> [3,] 0.3043478 3.666667 1.571429
>> [4,] 0.3333333 3.000000 1.500000
>> probeset3:probeset4 probeset3:probeset5 probeset3:probeset6
>> [1,] 0.6923077 0.5294118 0.4285714
>> [2,] 0.7142857 0.5555556 0.4545455
>> [3,] 0.7333333 0.5789474 0.4782609
>> [4,] 0.7500000 0.6000000 0.5000000
>> probeset4:probeset1 probeset4:probeset2 probeset4:probeset3
>> [1,] 13 2.600000 1.444444
>> [2,] 7 2.333333 1.400000
>> [3,] 5 2.142857 1.363636
>> [4,] 4 2.000000 1.333333
>> probeset4:probeset5 probeset4:probeset6 probeset5:probeset1
>> [1,] 0.7647059 0.6190476 17.000000
>> [2,] 0.7777778 0.6363636 9.000000
>> [3,] 0.7894737 0.6521739 6.333333
>> [4,] 0.8000000 0.6666667 5.000000
>> probeset5:probeset2 probeset5:probeset3 probeset5:probeset4
>> [1,] 3.400000 1.888889 1.307692
>> [2,] 3.000000 1.800000 1.285714
>> [3,] 2.714286 1.727273 1.266667
>> [4,] 2.500000 1.666667 1.250000
>> probeset5:probeset6 probeset6:probeset1 probeset6:probeset2
>> [1,] 0.8095238 21.000000 4.200000
>> [2,] 0.8181818 11.000000 3.666667
>> [3,] 0.8260870 7.666667 3.285714
>> [4,] 0.8333333 6.000000 3.000000
>> probeset6:probeset3 probeset6:probeset4 probeset6:probeset5
>> [1,] 2.333333 1.615385 1.235294
>> [2,] 2.200000 1.571429 1.222222
>> [3,] 2.090909 1.533333 1.210526
>> [4,] 2.000000 1.500000 1.200000
>> As you can see P1:P2 is not 0.2000000 as indicated by the results of the code.
>> Am I doing something wrong?
>> Thanks once again.
>> Som.
>> Date: Sat, 12 May 2012 14:04:11 -0700
>> From: [hidden email]
>> To: [hidden email]
>> Subject: RE: Calculating all possible ratios
>> Hello,
>> I'm glad it helped. Now to make the result readable. (I had thought of it.)
>> pairwise.ratios <- function(x, prefix="probeset", char=":"){
>> n <- ncol(x)
>> cn <- colnames(x)
>> if(length(cn) == 0){
>> cn <- gsub(" ", "0", formatC(seq.int(n), width=nchar(n)))
>> cn <- paste(prefix, cn, sep="")
>> }
>> cmb <- combn(n, 2)
>> r1 <- apply(cmb, 2, function(j) x[, j[1]]/x[, j[2]])
>> r2 <- apply(cmb, 2, function(j) x[, j[2]]/x[, j[1]])
>> colnames(r1) <- apply(cmb, 2, function(j) paste(cn[j], collapse=char))
>> colnames(r2) <- apply(cmb, 2, function(j) paste(cn[rev(j)], collapse=char))
>> cbind(r1, r2)[, order(c(colnames(r1), colnames(r2)))]
>> }
>> m1 <- matrix(1:24, ncol=6)
>> pairwise.ratios(m1)
>> Rui Barradas
>> genome1976 wrote
>> Thanks so much Rui. I really appreciate all the help.
>> I implemented the code and it ran fine. I was wondering how I could include the probeset ids as probeset1:probeset2 in the final output so that I know which ratios are for which probeset pairs.
>> Thanks so much.
>> Som.
>> Date: Sat, 12 May 2012 09:29:54 -0700
>> From: [hidden email]
>> To: [hidden email]
>> Subject: Re: Calculating all possible ratios
>> Hello,
>> If by all possible gene ratios you mean all pairwise column ratios, try
>> the following.
>> # Make up some data
>> x <- matrix(1:24, ncol=6)
>> cmb <- combn(ncol(x), 2)
>> r1 <- apply(cmb, 2, function(j) x[, j[1]]/x[, j[2]])
>> r2 <- apply(cmb, 2, function(j) x[, j[2]]/x[, j[1]])
>> cbind(r1, r2)
>> Note that the dataset must be a numeric only matrix or data.frame.
>> Hope this helps,
>> Rui Barradas
>> Em 12-05-2012 11:00, [hidden email] escreveu:
>>> Date: Fri, 11 May 2012 12:39:23 -0700 (PDT)
>>> From: genome1976<[hidden email]>
>>> To:[hidden email]
>>> Subject: [R] Calculating all possible ratios
>>> Message-ID:<[hidden email]>
>>> Content-Type: text/plain
>>> I have a data matrix with genes as columns and samples as rows. I want to
>>> create all possible gene ratios.Is there an elegant and fast way to do it in
>>> R and write it to a dataframe?
>>> Thanks for any help.
>>> Som.
>>> --
>>> View this message in context:http://r.789695.n4.nabble.com/Calculating-all-possible-ratios-tp4627405.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>> [[alternative HTML version deleted]]
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>
>>
>>
>> If you reply to this email, your message will be added to the discussion below:
>> http://r.789695.n4.nabble.com/Calculating-all-possible-ratios-tp4627405p4629105.html
>>
>>
>>
>> To unsubscribe from Calculating all possible ratios, click here.
>> NAML
>>
>>
>>
>>
>>
>> If you reply to this email, your message will be added to the discussion below:
>> http://r.789695.n4.nabble.com/Calculating-all-possible-ratios-tp4627405p4629513.html
>>
>>
>>
>> To unsubscribe from Calculating all possible ratios, click here.
>> NAML
>> If you reply to this email, your message will be added to the discussion below:http://r.789695.n4.nabble.com/Calculating-all-possible-ratios-tp4627405p4629656.html
>> To unsubscribe from Calculating all possible ratios, click here.
>> NAML
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Calculating-all-possible-ratios-tp4627405p4645098.html
>> Sent from the R help mailing list archive at Nabble.com.
>> [[alternative HTML version deleted]]
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> ______________________________________________
>
> [hidden email] mailing list
>
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>
>
>
>
>
>
>
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://r.789695.n4.nabble.com/Calculating-all-possible-ratios-tp4627405p4645154.html
>
>
>
> To unsubscribe from Calculating all possible ratios, click here.
>
> NAML
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Calculating-all-possible-ratios-tp4627405p4645219.html
> Sent from the R help mailing list archive at Nabble.com.
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list