制表符分隔数据集ValueError具有多个元素的数组的真值是模糊错误

时间:2013-01-27 01:53:29

标签: python numpy

我有这个制表符分隔的数据集,如下所示:

PITG_00022  start_codon 262407  262409  -
PITG_00022  stop_codon  260777  260779  -
PITG_00022  exon    260867  262409  -
PITG_00022  CDS 260867  262409  -
PITG_00022  exon    260777  260826  -
PITG_00022  CDS 260780  260826  -
PITG_00023  start_codon 273160  273162  +
PITG_00023  stop_codon  274778  274780  +
PITG_00023  exon    272998  273288  +
PITG_00023  CDS 273160  273288  +
PITG_00023  exon    273368  273652  +
PITG_00023  CDS 273368  273652  +
PITG_00023  exon    273729  273788  +
PITG_00023  CDS 273729  273788  +
PITG_00023  exon    273885  273958  +
PITG_00023  CDS 273885  273958  +
PITG_00023  exon    274022  274127  +
PITG_00023  CDS 274022  274127  +
PITG_00023  exon    274194  274346  +

这是我的代码,在这里非常优雅地发布Renumbering tab delimited data in two columns relative to contents of a third column我做了一些更改,但我得到的错误(见下文)在进行这些更改之前就已存在:

import numpy
import pandas
import pandas as pd
import sys

sys.stdout = open("outtry2.txt", "w")
data = pd.read_csv('pinfestans-edited2.csv', sep='\t')#,
              #converters={'STRAND': lambda s: s[0]})
groups = data.groupby(['STRAND', 'GENE_ID'])

corrected = []

for (direction, gene_name), group in groups:
    print direction,gene_name
if group.index[group.TYPE=='start_codon']:
    start_exon = group.index[group.TYPE=='exon'][0]
if direction == '+':
    group['POSA'] = 1 + abs(group.POS1 - group.POS1[start_exon])
    group['POSB'] = 1 + abs(group.POS2 - group.POS1[start_exon])
else:
    group['POSA'] = 1 - abs(group.POS2 - group.POS2[start_exon])
    group['POSB'] = 1 - abs(group.POS1 - group.POS2[start_exon])
print group
corrected.append(group)

以下是输出示例:

 GENE_ID     TYPE         POS1    POS2    STRAND  POSA  POSB
104  PITG_00021  start_codon  258927  258929  +       1     3   
105  PITG_00021  stop_codon   260547  260549  +       1621  1623
106  PITG_00021  exon         258927  260549  +       1     1623
107  PITG_00021  CDS          258927  260546  +       1     1620
+ PITG_00023
     GENE_ID     TYPE         POS1    POS2    STRAND  POSA  POSB
114  PITG_00023  start_codon  273160  273162  +       163   165 
115  PITG_00023  stop_codon   274778  274780  +       1781  1783
116  PITG_00023  exon         272998  273288  +       1     291 
117  PITG_00023  CDS          273160  273288  +       163   291 
118  PITG_00023  exon         273368  273652  +       371   655 
119  PITG_00023  CDS          273368  273652  +       371   655 
120  PITG_00023  exon         273729  273788  +       732   791 
121  PITG_00023  CDS          273729  273788  +       732   791 
122  PITG_00023  exon         273885  273958  +       888   961 
123  PITG_00023  CDS          273885  273958  +       888   961 
124  PITG_00023  exon         274022  274127  +       1025  1130
125  PITG_00023  CDS          274022  274127  +       1025  1130
126  PITG_00023  exon         274194  274346  +       1197  1349

这是我运行一段时间后得到的错误:

>>>>Traceback (most recent call last):
  File "C:\Users\Chris\Documents\I.S\Part-2\Convert_to_exonfile.py", line 51, in <module>
if group.index[group.TYPE=='start_codon']:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>>>

现在我手工测试了一些计算,它似乎完全符合我的要求,只有STRAND列出的基因为+(请注意,样本输入中的PITG_00022基因是在输出中丢失)。我认为它首先在+ STRAND组中进行计算,但是一旦它到达 - STRAND组,就会出现错误。现在我读到了关于使用any()all()的内容,以及关于在类似的数组valueError线程中使用set()的帖子,但我无法弄清楚这将如何解决我的问题或者我将如何实现它。提前感谢任何帮助,谢谢。

0 个答案:

没有答案