annexeFunc/ChipLoading.R

###### loading ChIP data script####


chipLoading<-function(ChIP){
    print("Loading ChIP data")

    if(class(ChIP)!="data.frame"){
        if(grepl(".Rda",ChIP)){
            ChIP<-get(load(ChIP))
        } else if(grepl(".bed",ChIP)){
          # Tab Format

         if(length(grep(x=readLines(ChIP,5),pattern="\t"))>0){
             if(length(grep(x=readLines(ChIP,5),pattern="\t"))!=5){
             buffer<-5-length(grep(x=readLines(ChIP,5),pattern="\t"))
             ChIP<-read.delim(ChIP, header=F,sep="\t", stringsAsFactors=F, skip=buffer)
             } else {
             ChIP<-read.delim(ChIP, header=F,sep="\t", stringsAsFactors=F)
             }
             # if "chr" is missing
             if(length(grep(x=ChIP[,1], pattern="chr"))==0 ){
                 ChIP[,1]<-paste0("chr",ChIP[,1])
             }
             # bed format test
             if(ncol(ChIP)<4){
                 stop(paste0(deparse(substitute(ChIP)),
                 " is not in bed file Format. Please convert to bed/bedGraph file format"))}
             # Remove extra columns from format change
             if(ncol(ChIP)>4){
                 ChIP<-ChIP[,-4]
             }
         } else if(length(grep(x=readLines(ChIP,5),pattern=" "))>0){
           #spacing format
           if(length(grep(x=readLines(ChIP,5),pattern="\t"))!=5){
           buffer<-5-length(grep(x=readLines(ChIP,5),pattern="\t"))
           ChIP<-read.delim(ChIP, header=F,sep="\t", stringsAsFactors=F, skip=buffer)
           } else {
           ChIP<-read.delim(ChIP, header=F,sep="\t", stringsAsFactors=F)
           }
             # if "chr" is missing
             if(length(grep(x=ChIP[,1], pattern="chr"))==0 ){
                 ChIP[,1]<-paste0("chr",ChIP[,1])
             }
             # bed format test
             if(ncol(ChIP)<4){
                 stop(paste0(deparse(substitute(CHIP)),
                 " is not in bed file Format. Please convert to bed/bedGraph file format"))}
             # Remove extra columns from format change
             if(ncol(ChIP)>4){
                 ChIP<-ChIP[,-4]
             }
         } else if(length(grep(x=readLines(ChIP,5),pattern=","))>0){
           # CSV format
           if(length(grep(x=readLines(ChIP,5),pattern="\t"))!=5){
           buffer<-5-length(grep(x=readLines(ChIP,5),pattern="\t"))
           ChIP<-read.delim(ChIP, header=F,sep="\t", stringsAsFactors=F, skip=buffer)
           } else {
           ChIP<-read.delim(ChIP, header=F,sep="\t", stringsAsFactors=F)
           }
             # if "chr" is missing
             if(length(grep(x=ChIP[,1], pattern="chr"))==0 ){
                 ChIP[,1]<-paste0("chr",ChIP[,1])
             }
             # bed format test
             if(ncol(ChIP)<4){
                 stop(paste0(deparse(substitute(CHIP)),
                 " is not in bed file Format. Please convert to bed/bedGraph file format"))}
             # Remove extra columns from format change
             if(ncol(ChIP)>4){
                 ChIP<-ChIP[,-4]

           }
        }

    } else if(grepl(".bedGraph",ChIP)){
      if(length(grep(x=readLines(ChIP,5),pattern="\t"))>0){
        if(length(grep(x=readLines(ChIP,5),pattern="\t"))!=5){
        buffer<-5-length(grep(x=readLines(ChIP,5),pattern="\t"))
        ChIP<-read.delim(ChIP, header=F,sep="\t", stringsAsFactors=F, skip=buffer)
        } else {
        ChIP<-read.delim(ChIP, header=F,sep="\t", stringsAsFactors=F)
        }
          # if "chr" is missing
          if(length(grep(x=ChIP[,1], pattern="chr"))==0 ){
              ChIP[,1]<-paste0("chr",ChIP[,1])
          }
          # bed format test
          if(ncol(ChIP)<4){
              stop(paste0(deparse(substitute(ChIP)),
              " is not in bed file Format. Please convert to bed/bedGraph file format"))}
          # Remove extra columns from format change
          if(ncol(ChIP)>4){
              ChIP<-ChIP[,-4]
          }
      } else if(length(grep(x=readLines(ChIP,5),pattern=" "))>0){
        #spacing format
        if(length(grep(x=readLines(ChIP,5),pattern=" "))!=5){
        buffer<-5-length(grep(x=readLines(ChIP,5),pattern=" "))
        ChIP<-read.delim(ChIP, header=F,sep=" ", stringsAsFactors=F, skip=buffer)
        } else {
        ChIP<-read.delim(ChIP, header=F,sep=" ", stringsAsFactors=F)
        }
          # if "chr" is missing
          if(length(grep(x=ChIP[,1], pattern="chr"))==0 ){
              ChIP[,1]<-paste0("chr",ChIP[,1])
          }
          # bed format test
          if(ncol(ChIP)<4){
              stop(paste0(deparse(substitute(CHIP)),
              " is not in bed file Format. Please convert to bed/bedGraph file format"))}
          # Remove extra columns from format change
          if(ncol(ChIP)>4){
              ChIP<-ChIP[,-4]
          }
      } else if(length(grep(x=readLines(ChIP,5),pattern=","))>0){
        # CSV format
        if(length(grep(x=readLines(ChIP,5),pattern=","))!=5){
        buffer<-5-length(grep(x=readLines(ChIP,5),pattern=","))
        ChIP<-read.csv(ChIP, header=F, stringsAsFactors=F, skip=buffer)
        } else {
        ChIP<-read.csv(ChIP, header=F, stringsAsFactors=F)
        }
          # if "chr" is missing
          if(length(grep(x=ChIP[,1], pattern="chr"))==0 ){
              ChIP[,1]<-paste0("chr",ChIP[,1])
          }
          # bed format test
          if(ncol(ChIP)<4){
              stop(paste0(deparse(substitute(CHIP)),
              " is not in bed file Format. Please convert to bed/bedGraph file format"))}
          # Remove extra columns from format change
          if(ncol(ChIP)>4){
              ChIP<-ChIP[,-4]

        }
     }
   } else if(grepl(".wig", ChIP)){
     if(length(grep(x=readLines(ChIP,5),pattern="\t"))>0){
       if(length(grep(x=readLines(ChIP,5),pattern="\t"))!=5){
       buffer<-5-length(grep(x=readLines(ChIP,5),pattern="\t"))
       ChIP<-read.delim(ChIP, header=F,sep="\t", stringsAsFactors=F, skip=buffer)
       } else {
       ChIP<-read.delim(ChIP, header=F,sep="\t", stringsAsFactors=F)
       }
         # if "chr" is missing
         if(length(grep(x=ChIP[,1], pattern="chr"))==0 ){
             ChIP[,1]<-paste0("chr",ChIP[,1])
         }
         # bed format test
         if(ncol(ChIP)<4){
             stop(paste0(deparse(substitute(ChIP)),
             " is not in bed file Format. Please convert to bed/bedGraph file format"))}
         # Remove extra columns from format change
         if(ncol(ChIP)>4){
             ChIP<-ChIP[,-4]
         }
     } else if(length(grep(x=readLines(ChIP,5),pattern=" "))>0){
       #spacing format
       if(length(grep(x=readLines(ChIP,5),pattern=" "))!=5){
       buffer<-5-length(grep(x=readLines(ChIP,5),pattern=" "))
       ChIP<-read.delim(ChIP, header=F,sep=" ", stringsAsFactors=F, skip=buffer)
       } else {
       ChIP<-read.delim(ChIP, header=F,sep=" ", stringsAsFactors=F)
       }
         # if "chr" is missing
         if(length(grep(x=ChIP[,1], pattern="chr"))==0 ){
             ChIP[,1]<-paste0("chr",ChIP[,1])
         }
         # bed format test
         if(ncol(ChIP)<4){
             stop(paste0(deparse(substitute(CHIP)),
             " is not in bed file Format. Please convert to bed/bedGraph file format"))}
         # Remove extra columns from format change
         if(ncol(ChIP)>4){
             ChIP<-ChIP[,-4]
         }
     } else if(length(grep(x=readLines(ChIP,5),pattern=","))>0){
       # CSV format
       if(length(grep(x=readLines(ChIP,5),pattern=","))!=5){
       buffer<-5-length(grep(x=readLines(ChIP,5),pattern=","))
       ChIP<-read.csv(ChIP, header=F, stringsAsFactors=F, skip=buffer)
       } else {
       ChIP<-read.csv(ChIP, header=F, stringsAsFactors=F)
       }
         # if "chr" is missing
         if(length(grep(x=ChIP[,1], pattern="chr"))==0 ){
             ChIP[,1]<-paste0("chr",ChIP[,1])
         }
         # bed format test
         if(ncol(ChIP)<4){
             stop(paste0(deparse(substitute(CHIP)),
             " is not in bed file Format. Please convert to bed/bedGraph file format"))}
         # Remove extra columns from format change
         if(ncol(ChIP)>4){
             ChIP<-ChIP[,-4]

       }
    }
   }
  }
    return(ChIP)
}
patrickCNMartin/ChIPanalyserDev documentation built on Nov. 14, 2019, 4:39 p.m.