##
## MISO into SQL database interface
##
import os
import sys
import time
import zipfile
import sqlite3
import shutil
import fnmatch
import glob
import StringIO
import misopy
import misopy.misc_utils as misc_utils
import misopy.miso_utils as miso_utils
# File extension for MISO SQLite databases
MISO_DB_EXT = ".miso_db"
class MISODatabase:
"""
Representation of a MISO SQLite database.
"""
def __init__(self, db_fname, comp_to_uncomp=None):
self.comp_to_uncomp = comp_to_uncomp
# Get mapping of uncompressed to compressed
# event IDs
self.uncomp_to_comp = None
if self.comp_to_uncomp is not None:
# Make mapping from uncompressed to compressed IDs
self.uncomp_to_comp = misc_utils.inv_dict(self.comp_to_uncomp)
if not os.path.isfile(db_fname):
raise Exception, "%s does not exist." %(db_fname)
self.db_fname = db_fname
# Create table names that start with 'table_' to properly handle
# Ensembl headers, which are numeric only and tables cannot
# be named numerically.
self.table_name = "table_%s" %(get_table_name_from_file(self.db_fname))
if self.table_name is None:
print "Error: Cannot retrieve name of MISO db file %s" \
%(self.db_fname)
return None
self.conn = sqlite3.connect(self.db_fname)
# Determine event name format
self.is_db_events_compressed = self.is_event_name_compressed()
def is_event_name_compressed(self):
"""
Determine if the events in the database are compressed
or not.
"""
c = self.conn.cursor()
results = \
c.execute("SELECT * from %s" %(self.table_name