【问题标题】:Python - Defining a chunk function to encode genomic dataPython - 定义一个块函数来编码基因组数据
【发布时间】:2020-02-10 20:13:45
【问题描述】:

我正在尝试将存储在数据框中的字符串中的基因组编码为相应数值的数组。

这是我的一些数据框(由于某种原因,它没有给我所有 5 列,只有 2 列):

Antibiotic  ...                                             Genome
0       isoniazid  ...  ccctgacacatcacggcgcctgaccgacgagcagaagatccagctc...
1       isoniazid  ...  gggggtgctggcggggccggcgccgataaccccaccggcatcggcg...
2       isoniazid  ...  aatcacaccccgcgcgattgctagcatcctcggacacactgcacgc...
3       isoniazid  ...  gttgttgttgccgagattcgcaatgcccaggttgttgttgccgaga...
4       isoniazid  ...  ttgaccgatgaccccggttcaggcttcaccacagtgtggaacgcgg...

所以我需要逐个字符地拆分这些字符串并将它们分配给浮点数。这是我使用的查找表:

lookup = {
  'a': 0.25,
  'g': 0.50,
  'c': 0.75,
  't': 1.00
  # z: 0.00
}

我尝试直接使用:

dataframe['Genome'].apply(lambda bps: pd.Series([lookup[bp] if bp in lookup else 0.0 for bp in bps.lower()])).values

但我有太多数据无法放入内存,因此我尝试使用块进行处理,但在定义重新处理函数时遇到了麻烦。

到目前为止,这是我的代码:

lookup = {
  'a': 0.25,
  'g': 0.50,
  'c': 0.75,
  't': 1.00
  # z: 0.00
}


dfpath = 'C:\\Users\\CAAVR\\Desktop\\Ison.csv'
dataframe = pd.read_csv(dfpath, chunksize=10)

chunk_list = []
def preprocess(chunk):
  chunk['Genome'].apply(lambda bps: pd.Series([lookup[bp] if bp in lookup else 0.0 for bp in bps.lower()])).values
  return;


for chunk in dataframe:
  chunk_filter = preprocess(chunk)
  chunk_list.append(chunk_filter)
  dataframe1 = pd.concat(chunk_list)

print(dataframe1)

提前致谢!

【问题讨论】:

    标签: python pandas preprocessor chunks


    【解决方案1】:

    您有chunk_filter = preprocess(chunk),但您的preprocess() 函数没有返回任何内容,因此chunk_filter 始终毫无意义。修改您的预处理函数以存储apply() 调用的结果,然后返回该值。例如:

    def preprocess(chunk):
      processed_chunk = chunk['Genome'].apply(lambda bps: pd.Series([lookup[bp] if bp in lookup else 0.0 for bp in bps.lower()])).values
      return processed_chunk;
    

    通过这样做,您实际上从预处理函数返回数据,以便可以将其附加到块列表中。正如您目前所拥有的那样,预处理功能可以正常工作,但实际上会丢弃结果。

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多