main.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. mv $indir/$dna_fasta $outdir/DNA/
  102. mv $indir/$gtf $outdir/GTF/
  103. base=`perl $SCRIPTMANAGER_LIB/PrepRef/calculate_fasta_basepair/calculate_fasta_basepair.pl -i $outdir/DNA/$dna_fasta|cut -f2 -d ":"`
  104. cat>$outdir/DNA/RefDatabase_info<<EOF
  105. Genome ${dna_fasta}
  106. Genebuild by ${url}
  107. Database version ${version}
  108. Base Pairs ${base}
  109. EOF
  110. # check chr_list 动物挑选10倍内的 其他是100倍 scaffold 和contig选前24;如果有漏的建议自己生成一下
  111. cut -f1,2 $outdir/DNA/hrds.txt|sed 's/^>//;s#/len=##' | sort -k2 -nr |\
  112. awk 'BEGIN{FS=OFS="\t"}{split($2,a,"");print $0,length(a)}' | \
  113. awk 'BEGIN{FS=OFS="\t"}{a[$3]++;if(length(a)<=2)print $1,$2,length(a)}' > $outdir/DNA/chr.tmp
  114. contig_n=`grep -Pc -i "contig|scaffold" $outdir/DNA/chr.tmp`
  115. no_contig_n=`grep -Pvc -i "contig|scaffold" $outdir/DNA/chr.tmp`
  116. if [ $contig_n -eq 0 ]
  117. then
  118. if [ $kindom == "animal" ]
  119. then
  120. awk 'BEGIN{FS=OFS="\t"}$3<=2{print $1}' $outdir/DNA/chr.tmp |sort -V > $outdir/DNA/chr_list
  121. else
  122. awk 'BEGIN{FS=OFS="\t"}$3==1{print $1}' $outdir/DNA/chr.tmp |sort -V > $outdir/DNA/chr_list
  123. fi
  124. else
  125. head -24 $outdir/DNA/chr.tmp |cut -f1 > $outdir/DNA/chr_list
  126. fi
  127. perl $SCRIPTMANAGER_LIB/PrepRef/create_cytoband/create_cytoband.pl -i $outdir/DNA/hrds.txt -c $outdir/DNA/chr_list -o $outdir/DNA/cytoband.txt
  128. # GTF 相关文件
  129. /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
  130. perl $SCRIPTMANAGER_LIB/PrepRef/gtf2bed/gtf2bed.pl $outdir/GTF/$gtf > $outdir/GTF/GTF.bed
  131. python $SCRIPTMANAGER_LIB/DE/exon_DEXSeq/dexseq_prepare_annotation.py $outdir/GTF/$gtf $outdir/GTF/DEXSeq.gff
  132. # lncRNA 这个脚本要优化一下 (暂未处理ncbi里的转录本没有加上gene行)
  133. perl $script_path/abstract_lncRNA_gtf.pl -i $outdir/GTF/$gtf -o $outdir/GTF/lncRNA.gtf
  134. if [ ! -s "$outdir/GTF/lncRNA.gtf" ]
  135. then
  136. touch $outdir/GTF/lncRNA.gtf
  137. fi
  138. gtfToGenePred -genePredExt $outdir/GTF/$gtf $outdir/annoDB/Anno_refGene.txt
  139. perl /Business/psn_company/t01/public/software/annovar/retrieve_seq_from_fasta.pl --format refGene --seqfile \
  140. $outdir/DNA/$dna_fasta $outdir/annoDB/Anno_refGene.txt --out $outdir/annoDB/Anno_refGeneMrna.fa
  141. # miRNA
  142. mature_org=`grep -P "\t$taxid$" $database_dir/miRBase/organisms.taxid.txt|cut -f1`
  143. if [ -s "$database_dir/miRBase/mature/${mature_org}_mature.fa" ]
  144. then
  145. cp $database_dir/miRBase/mature/${mature_org}_mature.fa $outdir/mature.fa
  146. else
  147. touch $outdir/mature.fa
  148. fi
  149. # DNA 索引
  150. samtools faidx $outdir/DNA/$dna_fasta
  151. 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
  152. touch $outdir/Annotation/annotation_summary.txt $outdir/Annotation/Annotation.xls $outdir/PPI/PPI.txt
  153. cat >$outdir/refCfg.ini<<eof
  154. DNA=$outdir/DNA/$dna_fasta
  155. GTF=$outdir/GTF/$gtf
  156. GTFbed=$outdir/GTF/GTF.bed
  157. hrds=$outdir/DNA/hrds.txt
  158. RefDatabase_info=$outdir/DNA/RefDatabase_info
  159. Annotation=$outdir/Annotation/Annotation.xls
  160. Anno_summary=$outdir/Annotation/annotation_summary.txt
  161. lncRNA_gtf=$outdir/GTF/lncRNA.gtf
  162. matureMiRNA=$outdir/mature.fa
  163. annoDB=$outdir/annoDB
  164. ppi=$outdir/PPI/PPI.txt
  165. cytoBand=$outdir/DNA/cytoband.txt
  166. chr_list=$outdir/DNA/chr_list
  167. DEXSeq_gff=$outdir/GTF/DEXSeq.gff
  168. hisat2SS=$outdir/GTF/genome.ss
  169. eof
  170. echo "除了注释文件、PPI.txt和转录因子;其他文件已生成;项目着急可以先使用:$outdir/refCfg.ini"
  171. # PPI
  172. cut -f1,5 $indir/$id_table |sed '/\t-$/d' > $outdir/PPI/gid_pid
  173. if [ `grep -Pc "^$taxid$" $database_dir/PPI/type_taxid.txt` -eq 1 ]
  174. then
  175. 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 &
  176. else
  177. 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 &
  178. fi
  179. # 注释
  180. mkdir -p $outdir/Annotation/tmp
  181. if [ $allgene -eq 0 ]
  182. then
  183. gene_number=`sed "/^gene_id/d" $indir/$id_table|grep protein_coding|cut -f1 |sort -u|wc -l`
  184. else
  185. gene_number=`sed "/^gene_id/d" $indir/$id_table|cut -f1 |sort -u|wc -l`
  186. fi
  187. do_eggnog=0
  188. # GO
  189. if [ "$go_file" != "none" ]
  190. then
  191. sed '/^Gene[ _]ID/d' $indir/$go_file > $outdir/Annotation/tmp/GO
  192. else
  193. 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}'\
  194. $indir/$id_table $database_dir/NCBI/gene_go.tsv | awk -F"\t" '!a[$1]++{print}' > $outdir/Annotation/tmp/ncbi.go
  195. go_n=`cat $outdir/Annotation/tmp/ncbi.go |wc -l`
  196. rate=`awk 'BEGIN{print "'$go_n'"/"'$gene_number'"}'`
  197. if [ $(echo "$rate >= 0.1" | $script_path/bc) -eq 1 ]
  198. then
  199. cp $outdir/Annotation/tmp/ncbi.go $outdir/Annotation/tmp/GO
  200. else
  201. do_eggnog=1
  202. fi
  203. fi
  204. # eggnog
  205. if [ "$eggnog_file" != "none" ]
  206. then
  207. cut -f1,2 $indir/$eggnog_file |sed '/^Gene[ _]ID/d' > $outdir/Annotation/tmp/eggNOG
  208. cut -f1,3 $indir/$eggnog_file |sed '/^Gene[ _]ID/d' > $outdir/Annotation/tmp/eggNOG_Category
  209. else
  210. 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}'\
  211. $indir/$id_table $database_dir/eggNOG/Ensembl_EggNOG.v5.0 |awk -F"\t" '!a[$1]++{print}' > $outdir/Annotation/tmp/ensembl.eggnog
  212. egg_n=`cat $outdir/Annotation/tmp/ensembl.eggnog|wc -l`
  213. rate=`awk 'BEGIN{print "'$egg_n'"/"'$gene_number'"}'`
  214. if [ `echo "$rate >= 0.1" | $script_path/bc ` -eq 1 ]
  215. then
  216. cut -f1,2 $outdir/Annotation/tmp/ensembl.eggnog |sed '/^Gene[ _]ID/d' > $outdir/Annotation/tmp/eggNOG
  217. cut -f1,3 $outdir/Annotation/tmp/ensembl.eggnog |sed '/^Gene[ _]ID/d' > $outdir/Annotation/tmp/eggNOG_Category
  218. else
  219. do_eggnog=1
  220. fi
  221. fi
  222. if [ $do_eggnog -eq 1 ]
  223. then
  224. 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 &
  225. fi
  226. # blastuniprot
  227. if [ -s "$indir/$uniprot_fasta" ] && [ -s "$indir/$uniprot_tsv" ]
  228. then
  229. $script_path/blast_uniprot.sh -i $fasta -t $fasta_type -d $indir/$uniprot_fasta -g $indir/$uniprot_tsv -o $outdir/Annotation/blast/
  230. fi
  231. # KEGG
  232. if [ "$kegg_file" == "none" ]
  233. then
  234. 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 &
  235. else
  236. sed '/^Gene[ _]ID/d' $indir/$kegg_file > $outdir/Annotation/tmp/KEGG
  237. fi
  238. # NR
  239. if [ "$nr_file" == "none" ]
  240. then
  241. if [ "$taxid" == "none" ]
  242. then
  243. 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 &
  244. else
  245. 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 &
  246. fi
  247. else
  248. sed '/^Gene[ _]ID/d' $indir/$nr_file > $outdir/Annotation/tmp/NR
  249. fi
  250. # Swissprot
  251. if [ "$swissprot_file" == "none" ]
  252. then
  253. 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 &
  254. else
  255. sed '/^Gene[ _]ID/d' $indir/$swissprot_file > $outdir/Annotation/tmp/Swissprot
  256. fi
  257. wait
  258. # 拷贝到 $outdir/Annotation/tmp/ 下合并注释
  259. for anno in GO KEGG eggNOG eggNOG_Category Swissprot NR
  260. do
  261. if [ ! -s "$outdir/Annotation/tmp/$anno" ]
  262. then
  263. if [ $anno == "GO" ]
  264. then
  265. if [ -f "$outdir/Annotation/blast/uniprot.go" ]
  266. then
  267. up_n=`cat $outdir/Annotation/blast/uniprot.go |wc -l`
  268. egg_go_n=`cat $outdir/Annotation/blast/GO.txt |wc -l`
  269. if [ $up_n -gt $egg_go_n ]
  270. then
  271. cp $outdir/Annotation/blast/uniprot.go $outdir/Annotation/tmp/$anno
  272. else
  273. cp $outdir/Annotation/blast/$anno.txt $outdir/Annotation/tmp/$anno
  274. fi
  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. fi
  282. done
  283. # Pathway
  284. if [ -s "$database_dir/KEGG/pathway/$org" ]
  285. then
  286. $script_path/Pathway.sh $outdir/Annotation/tmp/KEGG $org $outdir/Annotation/tmp/Pathway
  287. else
  288. $script_path/Pathway.sh $outdir/Annotation/tmp/KEGG $kindom $outdir/Annotation/tmp/Pathway
  289. fi
  290. # BP CC MF
  291. for i in BP CC MF
  292. do
  293. grep -P "\t$i$" $database_dir/GO/GO_description_total.txt |\
  294. 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}' \
  295. - $outdir/Annotation/tmp/GO | grep "GO:" |sed 's/\t;/\t/' > $outdir/Annotation/tmp/$i
  296. done
  297. # SwissprotName
  298. python3 $script_path/uniprot_info.py -s $outdir/Annotation/tmp/Swissprot -o $outdir/Annotation/tmp/
  299. # term2gene
  300. /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
  301. # 是否加entryid
  302. entry_id=""
  303. if [ `cut -f4 $indir/$id_table |grep -Pcv "\t-$" ` -gt 100 ]
  304. then
  305. cut -f1,4 $indir/$id_table|sed "/^gene_id/d" |sort -u > $outdir/Annotation/tmp/Entrez_geneID
  306. entry_id="Entrez_geneID"
  307. fi
  308. # merge Annotation
  309. if [ $allgene -eq 1 ]
  310. then
  311. allgene="-a"
  312. else
  313. allgene=""
  314. fi
  315. cd $outdir/Annotation/tmp/
  316. 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
  317. cd -
  318. # stat annotation
  319. perl $SCRIPTMANAGER_LIB//PrepRef/stat_Annotation/stat_Annotation.pl -i $outdir/Annotation/Annotation.xls -o $outdir/Annotation/annotation_summary.txt
  320. sed -i 's/Ensembl/ALL/' $outdir/Annotation//annotation_summary.txt
  321. sed -i 's/#/__/g' $outdir/Annotation/Annotation.xls
  322. # TF
  323. if [ "$kindom" == "fungi" ]
  324. then
  325. touch $outdir/09_TFs/TF.detail.xls
  326. else
  327. tf_species=`grep -P "\t$taxid$" $database_dir/TF/species.taxid.txt |cut -f1`
  328. if [ -n "$tf_species" ]
  329. then
  330. $script_path/TF.sh -i $fasta -o $outdir/09_TFs -t $fasta_type -s $tf_species -a $outdir/Annotation/Annotation.xls
  331. else
  332. $script_path/TF.sh -i $fasta -o $outdir/09_TFs -t $fasta_type -s $kindom -a $outdir/Annotation/Annotation.xls
  333. fi
  334. fi