main.test.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #!/bin/bash
  2. # set -ex
  3. # 脚本里需要耗cpu 内存的操作;均使用了bsub ;投递本流程 推荐 -n 2 -M 4G
  4. script_path=$(dirname `realpath $0`)
  5. trap "kill 0;exit 1" 2
  6. # 默认变量
  7. outdir="."
  8. # 默认Annotation 只包含 protein_coding 基因
  9. allgene=0
  10. # 路径
  11. database_dir=/Business/psn_company/Work/Transcriptome/Datum/Public/Database/genome_prepare
  12. # 投递最大cpu和内存参数
  13. cpu=6
  14. memory=50G
  15. # 解析命令行参数
  16. # getopt
  17. usage() {
  18. echo "Usage: $0 [-h|--help] [-i|--indir <indir>] [-o|--outdir <outdir>] [-c|--config <config>] [-n|--cpu <cpu>] [-M|--memory <memory>] [-v|--version] [-a|--allgene]"
  19. echo "Options:"
  20. echo " -h, --help Show this help message and exit."
  21. echo " -i, --indir Input directory containing the files that in config file"
  22. echo " -o, --outdir Output directory"
  23. echo " -c, --config Config file for the pipeline."
  24. echo " -n, --cpu Number of CPUs in bsub;default: 6"
  25. echo " -M, --memory Memory limit in bsub;default: 50G"
  26. echo " -v, --version Show the version of the pipeline and exit."
  27. echo " -a, --allgene Include all genes, not just protein-coding genes."
  28. exit 1
  29. }
  30. ARGS=`getopt -a -o hi:o:c:n:M:va -l help,indir:,outdir:,config:,cpu:,memory:,version,allgene -n $0 -- "$@"`
  31. [ $? != 0 ] && usage
  32. eval set -- "${ARGS}"
  33. while true
  34. do
  35. case "$1" in
  36. -h|--help)
  37. usage
  38. shift
  39. ;;
  40. -i|--indir)
  41. indir=$2
  42. shift 2
  43. ;;
  44. -o|--outdir)
  45. outdir=$2
  46. shift 2
  47. ;;
  48. -c|--config)
  49. config_file=$2
  50. shift 2
  51. ;;
  52. -n|--cpu)
  53. cpu=$2
  54. shift 2
  55. ;;
  56. -M|--memory)
  57. memory=$2
  58. shift 2
  59. ;;
  60. -v|--version)
  61. echo "genome prepare version 1.0"
  62. echo "Author: Hu Dabang"
  63. echo "Email: hudb@personalbio.cn"
  64. echo "run $0 -h for help"
  65. exit 1
  66. ;;
  67. -a|--allgene)
  68. allgene=1
  69. shift
  70. ;;
  71. --)
  72. shift
  73. break
  74. ;;
  75. esac
  76. done
  77. if [ ! -d "$indir" ] || [ ! -s "$config_file" ]
  78. then
  79. echo "ERROR: The input directory or config file does not exist."
  80. usage
  81. fi
  82. indir=`realpath $indir`
  83. config_file=`realpath $config_file`
  84. source $config_file
  85. # check file
  86. for f in `grep -P "_file=|fasta=|gtf=|table=" $config_file|sed '/=none$/d'|cut -f1 -d "="`
  87. do
  88. [ ! -s "$indir/${!f}" ] && echo "ERROR: The input file $indir/${!f} does not exist." && exit 1
  89. done
  90. mkdir -p $outdir/{DNA,GTF,Annotation,PPI,09_TFs,annoDB,LOG}
  91. outdir=`realpath $outdir`
  92. # check long chr
  93. perl $SCRIPTMANAGER_LIB/PrepRef/create_hrds/create_hrds.pl -i $indir/$dna_fasta -o $outdir/DNA/hrds.txt -org $latin_name
  94. long_chr=`cut -f1,2 $outdir/DNA/hrds.txt|sed 's/^>//;s#/len=##'|awk -F"\t" '$2>=536870912{print $1}'|tr "\n" ";"`
  95. if [ -n "$long_chr" ]
  96. then
  97. echo -e "WARNING: The following chromosomes have length larger than 536870912, \nPlease check the length of the following chromosomes:"
  98. echo "$long_chr"
  99. exit 1
  100. fi
  101. # 多行注释
  102. <<END
  103. mv $indir/$dna_fasta $outdir/DNA/
  104. mv $indir/$gtf $outdir/GTF/
  105. base=`perl $SCRIPTMANAGER_LIB/PrepRef/calculate_fasta_basepair/calculate_fasta_basepair.pl -i $outdir/DNA/$dna_fasta|cut -f2 -d ":"`
  106. cat>$outdir/DNA/RefDatabase_info<<eof
  107. Genome\t${dna_fasta}
  108. Genebuild by\t${url}
  109. Database version\t${version}
  110. Base Pairs\t${base}
  111. eof
  112. # check chr_list 动物挑选10倍内的 其他是100倍 scaffold 和contig选前24;如果有漏的建议自己生成一下
  113. cut -f1,2 $outdir/DNA/hrds.txt|sed 's/^>//;s#/len=##' | sort -k2 -nr |\
  114. awk 'BEGIN{FS=OFS="\t"}{split($2,a,"");print $0,length(a)}' | \
  115. awk 'BEGIN{FS=OFS="\t"}{a[$3]++;if(length(a)<=2)print $1,$2,length(a)}' > $outdir/DNA/chr.tmp
  116. contig_n=`grep -Pc -i "contig|scaffold" $outdir/DNA/chr.tmp`
  117. no_contig_n=`grep -Pvc -i "contig|scaffold" $outdir/DNA/chr.tmp`
  118. if [ $contig_n -eq 0 ]
  119. then
  120. if [ $kindom == "animal" ]
  121. then
  122. awk 'BEGIN{FS=OFS="\t"}$3<=2{print $1}' $outdir/DNA/chr.tmp |sort -V > $outdir/DNA/chr_list
  123. else
  124. awk 'BEGIN{FS=OFS="\t"}$3==1{print $1}' $outdir/DNA/chr.tmp |sort -V > $outdir/DNA/chr_list
  125. fi
  126. else
  127. head -24 $outdir/DNA/chr.tmp |cut -f1 > $outdir/DNA/chr_list
  128. fi
  129. perl $SCRIPTMANAGER_LIB/PrepRef/create_cytoband/create_cytoband.pl -i $outdir/DNA/hrds.txt -c $outdir/DNA/chr_list -o $outdir/DNA/cytoband.txt
  130. # GTF 相关文件
  131. /Business/psn_company/t01/software/source_pkg/YZ_data_software/hisat2-2.1.0/extract_splice_sites.py $outdir/GTF/$gtf > $outdir/GTF/genome.ss
  132. perl $SCRIPTMANAGER_LIB/PrepRef/gtf2bed/gtf2bed.pl $outdir/GTF/$gtf > $outdir/GTF/GTF.bed
  133. python $SCRIPTMANAGER_LIB/DE/exon_DEXSeq/dexseq_prepare_annotation.py $outdir/GTF/$gtf $outdir/GTF/DEXSeq.gff
  134. # lncRNA 这个脚本要优化一下 (暂未处理ncbi里的转录本没有加上gene行)
  135. perl $script_path/abstract_lncRNA_gtf.pl -i $outdir/GTF/$gtf -o $outdir/GTF/lncRNA.gtf
  136. if [ ! -s "$outdir/GTF/lncRNA.gtf" ]
  137. then
  138. touch $outdir/GTF/lncRNA.gtf
  139. fi
  140. gtfToGenePred -genePredExt $outdir/GTF/$gtf $outdir/annoDB/Anno_refGene.txt
  141. perl /Business/psn_company/t01/public/software/annovar/retrieve_seq_from_fasta.pl --format refGene --seqfile \
  142. $outdir/DNA/$dna_fasta $outdir/annoDB/Anno_refGene.txt --out $outdir/annoDB/Anno_refGeneMrna.fa
  143. END
  144. # miRNA
  145. mature_org=`grep -P "\t$taxid$" $database_dir/miRBase/organisms.taxid.txt|cut -f1`
  146. if [ -s "$database_dir/miRBase/mature/${mature_org}_mature.fa" ]
  147. then
  148. cp $database_dir/miRBase/mature/${mature_org}_mature.fa $outdir/mature.fa
  149. else
  150. touch $outdir/mature.fa
  151. fi
  152. # DNA 索引
  153. #samtools faidx $outdir/DNA/$dna_fasta
  154. #bsub -q psnpublic -o $outdir/LOG/hisat2.%J.o -e $outdir/LOG/hisat2.%J.e -n $cpu -M $memory -I hisat2-build $outdir/DNA/$dna_fasta $outdir/DNA/$dna_fasta
  155. #touch $outdir/Annotation/annotation_summary.txt $outdir/Annotation/Annotation.xls $outdir/PPI/PPI.txt
  156. cat >$outdir/refCfg.ini<<eof
  157. DNA=$outdir/DNA/$dna_fasta
  158. GTF=$outdir/GTF/$gtf
  159. GTFbed=$outdir/GTF/GTF.bed
  160. hrds=$outdir/DNA/hrds.txt
  161. RefDatabase_info=$outdir/DNA/RefDatabase_info
  162. Annotation=$outdir/Annotation/Annotation.xls
  163. Anno_summary=$outdir/Annotation/annotation_summary.txt
  164. lncRNA_gtf=$outdir/GTF/lncRNA.gtf
  165. matureMiRNA=$outdir/mature.fa
  166. annoDB=$outdir/annoDB
  167. ppi=$outdir/PPI/PPI.txt
  168. cytoBand=$outdir/DNA/cytoband.txt
  169. chr_list=$outdir/DNA/chr_list
  170. DEXSeq_gff=$outdir/GTF/DEXSeq.gff
  171. hisat2SS=$outdir/GTF/genome.ss
  172. eof
  173. echo "除了注释文件、PPI.txt和转录因子;其他文件已生成;项目着急可以先使用:$outdir/refCfg.ini"
  174. # PPI
  175. cut -f1,5 $indir/$id_table |sed '/\t-$/d' > $outdir/PPI/gid_pid
  176. if [ `grep -Pc "^$taxid$" $database_dir/PPI/type_taxid.txt` -eq 1 ]
  177. then
  178. bsub -q psnpublic -n $cpu -M $memory -o $outdir/LOG/ppi.%J.o -e $outdir/LOG/ppi.%J.e -I $script_path/PPI.sh -i $fasta -t $fasta_type -s $taxid -o $outdir/PPI/ -m $outdir/PPI/gid_pid &
  179. else
  180. bsub -q psnpublic -n $cpu -M $memory -o $outdir/LOG/ppi.%J.o -e $outdir/LOG/ppi.%J.e -I $script_path/PPI.sh -i $fasta -t $fasta_type -s $kindom -o $outdir/PPI/ -m $outdir/PPI/gid_pid &
  181. fi
  182. # 注释
  183. mkdir -p $outdir/Annotation/tmp
  184. if [ $allgene -eq 0 ]
  185. then
  186. gene_number=`sed "/^gene_id/d" $indir/$id_table|grep protein_coding|cut -f1 |sort -u|wc -l`
  187. else
  188. gene_number=`sed "/^gene_id/d" $indir/$id_table|cut -f1 |sort -u|wc -l`
  189. fi
  190. do_eggnog=0
  191. # GO
  192. if [ "$go_file" != "none" ]
  193. then
  194. sed '/^Gene[ _]ID/d' $indir/$go_file > $outdir/Annotation/tmp/GO
  195. else
  196. awk 'BEGIN{FS=OFS="\t"}FNR==NR{for(i=1;i<=6;i++){a[$i]=$1}}FNR!=NR{if(a[$1])print a[$1],$2}'\
  197. $indir/$id_table $database_dir/NCBI/gene_go.tsv | awk -F"\t" '!a[$1]++{print}' > $outdir/Annotation/tmp/ncbi.go
  198. go_n=`cat $outdir/Annotation/tmp/ncbi.go |wc -l`
  199. rate=`awk 'BEGIN{print "'$go_n'"/"'$gene_number'"}'`
  200. if [ $(echo "$rate >= 0.1" | $script_path/bc) -eq 1 ]
  201. then
  202. cp $outdir/Annotation/tmp/ncbi.go $outdir/Annotation/tmp/GO
  203. else
  204. do_eggnog=1
  205. fi
  206. fi
  207. # eggnog
  208. if [ "$eggnog_file" != "none" ]
  209. then
  210. cut -f1,2 $indir/$eggnog_file |sed '/^Gene[ _]ID/d' > $outdir/Annotation/tmp/eggNOG
  211. cut -f1,3 $indir/$eggnog_file |sed '/^Gene[ _]ID/d' > $outdir/Annotation/tmp/eggNOG_Category
  212. else
  213. awk 'BEGIN{FS=OFS="\t"}FNR==NR{for(i=1;i<=6;i++){a[$i]=$1}}FNR!=NR{if(a[$1])print a[$1],$2,$3}'\
  214. $indir/$id_table $database_dir/eggNOG/Ensembl_EggNOG.v5.0 |awk -F"\t" '!a[$1]++{print}' > $outdir/Annotation/tmp/ensembl.eggnog
  215. egg_n=`cat $outdir/Annotation/tmp/ensembl.eggnog|wc -l`
  216. rate=`awk 'BEGIN{print "'$egg_n'"/"'$gene_number'"}'`
  217. if [ `echo "$rate >= 0.1" | $script_path/bc ` -eq 1 ]
  218. then
  219. cut -f1,2 $outdir/Annotation/tmp/ensembl.eggnog |sed '/^Gene[ _]ID/d' > $outdir/Annotation/tmp/eggNOG
  220. cut -f1,3 $outdir/Annotation/tmp/ensembl.eggnog |sed '/^Gene[ _]ID/d' > $outdir/Annotation/tmp/eggNOG_Category
  221. else
  222. do_eggnog=1
  223. fi
  224. fi
  225. if [ $do_eggnog -eq 1 ]
  226. then
  227. bsub -q psnpublic -n $cpu -M $memory -o $outdir/LOG/eggnog_map.%J.o -e $outdir/LOG/eggnog_map.%J.e -I $script_path/eggNOG_GO.sh -i $fasta -t $fasta_type -s $kindom -o $outdir/Annotation/blast &
  228. fi
  229. # blastuniprot
  230. if [ "$uniprot_fasta" != "none" ] && [ "$uniprot_tsv" != "none" ]
  231. then
  232. $script_path/blast_uniprot.sh -i $fasta -t $fasta_type -d $indir/$uniprot_fasta -g $indir/$uniprot_tsv -o $outdir/Annotation/blast/
  233. fi
  234. # KEGG
  235. if [ "$kegg_file" == "none" ]
  236. then
  237. bsub -q psnpublic -n $cpu -M $memory -o $outdir/LOG/kegg.%J.o -e $outdir/LOG/kegg.%J.e -I $script_path/KEGG.sh -i $fasta -o $outdir/Annotation/blast -t $fasta_type -s $org -l $indir/$id_table &
  238. else
  239. sed '/^Gene[ _]ID/d' $indir/$kegg_file > $outdir/Annotation/tmp/KEGG
  240. fi
  241. # NR
  242. if [ "$nr_file" == "none" ]
  243. then
  244. if [ "$taxid" == "none" ]
  245. then
  246. bsub -q psnpublic -n $cpu -M $memory -o $outdir/LOG/nr.%J.o -e $outdir/LOG/nr.%J.e -I $script_path/NR.sh -i $fasta -o $outdir/Annotation/blast -t $fasta_type -s $kindom &
  247. else
  248. bsub -q psnpublic -n $cpu -M $memory -o $outdir/LOG/nr.%J.o -e $outdir/LOG/nr.%J.e -I $script_path/NR.sh -i $fasta -o $outdir/Annotation/blast -t $fasta_type -s $taxid &
  249. fi
  250. else
  251. sed '/^Gene[ _]ID/d' $indir/$nr_file > $outdir/Annotation/tmp/NR
  252. fi
  253. # Swissprot
  254. if [ "$swissprot_file" == "none" ]
  255. then
  256. bsub -q psnpublic -n $cpu -M $memory -o $outdir/LOG/swissprot.%J.o -e $outdir/LOG/swissprot.%J.e -I $script_path/Swissprot.sh -i $fasta -o $outdir/Annotation/blast -t $fasta_type &
  257. else
  258. sed '/^Gene[ _]ID/d' $indir/$swissprot_file > $outdir/Annotation/tmp/Swissprot
  259. fi
  260. wait
  261. # 拷贝到 $outdir/Annotation/tmp/ 下合并注释
  262. for anno in GO KEGG eggNOG eggNOG_Category Swissprot NR
  263. do
  264. if [ ! -s "$outdir/Annotation/tmp/$anno" ]
  265. then
  266. if [ $anno == "GO" ]
  267. then
  268. if [ -f "$outdir/Annotation/blast/uniprot.go" ]
  269. then
  270. up_n=`cat $outdir/Annotation/blast/uniprot.go |wc -l`
  271. egg_go_n=`cat $outdir/Annotation/blast/GO.txt |wc -l`
  272. if [ $up_n -gt $egg_go_n ]
  273. then
  274. cp $outdir/Annotation/blast/uniprot.go $outdir/Annotation/tmp/$anno
  275. else
  276. cp $outdir/Annotation/blast/$anno.txt $outdir/Annotation/tmp/$anno
  277. fi
  278. else
  279. cp $outdir/Annotation/blast/$anno.txt $outdir/Annotation/tmp/$anno
  280. fi
  281. else
  282. cp $outdir/Annotation/blast/$anno.txt $outdir/Annotation/tmp/$anno
  283. fi
  284. fi
  285. done
  286. # Pathway
  287. if [ -s "$database_dir/KEGG/pathway/$org" ]
  288. then
  289. $script_path/Pathway.sh $outdir/Annotation/tmp/KEGG $org $outdir/Annotation/tmp/Pathway
  290. else
  291. $script_path/Pathway.sh $outdir/Annotation/tmp/KEGG $kindom $outdir/Annotation/tmp/Pathway
  292. fi
  293. # BP CC MF
  294. for i in BP CC MF
  295. do
  296. grep -P "\t$i$" $database_dir/GO/GO_description_total.txt |\
  297. awk 'BEGIN{FS=OFS="\t"}FNR==NR{a[$1]=$2}FNR!=NR&&/GO:/{des="";split($2,b,";");for(i in b){if(a[b[i]])des=des";"b[i]"//"a[b[i]]};print $1,des}' \
  298. - $outdir/Annotation/tmp/GO | grep "GO:" |sed 's/\t;/\t/' > $outdir/Annotation/tmp/$i
  299. done
  300. # SwissprotName
  301. python3 $script_path/uniprot_info.py -s $outdir/Annotation/tmp/Swissprot -o $outdir/Annotation/tmp/
  302. # term2gene
  303. /Business/psn_company/Work/Transcriptome/T01/software/miniconda3/envs/R4.2/bin/Rscript $script_path/term2gene.R $outdir/Annotation/tmp/GO $outdir/Annotation/term2gene.RData
  304. # 是否加entryid
  305. entry_id=""
  306. if [ `cut -f4 $indir/$id_table |grep -Pcv "\t-$" ` -gt 100 ]
  307. then
  308. cut -f1,4 $indir/$id_table|sed "/^gene_id/d" |sort -u > $outdir/Annotation/tmp/Entrez_geneID
  309. entry_id="Entrez_geneID"
  310. fi
  311. # merge Annotation
  312. if [ $allgene -eq 1 ]
  313. then
  314. allgene="-a"
  315. else
  316. allgene=""
  317. fi
  318. cd $outdir/Annotation/tmp/
  319. perl $script_path/merge_anno.v1.pl -g $outdir/GTF/$gtf $allgene -o $outdir/Annotation/Annotation.xls GO KEGG eggNOG eggNOG_Category Swissprot NR SwissprotName $entry_id BP CC MF Pathway
  320. cd -
  321. # stat annotation
  322. perl $SCRIPTMANAGER_LIB//PrepRef/stat_Annotation/stat_Annotation.pl -i $outdir/Annotation/Annotation.xls -o $outdir/Annotation/annotation_summary.txt
  323. sed -i 's/Ensembl/ALL/' $outdir/Annotation//annotation_summary.txt
  324. sed -i 's/#/__/g' $outdir/Annotation/Annotation.xls
  325. # TF
  326. if [ "$kindom" == "fungi" ]
  327. then
  328. touch $outdir/09_TFs/TF.detail.xls
  329. else
  330. tf_species=`grep -P "\t$taxid$" $database_dir/TF/species.taxid.txt |cut -f1`
  331. if [ -n "$tf_species" ]
  332. then
  333. $script_path/TF.sh -i $fasta -o $outdir/09_TFs -t $fasta_type -s $tf_species -a $outdir/Annotation/Annotation.xls
  334. else
  335. $script_path/TF.sh -i $fasta -o $outdir/09_TFs -t $fasta_type -s $kindom -a $outdir/Annotation/Annotation.xls
  336. fi
  337. fi