深度学习DNA序列onehot方法

在利用深度学习模型分析DNA序列时,需要对DNA序列进行one hot encoding。以下是使用PyTorch对DNA序列进行One-hot编码的三种方法,并整合在一个代码中,同时计算不同方法处理128条DNA序列的时间消耗:

import time
import torch
import torch.nn.functional as F
import numpy as np
 
# 定义字母与索引的映射关系
mapping = {'A': 0, 'T': 1, 'C': 2, 'G': 3}
 
# 定义DNA序列列表
sequences = ['ATCG' * 250] * 128  # DNA序列长度为1000bp,共128条序列

# 方法一:torch.nn.functional.one_hot函数
start_time = time.time()
 
onehot_sequences1 = []
for sequence in sequences:
    index_sequence = [mapping[base] for base in sequence]
    onehot_sequence = F.one_hot(torch.tensor(index_sequence), num_classes=4).float()
    onehot_sequences1.append(onehot_sequence)
 
end_time = time.time()
method1_time = end_time - start_time
 
# 方法二:torch.eye函数
start_time = time.time()
 
onehot_matrix = torch.eye(4)
onehot_sequences2 = []
for sequence in sequences:
    index_sequence = [mapping[base] for base in sequence]
    onehot_sequence = onehot_matrix[index_sequence]
    onehot_sequences2.append(onehot_sequence)
 
end_time = time.time()
method2_time = end_time - start_time
 
# 方法三:numpy进行转换
start_time = time.time()
 
onehot_matrix = np.eye(4)
onehot_sequences3 = []
for sequence in sequences:
    index_sequence = [mapping[base] for base in sequence]
    onehot_sequence = onehot_matrix[index_sequence]
    onehot_sequences3.append(onehot_sequence)
onehot_sequences3 = torch.from_numpy(np.array(onehot_sequences3)).float()
end_time = time.time()
method3_time = end_time - start_time
 
print("Method 1 time:", method1_time)
print("Method 2 time:", method2_time)
print("Method 3 time:", method3_time)

测试结果:

Method 1 time: 0.09143757820129395
Method 2 time: 0.02177143096923828
Method 3 time: 0.035161733627319336

 

发表评论

匿名网友