本文主要是介绍用python脚本将DNA序列的.fa文件格式转换为.npy,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#.fa 文件转换为 .npy之后作为DL学习的原始数据
# from .fa gettig npy(train/valid/test)
import os
import numpy as nppath = os.getcwd()
#################get the ENHANCER proper .fa file#################
enh_list = []
f_enh = open(path+'/'+'C_10K_GM12878.csv_enhancer.fa','r') #-***- enhancer.fa -***- #
for line in f_enh.readlines():line = line.strip("/n")enh_list.append(line)
f_enh.close()
enh_list = enh_list[0:1610] #16106 - 6 is the the time of 10
def Data_Set_enh(tr_enh_num,va_enh_num,te_enh_num): #0.8/0.1/0.1enh_tr_num = tr_enh_num * len(enh_list)enh_va_num = va_enh_num * len(enh_list)enh_te_num = te_enh_num * len(enh_list)enh_tr = enh_list[0:enh_tr_num]enh_va = enh_list[enh_tr_num:enh_tr_num+enh_va_num] enh_te = enh_list[enh_tr_num+enh_va_num:]return enh_tr,enh_va,enh_te################get the PROMOTER proper .fa file################
pro_list = []
f_pro = open(path+'/'+'C_10K_GM12878.csv_promoter.fa','r') #-***- promoter.fa -***- #
for line in f_pro.readlines():line = line.strip("/n")pro_list.append(line)
f_pro.close()
pro_list = pro_list[0:1610] #16106 - 6 is the the time of 10
def Data_Set_pro(tr_pro_num,va_pro_num,te_pro_num): #0.8/0.1/0.1pro_tr_num = tr_pro_num * len(pro_list)pro_va_num = va_pro_num * len(pro_list)pro_te_num = te_pro_num * len(pro_list)pro_tr = enh_list[0:pro_tr_num]pro_va = enh_list[pro_tr_num:pro_tr_num+pro_va_num] pro_te = enh_list[pro_tr_num+pro_va_num:]return pro_tr,pro_va,pro_te##########-*- get the Neg_example proper .fa file -*-##########
neg_list = []
f_neg = open(path+'/'+'C_GM12878_neg_exampl.fa','r') # -***- Neg.fa -***- #
for line in f_neg.readlines():line = line.strip("/n")neg_list.append(line)
f_neg.close()enh_neg_list = neg_list[0:1610] #16106 - 6 is the the time of 10
def Data_Set_enh_neg(tr_enh_neg_num,va_enh_neg_num,te_enh_neg_num): #0.8/0.1/0.1enh_neg_tr_num = tr_enh_neg_num * len(enh_neg_list)enh_neg_va_num = va_enh_neg_num * len(enh_neg_list)enh_neg_te_num = te_enh_neg_num * len(enh_neg_list)enh_neg_tr = enh_neg_list[0:enh_neg_tr_num]enh_neg_va = enh_neg_list[enh_neg_tr_num:enh_neg_tr_num+enh_neg_va_num] enh_neg_te = enh_neg_list[enh_neg_tr_num+enh_neg_va_num:]return enh_neg_tr,enh_neg_va,enh_neg_tepro_neg_list = neg_list[1610:1610*2] #(16106 --2 *16100)
def Data_Set_pro_neg(tr_pro_neg_num,va_pro_neg_num,te_pro_neg_num): #0.8/0.1/0.1pro_neg_tr_num = tr_pro_neg_num * len(pro_neg_list)pro_neg_va_num = va_pro_neg_num * len(pro_neg_list)pro_neg_te_num = te_pro_neg_num * len(pro_neg_list)pro_neg_tr = pro_neg_list[0:pro_neg_tr_num]pro_neg_va = pro_neg_list[pro_neg_tr_num:pro_neg_tr_num+pro_neg_va_num] pro_neg_te = pro_neg_list[pro_neg_tr_num+pro_neg_va_num:]return pro_neg_tr,pro_neg_va,pro_neg_te###########################################################################
enh_tr,enh_va,enh_te = Data_Set_enh(0.8,0.1,0.1)
pro_tr,pro_va,pro_te = Data_Set_pro(0.8,0.1,0.1)
enh_neg_tr,enh_neg_va,enh_neg_te = Data_Set_enh_neg(0.8,0.1,0.1)
pro_neg_tr,pro_neg_va,pro_neg_te = Data_Set_pro_neg(0.8,0.1,0.1)enh_tr.extend(enh_neg_tr)
enh_va.extend(enh_neg_va)
enh_te.extend(enh_neg_te)pro_tr.extend(pro_neg_tr)
pro_va.extend(pro_neg_va)
pro_te.extend(pro_neg_te)Enh_train,Enh_valid,Enh_test = enh_tr,enh_va,enh_te
Pro_train,Pro_valid,Pro_test = pro_tr,pro_va,pro_tedef get_seq(x,empty_list): # x is equal the list (Enh_train,Enh_valid,Enh_test...)empty_list = []for i in range(len(x)//2):empty_list.append(x[2*i+1]) return empty_list# get all the type of list in seq (positive + negative examples)
Enh_train_seq = get_seq(Enh_train,Enh_train_seq)
Enh_valid_seq = get_seq(Enh_valid,Enh_valid_seq)
Enh_test_seq = get_seq(Enh_test,Enh_test_seq)
Pro_train_seq = get_seq(Pro_train,Pro_train_seq)
Pro_valid_seq = get_seq(Pro_valid,Pro_valid_seq)
Pro_test_seq = get_seq(Pro_test,Pro_test_seq)#-*-# get the shuffle of input_seq and their label ("same shuffle") #-*-#
def Data_npy_label(input_seq_1,inupt_seq_2): # seq_1 must match with seq_2!!!!!a_1,a_2 = input_seq_1,input_seq_2N_1 = np.zeros((len(a_1),len(a_1[0]),4,1),dtype=np.float32)N_2 = np.zeros((len(a_2),len(a_2[0]),4,1),dtype=np.float32)for j in range(len(a_1)):for i in range(len(a_1[0])):if a_1[j][i] == 'A':N_1[j][i][0][:] = 1elif a_1[j][i] =='T':N_1[j][i][1][:] = 1elif a_1[j][i] == 'G':N_1[j][i][2][:] = 1elif a_1[j][i] == 'C':N_1[j][i][3][:] = 1for k in range(len(a_2)):for t in range(len(a_2[0])):if a_2[k][t] == 'A':N_2[k][t][0][:] = 1elif a_2[k][t] =='T':N_2[k][t][1][:] = 1elif a_2[k][t] == 'G':N_2[k][t][2][:] = 1elif a_2[k][t] == 'C':N_2[k][t][3][:] = 1label_1 = [1]*(len(a_1)//2) + [0]*(len(a_1)//2)label_array_1 = np.array(label_1)label_2 = [1]*(len(a_2)//2) + [0]*(len(a_2)//2)label_array_2 = np.array(label_2)shuffle_index = np.arange(len(a_1)) # It is easy to know len(a_1) = len(a_2)np.random.shuffle(shuffle_index) #shuffle it !!!N_1 = N_1[shuffle_index]label_array_1 = label_array_1[shuffle_index]N_2 = N_2[shuffle_index]label_array_2 = label_array_2[shuffle_index]return N_1,label_array_1,N_2,label_array_2 Enh_train_npy,Enh_train_label,Pro_train_npy,Pro_train_label = Data_npy_label(Enh_train_seq,Pro_train_seq)
Enh_valid_npy,Enh_valid_label,Pro_valid_npy,Pro_valid_label = Data_npy_label(Enh_valid_seq,Pro_valid_seq)
Enh_test_npy,Enh_test_label,Pro_test_npy,Pro_test_label = Data_npy_label(Enh_test_seq,Pro_valid_seq)np.save('Enh_train.npy',Enh_train_npy)
np.save('Enh_train_label.npy',Enh_train_label)
np.save('Pro_train.npy',Pro_train_npy)
np.save('Pro_train_label.npy',Pro_train_label)np.save('Enh_valid.npy',Enh_valid_npy)
np.save('Enh_valid_label.npy',Enh_valid_label)
np.save('Pro_valid.npy',Pro_valid_npy)
np.save('Pro_valid_label.npy',Pro_valid_label)np.save('Enh_test.npy',Enh_test_npy)
np.save('Pro_test_label.npy',Enh_test_label)
np.save('Enh_test_label.npy',Pro_test_npy)
np.save('Pro_test_label.npy',Pro_test_label)
这篇关于用python脚本将DNA序列的.fa文件格式转换为.npy的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!