Skip to content
Snippets Groups Projects
Commit c55aeeb5 authored by tjc's avatar tjc
Browse files

implement next()

git-svn-id: svn+ssh://svn.internal.sanger.ac.uk/repos/svn/pathsoft/artemis/trunk@15158 ee4ac58c-ac51-4696-9907-e4b3aa274f04
parent 61fd7d31
No related branches found
No related tags found
No related merge requests found
/*
* created: 2010
*
* This file is part of Artemis
*
* Copyright(C) 2010 Genome Research Limited
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or(at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package uk.ac.sanger.artemis.components.variant; package uk.ac.sanger.artemis.components.variant;
import java.io.File; import java.io.File;
...@@ -16,60 +39,91 @@ import net.sf.samtools.util.BlockCompressedInputStream; ...@@ -16,60 +39,91 @@ import net.sf.samtools.util.BlockCompressedInputStream;
class BCFReader class BCFReader
{ {
public static final int TAD_LIDX_SHIFT = 13; // linear index shift public static final int TAD_LIDX_SHIFT = 13; // linear index shift
private static Pattern formatPattern = Pattern.compile("[^0-9]+"); private static Pattern formatPattern = Pattern.compile("[^0-9]+");
private BlockCompressedInputStream is;
public void query(File bcf, long offset, int beg, int end) throws IOException public void query(File bcf, long offset) throws IOException
{ {
BlockCompressedInputStream is = new BlockCompressedInputStream(bcf); is = new BlockCompressedInputStream(bcf);
is.seek(offset); is.seek(offset);
}
for(int i=0; i<7299; i++)
public VCFRecord next(int bid, int beg, int end) throws IOException
{
try
{ {
int id = readInt(is); VCFRecord bcfRecord = readVCFRecord();
int pos = readInt(is)+1; if(bcfRecord.pos >= beg && bcfRecord.pos <= end)
float qual = readFloat(is); return bcfRecord;
int slen = readInt(is); else if(bcfRecord.pos < beg)
System.out.print("ID: "+id + " POS: "+pos + " QUAL: "+qual + " "); {
byte[] str = new byte[slen]; while( (bcfRecord = readVCFRecord()).pos <= beg )
is.read(str); {
if(bcfRecord.pos >= beg && bcfRecord.pos <= end)
return bcfRecord;
}
}
}
catch(Exception e)
{
if(is.read() != -1) // eof
e.printStackTrace();
}
return null;
}
private VCFRecord readVCFRecord() throws IOException
{
VCFRecord bcfRecord = new VCFRecord();
bcfRecord.seqID = readInt(is);
bcfRecord.pos = readInt(is)+1;
bcfRecord.quality = readFloat(is);
int slen = readInt(is);
byte[] str = new byte[slen];
is.read(str);
String parts[] = getParts(str); String parts[] = getParts(str);
bcfRecord.ref = parts[0];
bcfRecord.alt = parts[1];
String fmt = parts[parts.length-1];
if(formatPattern.matcher(fmt).matches())
{
bcfRecord.info = parts[parts.length-2];
bcfRecord.format = parts[parts.length-1];
String ref = parts[0]; int nc = 3;
String alt = parts[1]; if(bcfRecord.alt.equals("."))
String fmt = parts[parts.length-1]; nc = 1;
if(formatPattern.matcher(fmt).matches()) String fmts[] = bcfRecord.format.split(":");
for(int j=0; j<fmts.length; j++)
{ {
String info = parts[parts.length-2]; int nb = getByteSize(fmts[j],1,nc);
System.out.println(info+" "+fmt); str = new byte[nb];
is.read(str);
String format = parts[parts.length-1]; final String value;
if(fmts[j].equals("GT"))
int nc = 3; value = getGTString(str[0]);
System.out.println("ALT:"+alt+" REF:"+ref); else if(fmts[j].equals("PL"))
if(alt.equals(".")) value = getPLString(str, nc);
nc = 1; else if(fmts[j].equals("DP")||fmts[j].equals("SP")||fmts[j].equals("GQ"))
value = Integer.toString(byteToInt(str[0]));
String fmts[] = format.split(":"); else
for(int j=0; j<fmts.length; j++) value = "";
{ bcfRecord.data.put(fmts[j], value);
int nb = getByteSize(fmts[j],1,nc);
str = new byte[nb];
is.read(str);
if(fmts[j].equals("GT"))
System.out.println("nbytes = "+nb+" GT:"+getGTString(str[0]));
else if(fmts[j].equals("PL"))
System.out.println("nbytes = "+nb+" PL:"+getPLString(str, nc));
else if(fmts[j].equals("DP")||fmts[j].equals("SP")||fmts[j].equals("GQ"))
{
System.out.println("nbytes = "+nb+" "+fmts[j]+":"+byteToInt(str[0]));
}
}
} }
} }
else
bcfRecord.info = parts[parts.length-1];
return bcfRecord;
} }
/** /**
...@@ -161,37 +215,34 @@ class BCFReader ...@@ -161,37 +215,34 @@ class BCFReader
return (int)(b & 0xFF); return (int)(b & 0xFF);
} }
public static List<Index> load(File bcfIndex) throws IOException protected static List<BCFIndex> loadIndex(File bcfIndex) throws IOException
{ {
FileInputStream fis = new FileInputStream(bcfIndex); FileInputStream fis = new FileInputStream(bcfIndex);
BlockCompressedInputStream is = new BlockCompressedInputStream(fis); BlockCompressedInputStream is = new BlockCompressedInputStream(fis);
byte[] magic = new byte[4]; byte[] magic = new byte[4];
is.read(magic); is.read(magic);
System.out.println(new String(magic));
int n = readInt(is); if(!new String(magic).equals("BCI\4"))
System.err.println("Not a BCF index file:: "+new String(magic));
List<Index> idx = new Vector<Index>(n); int n = readInt(is);
List<BCFIndex> idx = new Vector<BCFIndex>(n);
for(int i=0; i<n; i++) for(int i=0; i<n; i++)
{ {
Index idx2 = new Index(); BCFIndex idx2 = new BCFIndex();
idx2.n = readInt(is); idx2.n = readInt(is);
idx2.index2_offset = new long[idx2.n]; idx2.index2_offset = new long[idx2.n];
for(int j=0; j<idx2.n; j++) for(int j=0; j<idx2.n; j++)
{
idx2.index2_offset[j] = readLong(is); idx2.index2_offset[j] = readLong(is);
}
if(is.read() == -1)
System.out.println("EOF");
idx.add(idx2); idx.add(idx2);
} }
return idx; return idx;
} }
public long queryIndex(List<Index> idx, int tid, int beg) protected long queryIndex(List<BCFIndex> idx, int tid, int beg)
{ {
long min_off = -1; long min_off = -1;
if (beg < 0) if (beg < 0)
...@@ -206,20 +257,20 @@ class BCFReader ...@@ -206,20 +257,20 @@ class BCFReader
return min_off; return min_off;
} }
public static int readInt(final InputStream is) throws IOException { protected static int readInt(final InputStream is) throws IOException {
byte[] buf = new byte[4]; byte[] buf = new byte[4];
is.read(buf); is.read(buf);
return ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).getInt(); return ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).getInt();
} }
public static float readFloat(final InputStream is) throws IOException { protected static float readFloat(final InputStream is) throws IOException {
byte[] buf = new byte[4]; byte[] buf = new byte[4];
is.read(buf); is.read(buf);
return ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).getFloat(); return ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).getFloat();
} }
public static long readLong(final InputStream is) throws IOException { protected static long readLong(final InputStream is) throws IOException {
byte[] buf = new byte[8]; byte[] buf = new byte[8];
is.read(buf); is.read(buf);
return ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).getLong(); return ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).getLong();
...@@ -229,25 +280,25 @@ class BCFReader ...@@ -229,25 +280,25 @@ class BCFReader
{ {
try try
{ {
List<Index> idx = load(new File(args[0])); List<BCFIndex> idx = loadIndex(new File(args[0]));
int sbeg; int sbeg = 0;
int send; int send = Integer.MAX_VALUE;
if(args.length < 3) if(args.length > 2)
{
sbeg = 326758;
send = sbeg+1;
}
else
{ {
sbeg = Integer.parseInt(args[2]); sbeg = Integer.parseInt(args[2]);
send = Integer.parseInt(args[3]); send = Integer.parseInt(args[3]);
} }
BCFReader reader = new BCFReader(); BCFReader reader = new BCFReader();
long off = reader.queryIndex(idx, 0, sbeg); int bid = 0;
System.out.println(off); long off = reader.queryIndex(idx, bid, sbeg);
reader.query(new File(args[1]), off, sbeg, send); reader.query(new File(args[1]), off);
VCFRecord bcfRecord;
while( (bcfRecord = reader.next(bid, sbeg, send)) != null )
System.out.println(bcfRecord.toString());
} }
catch (IOException e) catch (IOException e)
{ {
...@@ -257,16 +308,8 @@ class BCFReader ...@@ -257,16 +308,8 @@ class BCFReader
} }
} }
class BCFRecord
{
int seqID;
int pos;
float quality;
String info;
String format;
}
class Index class BCFIndex
{ {
int n; int n;
long index2_offset[]; long index2_offset[];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment