Example #1
0
    def __call__(self, seqrecords):
        'It trims the edges of the given seqrecords.'
        stats = self._stats
        stats[PROCESSED_PACKETS] += 1
        mask = self.mask
        processed_seqs = []
        for seqrecord in seqrecords:
            stats[PROCESSED_SEQS] += 1

            if not TRIMMING_RECOMMENDATIONS in seqrecord.annotations:
                processed_seqs.append(copy_seqrecord(seqrecord))
                continue

            trim_rec = seqrecord.annotations[TRIMMING_RECOMMENDATIONS]
            #fixing the trimming recommendations
            if TRIMMING_RECOMMENDATIONS in seqrecord.annotations:
                del seqrecord.annotations[TRIMMING_RECOMMENDATIONS]

            trim_segments = []
            for trim_kind in TRIMMING_KINDS:
                trim_segments.extend(trim_rec.get(trim_kind, []))

            #masking
            if mask:
                seqrecord = _mask_sequence(seqrecord, trim_segments)
            else:
                #trimming
                if trim_segments:
                    trim_limits = get_longest_complementary_segment(
                                                 trim_segments, len(seqrecord))
                    if trim_limits is None:
                        # there's no sequence left
                        continue
                else:
                    trim_limits = []

                if trim_limits:
                    seqrecord = seqrecord[trim_limits[0]:trim_limits[1] + 1]

            processed_seqs.append(seqrecord)

            stats[YIELDED_SEQS] += 1
        return processed_seqs
Example #2
0
def _mask_sequence(seqrecord, segments):
    'It masks the given segments of the sequence'

    if not segments:
        return seqrecord
    segments = merge_overlaping_segments(segments)
    segments = get_all_segments(segments, len(seqrecord))
    seq = str(seqrecord.seq)
    new_seq = ''
    for segment in segments:
        start = segment[0][0]
        end = segment[0][1] + 1
        seq_ = seq[start:end]

        if segment[1]:
            seq_ = seq_.lower()
        new_seq += seq_
    return copy_seqrecord(seqrecord, seq=Seq(new_seq,
                                             alphabet=seqrecord.seq.alphabet))