pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

www private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Bloguero_Connor on Fri 21 Mar 22:47
download | new post

  1. """This module contains code to access EZRetrieve.
  2. Functions:
  3. retrieve_single  Retrieve a single sequence from EZRetrieve.
  4. parse_single     Parse the results from EZRetrieve into a SeqRecord.
  5. """
  6.  
  7. def retrieve_single(id, from_, to, retrieve_by=None, organism=None,
  8.                     parse_results=1):
  9.     import urllib
  10.    
  11.     CGI = "http://siriusb.umdnj.edu:18080/EZRetrieve/single_r_run.jsp"
  12.     org2value = {"Hs" : "0", "Mm" : "1", "Rn" : 2}
  13.     organism = organism or "Hs"
  14.     assert organism in org2value
  15.  
  16.     acctype2value = {"genbank":0, "unigene":1, "locuslink":2, "image":3}
  17.     retrieve_by = retrieve_by or "GenBank"
  18.     retrieve_by = retrieve_by.lower()
  19.     assert retrieve_by in acctype2value
  20.  
  21.     params = {
  22.         "input" : str(id),
  23.         "from" : str(from_),
  24.         "to" : str(to),
  25.         "org" : org2value[organism],
  26.         "AccType" : acctype2value[retrieve_by],
  27.         }
  28.     options = urllib.urlencode(params)
  29.     handle = urllib.urlopen(CGI, options)
  30.     if parse_results:
  31.         results = parse_single(handle)
  32.     else:
  33.         results = handle.read()
  34.     return results
  35.  
  36. def parse_single(handle):
  37.     """Return a SeqRecord object for the sequence.  May raise an
  38.     AssertionError if there was a problem retrieving the sequence.
  39.     """
  40.     import re
  41.     from Bio.Seq import Seq
  42.     from Bio.SeqRecord import SeqRecord
  43.     results = handle.read()
  44.     lresults = results.lower()
  45.    
  46.     i = results.find("Error: ")
  47.     if i >= 0:
  48.         j = lresults.index("<br>", i)
  49.         errmsg = results[i:j].strip()
  50.         raise AssertionError, errmsg
  51.  
  52.     i = lresults.find("<b>>")
  53.     assert i >= 0, "Couldn't find sequence."
  54.     j = lresults.find("<br><br>", i)
  55.     seqdata = results[i:j]
  56.     reobj = re.compile(r"<[^>]*>", re.IGNORECASE|re.DOTALL)
  57.     seqdata = reobj.sub("", seqdata)
  58.     seqdata = re.sub(r"\s+", r"\n", seqdata)
  59.     seqdata = seqdata.strip() + "\n"
  60.     seqtitle = seqdata[1:seqdata.index('\n')]
  61.     seqdata = seqdata[seqdata.index('\n'):].replace('\n','')
  62.     seq=Seq(seqdata)
  63.     seq=SeqRecord(seq, id=seqtitle, description='')
  64.     return seq

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me