001、
root@PC1:/home/test# ls a.fasta target.txt test.py root@PC1:/home/test# cat test.py ## 测试程序 #!/usr/bin/python in_file = open("a.fasta", "r") target = open("target.txt", "r") lines = target.readlines() dict1 = dict() for i in in_file: i = i.strip() if i[0] == ">": key = i dict1[key] = [] else: dict1[key].append(i) for i in dict1.keys(): if (i + "\n") in lines: print(i) for j in dict1[i]: print(j) in_file.close() target.close() root@PC1:/home/test# cat a.fasta ## 测试fasta文件 >gene2 myc AGCTGCCTAAGCMMMM GGCATAGCTAATCG >gene3 jun ACCGAATCGGAGCGATGEEE GGCATTAAAGATCTAGCT >gene1 malat1 AGGCTAGCGAG GCGCGAGKKK GATTAGGCG >gene4 jun ACCGAATCGGAGCGATGYY GGCATTAAAGATCTAGCTXXX root@PC1:/home/test# cat target.txt ## 目的scaffold >gene3 jun >gene1 malat1 root@PC1:/home/test# python test.py ## 执行程序 >gene3 jun ACCGAATCGGAGCGATGEEE GGCATTAAAGATCTAGCT >gene1 malat1 AGGCTAGCGAG GCGCGAGKKK GATTAGGCG
参考:https://www.jianshu.com/p/5ee54bea4cb0