# 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
from twill.commands import find, formfile, follow, fv, go, show, \
    showforms, showlinks, submit, agent
from inmembrane.helpers import log_stderr, parse_fasta_header
        except:
            polltime = polltime * 2

        if polltime >= 7200:  # 2 hours
            log_stderr("# TMB-HUNT error: Taking too long.")
            return

    txt_out = show()

    # write raw TMB-HUNT results
    fh = open(out, 'w')
    fh.write(txt_out)
    fh.close()

    return parse_tmbhunt(proteins, out)


def parse_tmbhunt(proteins, out):
    """
    Takes the filename of a TMB-HUNT output file (text format)
    & parses the outer membrane beta-barrel predictions into the proteins dictionary.
    """
    # parse TMB-HUNT text output
    tmbhunt_classes = {}
    for l in open(out, 'r'):
        # inmembrane.log_stderr("# TMB-HUNT raw: " + l[:-1])
        if l[0] == ">":
            # TMB-HUNT munges FASTA ids by making them all uppercase,
            # so we find the equivalent any-case id in our proteins list
            # and use that. ugly but necessary.

# 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/tmbhunt_web.py
import sys, os, time, StringIO
import twill
from twill.commands import find, formfile, follow, fv, go, show, \
    showforms, showlinks, submit, agent
from inmembrane.helpers import log_stderr, parse_fasta_header

        except:
            polltime = polltime * 2

        if polltime >= 7200:  # 2 hours
            log_stderr("# TMB-HUNT error: Taking too long.")
            return

    txt_out = show()

    # write raw TMB-HUNT results
    fh = open(out, 'w')
    fh.write(txt_out)
    fh.close()

    return parse_tmbhunt(proteins, out)


def parse_tmbhunt(proteins, out):
    """
    Takes the filename of a TMB-HUNT output file (text format)
    & parses the outer membrane beta-barrel predictions into the proteins dictionary.
    """
    # parse TMB-HUNT text output
    tmbhunt_classes = {}
    for l in open(out, 'r'):
        # inmembrane.log_stderr("# TMB-HUNT raw: " + l[:-1])
        if l[0] == ">":
            # TMB-HUNT munges FASTA ids by making them all uppercase,
            # so we find the equivalent any-case id in our proteins list
            # and use that. ugly but necessary.

            seqid, desc = parse_fasta_header(l)