annotate sra.py @ 0:ffdd41766195 draft

Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
author matt-shirley <mdshw5@gmail.com>
date Tue, 27 Nov 2012 13:44:28 -0500
parents
children 28fda7631857
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
1 """
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
2 Sra class
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
3 """
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
4
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
5 import galaxy.datatypes.binary
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
6 from galaxy.datatypes.binary import Binary
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
7 import data, logging, binascii
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
8 from galaxy.datatypes.metadata import MetadataElement
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
9 from galaxy.datatypes import metadata
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
10 from galaxy.datatypes.sniff import *
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
11 from galaxy import eggs
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
12 import pkg_resources
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
13 pkg_resources.require( "bx-python" )
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
14 import os, subprocess, tempfile
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
15 import struct
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
16
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
17 class Sra( Binary ):
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
18 """ Sequence Read Archive (SRA) """
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
19 file_ext = "sra"
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
20
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
21 def __init__( self, **kwd ):
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
22 Binary.__init__( self, **kwd )
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
23 def sniff( self, filename ):
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
24 # The first 8 bytes of any NCBI sra file is 'NCIB.sra', and the file is binary. EBI and DDBJ files may differ. For details
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
25 # about the format, see http://www.ncbi.nlm.nih.gov/books/n/helpsra/SRA_Overview_BK/#SRA_Overview_BK.4_SRA_Data_Structure
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
26 try:
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
27 header = open( filename ).read(8)
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
28 if binascii.b2a_hex( header ) == binascii.hexlify( 'NCBI.sra' ):
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
29 return True
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
30 return False
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
31 except:
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
32 return False
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
33 def set_peek( self, dataset, is_multi_byte=False ):
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
34 if not dataset.dataset.purged:
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
35 dataset.peek = "Binary sra file"
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
36 dataset.blurb = data.nice_size( dataset.get_size() )
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
37 else:
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
38 dataset.peek = 'file does not exist'
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
39 dataset.blurb = 'file purged from disk'
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
40 def display_peek( self, dataset ):
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
41 try:
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
42 return dataset.peek
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
43 except:
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
44 return "Binary sra file (%s)" % ( data.nice_size( dataset.get_size() ) )
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
45
ffdd41766195 Initial version - still need to test if datatype works correctly, and implement scripted download of SRA binaries.
matt-shirley <mdshw5@gmail.com>
parents:
diff changeset
46 Binary.register_sniffable_binary_format("sra", "sra", Sra)