统计文件test.genome.fa,中每一条contig序列的长度、GC含量(GC占所有非N序列的比例)、重复序列含量(小写字母占所有非N序列的比例)
对scaffold和contig的说明:
以上这条序列即为scaffold序列。其包括两条以N为分割的contig序列。
python代码如下:
seq={}
contig_dic={}
#读入fasta文件
with open(r"test.genome_fa.txt") as f:
for line in f:
if line.startswith('>'):
name=line.replace('>','').split()[0]
seq[name]=''
else:
seq[name]+=line.replace('\n','').strip()
seq_list=list(seq.keys())
total_contig_list=[]
for i in seq_list:
scaffold=seq[i]
contig_list=[]
out_Str = ''
for i in range(0,len(scaffold)):
if scaffold[i] !='N':
out_Str+=scaffold[i]
else:
if scaffold[i+1]!='N':
contig_list.append(out_Str)
out_Str=''
total_contig_list.append(contig_list)
#对同一scaffold中的contig进行命名,并将contig存放到contig_dic内
for i in range(0,len(seq_list)):
contig_list=total_contig_list[i]
num=1
contig_num=len(contig_list)
if(num<=contig_num):
contig_dic[seq_list[i]+'_contig1']=contig_list[0]
num=2
if(num<=contig_num):
contig_dic[seq_list[i]+'_contig2']=contig_list[1]
num=3
if (num <= contig_num):
contig_dic[seq_list[i] + '_contig3'] = contig_list[2]
contig_list=list(contig_dic.keys())
contig_len={}
contig_CG={}
contig_repe={}
for i in contig_list:
contig=contig_dic[i]
contig_len[i]=len(contig)
basic_len=len(contig)
basic_CG_len=0
basic_repe_len=0
for basic in contig:
if basic == 'C' or basic == 'G':
basic_CG_len=basic_CG_len+1
if basic.islower()==1:
basic_repe_len=basic_repe_len+1
contig_CG[i]=basic_CG_len/basic_len
contig_repe[i]=basic_repe_len/basic_len
#根据contig长度进行排序
list_sort=sorted(contig_len.items(),key = lambda k : k[1])
index=[]
for i in list_sort:
index.append(i[0])
index.reverse()
#输出每一条contig的长度、CG含量、重复序列含量
with open(r'contig.statisitics.txt','w') as f:
print("contig id\tcontig length\tCG\trepetition",file=f)
for i in index:
print("%s\t%d\t%f\t%f" % (i,contig_len[i],contig_CG[i],contig_repe[i]),file=f)