用python脚本将DNA序列的.fa文件格式转换为.npy

2024-02-26 04:38

本文主要是介绍用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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/747733

相关文章

Python将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

微信公众号脚本-获取热搜自动新建草稿并发布文章

《微信公众号脚本-获取热搜自动新建草稿并发布文章》本来想写一个自动化发布微信公众号的小绿书的脚本,但是微信公众号官网没有小绿书的接口,那就写一个获取热搜微信普通文章的脚本吧,:本文主要介绍微信公众... 目录介绍思路前期准备环境要求获取接口token获取热搜获取热搜数据下载热搜图片给图片加上标题文字上传图片

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Python Websockets库的使用指南

《PythonWebsockets库的使用指南》pythonwebsockets库是一个用于创建WebSocket服务器和客户端的Python库,它提供了一种简单的方式来实现实时通信,支持异步和同步... 目录一、WebSocket 简介二、python 的 websockets 库安装三、完整代码示例1.

揭秘Python Socket网络编程的7种硬核用法

《揭秘PythonSocket网络编程的7种硬核用法》Socket不仅能做聊天室,还能干一大堆硬核操作,这篇文章就带大家看看Python网络编程的7种超实用玩法,感兴趣的小伙伴可以跟随小编一起... 目录1.端口扫描器:探测开放端口2.简易 HTTP 服务器:10 秒搭个网页3.局域网游戏:多人联机对战4.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

Java进行文件格式校验的方案详解

《Java进行文件格式校验的方案详解》这篇文章主要为大家详细介绍了Java中进行文件格式校验的相关方案,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、背景异常现象原因排查用户的无心之过二、解决方案Magandroidic Number判断主流检测库对比Tika的使用区分zip