Using nested loops to analyse up-regulated and down-regulated genes
up vote
2
down vote
favorite
I am new to R Programming and I am currently looking to analyse a huge dataset of genes. I am attempting to alter a nested loop that currently works to see which genes are up-regulated given varying adjusted P values and LogFC values to see the same but for down-regulated genes. What I have generated for the up-regulated genes is a 4x4 table looking to see how the number of genes that meet the cut off values I have selected for the P adjusted value and the LogFC varies. The image shows the coding used for the nested loop What I am getting when I attempt to execute this for the down-regulated genes is the same values - I am assuming R is not registering any changes and returns the same numbers.
Any advice would be much appreciated.
#--------------------------------------------------------------------------------
# Function to calculate number of up regulated genes
#--------------------------------------------------------------------------------
get.upregulated.genes <- function(dafra, p.value.max, log.fc.min) {
gene.count <- subset(dafra, adj.P.Val <= p.value.max & logFC >= log.fc.min) %>%
.[["Gene.symbol"]] %>% unique %>% length
return(gene.count)
}
get.upregulated.genes(dafra=df3, p.value.max=0.05, log.fc.min=1) # 429
p.values <- c(0.001, 0.005, 0.01, 0.05)
log.fcs <- rev(c(0.5, 1, 1.5, 2))
log.fcs
mx.up <- matrix(rep(NA, 16), ncol=4)
mx.up
colnames(mx.up) <- p.values
rownames(mx.up) <- log.fcs
for (x in p.values) {
for (y in log.fcs) {
z <- get.upregulated.genes(df3, x, y)
mx.up[as.character(y),as.character(x)] <- z
}
}
mx.up
View(mx.up)
for (x in p.values) {
for (y in log.fcs) {
z <- get.upregulated.genes(df3, x, y)
print(x)
print(y)
print(z)
print("------")
}
}
r dplyr bioinformatics
New contributor
add a comment |
up vote
2
down vote
favorite
I am new to R Programming and I am currently looking to analyse a huge dataset of genes. I am attempting to alter a nested loop that currently works to see which genes are up-regulated given varying adjusted P values and LogFC values to see the same but for down-regulated genes. What I have generated for the up-regulated genes is a 4x4 table looking to see how the number of genes that meet the cut off values I have selected for the P adjusted value and the LogFC varies. The image shows the coding used for the nested loop What I am getting when I attempt to execute this for the down-regulated genes is the same values - I am assuming R is not registering any changes and returns the same numbers.
Any advice would be much appreciated.
#--------------------------------------------------------------------------------
# Function to calculate number of up regulated genes
#--------------------------------------------------------------------------------
get.upregulated.genes <- function(dafra, p.value.max, log.fc.min) {
gene.count <- subset(dafra, adj.P.Val <= p.value.max & logFC >= log.fc.min) %>%
.[["Gene.symbol"]] %>% unique %>% length
return(gene.count)
}
get.upregulated.genes(dafra=df3, p.value.max=0.05, log.fc.min=1) # 429
p.values <- c(0.001, 0.005, 0.01, 0.05)
log.fcs <- rev(c(0.5, 1, 1.5, 2))
log.fcs
mx.up <- matrix(rep(NA, 16), ncol=4)
mx.up
colnames(mx.up) <- p.values
rownames(mx.up) <- log.fcs
for (x in p.values) {
for (y in log.fcs) {
z <- get.upregulated.genes(df3, x, y)
mx.up[as.character(y),as.character(x)] <- z
}
}
mx.up
View(mx.up)
for (x in p.values) {
for (y in log.fcs) {
z <- get.upregulated.genes(df3, x, y)
print(x)
print(y)
print(z)
print("------")
}
}
r dplyr bioinformatics
New contributor
Is it ok to paste the code in to the question, please?
– Jonny Phelps
Nov 19 at 13:15
Yes sure, please access it from the question above
– Hiba Al-khaffaji
Nov 19 at 14:04
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am new to R Programming and I am currently looking to analyse a huge dataset of genes. I am attempting to alter a nested loop that currently works to see which genes are up-regulated given varying adjusted P values and LogFC values to see the same but for down-regulated genes. What I have generated for the up-regulated genes is a 4x4 table looking to see how the number of genes that meet the cut off values I have selected for the P adjusted value and the LogFC varies. The image shows the coding used for the nested loop What I am getting when I attempt to execute this for the down-regulated genes is the same values - I am assuming R is not registering any changes and returns the same numbers.
Any advice would be much appreciated.
#--------------------------------------------------------------------------------
# Function to calculate number of up regulated genes
#--------------------------------------------------------------------------------
get.upregulated.genes <- function(dafra, p.value.max, log.fc.min) {
gene.count <- subset(dafra, adj.P.Val <= p.value.max & logFC >= log.fc.min) %>%
.[["Gene.symbol"]] %>% unique %>% length
return(gene.count)
}
get.upregulated.genes(dafra=df3, p.value.max=0.05, log.fc.min=1) # 429
p.values <- c(0.001, 0.005, 0.01, 0.05)
log.fcs <- rev(c(0.5, 1, 1.5, 2))
log.fcs
mx.up <- matrix(rep(NA, 16), ncol=4)
mx.up
colnames(mx.up) <- p.values
rownames(mx.up) <- log.fcs
for (x in p.values) {
for (y in log.fcs) {
z <- get.upregulated.genes(df3, x, y)
mx.up[as.character(y),as.character(x)] <- z
}
}
mx.up
View(mx.up)
for (x in p.values) {
for (y in log.fcs) {
z <- get.upregulated.genes(df3, x, y)
print(x)
print(y)
print(z)
print("------")
}
}
r dplyr bioinformatics
New contributor
I am new to R Programming and I am currently looking to analyse a huge dataset of genes. I am attempting to alter a nested loop that currently works to see which genes are up-regulated given varying adjusted P values and LogFC values to see the same but for down-regulated genes. What I have generated for the up-regulated genes is a 4x4 table looking to see how the number of genes that meet the cut off values I have selected for the P adjusted value and the LogFC varies. The image shows the coding used for the nested loop What I am getting when I attempt to execute this for the down-regulated genes is the same values - I am assuming R is not registering any changes and returns the same numbers.
Any advice would be much appreciated.
#--------------------------------------------------------------------------------
# Function to calculate number of up regulated genes
#--------------------------------------------------------------------------------
get.upregulated.genes <- function(dafra, p.value.max, log.fc.min) {
gene.count <- subset(dafra, adj.P.Val <= p.value.max & logFC >= log.fc.min) %>%
.[["Gene.symbol"]] %>% unique %>% length
return(gene.count)
}
get.upregulated.genes(dafra=df3, p.value.max=0.05, log.fc.min=1) # 429
p.values <- c(0.001, 0.005, 0.01, 0.05)
log.fcs <- rev(c(0.5, 1, 1.5, 2))
log.fcs
mx.up <- matrix(rep(NA, 16), ncol=4)
mx.up
colnames(mx.up) <- p.values
rownames(mx.up) <- log.fcs
for (x in p.values) {
for (y in log.fcs) {
z <- get.upregulated.genes(df3, x, y)
mx.up[as.character(y),as.character(x)] <- z
}
}
mx.up
View(mx.up)
for (x in p.values) {
for (y in log.fcs) {
z <- get.upregulated.genes(df3, x, y)
print(x)
print(y)
print(z)
print("------")
}
}
r dplyr bioinformatics
r dplyr bioinformatics
New contributor
New contributor
edited Nov 19 at 14:13
zx8754
28.6k76394
28.6k76394
New contributor
asked Nov 19 at 11:23
Hiba Al-khaffaji
112
112
New contributor
New contributor
Is it ok to paste the code in to the question, please?
– Jonny Phelps
Nov 19 at 13:15
Yes sure, please access it from the question above
– Hiba Al-khaffaji
Nov 19 at 14:04
add a comment |
Is it ok to paste the code in to the question, please?
– Jonny Phelps
Nov 19 at 13:15
Yes sure, please access it from the question above
– Hiba Al-khaffaji
Nov 19 at 14:04
Is it ok to paste the code in to the question, please?
– Jonny Phelps
Nov 19 at 13:15
Is it ok to paste the code in to the question, please?
– Jonny Phelps
Nov 19 at 13:15
Yes sure, please access it from the question above
– Hiba Al-khaffaji
Nov 19 at 14:04
Yes sure, please access it from the question above
– Hiba Al-khaffaji
Nov 19 at 14:04
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
No need for forloops
, try something like this (not tested, as there is no example data):
# get all combos
x <- expand.grid(
p.values = c(0.001, 0.005, 0.01, 0.05),
log.fcs = rev(c(0.5, 1, 1.5, 2)))
# loop rowby then get length of unique genes
apply(x, 1, function(i){
length(unique(df3[ df3$adj.P.Val <= i[ 1 ] & df3$logFC >= i[ 2 ], "Gene.symbol" ]))
})
Thanks for the response, unfortunately the second park did not work - Error in[.data.frame
(df3, df3$adj.P.Val <= i[1] & logFC >= i[2], "Gene.symbol") : object 'logFC' not found. This is the message that came up, any ideas what I need to alter?
– Hiba Al-khaffaji
Nov 19 at 16:52
@HibaAl-khaffaji try again please, edited.
– zx8754
Nov 19 at 17:01
Second time lucky! Thank you so much, just one final question if you don't mind. How can I carry this out a second time round for the down regulated genes? What I find is that when R executes the command, I get the same values as the up regulated genes so I am assuming I need to make it clear somehow that I now want the genes that are down regulated to be used?
– Hiba Al-khaffaji
Nov 19 at 17:07
@HibaAl-khaffaji please define what you mean by "down regulated genes". It would make it so much easier if you can provide example input data, and expected output.
– zx8754
Nov 19 at 17:55
I am looking at a huge gene expression dataset from GEO2R (GSE108363) - In the RScript I have been working on I have written code to extract genes that are up regulated and down regulated # Upregulated genes: up.df <- subset(df3, adj.P.Val <= 0.05 & logFC >= 1) # Downregulated genes: down.df <- subset(df3, adj.P.Val <= 0.05 & logFC <= -1) This is the code I used for that particular part.
– Hiba Al-khaffaji
Nov 19 at 20:14
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
No need for forloops
, try something like this (not tested, as there is no example data):
# get all combos
x <- expand.grid(
p.values = c(0.001, 0.005, 0.01, 0.05),
log.fcs = rev(c(0.5, 1, 1.5, 2)))
# loop rowby then get length of unique genes
apply(x, 1, function(i){
length(unique(df3[ df3$adj.P.Val <= i[ 1 ] & df3$logFC >= i[ 2 ], "Gene.symbol" ]))
})
Thanks for the response, unfortunately the second park did not work - Error in[.data.frame
(df3, df3$adj.P.Val <= i[1] & logFC >= i[2], "Gene.symbol") : object 'logFC' not found. This is the message that came up, any ideas what I need to alter?
– Hiba Al-khaffaji
Nov 19 at 16:52
@HibaAl-khaffaji try again please, edited.
– zx8754
Nov 19 at 17:01
Second time lucky! Thank you so much, just one final question if you don't mind. How can I carry this out a second time round for the down regulated genes? What I find is that when R executes the command, I get the same values as the up regulated genes so I am assuming I need to make it clear somehow that I now want the genes that are down regulated to be used?
– Hiba Al-khaffaji
Nov 19 at 17:07
@HibaAl-khaffaji please define what you mean by "down regulated genes". It would make it so much easier if you can provide example input data, and expected output.
– zx8754
Nov 19 at 17:55
I am looking at a huge gene expression dataset from GEO2R (GSE108363) - In the RScript I have been working on I have written code to extract genes that are up regulated and down regulated # Upregulated genes: up.df <- subset(df3, adj.P.Val <= 0.05 & logFC >= 1) # Downregulated genes: down.df <- subset(df3, adj.P.Val <= 0.05 & logFC <= -1) This is the code I used for that particular part.
– Hiba Al-khaffaji
Nov 19 at 20:14
add a comment |
up vote
1
down vote
No need for forloops
, try something like this (not tested, as there is no example data):
# get all combos
x <- expand.grid(
p.values = c(0.001, 0.005, 0.01, 0.05),
log.fcs = rev(c(0.5, 1, 1.5, 2)))
# loop rowby then get length of unique genes
apply(x, 1, function(i){
length(unique(df3[ df3$adj.P.Val <= i[ 1 ] & df3$logFC >= i[ 2 ], "Gene.symbol" ]))
})
Thanks for the response, unfortunately the second park did not work - Error in[.data.frame
(df3, df3$adj.P.Val <= i[1] & logFC >= i[2], "Gene.symbol") : object 'logFC' not found. This is the message that came up, any ideas what I need to alter?
– Hiba Al-khaffaji
Nov 19 at 16:52
@HibaAl-khaffaji try again please, edited.
– zx8754
Nov 19 at 17:01
Second time lucky! Thank you so much, just one final question if you don't mind. How can I carry this out a second time round for the down regulated genes? What I find is that when R executes the command, I get the same values as the up regulated genes so I am assuming I need to make it clear somehow that I now want the genes that are down regulated to be used?
– Hiba Al-khaffaji
Nov 19 at 17:07
@HibaAl-khaffaji please define what you mean by "down regulated genes". It would make it so much easier if you can provide example input data, and expected output.
– zx8754
Nov 19 at 17:55
I am looking at a huge gene expression dataset from GEO2R (GSE108363) - In the RScript I have been working on I have written code to extract genes that are up regulated and down regulated # Upregulated genes: up.df <- subset(df3, adj.P.Val <= 0.05 & logFC >= 1) # Downregulated genes: down.df <- subset(df3, adj.P.Val <= 0.05 & logFC <= -1) This is the code I used for that particular part.
– Hiba Al-khaffaji
Nov 19 at 20:14
add a comment |
up vote
1
down vote
up vote
1
down vote
No need for forloops
, try something like this (not tested, as there is no example data):
# get all combos
x <- expand.grid(
p.values = c(0.001, 0.005, 0.01, 0.05),
log.fcs = rev(c(0.5, 1, 1.5, 2)))
# loop rowby then get length of unique genes
apply(x, 1, function(i){
length(unique(df3[ df3$adj.P.Val <= i[ 1 ] & df3$logFC >= i[ 2 ], "Gene.symbol" ]))
})
No need for forloops
, try something like this (not tested, as there is no example data):
# get all combos
x <- expand.grid(
p.values = c(0.001, 0.005, 0.01, 0.05),
log.fcs = rev(c(0.5, 1, 1.5, 2)))
# loop rowby then get length of unique genes
apply(x, 1, function(i){
length(unique(df3[ df3$adj.P.Val <= i[ 1 ] & df3$logFC >= i[ 2 ], "Gene.symbol" ]))
})
edited Nov 19 at 17:01
answered Nov 19 at 14:26
zx8754
28.6k76394
28.6k76394
Thanks for the response, unfortunately the second park did not work - Error in[.data.frame
(df3, df3$adj.P.Val <= i[1] & logFC >= i[2], "Gene.symbol") : object 'logFC' not found. This is the message that came up, any ideas what I need to alter?
– Hiba Al-khaffaji
Nov 19 at 16:52
@HibaAl-khaffaji try again please, edited.
– zx8754
Nov 19 at 17:01
Second time lucky! Thank you so much, just one final question if you don't mind. How can I carry this out a second time round for the down regulated genes? What I find is that when R executes the command, I get the same values as the up regulated genes so I am assuming I need to make it clear somehow that I now want the genes that are down regulated to be used?
– Hiba Al-khaffaji
Nov 19 at 17:07
@HibaAl-khaffaji please define what you mean by "down regulated genes". It would make it so much easier if you can provide example input data, and expected output.
– zx8754
Nov 19 at 17:55
I am looking at a huge gene expression dataset from GEO2R (GSE108363) - In the RScript I have been working on I have written code to extract genes that are up regulated and down regulated # Upregulated genes: up.df <- subset(df3, adj.P.Val <= 0.05 & logFC >= 1) # Downregulated genes: down.df <- subset(df3, adj.P.Val <= 0.05 & logFC <= -1) This is the code I used for that particular part.
– Hiba Al-khaffaji
Nov 19 at 20:14
add a comment |
Thanks for the response, unfortunately the second park did not work - Error in[.data.frame
(df3, df3$adj.P.Val <= i[1] & logFC >= i[2], "Gene.symbol") : object 'logFC' not found. This is the message that came up, any ideas what I need to alter?
– Hiba Al-khaffaji
Nov 19 at 16:52
@HibaAl-khaffaji try again please, edited.
– zx8754
Nov 19 at 17:01
Second time lucky! Thank you so much, just one final question if you don't mind. How can I carry this out a second time round for the down regulated genes? What I find is that when R executes the command, I get the same values as the up regulated genes so I am assuming I need to make it clear somehow that I now want the genes that are down regulated to be used?
– Hiba Al-khaffaji
Nov 19 at 17:07
@HibaAl-khaffaji please define what you mean by "down regulated genes". It would make it so much easier if you can provide example input data, and expected output.
– zx8754
Nov 19 at 17:55
I am looking at a huge gene expression dataset from GEO2R (GSE108363) - In the RScript I have been working on I have written code to extract genes that are up regulated and down regulated # Upregulated genes: up.df <- subset(df3, adj.P.Val <= 0.05 & logFC >= 1) # Downregulated genes: down.df <- subset(df3, adj.P.Val <= 0.05 & logFC <= -1) This is the code I used for that particular part.
– Hiba Al-khaffaji
Nov 19 at 20:14
Thanks for the response, unfortunately the second park did not work - Error in
[.data.frame
(df3, df3$adj.P.Val <= i[1] & logFC >= i[2], "Gene.symbol") : object 'logFC' not found. This is the message that came up, any ideas what I need to alter?– Hiba Al-khaffaji
Nov 19 at 16:52
Thanks for the response, unfortunately the second park did not work - Error in
[.data.frame
(df3, df3$adj.P.Val <= i[1] & logFC >= i[2], "Gene.symbol") : object 'logFC' not found. This is the message that came up, any ideas what I need to alter?– Hiba Al-khaffaji
Nov 19 at 16:52
@HibaAl-khaffaji try again please, edited.
– zx8754
Nov 19 at 17:01
@HibaAl-khaffaji try again please, edited.
– zx8754
Nov 19 at 17:01
Second time lucky! Thank you so much, just one final question if you don't mind. How can I carry this out a second time round for the down regulated genes? What I find is that when R executes the command, I get the same values as the up regulated genes so I am assuming I need to make it clear somehow that I now want the genes that are down regulated to be used?
– Hiba Al-khaffaji
Nov 19 at 17:07
Second time lucky! Thank you so much, just one final question if you don't mind. How can I carry this out a second time round for the down regulated genes? What I find is that when R executes the command, I get the same values as the up regulated genes so I am assuming I need to make it clear somehow that I now want the genes that are down regulated to be used?
– Hiba Al-khaffaji
Nov 19 at 17:07
@HibaAl-khaffaji please define what you mean by "down regulated genes". It would make it so much easier if you can provide example input data, and expected output.
– zx8754
Nov 19 at 17:55
@HibaAl-khaffaji please define what you mean by "down regulated genes". It would make it so much easier if you can provide example input data, and expected output.
– zx8754
Nov 19 at 17:55
I am looking at a huge gene expression dataset from GEO2R (GSE108363) - In the RScript I have been working on I have written code to extract genes that are up regulated and down regulated # Upregulated genes: up.df <- subset(df3, adj.P.Val <= 0.05 & logFC >= 1) # Downregulated genes: down.df <- subset(df3, adj.P.Val <= 0.05 & logFC <= -1) This is the code I used for that particular part.
– Hiba Al-khaffaji
Nov 19 at 20:14
I am looking at a huge gene expression dataset from GEO2R (GSE108363) - In the RScript I have been working on I have written code to extract genes that are up regulated and down regulated # Upregulated genes: up.df <- subset(df3, adj.P.Val <= 0.05 & logFC >= 1) # Downregulated genes: down.df <- subset(df3, adj.P.Val <= 0.05 & logFC <= -1) This is the code I used for that particular part.
– Hiba Al-khaffaji
Nov 19 at 20:14
add a comment |
Hiba Al-khaffaji is a new contributor. Be nice, and check out our Code of Conduct.
Hiba Al-khaffaji is a new contributor. Be nice, and check out our Code of Conduct.
Hiba Al-khaffaji is a new contributor. Be nice, and check out our Code of Conduct.
Hiba Al-khaffaji is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373588%2fusing-nested-loops-to-analyse-up-regulated-and-down-regulated-genes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is it ok to paste the code in to the question, please?
– Jonny Phelps
Nov 19 at 13:15
Yes sure, please access it from the question above
– Hiba Al-khaffaji
Nov 19 at 14:04