Package obitools :: Package ecotag :: Module parser
[hide private]
[frames] | no frames]

Source Code for Module obitools.ecotag.parser

  1  from itertools import imap 
  2  from obitools import utils 
  3   
  4  from obitools.ecotag import EcoTagResult 
5 6 -class EcoTagFileIterator(utils.ColumnFile):
7 8 @staticmethod
9 - def taxid(x):
10 x = int(x) 11 if x < 0: 12 return None 13 else: 14 return x
15 16 @staticmethod
17 - def scientificName(x):
18 if x=='--': 19 return None 20 else: 21 return x
22 23 @staticmethod
24 - def value(x):
25 if x=='--': 26 return None 27 else: 28 return float(x)
29 30 @staticmethod
31 - def count(x):
32 if x=='--': 33 return None 34 else: 35 return int(x)
36 37
38 - def __init__(self,stream):
59 60 _colname = ['identification', 61 'seqid', 62 'best_match_ac', 63 'max_identity', 64 'min_identity', 65 'theorical_min_identity', 66 'count', 67 'match_count', 68 'taxid', 69 'scientific_name', 70 'rank', 71 'order_taxid', 72 'order_sn', 73 'family_taxid', 74 'family_sn', 75 'genus_taxid', 76 'genus_sn', 77 'species_taxid', 78 'species_sn', 79 'sequence'] 80
81 - def next(self):
82 if self._memory is not None: 83 data=self._memory 84 self._memory=None 85 else: 86 data = utils.ColumnFile.next(self) 87 data = EcoTagResult(imap(None,EcoTagFileIterator._colname[:len(data)],data)) 88 89 if data['identification']=='ID': 90 data.cd=[] 91 try: 92 nextone = utils.ColumnFile.next(self) 93 nextone = EcoTagResult(imap(None,EcoTagFileIterator._colname[:len(nextone)],nextone)) 94 except StopIteration: 95 nextone = None 96 while nextone is not None and nextone['identification']=='CD': 97 data.cd.append(nextone) 98 try: 99 nextone = utils.ColumnFile.next(self) 100 nextone = EcoTagResult(imap(None,EcoTagFileIterator._colname[:len(nextone)],nextone)) 101 except StopIteration: 102 nextone = None 103 self._memory=nextone 104 105 return data
106
107 -def ecoTagIdentifiedFilter(ecoTagIterator):
108 for x in ecoTagIterator: 109 if x['identification']=='ID': 110 yield x
111
112 113 -class EcoTagAbstractIterator(utils.ColumnFile):
114 115 _colname = ['scientific_name', 116 'taxid', 117 'rank', 118 'count', 119 'max_identity', 120 'min_identity'] 121 122 123 @staticmethod
124 - def taxid(x):
125 x = int(x) 126 if x < 0: 127 return None 128 else: 129 return x
130
131 - def __init__(self,stream):
132 utils.ColumnFile.__init__(self, 133 stream, '\t', True, 134 (str, 135 EcoTagFileIterator.taxid, 136 str, 137 int, 138 float,float,float))
139
140 - def next(self):
141 data = utils.ColumnFile.next(self) 142 data = dict(imap(None,EcoTagAbstractIterator._colname,data)) 143 144 return data
145
146 -def ecoTagAbstractFilter(ecoTagAbsIterator):
147 for x in ecoTagAbsIterator: 148 if x['taxid'] is not None: 149 yield x
150