Add sqlite datatype and corresponding dataprovider

This commit is contained in:
Ira Cooke
2014-07-07 11:16:09 +10:00
parent e16b5a9b27
commit 834a586d3f
3 changed files with 72 additions and 0 deletions
+2
View File
@@ -177,6 +177,7 @@
<datatype extension="taxonomy" type="galaxy.datatypes.tabular:Taxonomy" display_in_upload="true"/>
<datatype extension="tabular" type="galaxy.datatypes.tabular:Tabular" display_in_upload="true" description="Any data in tab delimited format (tabular)." description_url="https://wiki.galaxyproject.org/Learn/Datatypes#Tabular_.28tab_delimited.29"/>
<datatype extension="twobit" type="galaxy.datatypes.binary:TwoBit" mimetype="application/octet-stream" display_in_upload="true"/>
<datatype extension="sqlite" type="galaxy.datatypes.binary:SQlite" mimetype="application/octet-stream" display_in_upload="true"/>
<datatype extension="txt" type="galaxy.datatypes.data:Text" display_in_upload="true" description="Any text file." description_url="https://wiki.galaxyproject.org/Learn/Datatypes#Plain_text"/>
<datatype extension="linecount" type="galaxy.datatypes.data:LineCount" display_in_upload="false"/>
<datatype extension="memexml" type="galaxy.datatypes.xml:MEMEXml" mimetype="application/xml" display_in_upload="true"/>
@@ -262,6 +263,7 @@
-->
<sniffer type="galaxy.datatypes.tabular:Vcf"/>
<sniffer type="galaxy.datatypes.binary:TwoBit"/>
<sniffer type="galaxy.datatypes.binary:SQlite"/>
<sniffer type="galaxy.datatypes.binary:Bam"/>
<sniffer type="galaxy.datatypes.binary:Sff"/>
<sniffer type="galaxy.datatypes.xml:Phyloxml"/>
+43
View File
@@ -12,6 +12,7 @@ import struct
import subprocess
import tempfile
import zipfile
import sqlite3
from urllib import urlencode, quote_plus
from galaxy import eggs
@@ -545,3 +546,45 @@ class TwoBit (Binary):
return "Binary TwoBit format nucleotide file (%s)" % (data.nice_size(dataset.get_size()))
Binary.register_sniffable_binary_format("twobit", "twobit", TwoBit)
@dataproviders.decorators.has_dataproviders
class SQlite ( Binary ):
file_ext = "sqlite"
# Connects and runs a query that should work on any real database
# If the file is not sqlite, an exception will be thrown and the sniffer will return false
def sniff( self, filename ):
try:
conn = sqlite3.connect(filename)
schema_version=conn.cursor().execute("pragma schema_version").fetchone()
conn.close()
if schema_version is not None:
return True
return False
except:
return False
def set_peek( self, dataset, is_multi_byte=False ):
if not dataset.dataset.purged:
dataset.peek = "SQLite Database"
dataset.blurb = data.nice_size( dataset.get_size() )
else:
dataset.peek = 'file does not exist'
dataset.blurb = 'file purged from disk'
def display_peek( self, dataset ):
try:
return dataset.peek
except:
return "SQLite Database (%s)" % ( data.nice_size( dataset.get_size() ) )
@dataproviders.decorators.dataprovider_factory( 'sqlite', dataproviders.dataset.SQliteDataProvider.settings )
def sqlite_dataprovider( self, dataset, **settings ):
dataset_source = dataproviders.dataset.DatasetDataProvider( dataset )
return dataproviders.dataset.SQliteDataProvider( dataset_source, **settings )
Binary.register_sniffable_binary_format("sqlite","sqlite",SQlite)
@@ -11,6 +11,7 @@ import base
import line
import column
import external
import sqlite3
from galaxy import eggs
eggs.require( 'bx-python' )
@@ -700,3 +701,29 @@ class BGzipTabixDataProvider( base.DataProvider ):
#TODO: as samtools - need more info on output format
raise NotImplementedError()
super( BGzipTabixDataProvider, self ).__init__( dataset, **kwargs )
class SQliteDataProvider ( base.DataProvider ):
"""
Data provider that uses a sqlite database file as its source.
Allows any query to be run and returns the resulting rows as sqlite3 row objects
"""
settings = {
'query' : 'str'
}
def __init__( self, source, query=None, **kwargs ):
self.query=query
self.connection = sqlite3.connect(source.dataset.file_name);
self.connection.row_factory = sqlite3.Row
super( SQliteDataProvider, self ).__init__( source, **kwargs )
def __iter__( self ):
if self.query is not None:
for row in self.connection.cursor().execute(self.query):
yield row
else:
yield