# Path: inmembrane/plugins/lipop1.py
# def parse_lipop(text, proteins, id_mapping=None):
#     """
#     Parses the text output of the LipoP program and returns a 'proteins'
#     datastructure with annotations.
# 
#     The parser can also that the HTML returned by the LipoP web interface.
#     If a dictionary of {safe_seqid : seqid} mappings is given, the parser
#     will expect the input text to contain safe_seqids.
#     """
# 
#     if id_mapping is None:
#         id_mapping = []
# 
#     # initialize fields in each protein
#     for seqid in proteins:
#         proteins[seqid]['is_lipop'] = False
#         proteins[seqid]['lipop_cleave_position'] = None
# 
#     for l in text.split('\n'):
#         words = l.split()
# 
#         if 'SpII score' in l:
#             seqid = parse_fasta_header(words[1])[0]
#             if id_mapping:
#                 seqid = id_mapping[seqid]
#             if 'cleavage' in l:
#                 pair = words[5].split("=")[1]
#                 i = int(pair.split('-')[0])
#             else:
#                 i = None
#             proteins[seqid]['is_lipop'] = 'Sp' in words[2]
#             proteins[seqid]['lipop_cleave_position'] = i
# 
#         # check for an E.coli style inner membrane retention signal
#         # Asp+2 to cleavage site. There are other apparent retention
#         # signals in E. coli and other gram- bacteria in addition to
#         # the Asp+2 which we don't detect here (yet).
#         # (Yamaguchi et al, 1988; Tokuda and Matsuyama, 2005 [review])
#         if dict_get(proteins[seqid], 'lipop_cleave_position'):
#             plus_two = proteins[seqid]['lipop_cleave_position'] + 1
#             if proteins[seqid]['seq'][plus_two] == 'D':
#                 proteins[seqid]['lipop_im_retention_signal'] = True
# 
#     return proteins
# 
# 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)
# 
# Path: inmembrane/helpers.py
# def generate_safe_seqids(proteins):
#     """
#     Takes a 'proteins' dictionary, keyed by sequence id, and
#     adds a 'safe' sequence id ('safe_seqid') that is less likely
#     to be munged or break various external programs.
# 
#     Returns a tuple of the updated proteins dictionary and a dictionary
#     mapping the 'safe' sequence ids to the original ids.
#     """
#     id_mapping = {}
#     count = 0
#     for seqid in proteins:
#         safe_id = re.sub(r'[^\w]', "", seqid) + '_' + `count`
#         id_mapping[safe_id] = seqid
#         proteins[seqid]['safe_seqid'] = safe_id
#         count += 1
# 
#     return (proteins, id_mapping)
# 
# def proteins_to_fasta(proteins, seqids=[], use_safe_seqid=False, width=50):
#     """
#     Takes a proteins dictionary and returns a string containing
#     all the sequences in FASTA format. Option parameters are
#     a list of seqids to output (seqids) and the line width (width).
#     """
#     if seqids:
#         idlist = seqids
#     else:
#         idlist = proteins
# 
#     fasta_out = ""
#     for seqid in idlist:
#         seq_wrap = textwrap.fill(proteins[seqid]['seq'], width)
#         if use_safe_seqid:
#             header = proteins[seqid]['safe_seqid']
#         else:
#             header = proteins[seqid]['name']
#         fasta_out += ">%s\n%s\n" % (header, seq_wrap)
#     return fasta_out


import sys, os, time
import requests
import inmembrane
from StringIO import StringIO
from BeautifulSoup import BeautifulSoup
    from collections import OrderedDict
    from ordereddict import OrderedDict
from inmembrane.plugins.lipop1 import parse_lipop
from inmembrane.helpers import log_stderr
from inmembrane.helpers import generate_safe_seqids, proteins_to_fasta
            resultpage = requests.get(pollingurl).text
            waittime += 1;
            retries += 1
            waittime = min(waittime, 20)

        sys.stderr.write(" .. done !\n")

        if __DEBUG__:
            log_stderr(resultpage)
            # Example:
            #
            #   <pre>
            # # lcl_AE004092.1_cdsid_AAK33146.1 CYT score=-0.200913
            # # Cut-off=-3
            # lcl_AE004092.1_cdsid_AAK33146.1	LipoP1.0:Best	CYT	1	1	-0.200913
            # <P>
            # <hr>
            # # lcl_AE004092.1_cdsid_AAK33147.1 CYT score=-0.200913
            # # Cut-off=-3
            # lcl_AE004092.1_cdsid_AAK33147.1	LipoP1.0:Best	CYT	1	1	-0.200913
            # <P>
            # <hr>

        allresultpages += clean_result_page(resultpage)

    # we store the cleaned up result pages concatenated together
    fh = open(outfile, 'a+')
    fh.write(allresultpages)
    fh.close()


# Path: inmembrane/plugins/lipop1.py
# def parse_lipop(text, proteins, id_mapping=None):
#     """
#     Parses the text output of the LipoP program and returns a 'proteins'
#     datastructure with annotations.
# 
#     The parser can also that the HTML returned by the LipoP web interface.
#     If a dictionary of {safe_seqid : seqid} mappings is given, the parser
#     will expect the input text to contain safe_seqids.
#     """
# 
#     if id_mapping is None:
#         id_mapping = []
# 
#     # initialize fields in each protein
#     for seqid in proteins:
#         proteins[seqid]['is_lipop'] = False
#         proteins[seqid]['lipop_cleave_position'] = None
# 
#     for l in text.split('\n'):
#         words = l.split()
# 
#         if 'SpII score' in l:
#             seqid = parse_fasta_header(words[1])[0]
#             if id_mapping:
#                 seqid = id_mapping[seqid]
#             if 'cleavage' in l:
#                 pair = words[5].split("=")[1]
#                 i = int(pair.split('-')[0])
#             else:
#                 i = None
#             proteins[seqid]['is_lipop'] = 'Sp' in words[2]
#             proteins[seqid]['lipop_cleave_position'] = i
# 
#         # check for an E.coli style inner membrane retention signal
#         # Asp+2 to cleavage site. There are other apparent retention
#         # signals in E. coli and other gram- bacteria in addition to
#         # the Asp+2 which we don't detect here (yet).
#         # (Yamaguchi et al, 1988; Tokuda and Matsuyama, 2005 [review])
#         if dict_get(proteins[seqid], 'lipop_cleave_position'):
#             plus_two = proteins[seqid]['lipop_cleave_position'] + 1
#             if proteins[seqid]['seq'][plus_two] == 'D':
#                 proteins[seqid]['lipop_im_retention_signal'] = True
# 
#     return proteins
# 
# 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)
# 
# Path: inmembrane/helpers.py
# def generate_safe_seqids(proteins):
#     """
#     Takes a 'proteins' dictionary, keyed by sequence id, and
#     adds a 'safe' sequence id ('safe_seqid') that is less likely
#     to be munged or break various external programs.
# 
#     Returns a tuple of the updated proteins dictionary and a dictionary
#     mapping the 'safe' sequence ids to the original ids.
#     """
#     id_mapping = {}
#     count = 0
#     for seqid in proteins:
#         safe_id = re.sub(r'[^\w]', "", seqid) + '_' + `count`
#         id_mapping[safe_id] = seqid
#         proteins[seqid]['safe_seqid'] = safe_id
#         count += 1
# 
#     return (proteins, id_mapping)
# 
# def proteins_to_fasta(proteins, seqids=[], use_safe_seqid=False, width=50):
#     """
#     Takes a proteins dictionary and returns a string containing
#     all the sequences in FASTA format. Option parameters are
#     a list of seqids to output (seqids) and the line width (width).
#     """
#     if seqids:
#         idlist = seqids
#     else:
#         idlist = proteins
# 
#     fasta_out = ""
#     for seqid in idlist:
#         seq_wrap = textwrap.fill(proteins[seqid]['seq'], width)
#         if use_safe_seqid:
#             header = proteins[seqid]['safe_seqid']
#         else:
#             header = proteins[seqid]['name']
#         fasta_out += ">%s\n%s\n" % (header, seq_wrap)
#     return fasta_out


# Path: inmembrane/plugins/lipop_scrape_web.py
import sys, os, time
import requests
import inmembrane
from StringIO import StringIO
from BeautifulSoup import BeautifulSoup
    from collections import OrderedDict
    from ordereddict import OrderedDict
from inmembrane.plugins.lipop1 import parse_lipop
from inmembrane.helpers import log_stderr
from inmembrane.helpers import generate_safe_seqids, proteins_to_fasta

            resultpage = requests.get(pollingurl).text
            waittime += 1;
            retries += 1
            waittime = min(waittime, 20)

        sys.stderr.write(" .. done !\n")

        if __DEBUG__:
            log_stderr(resultpage)
            # Example:
            #
            #   <pre>
            # # lcl_AE004092.1_cdsid_AAK33146.1 CYT score=-0.200913
            # # Cut-off=-3
            # lcl_AE004092.1_cdsid_AAK33146.1	LipoP1.0:Best	CYT	1	1	-0.200913
            # <P>
            # <hr>
            # # lcl_AE004092.1_cdsid_AAK33147.1 CYT score=-0.200913
            # # Cut-off=-3
            # lcl_AE004092.1_cdsid_AAK33147.1	LipoP1.0:Best	CYT	1	1	-0.200913
            # <P>
            # <hr>

        allresultpages += clean_result_page(resultpage)

    # we store the cleaned up result pages concatenated together
    fh = open(outfile, 'a+')
    fh.write(allresultpages)
    fh.close()


    proteins = parse_lipop(allresultpages, proteins, id_mapping=id_mapping)