前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在非Linux系统的电脑也可以使用命令行工具操作R语言

在非Linux系统的电脑也可以使用命令行工具操作R语言

作者头像
生信技能树
发布2021-10-12 12:04:15
1.2K0
发布2021-10-12 12:04:15
举报
文章被收录于专栏:生信技能树

一般来说,我们在Linux系统的电脑(通常是服务器等超级计算机)上面工作时候不喜欢界面版本的rstudio,会直接在命令行界面交互式使用R语言,或者直接写好r脚本后,直接 Rscript命令就可以运行一个脚本。

而在个人电脑,通常是Mac或者Windows,都是直接使用界面版本的rstudio更方便的交互式使用R语言。交互式的好处是所见即所得,每个代码随时响应随时看到效果,尤其适合各种各样的统计可视化需求。

但凡是有利必有弊,如果一个任务需要重复运行成百上千次,也就是说我们每个处理都需要交互,那就是繁重的工作量了。但是如果是调试好的脚本,在命令行工具操作R语言直接 Rscript命令就可以运行一个脚本,运行成百上千次而无需交互。

在Mac或者Windows这样的个人电脑,我们也是可以借助git软件来做到使用命令行工具操作R语言。我这里以Mac为例子,在r里面输入下面的函数 :

代码语言:javascript
复制
>  R.home()
[1] "/Library/Frameworks/R.framework/Resources"
> 

就可以查看到自己的r语言这个软件的安装路径啦,现在就可以简单看看这个路径下面的:

代码语言:javascript
复制
ls -lh /Library/Frameworks/R.framework/Resources/bin/|cut -d " " -f 7-

  1.8K Apr  1 10:39 BATCH
  2.1K Apr  1 10:39 COMPILE
  825B Apr  1 10:39 INSTALL
  1.3K Apr  1 10:39 LINK
  8.9K Apr  1 10:39 R
  160B Apr  1 10:39 REMOVE
  1.4K Apr  1 10:39 Rcmd
  318B Apr  1 10:39 Rd2pdf
  361B Apr  1 10:39 Rdconv
  260B Apr  1 10:39 Rdiff
  312B Apr  1 10:39 Rprof
   53K Apr  1 10:41 Rscript
  157B Apr  1 10:39 SHLIB
  320B Apr  1 10:39 Stangle
  318B Apr  1 10:39 Sweave
  352B Apr  1 10:39 build
  319B Apr  1 10:39 check
   12K Apr  1 10:39 config
   96B Apr  1 10:41 exec
  2.9M Apr  1 10:41 fc-cache
   14K Apr  1 10:39 javareconf
  342K Apr  1 10:39 libtool
  3.4K Apr  1 10:39 mkinstalldirs
  481B Apr  1 10:39 pager
  2.3M Apr  1 10:41 qpdf
  4.0K Apr  1 10:39 rtags

虽然,这里面的软件命令如此多,但是主要的可执行程序是R和Rscript,这个时候大概率上我们借助git软件来做到使用命令行工具是无法使用上面的命令,所以接下来需要在命令行使用:

代码语言:javascript
复制
alias R='/Library/Frameworks/R.framework/Resources/bin/R'
alias Rscript='/Library/Frameworks/R.framework/Resources/bin/Rscript'

就可以看到运行 R 这个命令啦,如下所示 :

代码语言:javascript
复制
(base) jmzeng@jmzengs-iMac peaks_anno % R

R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin17.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> 

这个时候如果写脚本,文件名是 anno.R ,内容如下所示:

代码语言:javascript
复制
t1<-Sys.time()
temp_args <- commandArgs(trailingOnly = T)

if(length(temp_args) != 3){
  cat("运行命令方式:Rscript test.R folder sample species \n")
  quit("no")
}else{
  bedPeaksFile <- temp_args[1]
  species <-temp_args[2]
  folder <-temp_args[3]
  #BiocManager::install(c('ChIPseeker','TxDb.Hsapiens.UCSC.hg38.knownGene','org.Hs.eg.db'))
  # BiocManager::install("TxDb.Mmusculus.UCSC.mm10.knownGene")
  # BiocManager::install("ChIPseeker")
  require(ChIPseeker)
  require(TxDb.Hsapiens.UCSC.hg38.knownGene)
  require(TxDb.Mmusculus.UCSC.mm10.knownGene) 
  require(org.Hs.eg.db) 
  require(org.Mm.eg.db) 
  
  bedPeaksFile 
  cat(paste0('Now we process ', bedPeaksFile  ))
  peak <- readPeakFile( bedPeaksFile )  
  # table(seqlevels(peak))
  # keepChr= !grepl('_',seqlevels(peak))
  # seqlevels(peak, pruning.mode="coarse") <- seqlevels(peak)[keepChr]
  # table(seqlevels(peak))
  cat(paste0('there are ',length(peak),' peaks for this data' ))
  
  if(species=='human'){
    txdb <- TxDb.Hsapiens.UCSC.hg38.knownGene 
    peakAnno <- annotatePeak(peak, tssRegion=c(-3000, 3000), 
                             TxDb=txdb, annoDb="org.Hs.eg.db") 
  }else{
    txdb <- TxDb.Mmusculus.UCSC.mm10.knownGene 
    peakAnno <- annotatePeak(peak, tssRegion=c(-3000, 3000), 
                             TxDb=txdb, annoDb="org.Mm.eg.db") 
  }
  
  df=as.data.frame(peakAnno)
  cg_df = df[,c(1,2,4,23)]
  head(cg_df)
  cl=ifelse(grepl('Promoter',df$annotation),'Promoter',
            ifelse(grepl('Intron',df$annotation),'Intron',
                   ifelse(grepl('Intergenic',df$annotation),'Intergenic',
                          ifelse(grepl('Exon',df$annotation),'Exon',
                                 'other'))))
  table(cl)
  cg_df$anno=cl
  plotAnnoPie(peakAnno)
  write.table(cg_df,sep = '\t',quote = F,row.names = F,col.names = F,
              file = file.path(folder,
                               gsub('sort_peaks.narrowPeak.bed','ChIPseeker_anno.txt',
                                    basename(bedPeaksFile))))
  
  print("all done!!!")
  t2<-Sys.time()
  t2
  df <- t1-t2
  print(df)
} 

就可以命令行运行:

代码语言:javascript
复制
 Rscript anno.R  sort_peaks.narrowPeak.bed  human tf_human/

假如你有成百上千个bed文件,就可以使用这个格式的命令行,批量提交。并不需要手动交互式一个个文件名修改和处理哦。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-09-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信技能树 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档