
function T=myread_bowtie(fname)
% leaner than samread, 20x faster and ~10x less ram?
%%
fname="results_bowtie.txt";
%tic

%c=fileread(fname);
fid=fopen(fname);

c=fread(fid)





%%
q=regexp(c,'(?<=[\n|^])[\w]*\t[\w]*\t[\w]*','match');
q1=regexp(q,'\t','split')';
q1=vertcat(q1{:});
nm1=regexp(c,'(?<=NM:i:)\w*','match')';
nm1=uint8(double(string(nm1)));







fclose(fid);
T=struct('QueryName',q1(:,1), ...
    'Flag',num2cell(uint8(double(string(q1(:,2))))), ...
    'ReferenceName',q1(:,3), ...
    'NM',num2cell(nm1));

% 
% T.QueryName=q1(:,1);
% T.ReferenceName=q1(:,3);
% T.Flag=uint8(double(string(q1(:,2))));
% T.NM=nm1;
%toc
%toc
% 
% %%
% tic
% 
% 
% queryname=regexp(c,'(?<=\n)[^@][\w]*(?=\t)','match')';
% refname=regexp(c(1:100000),'(?<=\n[^@][\w]*\t[\w]*\t)[\w]*','match')';
% %flagval=
% 
% toc
% %%
% clear expr c
% %toc
% %matches=repmat(matches,10,1);
% % parse sam file
% 
% %tic
% tab=char(9);
% matches1=regexp(matches,tab,'split');
% nm1=regexp(matches,'(?<=NM:i:)\w*','match');
% nm1=uint8(double(string(nm1)));
% clear matches
% %toc
% %tic
% 
% flagval=zeros(numel(matches1),1);
% queryname=cell(numel(matches1),1);
% refname=queryname;
% 
% for n=1:numel(matches1) % this is faster than cellfun
%     queryname{n}=matches1{n}{1};
%     refname{n}=matches1{n}{3};
%     flagval(n)=uint16(double(string(matches1{n}{2})));
% end
% T=table;
% T.QueryName=queryname;
% T.ReferenceName=refname;
% T.Flag=flagval;
% T.NM=nm1;
% toc
% 
% 
% 
% 
end

