def setup_maf_header(self): """ Sets up the maf header. """ # Reader header _hdr = MafHeader.from_reader(reader=self.maf_reader) if not self.options["reference_fasta_index"]: self.maf_header = MafHeader.from_defaults( version=self.options["version"], annotation=self.options["annotation"], sort_order=BarcodesAndCoordinate(), contigs=_hdr.contigs(), ) else: self.maf_header = MafHeader.from_defaults( version=self.options["version"], annotation=self.options["annotation"], sort_order=BarcodesAndCoordinate(), fasta_index=self.options["reference_fasta_index"], ) self.maf_header.validation_stringency = ValidationStringency.Strict header_date = BaseRunner.get_header_date() self.maf_header[header_date.key] = header_date try: nkey = _hdr["normal.aliquot"] self.maf_header["normal.aliquot"] = nkey except KeyError as e: if not self.options["tumor_only"]: raise e tkey = _hdr["tumor.aliquot"] self.maf_header["tumor.aliquot"] = tkey
def setup_maf_header(self): """ Sets up the maf header. """ self.maf_header = MafHeader.from_defaults( version=self.options["version"], annotation=self.options["annotation"], sort_order=BarcodesAndCoordinate(), fasta_index=self.options["reference_fasta_index"], ) header_date = BaseRunner.get_header_date() self.maf_header[header_date.key] = header_date if not self.options["tumor_only"]: normal_aliquot = MafHeaderRecord( key="normal.aliquot", value=self.options["normal_aliquot_uuid"] if not self.options["tumor_only"] else "", ) self.maf_header[normal_aliquot.key] = normal_aliquot tumor_aliquot = MafHeaderRecord( key="tumor.aliquot", value=self.options["tumor_aliquot_uuid"] ) self.maf_header[tumor_aliquot.key] = tumor_aliquot
def __init__(self, iters, fasta_index=None, contigs=None, by_barcodes=True, peekable_iterator_class=PeekableIterator): """ :param iters: the list of iterators. :param fasta_index: the path to the FASTA index for defining ordering across chromosomes. :param contigs: the list of contigs to use for sorting instead of parsing from FASTA index. :param by_barcodes: True to require the same tumor and matched normal barcodes for returned locatables, False otherwise :param peekable_iterator_class: PeekableIterator class to use when traversing individual MAFs. This allows developers to add in custom filters and custom handling of MAFs. """ self._by_barcodes = by_barcodes if self._by_barcodes: self._sort_order = BarcodesAndCoordinate(fasta_index=fasta_index, contigs=contigs) self._overlap_f = self.__overlaps_with_barcode else: self._sort_order = Coordinate(fasta_index=fasta_index) self._overlap_f = self.__overlaps # Trust, but verify _iters = [_SortOrderEnforcingIterator(_iter, self._sort_order) for _iter in iters] self._iters = [peekable_iterator_class(_iter) for _iter in _iters] self._sort_key = self._sort_order.sort_key()
def __init__( self, iters: List[MafReader], fasta_index: Optional[str] = None, contigs: List[str] = None, by_barcodes: bool = True, peekable_iterator_class: Type[PeekableIterator] = PeekableIterator, ): """ :param iters: the list of iterators. :param fasta_index: the path to the FASTA index for defining ordering across chromosomes. :param contigs: the list of contigs to use for sorting instead of parsing from FASTA index. :param by_barcodes: True to require the same tumor and matched normal barcodes for returned locatables, False otherwise :param peekable_iterator_class: PeekableIterator class to use when traversing individual MAFs. This allows developers to add in custom filters and custom handling of MAFs. """ self._overlap_f: Union[Callable[ [_BarcodesAndCoordinateKey, _BarcodesAndCoordinateKey], bool], Callable[[Locatable, Locatable], bool], ] if not by_barcodes: _sort_order = Coordinate(fasta_index=fasta_index) self._overlap_f = self.__overlaps else: _sort_order = BarcodesAndCoordinate(fasta_index=fasta_index, contigs=contigs) self._overlap_f = self.__overlaps_with_barcode self._sort_order: Coordinate = _sort_order self._by_barcodes: bool = by_barcodes # Trust, but verify _iters = [ _SortOrderEnforcingIterator(_iter, self._sort_order) for _iter in iters ] self._iters: List[PeekableIterator] = [ peekable_iterator_class(_iter) for _iter in _iters ] self._sort_key: TSortKey = self._sort_order.sort_key()
def test_less_than_max_in_memory(self): max_objects_in_ram = 100 num_records = max_objects_in_ram - 1 sorter = Sorter(max_objects_in_ram, self.codec(), BarcodesAndCoordinate().sort_key(), always_spill=False) # add them in reverse order for i in range(num_records): record = DummyRecord("A", "B", "C", 1, num_records - i - 1) sorter += record records = [r for r in sorter] sorter.close() self.assertEqual(len(records), num_records) for i in range(num_records): record = records[i] self.assertEqual(record.value("End_Position"), i)
def setup_maf_header(self): """ Sets up the maf header. """ # Reader header _hdr = MafHeader.from_reader(reader=self.maf_readers[0]) self.maf_header = MafHeader.from_defaults( version=self.options['version'], annotation=self.options['annotation'], sort_order=BarcodesAndCoordinate(), contigs=_hdr.contigs()) self.maf_header.validation_stringency = ValidationStringency.Strict header_date = BaseRunner.get_header_date() self.maf_header[header_date.key] = header_date nkey = _hdr["normal.aliquot"] self.maf_header["normal.aliquot"] = nkey tkey = _hdr["tumor.aliquot"] self.maf_header["tumor.aliquot"] = tkey
def test_empty(self): sorter = Sorter(100, self.codec(), BarcodesAndCoordinate().sort_key()) records = [r for r in sorter] sorter.close() self.assertEqual(len(records), 0)