ragtag_utilities/ContigAlignment.py
def merge_alns(self, merge_dist=100000, careful_merge=False):
        """
        Merge adjacent alignments that have the same reference sequence, the same orientation, and are less than
        merge_dist away from each other.
        """
        # Sort the alignments
        self.sort_by_ref()

        # Make a copy of the alignment info
        query_starts = self._query_starts
        query_ends = self._query_ends
        strands = self._strands
        ref_headers = self._ref_headers
        ref_lens = self._ref_lens
        ref_starts = self._ref_starts
        ref_ends = self._ref_ends
        residue_matches = self._residue_matches
        aln_lens = self._aln_lens
        mapqs = self._mapqs

        # Keep track of which alignments we are comparing
        i = 0
        j = 1
        while j < len(ref_headers):
            r_consec_aln_dist = ref_starts[j] - ref_ends[i]
            q_consec_aln_dist = query_starts[j] - query_ends[i]
            conditions = [
                ref_headers[i] == ref_headers[j],
                strands[i] == strands[j],
                (-1*merge_dist) <= r_consec_aln_dist <= merge_dist
            ]
            if careful_merge:
                conditions.append((-1*merge_dist) <= q_consec_aln_dist <= merge_dist)

            if all(conditions):
                # Merge the alignments in place of the first alignment
                query_starts[i] = min(query_starts[i], query_starts[j])
                query_ends[i] = max(query_ends[i], query_ends[j])
                ref_starts[i] = min(ref_starts[i], ref_starts[j])
                ref_ends[i] = max(ref_ends[i], ref_ends[j])

                perc_r_match_i = residue_matches[i] / aln_lens[i]
                perc_r_match_j = residue_matches[j] / aln_lens[j]
                avg_perc_r_match = (perc_r_match_i + perc_r_match_j) / 2
                aln_lens[i] = ref_ends[i] - ref_starts[i]
                residue_matches[i] = round(avg_perc_r_match * aln_lens[i])

                mapqs[i] = self._average_mapqs(mapqs[i], mapqs[j])

                # Remove the redundant alignment
                query_starts.pop(j)
                query_ends.pop(j)
                strands.pop(j)
                ref_headers.pop(j)
                ref_lens.pop(j)
                ref_starts.pop(j)
                ref_ends.pop(j)
                residue_matches.pop(j)
                aln_lens.pop(j)
                mapqs.pop(j)
            else:
                i += 1
                j += 1

        # Make a new object with the merged data
        x = ContigAlignment(
            self.query_header,
            self.query_len,
            query_starts,
            query_ends,
            strands,
            ref_headers,
            ref_lens,
            ref_starts,
            ref_ends,
            residue_matches,
            aln_lens,
            mapqs
        )

        # remove contained alignments.
        return x.filter_query_contained()

        Merge adjacent alignments that have the same reference sequence, the same orientation, and are less than
        merge_dist away from each other.
        
Merge adjacent alignments that have the same reference sequence, the same orientation, and are less than
merge_dist away from each other.
Merge adjacent alignments that have the same reference sequence the same orientation and are less than merge_dist away from each other .
def merge_alns(self, merge_dist=100000, careful_merge=False):
        self.sort_by_ref()
        query_starts = self._query_starts
        query_ends = self._query_ends
        strands = self._strands
        ref_headers = self._ref_headers
        ref_lens = self._ref_lens
        ref_starts = self._ref_starts
        ref_ends = self._ref_ends
        residue_matches = self._residue_matches
        aln_lens = self._aln_lens
        mapqs = self._mapqs
        i = 0
        j = 1
        while j < len(ref_headers):
            r_consec_aln_dist = ref_starts[j] - ref_ends[i]
            q_consec_aln_dist = query_starts[j] - query_ends[i]
            conditions = [
                ref_headers[i] == ref_headers[j],
                strands[i] == strands[j],
                (-1*merge_dist) <= r_consec_aln_dist <= merge_dist
            ]
            if careful_merge:
                conditions.append((-1*merge_dist) <= q_consec_aln_dist <= merge_dist)
            if all(conditions):
                query_starts[i] = min(query_starts[i], query_starts[j])
                query_ends[i] = max(query_ends[i], query_ends[j])
                ref_starts[i] = min(ref_starts[i], ref_starts[j])
                ref_ends[i] = max(ref_ends[i], ref_ends[j])
                perc_r_match_i = residue_matches[i] / aln_lens[i]
                perc_r_match_j = residue_matches[j] / aln_lens[j]
                avg_perc_r_match = (perc_r_match_i + perc_r_match_j) / 2
                aln_lens[i] = ref_ends[i] - ref_starts[i]
                residue_matches[i] = round(avg_perc_r_match * aln_lens[i])
                mapqs[i] = self._average_mapqs(mapqs[i], mapqs[j])
                query_starts.pop(j)
                query_ends.pop(j)
                strands.pop(j)
                ref_headers.pop(j)
                ref_lens.pop(j)
                ref_starts.pop(j)
                ref_ends.pop(j)
                residue_matches.pop(j)
                aln_lens.pop(j)
                mapqs.pop(j)
            else:
                i += 1
                j += 1
        x = ContigAlignment(
            self.query_header,
            self.query_len,
            query_starts,
            query_ends,
            strands,
            ref_headers,
            ref_lens,
            ref_starts,
            ref_ends,
            residue_matches,
            aln_lens,
            mapqs
        )
        return x.filter_query_contained()
def merge_alns ( self , merge_dist = 100000 , careful_merge = False ) : self . sort_by_ref ( ) query_starts = self . _query_starts query_ends = self . _query_ends strands = self . _strands ref_headers = self . _ref_headers ref_lens = self . _ref_lens ref_starts = self . _ref_starts ref_ends = self . _ref_ends residue_matches = self . _residue_matches aln_lens = self . _aln_lens mapqs = self . _mapqs i = 0 j = 1 while j < len ( ref_headers ) : r_consec_aln_dist = ref_starts [ j ] - ref_ends [ i ] q_consec_aln_dist = query_starts [ j ] - query_ends [ i ] conditions = [ ref_headers [ i ] == ref_headers [ j ] , strands [ i ] == strands [ j ] , ( - 1 * merge_dist ) <= r_consec_aln_dist <= merge_dist ] if careful_merge : conditions . append ( ( - 1 * merge_dist ) <= q_consec_aln_dist <= merge_dist ) if all ( conditions ) : query_starts [ i ] = min ( query_starts [ i ] , query_starts [ j ] ) query_ends [ i ] = max ( query_ends [ i ] , query_ends [ j ] ) ref_starts [ i ] = min ( ref_starts [ i ] , ref_starts [ j ] ) ref_ends [ i ] = max ( ref_ends [ i ] , ref_ends [ j ] ) perc_r_match_i = residue_matches [ i ] / aln_lens [ i ] perc_r_match_j = residue_matches [ j ] / aln_lens [ j ] avg_perc_r_match = ( perc_r_match_i + perc_r_match_j ) / 2 aln_lens [ i ] = ref_ends [ i ] - ref_starts [ i ] residue_matches [ i ] = round ( avg_perc_r_match * aln_lens [ i ] ) mapqs [ i ] = self . _average_mapqs ( mapqs [ i ] , mapqs [ j ] ) query_starts . pop ( j ) query_ends . pop ( j ) strands . pop ( j ) ref_headers . pop ( j ) ref_lens . pop ( j ) ref_starts . pop ( j ) ref_ends . pop ( j ) residue_matches . pop ( j ) aln_lens . pop ( j ) mapqs . pop ( j ) else : i += 1 j += 1 x = ContigAlignment ( self . query_header , self . query_len , query_starts , query_ends , strands , ref_headers , ref_lens , ref_starts , ref_ends , residue_matches , aln_lens , mapqs ) return x . filter_query_contained ( )
Merge adjacent alignments that have the same reference sequence, the same orientation, and are less than
merge_dist away from each other.
Merge adjacent alignments that have the same reference sequence the same orientation and are less than merge_dist away from each other .
"""
        Merge adjacent alignments that have the same reference sequence, the same orientation, and are less than
        merge_dist away from each other.
        """ # Sort the alignments # Make a copy of the alignment info # Keep track of which alignments we are comparing # Merge the alignments in place of the first alignment # Remove the redundant alignment # Make a new object with the merged data # remove contained alignments.
self
merge_dist
careful_merge