Hi,
thanks for getting back to me, it is just for my job :)
so I tried it:
library(httr)
library(jsonlite)
library(xml2)
library(SparkR, lib.loc = c(file.path(Sys.getenv("SPARK_HOME"),
"R", "lib")))
sparkR.session(master = "local[*]", sparkConfig
list(spark.driver.memory = "2g"))
server <- "http://rest.ensembl.org"
f1 <- read.text("1g.txt")
f2 <- read.text("1n.txt")
for ( a in as.list(f1) ) {
for ( b in as.list(f2) ) {
ext <- paste0( "/ld/human/pairwise/",
a,
"/",
b,
"?population_name=1000GENOMES:phase_3:KHV")
r <- GET(paste(server, ext, sep = ""),
content_type("application/json"))
write(r,file="list.txt",append=TRUE)
}
}
and I got this error:
Error in as.list.default(f1) :
no method for coercing this S4 class to a vector
Please advise
On Fri, Jun 19, 2020 at 3:28 PM <cpolwart at chemo.org.uk>
wrote:>
> so (untested) if you did something like
>
> f1 <- read.text("1g.txt")
> f2 <- read.text("1n.txt")
>
> for ( a in as.list(f1) ) {
>
> for ( b in as.list(f2) ) {
>
> ext <- paste0( "/ld/human/pairwise/",
> a,
> "/",
> b,
> "?population_name=1000GENOMES:phase_3:KHV")
>
> r <- GET(paste(server, ext, sep = ""),
> content_type("application/json"))
>
> # You presumably need to do something with 'r' at
the
> moment its over written by the next loop.. were
> # you appending it to list.txt? Possibly its just a bit
> of the R output you want.?
>
> write(r,file="list.txt",append=TRUE)
>
>
> }
>
> }
>
>
> Are we doing your PhD for you ;-) Do we get to share ;-)
>
>
> On 2020-06-19 20:34, Ana Marija wrote:
> > Hello,
> >
> > I have two files (each has 300 lines)like this:
> >
> > head 1g.txt
> > rs6792369
> > rs1414517
> > rs16857712
> > rs16857703
> > rs12239392
> > ...
> >
> > head 1n.txt
> > rs1042779
> > rs2360630
> > rs10753597
> > rs7549096
> > rs2343491
> > ...
> >
> > For each pair of rs# from those two files I can run this command in R
> >
> > library(httr)
> > library(jsonlite)
> > library(xml2)
> >
> > server <- "http://rest.ensembl.org"
> > ext <-
> >
"/ld/human/pairwise/rs6792369/rs1042779?population_name=1000GENOMES:phase_3:KHV"
> >
> > r <- GET(paste(server, ext, sep = ""),
> > content_type("application/json"))
> >
> > stop_for_status(r)
> > head(fromJSON(toJSON(content(r))))
> > d_prime r2 variation1 variation2 population_name
> > 1 0.975513 0.951626 rs6792369 rs1042779 1000GENOMES:phase_3:KHV
> >
> > What I would like to do is to do is to run this command for every SNP
> > in one list (1g.txt) to each SNP in another list (1n.txt). Where SNP#
> > is rs# and output every line of result in list.txt
> >
> > The process is illustrated in the attachment.
> >
> > Please help,
> > Ana
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
cpoiw@rt m@iii@g oii chemo@org@uk
2020-Jun-19 21:41 UTC
[R] How to loop over two files ...
Oh - read.text isn't in base! Not sure where is came from (my head
mostly!) You may have something that adds it but better to use
something that works. So try using:
library(readr)
f1 <- read_tsv("1g.txt", col.names=F)
This will give you a tibble with f1$X1 with the file in it
then loop it with (a in as.list(f1[,1])
Others will have much slicker code than me!
On 2020-06-19 22:02, Ana Marija wrote:> Hi,
>
> thanks for getting back to me, it is just for my job :)
>
> so I tried it:
>
> library(httr)
> library(jsonlite)
> library(xml2)
> library(SparkR, lib.loc = c(file.path(Sys.getenv("SPARK_HOME"),
"R",
> "lib")))
> sparkR.session(master = "local[*]", sparkConfig >
list(spark.driver.memory = "2g"))
>
> server <- "http://rest.ensembl.org"
>
> f1 <- read.text("1g.txt")
> f2 <- read.text("1n.txt")
>
> for ( a in as.list(f1) ) {
>
> for ( b in as.list(f2) ) {
>
> ext <- paste0( "/ld/human/pairwise/",
> a,
> "/",
> b,
> "?population_name=1000GENOMES:phase_3:KHV")
>
> r <- GET(paste(server, ext, sep = ""),
> content_type("application/json"))
>
> write(r,file="list.txt",append=TRUE)
>
>
> }
>
> }
>
> and I got this error:
> Error in as.list.default(f1) :
> no method for coercing this S4 class to a vector
>
> Please advise
>
> On Fri, Jun 19, 2020 at 3:28 PM <cpolwart at chemo.org.uk> wrote:
>>
>> so (untested) if you did something like
>>
>> f1 <- read.text("1g.txt")
>> f2 <- read.text("1n.txt")
>>
>> for ( a in as.list(f1) ) {
>>
>> for ( b in as.list(f2) ) {
>>
>> ext <- paste0( "/ld/human/pairwise/",
>> a,
>> "/",
>> b,
>> "?population_name=1000GENOMES:phase_3:KHV")
>>
>> r <- GET(paste(server, ext, sep = ""),
>> content_type("application/json"))
>>
>> # You presumably need to do something with 'r'
at the
>> moment its over written by the next loop.. were
>> # you appending it to list.txt? Possibly its just a
>> bit
>> of the R output you want.?
>>
>> write(r,file="list.txt",append=TRUE)
>>
>>
>> }
>>
>> }
>>
>>
>> Are we doing your PhD for you ;-) Do we get to share ;-)
>>
>>
>> On 2020-06-19 20:34, Ana Marija wrote:
>> > Hello,
>> >
>> > I have two files (each has 300 lines)like this:
>> >
>> > head 1g.txt
>> > rs6792369
>> > rs1414517
>> > rs16857712
>> > rs16857703
>> > rs12239392
>> > ...
>> >
>> > head 1n.txt
>> > rs1042779
>> > rs2360630
>> > rs10753597
>> > rs7549096
>> > rs2343491
>> > ...
>> >
>> > For each pair of rs# from those two files I can run this command
in R
>> >
>> > library(httr)
>> > library(jsonlite)
>> > library(xml2)
>> >
>> > server <- "http://rest.ensembl.org"
>> > ext <-
>> >
"/ld/human/pairwise/rs6792369/rs1042779?population_name=1000GENOMES:phase_3:KHV"
>> >
>> > r <- GET(paste(server, ext, sep = ""),
>> > content_type("application/json"))
>> >
>> > stop_for_status(r)
>> > head(fromJSON(toJSON(content(r))))
>> > d_prime r2 variation1 variation2 population_name
>> > 1 0.975513 0.951626 rs6792369 rs1042779 1000GENOMES:phase_3:KHV
>> >
>> > What I would like to do is to do is to run this command for every
SNP
>> > in one list (1g.txt) to each SNP in another list (1n.txt). Where
SNP#
>> > is rs# and output every line of result in list.txt
>> >
>> > The process is illustrated in the attachment.
>> >
>> > Please help,
>> > Ana
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more,
see
>> > 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.
I tried it: > library(httr)> library(jsonlite) > library(xml2) > library(readr) > server <- "http://rest.ensembl.org" > f1 <- read_tsv("1g", col_names=F)Parsed with column specification: cols( X1 = col_character() )> f2 <- read_tsv("1n", col_names=F)Parsed with column specification: cols( X1 = col_character() )> > for ( a in as.list(f1[,1]) ) {+ + for ( b in as.list(f2[,1]) ) { + + ext <- paste0( "/ld/human/pairwise/", + a, + "/", + b, + "?population_name=1000GENOMES:phase_3:KHV") + + r <- GET(paste(server, ext, sep = ""), + content_type("application/json")) + + write(r,file="list.txt",append=TRUE) + + + } + + } Error in parse_url(url) : length(url) == 1 is not TRUE> traceback()10: stop(simpleError(msg, call = if (p <- sys.parent(1L)) sys.call(p))) 9: stopifnot(length(url) == 1) 8: parse_url(url) 7: is.url(url) 6: stopifnot(is.url(url)) 5: build_url(parse_url(url)[c("scheme", "hostname", "port")]) 4: handle_name(url) 3: handle_find(url) 2: handle_url(handle, url, ...) 1: GET(paste(server, ext, sep = ""), content_type("application/json")) On Fri, Jun 19, 2020 at 4:41 PM <cpolwart at chemo.org.uk> wrote:> > Oh - read.text isn't in base! Not sure where is came from (my head > mostly!) You may have something that adds it but better to use > something that works. So try using: > > library(readr) > f1 <- read_tsv("1g.txt", col.names=F) > > This will give you a tibble with f1$X1 with the file in it > > then loop it with (a in as.list(f1[,1]) > > Others will have much slicker code than me! > > On 2020-06-19 22:02, Ana Marija wrote: > > Hi, > > > > thanks for getting back to me, it is just for my job :) > > > > so I tried it: > > > > library(httr) > > library(jsonlite) > > library(xml2) > > library(SparkR, lib.loc = c(file.path(Sys.getenv("SPARK_HOME"), "R", > > "lib"))) > > sparkR.session(master = "local[*]", sparkConfig > > list(spark.driver.memory = "2g")) > > > > server <- "http://rest.ensembl.org" > > > > f1 <- read.text("1g.txt") > > f2 <- read.text("1n.txt") > > > > for ( a in as.list(f1) ) { > > > > for ( b in as.list(f2) ) { > > > > ext <- paste0( "/ld/human/pairwise/", > > a, > > "/", > > b, > > "?population_name=1000GENOMES:phase_3:KHV") > > > > r <- GET(paste(server, ext, sep = ""), > > content_type("application/json")) > > > > write(r,file="list.txt",append=TRUE) > > > > > > } > > > > } > > > > and I got this error: > > Error in as.list.default(f1) : > > no method for coercing this S4 class to a vector > > > > Please advise > > > > On Fri, Jun 19, 2020 at 3:28 PM <cpolwart at chemo.org.uk> wrote: > >> > >> so (untested) if you did something like > >> > >> f1 <- read.text("1g.txt") > >> f2 <- read.text("1n.txt") > >> > >> for ( a in as.list(f1) ) { > >> > >> for ( b in as.list(f2) ) { > >> > >> ext <- paste0( "/ld/human/pairwise/", > >> a, > >> "/", > >> b, > >> "?population_name=1000GENOMES:phase_3:KHV") > >> > >> r <- GET(paste(server, ext, sep = ""), > >> content_type("application/json")) > >> > >> # You presumably need to do something with 'r' at the > >> moment its over written by the next loop.. were > >> # you appending it to list.txt? Possibly its just a > >> bit > >> of the R output you want.? > >> > >> write(r,file="list.txt",append=TRUE) > >> > >> > >> } > >> > >> } > >> > >> > >> Are we doing your PhD for you ;-) Do we get to share ;-) > >> > >> > >> On 2020-06-19 20:34, Ana Marija wrote: > >> > Hello, > >> > > >> > I have two files (each has 300 lines)like this: > >> > > >> > head 1g.txt > >> > rs6792369 > >> > rs1414517 > >> > rs16857712 > >> > rs16857703 > >> > rs12239392 > >> > ... > >> > > >> > head 1n.txt > >> > rs1042779 > >> > rs2360630 > >> > rs10753597 > >> > rs7549096 > >> > rs2343491 > >> > ... > >> > > >> > For each pair of rs# from those two files I can run this command in R > >> > > >> > library(httr) > >> > library(jsonlite) > >> > library(xml2) > >> > > >> > server <- "http://rest.ensembl.org" > >> > ext <- > >> > "/ld/human/pairwise/rs6792369/rs1042779?population_name=1000GENOMES:phase_3:KHV" > >> > > >> > r <- GET(paste(server, ext, sep = ""), > >> > content_type("application/json")) > >> > > >> > stop_for_status(r) > >> > head(fromJSON(toJSON(content(r)))) > >> > d_prime r2 variation1 variation2 population_name > >> > 1 0.975513 0.951626 rs6792369 rs1042779 1000GENOMES:phase_3:KHV > >> > > >> > What I would like to do is to do is to run this command for every SNP > >> > in one list (1g.txt) to each SNP in another list (1n.txt). Where SNP# > >> > is rs# and output every line of result in list.txt > >> > > >> > The process is illustrated in the attachment. > >> > > >> > Please help, > >> > Ana > >> > > >> > ______________________________________________ > >> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >> > 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.