# Path: inmembrane/helpers.py
# def log_stderr(s, width=76, comment=True):
#     """
#     Wrapper for all stderr out. Allows future customization.
#     """
#     if LOG_SILENT:
#         return
#     if s and s[-1] != "\n":
#         s += "\n"
#     if not s.startswith("#"):
#         s = "# " + s
#     sys.stderr.write(s)
# 
# def parse_fasta_header(header):
#     """
#     Parses a FASTA format header (with our without the initial '>') and returns a
#     tuple of sequence id and sequence name/description.
# 
#     If NCBI SeqID format (gi|gi-number|gb|accession etc, is detected
#     the first id in the list is used as the canonical id (see see
#     http://www.ncbi.nlm.nih.gov/books/NBK21097/#A631 ).
#     """
#     if header[0] == '>':
#         header = header[1:]
#     tokens = header.split('|')
#     # check to see if we have an NCBI-style header
#     if header.find("|") != -1 and len(tokens[0]) <= 3:
#         # "gi|ginumber|gb|accession bla bla" becomes "gi|ginumber"
#         seqid = "%s|%s" % (tokens[0], tokens[1].split()[0])
#         name = tokens[-1:][0].strip()
#     # otherwise just split on spaces & hope for the best
#     else:
#         tokens = header.split()
#         seqid = tokens[0]
#         name = header[0:-1].strip()
# 
#     return seqid, name


import sys, os, time, StringIO
import twill
import inmembrane
from twill.commands import find, formfile, follow, fv, go, show, \
    showforms, showlinks, submit, agent
from BeautifulSoup import BeautifulSoup
from inmembrane.helpers import log_stderr, parse_fasta_header
# -*- coding: utf-8 -*-
__DEBUG__ = False

citation = {'ref': u"﻿Berven FS, Flikka K, Jensen HB, Eidhammer I (2004) "
                   u"BOMP: a program to predict integral beta-barrel outer "
                   u"membrane proteins encoded within genomes of Gram-negative "
                   u"bacteria. Nucleic acids research 32: W394-9. \n"
                   u"<http://dx.crossref.org/10.1093/nar/gkh351>",
            'name': "BOMP"
            }





def annotate(params, proteins, \
             url="http://services.cbu.uib.no/tools/bomp/", force=False):
    """
    Uses the BOMP web service (http://services.cbu.uib.no/tools/bomp/) to
    predict if proteins are outer membrane beta-barrels.
    """
    # set the user-agent so web services can block us if they want ... :/
    python_version = sys.version.split()[0]
    agent("Python-urllib/%s (twill; inmembrane/%s)" %
          (python_version, inmembrane.__version__))

    bomp_out = 'bomp.out'

# Path: inmembrane/helpers.py
# def log_stderr(s, width=76, comment=True):
#     """
#     Wrapper for all stderr out. Allows future customization.
#     """
#     if LOG_SILENT:
#         return
#     if s and s[-1] != "\n":
#         s += "\n"
#     if not s.startswith("#"):
#         s = "# " + s
#     sys.stderr.write(s)
# 
# def parse_fasta_header(header):
#     """
#     Parses a FASTA format header (with our without the initial '>') and returns a
#     tuple of sequence id and sequence name/description.
# 
#     If NCBI SeqID format (gi|gi-number|gb|accession etc, is detected
#     the first id in the list is used as the canonical id (see see
#     http://www.ncbi.nlm.nih.gov/books/NBK21097/#A631 ).
#     """
#     if header[0] == '>':
#         header = header[1:]
#     tokens = header.split('|')
#     # check to see if we have an NCBI-style header
#     if header.find("|") != -1 and len(tokens[0]) <= 3:
#         # "gi|ginumber|gb|accession bla bla" becomes "gi|ginumber"
#         seqid = "%s|%s" % (tokens[0], tokens[1].split()[0])
#         name = tokens[-1:][0].strip()
#     # otherwise just split on spaces & hope for the best
#     else:
#         tokens = header.split()
#         seqid = tokens[0]
#         name = header[0:-1].strip()
# 
#     return seqid, name


# Path: inmembrane/plugins/bomp_web.py
import sys, os, time, StringIO
import twill
import inmembrane
from twill.commands import find, formfile, follow, fv, go, show, \
    showforms, showlinks, submit, agent
from BeautifulSoup import BeautifulSoup
from inmembrane.helpers import log_stderr, parse_fasta_header

# -*- coding: utf-8 -*-
__DEBUG__ = False

citation = {'ref': u"﻿Berven FS, Flikka K, Jensen HB, Eidhammer I (2004) "
                   u"BOMP: a program to predict integral beta-barrel outer "
                   u"membrane proteins encoded within genomes of Gram-negative "
                   u"bacteria. Nucleic acids research 32: W394-9. \n"
                   u"<http://dx.crossref.org/10.1093/nar/gkh351>",
            'name': "BOMP"
            }





def annotate(params, proteins, \
             url="http://services.cbu.uib.no/tools/bomp/", force=False):
    """
    Uses the BOMP web service (http://services.cbu.uib.no/tools/bomp/) to
    predict if proteins are outer membrane beta-barrels.
    """
    # set the user-agent so web services can block us if they want ... :/
    python_version = sys.version.split()[0]
    agent("Python-urllib/%s (twill; inmembrane/%s)" %
          (python_version, inmembrane.__version__))

    bomp_out = 'bomp.out'

    log_stderr("# BOMP(web) %s > %s" % (params['fasta'], bomp_out))