views:

262

answers:

3

I'm accessing public mySQL database using JDBC and mySQL java connector. exonCount is int(10), exonStarts and exonEnds are longblob fields.

javaaddpath('mysql-connector-java-5.1.12-bin.jar')
host = 'genome-mysql.cse.ucsc.edu';
user = 'genome';
password = '';
dbName = 'hg18'; 
jdbcString = sprintf('jdbc:mysql://%s/%s', host, dbName);
jdbcDriver = 'com.mysql.jdbc.Driver';
dbConn = database(dbName, user , password, jdbcDriver, jdbcString);
gene.Symb = 'CDKN2B';
% Check to make sure that we successfully connected
if isconnection(dbConn)
    qry = sprintf('SELECT exonCount, exonStarts, exonEnds FROM refFlat WHERE geneName=''%s''',gene.Symb);
    result = get(fetch(exec(dbConn, qry)), 'Data');
    fprintf('Connection failed: %s\n', dbConn.Message);
end

Here is the result:

result = 
    [2]    [18x1 int8]    [18x1 int8]
    [2]    [18x1 int8]    [18x1 int8]

result{1,2}'
ans =
   50   49   57   57   50   57   48   49   44   50   49   57   57   56   54   55   51   44

This is wrong. The length of vectors in 2nd and 3rd columns should match the numbers in the 1st column.

The 1st blob, for example, should be [21992901; 21998673]. How I can convert it?


Update:

Just after submitting this question I thought it might be hex representation of a string. And it was confirmed:

>> char(result{1,2}')
ans =
21992901,21998673,

So now I need to convert all blobs hex data into numeric vectors. Still thinking to do it in a vectorized way, since number of rows can be large.

A: 

Here is what I do:

function res = blob2num(x)
res = str2double(regexp(char(x'),'[^,]+','match')');

then

exons = cellfun(@blob2num,result(:,2:3)','UniformOutput',0)
exons = 
    [2x1 double]    [2x1 double]
    [2x1 double]    [2x1 double]

Any better solution? May be on the step of retrieving data?

yuk
+2  A: 

This will convert your character data to numeric vectors for all except the first column of data in result, placing the results back into the appropriate cells:

result(:,2:end) = cellfun(@(x) str2num(char(x'))',...  %# Apply fcn to each cell
                          result(:,2:end),...          %# Input cells
                          'UniformOutput',false);      %# Output as a cell array
gnovice
Yeah, that's right! Even easier. Thanks.
yuk
@gnovice: No, neither `str2num` or `str2double` work with comma delimiters. `str2num` returns every digit as separate number, and `str2double` just ignores commas as thousand separator.
yuk
@yuk: I fixed a small bug in the code. I forgot to transpose the initial cell contents. I was treating them as 1-by-18, when they are actually 18-by-1. It should work now.
gnovice
@gnovice: Now it works. Thanks again! (what's up with me today? :)
yuk
@gnovice: sweet, I didn't know str2num could do that!
Jonas
+1  A: 

I suggest using textscan

exons = cellfun(@(x) textscan(char(x'),'%d','Delimiter',','),...
result(:,2:end),'UniformOutput',false);

To get a cell array for each of the two numbers, you can replace the format string by %d,%d and drop the Delimiter option.

Jonas
There are 2 typos: 1) `'false'` should just be `false`. 2) The input to TEXTSCAN `x` needs to first be converted to a string with `char(x')`.
gnovice
@Jonas: I just accepted gnovice's answer before seeing yours. Thanks for alternative.
yuk
@gnovice: thanks!
Jonas
@yuk: Ah, the cruel world.
Jonas