0

我有下面给出的代码:

for pileupcolumn in samfile.pileup(max_depth = 1000000) :
  X.append(pileupcolumn.n)
  for pileupread in pileupcolumn.pileups:
     if (pileupread.alignment.mapping_quality <= 15):
             continue      
     if not pileupread.is_del and not pileupread.is_refskip:
             if pileupread.alignment.query_qualities[pileupread.query_position] < 30:
             # Skip entries with base phred scores < 10
                  continue
             if pileupread.alignment.is_reverse: #negative
                  ReverseList[pileupcolumn.pos] += pileupread.alignment.query_sequence[pileupread.query_position]
             else:
                  ForwardList[pileupcolumn.pos] += pileupread.alignment.query_sequence[pileupread.query_position]

上面的代码花费了很多时间,我想用 join 替换第 11 行和第 13 行的连接。有什么办法吗?

4

1 回答 1

1

不是连接,而是将值收集到一个列表中,然后在循环结束时加入列表。

for pileupcolumn in samfile.pileup(max_depth = 1000000) :
  X.append(pileupcolumn.n)
  forward = []
  reverse = []
  for pileupread in pileupcolumn.pileups:
     if (pileupread.alignment.mapping_quality <= 15):
             continue      
     if not pileupread.is_del and not pileupread.is_refskip:
             if pileupread.alignment.query_qualities[pileupread.query_position] < 30:
             # Skip entries with base phred scores < 10
                  continue
             if pileupread.alignment.is_reverse: #negative
                  reverse.append(pileupread.alignment.query_sequence[pileupread.query_position])
             else:
                  forward.append(pileupread.alignment.query_sequence[pileupread.query_position])
  ReverseList[pileupcolumn.pos] += ''.join(reverse)
  ForwardList[pileupcolumn.pos] += ''.join(forward)

最后我仍然使用连接,因为这种优化只适用于for pileupread循环。如果不同pileupcolumn的对象具有相同的pos,我们需要在该点连接。ReverseList如果在此代码运行之前元素已经具有值,我们也需要这种连接。

于 2017-09-29T22:41:05.967 回答