美文网首页Linux与生物信息
shell脚本练习:ascp从ENA批量下载数据脚本

shell脚本练习:ascp从ENA批量下载数据脚本

作者: 学生信的大叔 | 来源:发表于2022-01-11 20:49 被阅读0次

引言

我们往往需要从ENA下载测序数据,数据量过大时实用ascp 无疑是最佳选择。

ENA下载的包含数据链接的TSV格式文本到下载,每次都这么折腾,过于浪费时间。

这里,我写了个shell脚本,提取TSV格式文本 内的数据链接并用ascp下载,不过这个脚本只能用于linux平台(你在win上安装git-bash 和cmder也不行)。

友情提醒:ascp占用网络资源过多,如果是租的服务器,最好先咨询下服务器是否允许使用。

脚本使用

输入文件:filereport_read_run_*_tsv.txt 为从ENA下载的TSV格式的,包含所有Run链接的文本。如何下载该文件不是本文重点,可以自行搜索。

WeChat后台回复ascp下载 可以下载到我测试用的输入文件与该文脚本(7天有效期)。

提醒:测试用的输入文件内有300+下载链接,大家测试时注意截取一部分来测试。

#使用方法
$ bash ascp_ENA.sh <filereport_read_run_*_tsv.txt> <output_path>

脚本信息注释

  • input

    更改全局参数设置,一般不用改。其中的treads,指的是download 部分一次性下载文件个数。

  • check parameters

    如果参数个数不是2,打印使用信息

  • check the ascp

    检查是否已经安装Aspera-connect

    如果ascp没有安装,则会提示安装信息(如下),这里只是写了目前的最新版本,大家安装时灵活选择。

wget -c https://d3gcli72yxqn2z.cloudfront.net/connect_latest/v4/bin/ibm-aspera-connect_4.1.1.73_linux.tar.gz
tar -zxvf ibm-aspera-connect_4.1.1.73_linux.tar.gz
sh ibm-aspera-connect_4.1.1.73_linux.sh
echo "PATH=$PATH:$HOME/.aspera/connect/bin/" >> $HOME/.bashrc
source $HOME/.bashrc

  • check the input file

检查输入文件是否存在且可读。否,则打印帮助命令并跳出。

  • check the output dirctory

检查输出文件夹是否存在。否,则打印帮助命令并跳出。

  • download

检查提取输入文件链接内容,并下载。同时下载文件数为threads 值,此处为5。

脚本特点

  • 因输入造成的错误都不会继续往下执行,会及时跳出整个脚本,因为不太懂exit返回值,这里我都写作返回1了。
  • 输入文件为从ENA下载的TSV格式文件,只需要包含ENA的下载链接即可。按ftp.*gz 提取的链接部分。
  • 每次同时下载5个文件,你也可以修改脚本呢开头 threads=5赋值部分。这个多线程有些限制,大家可以看下参考 部分,其中有更好的多进行并发写法,但是我看不懂。
  • 脚本内已经尽量给出了报错信息,方便定位报错位置。

脚本限制

  • 没有检查ascp密钥文件位置,懒得写了。如果你用conda安装的,密钥文件可能不是这个路径。
  • 不能使用不同平台。
  • 没有设置默认值,不如perl赋值那么方便。具体为什么不能用逻辑“或”|| 在shell脚本中赋值,大家可以自行查下shell变量赋值特点。

脚本内容

#!/usr/bin/env bash
#Name: ascp_ENA.sh
#NOTE: You can only run this script on the linux system with bash 
#Usage: bash $0  <filereport_read_run_*_tsv.txt>  <output_path>

## input
input_file=$1
output_path=$2
threads=5

usage="Usage:\nbash $0  <filereport_read_run_*_tsv.txt>  <output_path>"

## check parameters
if [ $# != 2 ];then
    echo -e $usage
    exit 1
fi

## check the ascp
###NOTE: ascp must be install && add it to the envionment variable
ascp_install='\nThe ascp command must be installed locally and add environment variables!\n\nwget -c https://d3gcli72yxqn2z.cloudfront.net/connect_latest/v4/bin/ibm-aspera-connect_4.1.1.73_linux.tar.gz\ntar -zxvf ibm-aspera-connect_4.1.1.73_linux.tar.gz\nsh ibm-aspera-connect_4.1.1.73_linux.sh\necho "PATH=$PATH:$HOME/.aspera/connect/bin/" >> $HOME/.bashrc\nsource $HOME/.bashrc\n'
ascp -h > /dev/null
if [ $? == 0 ];then
    echo "Command ascp is ready !"
else
    echo -e ${ascp_install}
    exit 1
fi

## check the input file
if [ -r $1 ];then
    echo "Input file $1 is ready!"
else
    echo "ERROR: check the input file $1 !"
    echo -e $usage
    exit 1
fi

## check the output dirctory
if [ -d $2  ];then
    echo "Output dirctory $2 is ready !"
else 
    echo "ERROR: check the output dirctory $2 !"
    echo -e $usage
    exit 1
fi

## download
sleep 1
echo "Trying to download fastq files......"

lims=0
grep -o 'ftp.*gz' ${input_file} |sed -e 's/;/\n/g' -e 's/ftp.sra.ebi.ac.uk//g' | while read fq_ftp 
do
    all=$(echo ${fq_ftp} |wc -l)
    if [ $all == 0  ];then
        echo "The $1 don't have effective ftp address. Please check it!"
    fi
    ((lims++)) 
    ascp -QT -l 300m -P33001 -i ${HOME}/.aspera/connect/etc/asperaweb_id_dsa.openssh era-fasp@fasp.sra.ebi.ac.uk:${fq_ftp}   ${output_path} &  #这里必须放入后台,不然多线程就是空谈。
    #Example: ascp -QT -l 300m -P33001 -i ~/.aspera/connect/etc/asperaweb_id_dsa.openssh era-fasp@fasp.sra.ebi.ac.uk:/vol1/fastq/SRR949/SRR949627/SRR949627_1.fastq.gz ./

    if [ ${lims} -ge ${threads} ];then  #-ge 大于等于
        wait  # wait不加PID就是等待所有子进程全部结束才继续。
        lims=0  #重置限制,继续循环上面的命令
    fi
done

#bash ascp_for_ENA.sh  <filereport_read_run_*_tsv.txt>  <output path>  

参考

微信推文:气象编程 | shell多进程并发&进程数控制

相关文章

网友评论

    本文标题:shell脚本练习:ascp从ENA批量下载数据脚本

    本文链接:https://www.haomeiwen.com/subject/tudicrtx.html