Search code examples
rparsingexport-to-csvtaxonomyblast

How to write csv file in R for following data?


I want to get the Get taxonomic hierarchy using Accession Numbers. The simplest way i found to do is

library('taxize')
for (year in c("AY744148.1","AY656167.1","AY656168.1")){print(paste( year))}
classification(genbank2uid(id = year), db = "ncbi")

R is giving me following output, however i am unable to write a csv file from this

enter image description here

I want an outcome in csv file as shown in the picture below Output required enter image description here

your kind help in this regard is requested Regards Ali Zohaib


Solution

  • Perhaps you are after something like this?

    library('taxize')
    
    IDs <- c("AY744148.1","AY656167.1","AY656168.1") # your sequence of ids to iterate over
    output <- vector("list", length(years)) # prepare a list to write to
    
    # for each ID, run the function and store the result into a list
    for (i in 1:length(years)) {
      # output is a list of length one, so we need to subset it using "[["
      output[[i]] <- classification(genbank2uid(id = IDs[i]), db = "ncbi")[[1]]
      }
    
    # take list elements of output and merge them row-wise (rbind == row bind)
    output <- do.call(rbind, output)
    
                       name    rank    id
    1        other sequences no rank 28384
    2   artificial sequences no rank 81077
    3                vectors no rank 29278
    4  Cloning vector pBR322 species 47470
    5        other sequences no rank 28384
    6   artificial sequences no rank 81077
    7                vectors no rank 29278
    8  Cloning vector pGEM-3 species 90108
    9        other sequences no rank 28384
    10  artificial sequences no rank 81077
    11               vectors no rank 29278
    12 Cloning vector pGEM-3 species 90108
    

    You can write the result using

    write.table(output, file = "endfile.txt", row.names = FALSE, quote = FALSE)