python學習——讀取染色體長度(七:for循環對染色體序列進行反向互補)
阿新 • • 發佈:2019-05-01
導入模塊 int 終端 染色體 文件名 循環 open sys.argv pan
對fasta文件genome_test.fa中的染色體序列進行反向互補,並輸出到文件genome_test_RC.fa
genome_test.fa
>chr1
ATATATATAT
>chr2
ATATATATATCGCGCGCGCG
>chr3
ATATATATATCGCGCGCGCGATATATATAT
>chr4
ATATATATATCGCGCGCGCGATATATATATCGCGCGCGCG
>chr5
ATATATATATCGCGCGCGCGATATATATATCGCGCGCGCGATATATATAT
新疆Reverse_Complement.py文件,並輸入如下python腳本
python腳本
1 # import os # 導入模塊os 2 import sys # 導入模塊sys 3 f_fasta = sys.argv[1] # 從命令行獲取文件名 4 f = open(f_fasta) # 打開文件 5 f_RC = open("genome_test_RC.fa","w+") 6 # 逐行讀取 7 lines = f.readlines() 8 for line in lines: 9 line = line.strip() # 去掉行尾的換行符 10 if (line.startswith(">")): 11 chr_id = line + ‘_RC‘ 12 else: 13 chr_seq = line[::-1].replace(‘A‘,‘t‘).replace(‘T‘,‘a‘).replace(‘C‘,‘g‘).replace(‘G‘,‘c‘).upper() 14 # 輸出結果 15 print(chr_id) 16 print(chr_seq) 17 18 f_RC.write(chr_id + ‘\n‘) 19 f_RC.write(chr_seq + ‘\n‘) 20 f.close() 21 f_RC.close()
從cmd終端命令行輸入參數,調用上述python腳本,並對genome_test.fa進行處理
1 E:\15_python\DEBUG>python Reverse_Complement.py genome_test.fa
結果
genome_test_RC.fa
>chr1_RC
ATATATATAT
>chr2_RC
CGCGCGCGCGATATATATAT
>chr3_RC
ATATATATATCGCGCGCGCGATATATATAT
>chr4_RC
CGCGCGCGCGATATATATATCGCGCGCGCGATATATATAT
>chr5_RC
ATATATATATCGCGCGCGCGATATATATATCGCGCGCGCGATATATATAT
python學習——讀取染色體長度(七:for循環對染色體序列進行反向互補)