Exemple #1
0
    def convert_guppy_result(guppy_result):

        guppy_barcode = guppy_result['barcode']
        if guppy_barcode['name'] == 'unclassified':
            guppy_barcode = None
            guppy_adapter = None
        else:
            guppy_barcode = read_barcode(guppy_barcode)
            guppy_adapter = AdapterLayout(
                kit=BarcodeScannerGuppy.KIT_MAPPING_REV.get(guppy_result['kit'],
                                                            guppy_result['kit']),
                sequence="ATGC",
                barcode_set_1=None,
                barcode_set_2=None,
                description="")

        result = build_return_dict(
            best_barcode=guppy_barcode,
            best_barcode_score=guppy_result['score'],
            best_adapter=guppy_adapter,
            best_adapter_end=90,
            trim5p=guppy_result['trim5p'],
            trim3p=guppy_result['trim3p'],
            exit_status=guppy_result['exit_status'],
        )

        return result
Exemple #2
0
    def scan(self,
             read_sequence,
             read_qualities,
             bc_adapter_templates,
             nobc_adapter_templates,
             qcat_config=config.qcatConfig()):
        """
            Aligns all passed barcodes to the read sequence and chooses the one
            with the highest alignment score. Simplest version of barcode detection
            that is independent of the sequencing kit but has higher FN, FP and
            incorrect rate for barcode assignments. Currently does not implement any
            identity thresholds. Therefor, filtering by quality score is required.

            :param read_sequence: Sequence of the read
            :type read_sequence: str
            :param read_qualities: Base qualities of the read in Sanger encoding
            :type read_qualities: str
            :param barcode_set: List of possible Barcode tuples
            :type barcode_set: List
            :param qcat_config: qcatConfig object
            :type qcat_config: qcatConfig
            :return: see build_return_dict
            :rtype: Dictionary
            """

        exit_status = 0

        best_barcode, best_barcode_q_score, identity, barcode_end = \
            find_highest_scoring_barcode(barcode_region_read=read_sequence,
                                         barcode_set=self.barcodes,
                                         qcat_config=qcat_config,
                                         compute_identity=True)

        # barcode_err_probe = 0.0
        # if best_barcode:
        #     barcode_err_probe = compute_adapter_error_prob(read_qualities,
        #                                                    barcode_end,
        #                                                    len(
        #                                                        best_barcode.sequence))

        # If identity is too low or error prob too high, report as unclassified
        if identity < self.min_quality:
            return empty_return_dict()

        return build_return_dict(best_barcode=best_barcode,
                                 best_barcode_score=best_barcode_q_score,
                                 best_adapter=None,
                                 best_adapter_end=barcode_end,
                                 exit_status=exit_status)
Exemple #3
0
    def detect_barcode(self,
                       read_sequence,
                       read_qualities=None,
                       qcat_config=config.qcatConfig()):

        result = empty_return_dict()

        if not self.override_kit_name:
            detected_adapter, _ = self.scan_ends(read_sequence, qcat_config)
            override_kit_name = detected_adapter.kit
        else:
            override_kit_name = self.override_kit_name

        if override_kit_name in self.brill_models:
            brill_model, brill_model_len = self.brill_models[override_kit_name]

            if len(read_sequence) > brill_model_len:
                bc, qscore = self.run_brill(brill_model, brill_model_len, read_sequence)

                barcode = None
                adapter = None
                if bc and bc != 'none':
                    adapter = self.get_adapter(override_kit_name)
                    barcode = adapter.get_barcode_set()[int(bc) - 1]

                if bc and qscore >= self.min_quality:
                    result = build_return_dict(
                        best_barcode=barcode,
                        best_barcode_score=qscore,
                        best_adapter=adapter,
                        best_adapter_end=90,
                        trim5p=90,
                        trim3p=90,
                        exit_status=0
                    )

        return result
Exemple #4
0
    def scan(self,
             read_sequence,
             read_qualities,
             bc_adapter_templates,
             nobc_adapter_templates,
             qcat_config=config.qcatConfig()):
        """
            Detects sequencing adapter and identifies best matching barcode

            :param read_sequence: Read sequence containing adapter
            :type read_sequence: str
            :param read_qualities: Base qualities of the read in Sanger encoding
            :type read_qualities: str
            :param adapter_templates: List of AdapterLayout objects
            with Ns
            :type adapter_templates: List
            :param qcat_config: qcatConfig object
            :type qcat_config: qcatConfig
            :return: see build_return_dict
            :rtype: Dictionary
            """
        exit_status = 0

        # Finding best barcoded adapters
        ret = find_best_adapter_template(adapter_templates=bc_adapter_templates,
                                         read_sequence=read_sequence,
                                         qcat_config=qcat_config)

        best_adapter_template_index, aligned_adapter_end, best_adapter_score = ret

        # if barcoded adapter found
        best_adapter_template = bc_adapter_templates[
            best_adapter_template_index]

        # Detect barcode
        barcode_set_index = 0

        # For high quality adapter alignments just use the barcode region,
        # for low quality compare full adapter to the barcodes
        # Do not scann full adapter is it contains double barcoding
        # best_adapter_score > 90.0 or
        if best_adapter_score > 90.0 or best_adapter_template.is_doulbe_barcode():
            barcode_region_read = extract_barcode_region(
                read_sequence=read_sequence,
                adapter_template=best_adapter_template,
                barcode_set_index=barcode_set_index,
                alignment_stop_ref=aligned_adapter_end,
                qcat_config=qcat_config)
        else:
            barcode_region_read = read_sequence[:qcat_config.max_align_length]

        # First barcode
        up_context = best_adapter_template.get_upstream_context(
            qcat_config.barcode_context_length, barcode_set_index)
        down_context = best_adapter_template.get_downstream_context(
            qcat_config.barcode_context_length, barcode_set_index)
        barcode_set = best_adapter_template.get_barcode_set(
            barcode_set_index)
        if self.barcodes:
            barcode_set = self.barcodes

        # Find best barcode
        best_barcode, best_barcode_q_score, best_barcode_score, barcode_end = \
            find_highest_scoring_barcode(
                barcode_region_read=barcode_region_read,
                barcode_set=barcode_set,
                upstream_context=up_context,
                downstream_context=down_context,
                qcat_config=qcat_config)

        # If double barcode adapter
        barcode_set_index = 1
        if best_adapter_template.get_barcode_set(barcode_set_index):

            barcode_region_read = extract_barcode_region(
                read_sequence=read_sequence,
                adapter_template=best_adapter_template,
                barcode_set_index=barcode_set_index,
                alignment_stop_ref=aligned_adapter_end,
                qcat_config=qcat_config)

            # First barcode
            up_context = best_adapter_template.get_upstream_context(
                qcat_config.barcode_context_length, barcode_set_index)
            down_context = best_adapter_template.get_downstream_context(
                qcat_config.barcode_context_length, barcode_set_index)
            barcode_set = best_adapter_template.get_barcode_set(
                barcode_set_index)
            if self.barcodes:
                barcode_set = self.barcodes

            # Find best barcode
            best_barcode_2, best_barcode_q_score_2, best_barcode_score_2, barcode_end_2 = \
                find_highest_scoring_barcode(
                    barcode_region_read=barcode_region_read,
                    barcode_set=barcode_set,
                    upstream_context=up_context,
                    downstream_context=down_context,
                    qcat_config=qcat_config)
        else:
            logging.debug("Adapter type does not have second barcode")

        return build_return_dict(
            best_barcode=best_barcode,
            best_barcode_score=best_barcode_q_score,
            best_adapter=best_adapter_template,
            best_adapter_end=aligned_adapter_end,
            exit_status=exit_status
        )