<commit_before>#!/usr/bin/env python
#
# -----------------------------------------------------------------------------
# Copyright (c) 2016 The Regents of the University of California
#
# This file is part of kevlar (http://github.com/standage/kevlar) and is
# licensed under the MIT license: see LICENSE.
# -----------------------------------------------------------------------------

from __future__ import print_function
import argparse

import pysam
import khmer
from khmer import khmer_args

import kevlar


def match(record, seq):
    refrseq = str(seq[record.pos:record.pos+record.rlen].seq)
    return refrseq == records.seq


def subparser(subparsers):
    subparser = subparsers.add_parser('dump')
    subparser.add_argument('--seqid', metavar='SEQ',
                           help='dump reads not mapped to SEQ')
    subparser.add_argument('--genomemask', metavar='FILE', help='dump reads '
                           'with median k-mer abundance >= 1 in the specified '
                           'genome; if both --seqid and --genomemask are '
                           'declared, reads passing either filter will be '
                           'kept')
    subparser.add_argument('--maskmemory', metavar='SIZE', default=2e9,
                           type=khmer_args.memory_setting,
                           help='memory to be occupied by genome mask; default'
                           ' is 2G')
    subparser.add_argument('--mask-k', metavar='K', default=31, type=int,
                           help='k size for genome mask')
    subparser.add_argument('--out', metavar='FILE',
                           type=argparse.FileType('w'),
                           help='output file; default is terminal (stdout)')
    subparser.add_argument('refr', help='reference sequence in Fasta format')
    subparser.add_argument('reads', help='read alignments in BAM format')


def main(args):
    print('[kevlar::dump] Loading reference sequence', file=args.logfile)
    with open(args.refr, 'r') as genome:
        seqs = kevlar.seqio.parse_seq_dict(genome)

    if args.genomemask:
        print('[kevlar::dump] Loading genome mask', file=args.logfile)
        genomemask = khmer.Countgraph(args.mask_k, int(args.maskmemory / 4), 4)
        genomemask.consume_seqfile(args.genomemask)

    bam = pysam.AlignmentFile(args.reads, 'rb')
    for i, record in enumerate(bam):
        if i > 0 and i % 50000 == 0:
            print('...processed', i, 'records', file=args.logfile)

        # We only want primary sequences
        if record.is_secondary or record.is_supplementary:<commit_msg>Make pysam a soft requirement<commit_after>#!/usr/bin/env python
#
# -----------------------------------------------------------------------------
# Copyright (c) 2016 The Regents of the University of California
#
# This file is part of kevlar (http://github.com/standage/kevlar) and is
# licensed under the MIT license: see LICENSE.
# -----------------------------------------------------------------------------

from __future__ import print_function
import argparse

try:
    import pysam
except ImportError:
    pass
import khmer
from khmer import khmer_args

import kevlar


def match(record, seq):
    refrseq = str(seq[record.pos:record.pos+record.rlen].seq)
    return refrseq == records.seq


def subparser(subparsers):
    subparser = subparsers.add_parser('dump')
    subparser.add_argument('--seqid', metavar='SEQ',
                           help='dump reads not mapped to SEQ')
    subparser.add_argument('--genomemask', metavar='FILE', help='dump reads '
                           'with median k-mer abundance >= 1 in the specified '
                           'genome; if both --seqid and --genomemask are '
                           'declared, reads passing either filter will be '
                           'kept')
    subparser.add_argument('--maskmemory', metavar='SIZE', default=2e9,
                           type=khmer_args.memory_setting,
                           help='memory to be occupied by genome mask; default'
                           ' is 2G')
    subparser.add_argument('--mask-k', metavar='K', default=31, type=int,
                           help='k size for genome mask')
    subparser.add_argument('--out', metavar='FILE',
                           type=argparse.FileType('w'),
                           help='output file; default is terminal (stdout)')
    subparser.add_argument('refr', help='reference sequence in Fasta format')
    subparser.add_argument('reads', help='read alignments in BAM format')


def main(args):
    try:
        import pysam
    except ImportError:
        import sys
        print('[kevlar::dump] FATAL ERROR: cannot execute "kevlar dump" '
              'unless the "pysam" module is installed', file=sys.stderr)
        sys.exit(1)

    print('[kevlar::dump] Loading reference sequence', file=args.logfile)
    with open(args.refr, 'r') as genome:
        seqs = kevlar.seqio.parse_seq_dict(genome)

    if args.genomemask:
        print('[kevlar::dump] Loading genome mask', file=args.logfile)
        genomemask = khmer.Countgraph(args.mask_k, int(args.maskmemory / 4), 4)
        genomemask.consume_seqfile(args.genomemask)

    bam = pysam.AlignmentFile(args.reads, 'rb')
    for i, record in enumerate(bam):
        if i > 0 and i % 50000 == 0:
            print('...processed', i, 'records', file=args.logfile)

        # We only want primary sequences
        if record.is_secondary or record.is_supplementary:
{'fraction_non_alphanumeric': 0.16099690740403858, 'fraction_numerical': 0.0070947789703474625, 'mean_word_length': 2.0060142154182614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3045568', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1530, 'n_words': 410}