python中输出fasta文件中碱基的互补序列
001、
root@PC1:/home/test# cat test.py ## 测试程序 #!/usr/bin/python in_file = open("a.fasta", "r") dict1 = {} for i in in_file: i = i.strip() if i.startswith(">"): key = i dict1[key] = [] else: temp = i.replace("A","t").replace("T","a").replace("C","g").replace("G","c").upper() dict1[key].append(temp) for i,j in dict1.items(): print(i) for k in j: print(k) in_file.close() root@PC1:/home/test# cat a.fasta ## 测试fasta文件 >gene1 myc AGCTGCCTAAGC GGCATAGCTAATCG >gene2 jun ACCGAATCGGAGCGATG GGCATTAAAGATCTAGCT >gene3 malat1 AGGCTAGCGAG GCGCGAG GATTAGGCG root@PC1:/home/test# python test.py ## 执行程序 >gene1 myc TCGACGGATTCG CCGTATCGATTAGC >gene2 jun TGGCTTAGCCTCGCTAC CCGTAATTTCTAGATCGA >gene3 malat1 TCCGATCGCTC CGCGCTC CTAATCCGC
002、同时指定输出每行的碱基数目
root@PC1:/home/test# ls a.fasta test.py root@PC1:/home/test# cat test.py ## 测试程序 #!/usr/bin/python in_file = open("a.fasta", "r") dict1 = {} for i in in_file: i = i.strip() if i.startswith(">"): key = i dict1[key] = [] else: temp = i.replace("A","t").replace("T","a").replace("C","g").replace("G","c").upper() dict1[key].append(temp) for i,j in dict1.items(): print(i) j = "".join(j) for k in range(0, len(j), 10): print(j[k:k+10]) in_file.close() root@PC1:/home/test# cat a.fasta ## 测试fasta文件 >gene1 myc AGCTGCCTAAGC GGCATAGCTAATCG >gene2 jun ACCGAATCGGAGCGATG GGCATTAAAGATCTAGCT >gene3 malat1 AGGCTAGCGAG GCGCGAG GATTAGGCG root@PC1:/home/test# python test.py ## 执行程序 >gene1 myc TCGACGGATT CGCCGTATCG ATTAGC >gene2 jun TGGCTTAGCC TCGCTACCCG TAATTTCTAG ATCGA >gene3 malat1 TCCGATCGCT CCGCGCTCCT AATCCGC