引言
我們往往需要從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。
腳本特點
- 因輸入造成的錯誤都不會繼續(xù)往下執(zhí)行割择,會及時跳出整個腳本眷篇,因為不太懂exit返回值,這里我都寫作返回
1
了荔泳。 - 輸入文件為從ENA下載的TSV格式文件蕉饼,只需要包含ENA的下載鏈接即可。按
ftp.*gz
提取的鏈接部分玛歌。 - 每次同時下載5個文件昧港,你也可以修改腳本呢開頭
threads=5
賦值部分。這個多線程有些限制支子,大家可以看下參考
部分创肥,其中有更好的多進行并發(fā)寫法,但是我看不懂值朋。 - 腳本內已經盡量給出了報錯信息叹侄,方便定位報錯位置。
腳本限制
- 沒有檢查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就是等待所有子進程全部結束才繼續(xù)。
lims=0 #重置限制,繼續(xù)循環(huán)上面的命令
fi
done
#bash ascp_for_ENA.sh <filereport_read_run_*_tsv.txt> <output path>