#!/usr/bin/env python
#
# sprout - quick IRE sequence searcher
# Copyright (C) 2008-2012 Kai Blin <kai.blin@biotech.uni-tuebingen.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
"""sprout - quick IRE sequence searcher

Search for IRE sequences by sequence pattern and folding.
"""

import argparse
import logging
from helperlibs.bio import seqio
from ire import process_sequence, filter_fold

def main():
    parser = argparse.ArgumentParser(
                description='Quickly search for IRE sequences')
    parser.add_argument('-v', '--verbose', dest="verbose",
                        action='store_true', default=False,
                        help="print diagnostic messages while running")
    parser.add_argument('infile', metavar="<sequence file>",
                        help="sequence file to operate on (FASTA/GenBank/EMBL)")
    parser.add_argument('-e', '--extract', dest="extract",
                        action='store_true', default=False,
                        help="extract a 50-base RNA stretch for ordering")
    parser.add_argument('-f', '--forward-only', dest="forward_only",
                        action='store_true', default=False,
                        help="only consider forward matches")
    args = parser.parse_args()

    if args.verbose:
        logging.basicConfig(format='%(levelname)s: %(message)s',
                            level=logging.INFO)

    with open(args.infile) as handle:
        seq_iterator = seqio.parse(handle)
        seq_i = seq_iterator.next()
        seq = seq_i.seq.transcribe()

    matches = process_sequence(seq, args.forward_only)
    matches = filter_fold(matches)

    for match in matches:
        if args.extract:
            print ">(%s:%s)" % (match.start(), match.end())
            print seq[match.start()-16:match.end()+23]
        else:
            print match

if __name__ == "__main__":
    main()
