使用多列值解析数据

79 阅读3分钟

在 Python 中,需要将染色体数据输入到数据库,同时对数据格式进行转换。原始数据如下所示:

chrom   = chr1
txStart = 134212702 
exonCount = 8
exonStarts = 134212702,134221529,134222782,134224273,134224707,134226534,134227135,134227897,
exonEnds=   134213049,134221650,134222806,134224425,134224773,134226654,134227268,134229870,

huake_00152_.jpg 希望创建一个数据库,其中包含上述所有数据,包括启动子、外显子和内含子位置的信息。每组数据(来自单行,如上述示例)对应单个转录本。将启动子定义为 txStart (例如 134212702) - 2000。每个转录本的相对应的拥有 exonCount 指定数量的外显子(例如 8 个)。在 exonStarts 和 exonEnds 列表中,通过匹配值来指定每个外显子在较大转录本中的开始和结束位置。例如,在这种情况下,第一个外显子的范围是从 134212702 到 134213049。内含子位于外显子之间,因此第一个外显子将从 134213050 到 134221528。

2、解决方案

为了达到上述目的,可以采取以下步骤:

  1. 使用以下代码计算启动子位置:
PromoterStart = int(TxStart) - 2000
PromoterEnd = int(TxStart) -1
  1. 使用 map() 函数将 exonStarts 和 exonEnds 列表转换为整数列表:
ExonStart = map(int, ExonStart[:-1].split(","))
ExonEnd = map(int, ExonEnd[:-1].split(","))
  1. 使用 while 循环遍历外显子列表:
i = 0
while i < int(ExonCount):
    ExonCount = i +1
    if i != int(ExonCount) -1:
        IntronStart[i] = ExonEnd[i] +1
        IntronEnd[i] = ExonStart[i+1] -1
    i += 1
  1. 使用以下代码将数据插入数据库:
# 将数据插入数据库
  1. 使用以下代码计算新的启动子位置:
PromoterStart = int(TxStart) - 2000
PromoterEnd = int(TxStart) + 2000
  1. 使用以下代码过滤任何位于启动子区域内的外显子位置:
NewExonStart,NewExonEnd = zip(*filter(lambda y:not(y[0]>=PromoterStart and y[1]<=PromoterEnd),zip(ExonStart,ExonEnd)))
  1. 使用以下代码更新数据库中的数据:
# 更新数据库中的数据

通过上述步骤,可以将染色体数据输入到数据库中,同时对数据格式进行转换,并过滤掉启动子区域内的外显子位置。

代码例子:

# 定义数据
chrom = 'chr1'
txStart = 134212702
exonCount = 8
exonStarts = [134212702, 134221529, 134222782, 134224273, 134224707, 134226534, 134227135, 134227897]
exonEnds = [134213049, 134221650, 134222806, 134224425, 134224773, 134226654, 134227268, 134229870]

# 计算启动子位置
promoterStart = int(txStart) - 2000
promoterEnd = int(txStart) - 1

# 将 exonStarts 和 exonEnds 列表转换为整数列表
exonStarts = map(int, exonStarts)
exonEnds = map(int, exonEnds)

# 创建变量来存储内含子位置
intronStarts = []
intronEnds = []

# 遍历外显子列表
i = 0
while i < int(exonCount):
    exonCount = i + 1
    if i != int(exonCount) - 1:
        intronStarts.append(exonEnds[i] + 1)
        intronEnds.append(exonStart[i + 1] - 1)
    i += 1

# 计算新的启动子位置
newPromoterStart = int(txStart) - 2000
newPromoterEnd = int(txStart) + 2000

# 过滤任何位于启动子区域内的外显子位置
newExonStarts, newExonEnds = zip(*filter(lambda y: not (y[0] >= newPromoterStart and y[1] <= newPromoterEnd), zip(exonStarts, exonEnds)))

# 更新数据库中的数据
# 使用 newExonStarts 和 newExonEnds 更新数据库中的外显子位置