def _getReadAlignments(self, reference, start, end, readGroupSet, readGroup): """ Returns an iterator over the specified reads """ # TODO If reference is None, return against all references, # including unmapped reads. samFile = self.getFileHandle(self._dataUrl) referenceName = reference.getLocalId().encode() # TODO deal with errors from htslib start, end = self.sanitizeAlignmentFileFetch(start, end) readAlignments = samFile.fetch(referenceName, start, end) for readAlignment in readAlignments: tags = dict(readAlignment.tags) if readGroup is None: if 'RG' in tags: alignmentReadGroupLocalId = tags['RG'] readGroupCompoundId = datamodel.ReadGroupCompoundId( readGroupSet.getCompoundId(), str(alignmentReadGroupLocalId)) yield self.convertReadAlignment(readAlignment, readGroupSet, str(readGroupCompoundId)) else: if self._filterReads: if 'RG' in tags and tags['RG'] == self._localId: yield self.convertReadAlignment( readAlignment, readGroupSet, str(readGroup.getCompoundId())) else: yield self.convertReadAlignment( readAlignment, readGroupSet, str(readGroup.getCompoundId()))
def testReadGroup(self): readGroupSet = self.getReadGroupSet() dataset = readGroupSet.getParentContainer() localId = "readGroup" cid = datamodel.ReadGroupCompoundId(readGroupSet.getCompoundId(), localId) self.assertRaises(ValueError, datamodel.ReadGroupCompoundId, readGroupSet.getCompoundId()) self.assertEqual(cid.dataset, dataset.getLocalId()) self.assertEqual(cid.read_group_set, readGroupSet.getLocalId()) self.assertEqual(cid.read_group, localId) self.assertEqual(cid.dataset_id, dataset.getId()) self.assertEqual(cid.read_group_set_id, readGroupSet.getId())