R/cdfMergeStrands.R
f1d6fcf0
 ########################################################################/**
 # @RdocFunction cdfMergeStrands
 #
 # @title "Function to join CDF groups with the same names"
 #
 # \description{
 #  @get "title".
 #
 #  This @function is design to be used with @see "applyCdfGroups"
 #  on an Affymetrix Mapping (SNP) CDF @list structure.
 #
 #  This can be used to join the sense and anti-sense groups of the same 
 #  allele in SNP arrays.
 # }
 #
 # @synopsis
 #
 # \arguments{
 #  \item{groups}{A @list structure with groups.}
 #  \item{...}{Not used.}
 # }
 #
 # \value{
 #  Returns a @list structure with only two groups.
 # }
 #
5f865c77
 # \details{
 #  If a unit has two strands, they are merged such that the elements
7ba76859
 #  for the second strand are concatenated to the end of the elements
 #  of first strand (This is done separately for the two alleles).
5f865c77
 # }
 #
f1d6fcf0
 # \seealso{
 #  @see "applyCdfGroups".
 # }
 #
76cf4b26
 # @author "HB"
f1d6fcf0
 #
 # \references{
 #  [1] Affymetrix, \emph{Understanding Genotyping Probe Set Structure}, 2005.
 #      \url{http://www.affymetrix.com/support/developer/whitepapers/genotyping_probe_set_structure.affx}\cr
 # }
 #
 # @keyword programming
 # @keyword internal
 #**/#######################################################################
 cdfMergeStrands <- function(groups, ...) {
   nbrOfGroups <- length(groups);
a42c6a76
   if (nbrOfGroups != 2 && nbrOfGroups != 4 && nbrOfGroups %% 2 != 0)
f1d6fcf0
     return(groups);
 
   names <- names(groups);
   unames <- unique(names);
 
   res <- list();
a42c6a76
 
   # For each allele...
f1d6fcf0
   for (name in unames) {
     idx <- which(name == names);
a42c6a76
     group <- .subset2(groups, idx[1]);
     nfields <- length(group);
     if (nfields > 0) {
       ffs <- 1:nfields;
       idx <- idx[-1];
       while(length(idx) > 0) {
         groupNext <- .subset2(groups, idx[1]);
   
         # For each field...
         for (ff in ffs) {
           fields <- .subset2(group, ff);
           fieldsNext <- .subset2(groupNext, ff);
           ndim <- length(dim(fields));
           if (ndim <= 1) {
24277d85
             fields <- c(fields, fieldsNext);
a42c6a76
           } else if (ndim == 2) {
             fields <- cbind(fields, fieldsNext);
           } else {
             # This should never occur for a normal CDF structure.
24277d85
             fields <- c(fields, fieldsNext);
a42c6a76
           }
           group[[ff]] <- fields;
         }
   
         idx <- idx[-1];
       } # while(...)
f1d6fcf0
     }
a42c6a76
 
     res[[name]] <- group;
   } # for (name ...)
f1d6fcf0
 
   res;
a42c6a76
 } # cdfMergeStrands()
f1d6fcf0
 
 
 ############################################################################
 # HISTORY:
a42c6a76
 # 2008-02-22
 # o Updated so it now merges any unit with a even number of groups;
 #   a custom SNP CDF had three pairs of groups in part of their units.
f1d6fcf0
 # 2006-03-07
 # o Renamed from cdfStandJoiner() to cdfMergeStrands().
 # 2006-02-23
 # o Created.
 ############################################################################