linux shell中将fasta文件按照每行指定碱基数输出
2022/2/3 7:12:27
本文主要是介绍linux shell中将fasta文件按照每行指定碱基数输出,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、测试数据
root@PC1:/home/test# ls record.txt test.fa root@PC1:/home/test# cat test.fa >OR4F29_ENSG00000284733_ENST00000426406_20_955_995 AGCCCAGTTGGCTGGACCAATGGAT GGAGAGAATCACTCAGTGGTATCTGAG TTTTTGTTTCTGGGACTC >OR4F16_ENSG00000284662_ENST00000332831_20_955_995 AGCCCAGTTGGCTGGACCAATGGATGGAG AGAATCACTCAGTGGTATCTGAGTTTTTGTTTCTGGGACTCAC >OR4F29_ENSG00000284733_ENST00000426406_20_955_995 AGCCCAGTTGGCTGGA CCAATGGATGGAGAGAATCACTCAGTGGTATCTGAGTTTTT GTTTCTGGGACTCACT >OR4F16_ENSG00000284662_ENST00000332831_20_955_995 AGCCCAGTTGGCTGGACCAATGGATGGAGAGA ATCACTCAGTGGTATCTGAGTTTTTGTTTCTGG >OR4F29_ENSG00000284733_ENST00000426406_20_955_995 AGCCCAGTTGGCTGGA CCAATGGATGGAGAGAATCACTCAGTGGTATCTGAGTTTTT GTTTCTGGGACTCACT
2、脚本
root@PC1:/home/test# ls record.txt test.fa root@PC1:/home/test# cat record.txt #step1 grep -n "^>" test.fa | cut -d ":" -f 1 | paste -d " " -s | awk '{for(i = 1; i < NF; i++) printf("%d %d ", $i+1,$(i+1)-1); printf("\n")}' | awk '{for(i = 1; i <= NF; i++) if(i % 2 == 0) {print $i} else {printf("%s ", $i)}}' > topindex.txt #step2 sed -n "/^>/=" test.fa | awk 'END{print $0 + 1}' | paste - -d " " <(sed -n "$=" test.fa ) > endindex.txt #step3 此处6位指定每行多少个碱基,可以设定为其他数值 cat topindex.txt endindex.txt | while read {i,j}; do awk -v a=$i -v b=$j "NR == a, NR == b" test.fa | awk '{printf("%s", $0)} END {print}' | awk -v c=$i -F "" '{printf("tag%d",c); for(i = 1; i <= NF; i++) if(i % 6 == 0) {print $i} else {printf("%s ", $i)}; printf("\n"); idx = idx + 1} ' >> tempresult; done #step4 grep "^>" test.fa | paste - -d " " <(grep "^tag" tempresult ) | while read {i,j}; do sed -i "/$j/i $i" tempresult ; done #step5 sed 's/^tag[0-9]*//g' tempresult > result.fa rm endindex.txt topindex.txt tempresult
3、测试
root@PC1:/home/test# ls record.txt test.fa root@PC1:/home/test# bash record.txt root@PC1:/home/test# ls record.txt result.fa test.fa root@PC1:/home/test# cat result.fa ## 查看结果 >OR4F29_ENSG00000284733_ENST00000426406_20_955_995 A G C C C A G T T G G C T G G A C C A A T G G A T G G A G A G A A T C A C T C A G T G G T A T C T G A G T T T T T G T T T C T G G G A C T C T T T T T G T T T C T G G G A C T C >OR4F16_ENSG00000284662_ENST00000332831_20_955_995 A G C C C A G T T G G C T G G A C C A A T G G A T G G A G A G A A T C A C T C A G T G G T A T C T G A G T T T T T G T T T C T G G G A C T C A C A G A A T C A C T C A G T G G T A T C T G A G T T T T T G T T T C T G G G A C T C A C
4、每行20个碱基
root@PC1:/home/test# ls record.txt test.fa root@PC1:/home/test# cat record.txt #step1 grep -n "^>" test.fa | cut -d ":" -f 1 | paste -d " " -s | awk '{for(i = 1; i < NF; i++) printf("%d %d ", $i+1,$(i+1)-1); printf("\n")}' | awk '{for(i = 1; i <= NF; i++) if(i % 2 == 0) {print $i} else {printf("%s ", $i)}}' > topindex.txt #step2 sed -n "/^>/=" test.fa | awk 'END{print $0 + 1}' | paste - -d " " <(sed -n "$=" test.fa ) > endindex.txt #step3 此处改为20 cat topindex.txt endindex.txt | while read {i,j}; do awk -v a=$i -v b=$j "NR == a, NR == b" test.fa | awk '{printf("%s", $0)} END {print}' | awk -v c=$i -F "" '{printf("tag%d",c); for(i = 1; i <= NF; i++) if(i % 20 == 0) {print $i} else {printf("%s ", $i)}; printf("\n"); idx = idx + 1} ' >> tempresult; done #step4 grep "^>" test.fa | paste - -d " " <(grep "^tag" tempresult ) | while read {i,j}; do sed -i "/$j/i $i" tempresult ; done #step5 sed 's/^tag[0-9]*//g' tempresult > result.fa rm endindex.txt topindex.txt tempresult root@PC1:/home/test# bash record.txt root@PC1:/home/test# ls record.txt result.fa test.fa root@PC1:/home/test# cat result.fa ## 查看结果 >OR4F29_ENSG00000284733_ENST00000426406_20_955_995 A G C C C A G T T G G C T G G A C C A A T G G A T G G A G A G A A T C A C T C A G T G G T A T C T G A G T T T T T G T T T C T G G G A C T C T T T T T G T T T C T G G G A C T C >OR4F16_ENSG00000284662_ENST00000332831_20_955_995 A G C C C A G T T G G C T G G A C C A A T G G A T G G A G A G A A T C A C T C A G T G G T A T C T G A G T T T T T G T T T C T G G G A C T C A C A G A A T C A C T C A G T G G T A T C T G A G T T T T T G T T T C T G G G A C T C A C >OR4F29_ENSG00000284733_ENST00000426406_20_955_995 A G C C C A G T T G G C T G G A C C A A T G G A T G G A G A G A A T C A C T C A G T G G T A T C T G A G T T T T T G T T T C T G G G A C T C A C T G T T T C T G G G A C T C A C T >OR4F16_ENSG00000284662_ENST00000332831_20_955_995 A G C C C A G T T G G C T G G A C C A A T G G A T G G A G A G A A T C A C T C A G T G G T A T C T G A G T T T T T G T T T C T G G A T C A C T C A G T G G T A T C T G A G T T T T T G T T T C T G G >OR4F29_ENSG00000284733_ENST00000426406_20_955_995 A G C C C A G T T G G C T G G A C C A A T G G A T G G A G A G A A T C A C T C A G T G G T A T C T G A G T T T T T G T T T C T G G G A C T C A C T G T T T C T G G G A C T C A C T
这篇关于linux shell中将fasta文件按照每行指定碱基数输出的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-18git仓库有更新,jenkins 自动触发拉代码怎么配置的?-icode9专业技术文章分享
- 2024-12-18Jenkins webhook 方式怎么配置指定的分支?-icode9专业技术文章分享
- 2024-12-13Linux C++项目实战入门教程
- 2024-12-13Linux C++编程项目实战入门教程
- 2024-12-11Linux部署Scrapy教程:新手入门指南
- 2024-12-11怎么将在本地创建的 Maven 仓库迁移到 Linux 服务器上?-icode9专业技术文章分享
- 2024-12-10Linux常用命令
- 2024-12-06谁看谁服! Linux 创始人对于进程和线程的理解是…
- 2024-12-04操作系统教程:新手入门及初级技巧详解
- 2024-12-04操作系统入门:新手必学指南