<commit_before>module Unipept::Commands
  class Peptfilter
    attr_reader :root_command

    @root_command = Cri::Command.define do
      name 'peptfilter'
      summary 'Filter peptides based on specific criteria.'
      usage 'peptfilter [options]'
      description <<-EOS
      The peptfilter command filters a list of peptides according to specific criteria. The command expects a list of peptides that are passed to standard input.

      The input should have one peptide per line. FASTA headers are preserved in the output, so that peptides remain bundled.
      EOS
      # flag :u, :unique, "filter duplicate peptides."
      required nil, :minlen, 'only retain tryptic peptides that have at least min (default: 5) amino acids.'
      required nil, :maxlen, 'only retain tryptic peptides that have at most max (default: 50) amino acids.'
      required :l, :lacks, 'only retain tryptic peptides that lack all amino acids from the string of residues.'
      required :c, :contains, 'only retain tryptic peptides that contain all amino acids from the string of residues.'
      flag :h, :help, 'show help for this command' do |_value, cmd|
        puts cmd.help
        exit 0
      end
      run do |opts, _args, _cmd|
        minlen = opts.fetch(:minlen, '5').to_i
        maxlen = opts.fetch(:maxlen, '50').to_i
        lacks = opts.fetch(:lacks, '').chars.to_a
        contains = opts.fetch(:contains, '').chars.to_a
        $stdin.each_line do |pept|
          # FASTA headers
          if pept.start_with? '>'
            puts pept
            next
          end

          pept = pept.chomp
          puts pept if Peptfilter.filter(pept, minlen, maxlen, lacks, contains)
        end
      end
    end

    @root_command.add_command(Cri::Command.new_basic_help)

    # Invokes the peptfilter command-line tool with the given arguments.
    #
    # @param [Array<String>] args An array of command-line arguments
    #
    # @return [void]
    def self.run(args)
      @root_command.run(args)
    end

    # Checks if a peptide satisfies the min length, max length, lacks and contains requirements.
    # Returns true if
    # - the peptide length is equal or higher than min
    # - the peptide length is equal or lower than max
    # - the peptide doesn't contain any of the amino acids in lacks
    # - the peptide contains all of the amino acids in contains
    #
    # @param [String] peptide The peptide to check
    #
    # @param [Integer] min The minimal length requirement<commit_msg>running peptfiter with arguments no longer gives an invalid command error. fixes #46<commit_after>module Unipept::Commands
  class Peptfilter
    attr_reader :root_command

    @root_command = Cri::Command.define do
      name 'peptfilter'
      summary 'Filter peptides based on specific criteria.'
      usage 'peptfilter [options]'
      description <<-EOS
      The peptfilter command filters a list of peptides according to specific criteria. The command expects a list of peptides that are passed to standard input.

      The input should have one peptide per line. FASTA headers are preserved in the output, so that peptides remain bundled.
      EOS
      # flag :u, :unique, "filter duplicate peptides."
      required nil, :minlen, 'only retain tryptic peptides that have at least min (default: 5) amino acids.'
      required nil, :maxlen, 'only retain tryptic peptides that have at most max (default: 50) amino acids.'
      required :l, :lacks, 'only retain tryptic peptides that lack all amino acids from the string of residues.'
      required :c, :contains, 'only retain tryptic peptides that contain all amino acids from the string of residues.'
      flag :h, :help, 'show help for this command' do |_value, cmd|
        puts cmd.help
        exit 0
      end
      run do |opts, _args, _cmd|
        minlen = opts.fetch(:minlen, '5').to_i
        maxlen = opts.fetch(:maxlen, '50').to_i
        lacks = opts.fetch(:lacks, '').chars.to_a
        contains = opts.fetch(:contains, '').chars.to_a
        $stdin.each_line do |pept|
          # FASTA headers
          if pept.start_with? '>'
            puts pept
            next
          end

          pept = pept.chomp
          puts pept if Peptfilter.filter(pept, minlen, maxlen, lacks, contains)
        end
      end
    end

    # Invokes the peptfilter command-line tool with the given arguments.
    #
    # @param [Array<String>] args An array of command-line arguments
    #
    # @return [void]
    def self.run(args)
      @root_command.run(args)
    end

    # Checks if a peptide satisfies the min length, max length, lacks and contains requirements.
    # Returns true if
    # - the peptide length is equal or higher than min
    # - the peptide length is equal or lower than max
    # - the peptide doesn't contain any of the amino acids in lacks
    # - the peptide contains all of the amino acids in contains
    #
    # @param [String] peptide The peptide to check
    #
    # @param [Integer] min The minimal length requirement
{'fraction_non_alphanumeric': 0.07582089552238806, 'fraction_numerical': 0.0031840796019900496, 'mean_word_length': 2.6262626262626263, 'pattern_counts': {'":': 0, '<': 9, '<?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': '6688172', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1354, 'n_words': 594}