def generic_run(commandline): """Run an application with the given commandline (OBSOLETE). This expects a pre-built commandline that derives from AbstractCommandline, and returns a ApplicationResult object to get results from a program, along with handles of the standard output and standard error. WARNING - This will read in the full program output into memory! This may be in issue when the program writes a large amount of data to standard output. NOTE - This function is considered to be obsolete, and we intend to deprecate it and then remove it in future releases of Biopython. We now recommend you invoke subprocess directly, using str(commandline) to turn an AbstractCommandline wrapper into a command line string. This will give you full control of the tool's input and output as well. """ #We don't need to supply any piped input, but we setup the #standard input pipe anyway as a work around for a python #bug if this is called from a Windows GUI program. For #details, see http://bugs.python.org/issue1124861 child = subprocess.Popen(str(commandline), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=(sys.platform != "win32")) #Use .communicate as might get deadlocks with .wait(), see Bug 2804/2806 r_out, e_out = child.communicate() # capture error code: error_code = child.returncode return ApplicationResult(commandline, error_code), \ File.UndoHandle(StringIO.StringIO(r_out)), \ File.UndoHandle(StringIO.StringIO(e_out))
def feed(self, handle, consumer): """feed(self, handle, consumer) Feed in Prosite data for scanning. handle is a file-like object that contains prosite data. consumer is a Consumer object that will receive events as the report is scanned. """ if isinstance(handle, File.UndoHandle): uhandle = handle else: uhandle = File.UndoHandle(handle) consumer.finished = False while not consumer.finished: line = uhandle.peekline() if not line: break elif is_blank_line(line): # Skip blank lines between records uhandle.readline() continue elif line[:2] == 'ID': self._scan_record(uhandle, consumer) elif line[:2] == 'CC': self._scan_copyrights(uhandle, consumer) else: raise ValueError("There doesn't appear to be a record")
def parse(self, handle): if isinstance(handle, File.UndoHandle): uhandle = handle else: uhandle = File.UndoHandle(handle) self._scanner.feed(uhandle, self._consumer) return self._consumer.enzyme_record
def getRemotePDBHandle(self, id, rcsb_url=settings.rcsb_url): """ Get the coordinate file remotely from the RCSB. @param id: pdb code, 4 characters @type id: str @param rcsb_url: template url for pdb download (default: L{settings.rcsb_url}) @type rcsb_url: str @return: the requested pdb file as a file handle @rtype: open file handle @raise PDBParserError: if couldn't retrieve PDB file """ try: from Bio import File except: raise PDBParserError('Could not find Biopython - ' + \ 'remote fetching of PDBs is not supported.') handle = urllib.urlopen(rcsb_url % (id, id)) uhandle = File.UndoHandle(handle) if not uhandle.peekline(): raise PDBParserError("Couldn't retrieve ", rcsb_url) return uhandle
def getRemotePDBHandle(self, pdb_id, rcsb_url=settings.rcsb_url): """ Get the coordinate file remotely from the RCSB. :param id: pdb code, 4 characters :type id: str :param rcsb_url: template url for pdb download (default: :class:`settings.rcsb_url`) :type rcsb_url: str :return: the requested pdb file as a file handle :rtype: open file handle :raise PDBParserError: if couldn't retrieve PDB file """ try: from Bio import File except: raise PDBParserError('Could not find Biopython - ' + \ 'remote fetching of PDBs is not supported.') resource = urllib.request.urlopen(rcsb_url % pdb_id) self.encoding = resource.headers.get_content_charset() uhandle = File.UndoHandle(resource) if not uhandle.peekline(): raise PDBParserError("Couldn't retrieve ", rcsb_url) return uhandle
def getRemotePDBHandle(self, id): handle = urllib.request.urlopen(rcsb_url % (id)) uhandle = File.UndoHandle(handle) if not uhandle.peekline(): raise BaseException("Couldn't retrieve ", rcsb_url) return uhandle
def _open(cgi, params={}, get=1): """_open(cgi, params={}, get=1) -> UndoHandle Open a handle to SCOP. cgi is the URL for the cgi script to access. params is a dictionary with the options to pass to it. get is a boolean that describes whether a GET should be used. Does some simple error checking, and will raise an IOError if it encounters one. """ import urllib from Bio import File # Open a handle to SCOP. options = urllib.urlencode(params) if get: # do a GET fullcgi = cgi if options: fullcgi = "%s?%s" % (cgi, options) handle = urllib.urlopen(fullcgi) else: # do a POST handle = urllib.urlopen(cgi, options) # Wrap the handle inside an UndoHandle. uhandle = File.UndoHandle(handle) # Should I check for 404? timeout? etc? return uhandle
def __init__(self, handle): import warnings warnings.warn( "Bio.Compass.Iterator is deprecated; please use the parse() function in this module instead", Bio.BiopythonDeprecationWarning) self._uhandle = File.UndoHandle(handle) self._parser = RecordParser()
def test_safe_readline(self): data = """\ This file""" h = File.UndoHandle(StringIO(data)) safe_readline = ParserSupport.safe_readline self.assertEqual(safe_readline(h), "This\n") self.assertEqual(safe_readline(h), "file") self.assertRaises(ValueError, safe_readline, h)
def feed(self, handle, consumer): if isinstance(handle, File.UndoHandle): uhandle = handle else: uhandle = File.UndoHandle(handle) self._scan_header(uhandle, consumer) self._scan_matches(uhandle, consumer) self._scan_annotated_matches(uhandle, consumer)
def __init__(self, handle, parser=None): """Create a new iterator. Create a new iterator. handle is a file-like object. parser is an optional Parser object to change the results into another form. If set to None, then the raw contents of the file will be returned. """ import warnings warnings.warn("Bio.Sequencing.Phd.Iterator is deprecated. Please use Bio.Sequencing.Phd.parse(handle) instead of Bio.Sequencing.Phd.Iterator(handle, RecordParser())", DeprecationWarning) self._uhandle = File.UndoHandle(handle) self._parser = parser
def __init__(self, handle, parser=None): """Initialize the iterator. Arguments: o handle - A handle with IntelliGenetics entries to iterate through. o parser - An optional parser to pass the entries through before returning them. If None, then the raw entry will be returned. """ self.handle = File.UndoHandle(handle) self._reader = IntelliGeneticsReader(self.handle) self._parser = parser
def __init__(self, handle, parser=None): """Initialize the iterator. Arguments: o handle - A handle with NBRF entries to iterate through. o parser - An optional parser to pass the entries through before returning them. If None, then the raw entry will be returned. """ self.handle = File.UndoHandle(handle) self._reader = RecordReader.StartsWith(self.handle, ">") self._parser = parser
def __init__(self, handle, parser=None): """__init__(self, handle, parser=None) Create a new iterator. handle is a file-like object. parser is an optional Parser object to change the results into another form. If set to None, then the raw contents of the file will be returned. """ if type(handle) is not FileType and type(handle) is not InstanceType: raise ValueError("I expected a file handle or file-like object") self._uhandle = File.UndoHandle(handle) self._parser = parser
def test_safe_peekline(self): safe_peekline = ParserSupport.safe_peekline data = """\ This file""" h = File.UndoHandle(StringIO(data)) self.assertEqual(safe_peekline(h), "This\n") h.readline() self.assertEqual(safe_peekline(h), "file") h.readline() self.assertRaises(ValueError, safe_peekline, h) h.saveline("hello") self.assertEqual(safe_peekline(h), "hello")
def feed(self, handle): """feed(self, handle ) Feed in data for scanning. handle is a file-like object containing html. """ if isinstance(handle, File.UndoHandle): uhandle = handle else: uhandle = File.UndoHandle(handle) text = uhandle.read() sgmllib.SGMLParser.feed(self, text)
def feed(self, handle, consumer): """feed(self, handle, consumer) Feed in SwissProt data for scanning. handle is a file-like object that contains swissprot data. consumer is a Consumer object that will receive events as the report is scanned. """ if isinstance(handle, File.UndoHandle): uhandle = handle else: uhandle = File.UndoHandle(handle) self._scan_record(uhandle, consumer)
def test_undohandle_read_block(self): for block in [1, 2, 10]: s = StringIO(data) h = File.UndoHandle(s) h.peekline() new = "" while True: tmp = h.read(block) if not tmp: break new += tmp self.assertEqual(data, new) h.close()
def feed(self, handle, consumer): """ Feeds in MEME output for scanning. handle should implement the readline method. consumer is a Consumer object that can receive the salient events. """ if isinstance(handle, File.UndoHandle): uhandle = handle else: uhandle = File.UndoHandle(handle) self._scan_header(uhandle, consumer) self._scan_motifs(uhandle, consumer)
def feed(self, handle, consumer): """Feed in COMPASS ouput""" if isinstance(handle, File.UndoHandle): pass else: handle = File.UndoHandle(handle) assert isinstance(handle, File.UndoHandle), \ "handle must be an UndoHandle" if handle.peekline(): self._scan_record(handle, consumer)
def feed(self, handle, consumer): """S.feed(handle, consumer) Feed in a BLAST report for scanning. handle is a file-like object that contains the BLAST report. consumer is a Consumer object that will receive events as the report is scanned. """ from Bio import File # This stuff appears in 2.0.12. # <p><!-- # QBlastInfoBegin # Status=READY # QBlastInfoEnd # --><p> # <HTML> # <HEAD> # <TITLE>BLAST Search Results </TITLE> # </HEAD> # <BODY BGCOLOR="#FFFFFF" LINK="#0000FF" VLINK="#660099" ALINK="#660099 # <A HREF="http://www.ncbi.nlm.nih.gov/BLAST/blast_form.map"> <IMG SRC= # <BR><BR><PRE> # BLAST Formatted information # # </BODY> # </HTML> # </BODY> # </HTML> if isinstance(handle, File.UndoHandle): uhandle = handle else: uhandle = File.UndoHandle(handle) # Read HTML formatting up to the "BLAST" version line. read_and_call_until(uhandle, consumer.noevent, has_re=re.compile(r'<b>.?BLAST')) self._scan_header(uhandle, consumer) self._scan_rounds(uhandle, consumer) self._scan_database_report(uhandle, consumer) self._scan_parameters(uhandle, consumer) # Read HTML footer information. while uhandle.peekline(): read_and_call(uhandle, consumer.noevent)
def test_attempt_read_and_call(self): data = """\ >gi|132871|sp|P19947|RL30_BACSU 50S RIBOSOMAL PROTEIN L30 (BL27) MAKLEITLKRSVIGRPEDQRVTVRTLGLKKTNQTVVHEDNAAIRGMINKVSHLVSVKEQ >gi|132679|sp|P19946|RL15_BACSU 50S RIBOSOMAL PROTEIN L15 MKLHELKPSEGSRKTRNRVGRGIGSGNGKTAGKGHKGQNARSGGGVRPGFEGGQMPLFQRLPKRGFTNIN RKEYAVVNLDKLNGFAEGTEVTPELLLETGVISKLNAGVKILGNGKLEKKLTVKANKFSASAKEAVEAAG GTAEVI""" h = File.UndoHandle(StringIO(data)) lines = [] arac = ParserSupport.attempt_read_and_call self.assertTrue(arac(h, lines.append, contains="RIBOSOMAL PROTEIN")) self.assertFalse(arac(h, lines.append, start="foobar")) self.assertFalse(arac(h, lines.append, blank=1)) self.assertTrue(arac(h, lines.append, end="LVSVKEQ"))
def feed(self, handle, consumer): """feed(self, handle, consumer) Feed in Enzyme data for scanning. handle is a file-like object that contains keyword information. consumer is a Consumer object that will receive events as the report is scanned. """ if isinstance(handle, File.UndoHandle): uhandle = handle else: uhandle = File.UndoHandle(handle) while not is_blank_line(uhandle.peekline()): # Am I done yet? self._scan_record(uhandle, consumer)
def __init__(self, handle, parser=None): """__init__(self, handle, parser=None) Create a new iterator. handle is a file-like object. parser is an optional Parser object to change the results into another form. If set to None, then the raw contents of the file will be returned. """ import warnings warnings.warn("Bio.Prosite.Iterator is deprecated; we recommend using the function Bio.Prosite.parse instead. Please contact the Biopython developers at [email protected] you cannot use Bio.Prosite.parse instead of Bio.Prosite.Iterator.", DeprecationWarning) if type(handle) is not FileType and type(handle) is not InstanceType: raise ValueError("I expected a file handle or file-like object") self._uhandle = File.UndoHandle(handle) self._parser = parser
def feed(self, handle, consumer): """feed(self, handle, consumer) Feed in the keywlist.txt file for scanning. handle is a file-like object that contains keyword information. consumer is a Consumer object that will receive events as the report is scanned. """ if isinstance(handle, File.UndoHandle): uhandle = handle else: uhandle = File.UndoHandle(handle) self._scan_header(uhandle, consumer) self._scan_keywords(uhandle, consumer) self._scan_footer(uhandle, consumer)
def feed(self, handle, consumer): """feed(self, handle, consumer) Feed in a Medline unit record for scanning. handle is a file-like object that contains a Medline record. consumer is a Consumer object that will receive events as the report is scanned. """ if isinstance(handle, File.UndoHandle): uhandle = handle else: uhandle = File.UndoHandle(handle) # Read the Entrez header information, if it exists if attempt_read_and_call(uhandle, consumer.noevent, start='Entrez'): read_and_call(uhandle, consumer.noevent, start='----------------') self._scan_record(uhandle, consumer)
def __init__(self, handle, parser=None): """__init__(self, handle, parser=None) Create a new iterator. handle is a file-like object. parser is an optional Parser object to change the results into another form. If set to None, then the raw contents of the file will be returned. """ import warnings warnings.warn( "Bio.SwissProt.SProt.Iterator is deprecated. Please use the function Bio.SwissProt.parse instead if you want to get a SwissProt.SProt.Record, or Bio.SeqIO.parse if you want to get a SeqRecord. If these solutions do not work for you, please get in contact with the Biopython developers ([email protected]).", DeprecationWarning) if type(handle) is not FileType and type(handle) is not InstanceType: raise ValueError("I expected a file handle or file-like object") self._uhandle = File.UndoHandle(handle) self._parser = parser
def test_read_and_call(self): data = """\ >gi|132871|sp|P19947|RL30_BACSU 50S RIBOSOMAL PROTEIN L30 (BL27) MAKLEITLKRSVIGRPEDQRVTVRTLGLKKTNQTVVHEDNAAIRGMINKVSHLVSVKEQ >gi|132679|sp|P19946|RL15_BACSU 50S RIBOSOMAL PROTEIN L15 MKLHELKPSEGSRKTRNRVGRGIGSGNGKTAGKGHKGQNARSGGGVRPGFEGGQMPLFQRLPKRGFTNIN RKEYAVVNLDKLNGFAEGTEVTPELLLETGVISKLNAGVKILGNGKLEKKLTVKANKFSASAKEAVEAAG GTAEVI """ h = File.UndoHandle(StringIO(data)) lines = [] rac = ParserSupport.read_and_call rac(h, lines.append) self.assertEqual(lines[-1][:10], ">gi|132871") rac(h, lines.append, start="MAKLE", end="KEQ", contains="SVIG") self.assertRaises(ValueError, rac, h, lines.append, blank=1) self.assertRaises(ValueError, rac, h, lines.append, start="foobar") self.assertRaises(ValueError, rac, h, lines.append, end="foobar") self.assertRaises(ValueError, rac, h, lines.append, contains="foobar") self.assertRaises(ValueError, rac, h, lines.append, blank=0)
def feed(self, handle, consumer): """feed(self, handle, consumer) Feed in Prodoc data for scanning. handle is a file-like object that contains prosite data. consumer is a Consumer object that will receive events as the report is scanned. """ if isinstance(handle, File.UndoHandle): uhandle = handle else: uhandle = File.UndoHandle(handle) while 1: line = uhandle.peekline() if not line: break elif is_blank_line(line): # Skip blank lines between records uhandle.readline() continue else: self._scan_record(uhandle, consumer)
def test_one(self): h = File.UndoHandle(StringIO(data)) self.assertEqual(h.readline(), "This\n") self.assertEqual(h.peekline(), "is\n") self.assertEqual(h.readline(), "is\n") # TODO - Meaning of saveline lacking \n? h.saveline("saved\n") self.assertEqual(h.peekline(), "saved\n") h.saveline("another\n") self.assertEqual(h.readline(), "another\n") self.assertEqual(h.readline(), "saved\n") # Test readlines after saveline h.saveline("saved again\n") lines = h.readlines() self.assertEqual(len(lines), 3) self.assertEqual(lines[0], "saved again\n") self.assertEqual(lines[1], "a multi-line\n") self.assertEqual(lines[2], "file") # no trailing \n # should be empty now self.assertEqual(h.readline(), "") h.saveline("save after empty\n") self.assertEqual(h.readline(), "save after empty\n") self.assertEqual(h.readline(), "")