Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Merging Matrix

Help
2013-12-10
2014-01-06
  • Ivan Mendez
    Ivan Mendez
    2013-12-10

    Hi, i´ve been trying to merge eight matrix that contain different number of individuals (rows) i want to keep individuals with missing data using the command listed in the manual, but i get this error message
    list.merge=choose.files(caption="Choose Files to Merge")

    MERGING(transpose = "IndRows",exclude = F, replacewith = NA)
    Error en [<-.data.frame(*tmp*, value, value = NA) :
    missing values are not allowed in subscripted assignments of data frames
    Además: Mensajes de aviso perdidos
    In [<-.factor(*tmp*, ri, value = 0) :
    invalid factor level, NA generated

     
  • Nils Arrigo
    Nils Arrigo
    2013-12-17

    There it goes!

    # 1. Load packages, as usual.
    require(RawGeno)
    require(vegan)  
    
    # 2. Replace the buggy function with a functionning one
    # Copy-paste the following code in the R console,
    MERGING=function (transpose = "indRows", exclude = T, replacewith = 0){
        listfiles = list.merge
        dat = list()
        if (transpose == "indColumns") {
          for(i in 1:length(listfiles)) {
                dat[[i]] = t(read.delim(listfiles[i], row.names = 1,header = T))
            }
        } else {
            for (i in 1:length(listfiles)) {
                dat[[i]] = read.delim(listfiles[i], row.names = 1,header = T)
            }
        }
    
        s.names = NULL
        for(i in 1:length(listfiles)) {
            nn = data.frame(fileID = rep(i, nrow(dat[[i]])), 
                            s.names = rownames(dat[[i]]))
            s.names = rbind(s.names, nn)
        }
        all.samples = levels(s.names$s.names)
    
        matchings = NULL
        for(i in 1:length(listfiles)){
            nn = data.frame(fileID = rep(i, length(all.samples)), 
                            matchings = match(all.samples, rownames(dat[[i]])))
            matchings = rbind(matchings, nn)
        }
    
        finalmat = dat[[1]][matchings[matchings$fileID == 1, 2], ]
        for(i in 2:length(listfiles)){
            mat = dat[[i]][matchings[matchings$fileID == i, 2], ]
            finalmat = data.frame(finalmat, mat)
        }
        rownames(finalmat) = all.samples
        if (exclude == F){
            finalmat[is.na(finalmat)] = replacewith
        } else {  
            test = rowSums(finalmat)
            finalmat = finalmat[is.na(test) == F,]
        }
        assign("mergedTable", finalmat, pos = 1)
        cat(paste(length(listfiles), "Files Successfully Merged! Please Use the usual saving Menu to export files!\n"))
    }
    
    # Finally launch RawGeno and use it as usual.
    RawGeno()
    

    There was indeed a bug crashing the function when using the exclude = F option.
    This option allows keeping all specimens, and replace missing values with some character of your own choice.

    Usually, the MERGING function is used via RawGeno's GUI and uses exclude = T by default, so that is it exports only specimens with full cases.

    NB. this bug was thus NOT affecting the typical behavior of RawGeno when merging files and affected only users that worked from the command-line.

    Thanks for reporting bugs, I will include this patch in the next release.
    Best, Nils

     
  • Ivan Mendez
    Ivan Mendez
    2014-01-06

    Thanks, It´s working now!