A dosage file (used in computational genetics) is formatted like this:
// ID TAG geno1 geno2 geno3 ... 1->76016 DOSE 1.871 1.832 1.897 1.995 1.884 1.856 1.405 1.853 1.998 1.885 1.390 1.971 1.890 1.699 2->76018 DOSE 1.877 1.832 1.897 1.995 1.884 1.856 1.405 1.853 1.998 1.885 1.390 1.971 1.890 1.699 3->76063 DOSE 1.877 1.832 1.893 1.995 1.884 1.856 1.405 1.853 1.998 1.885 1.390 1.971 1.890 1.699 4->76015 DOSE 1.877 1.832 1.897 1.994 1.884 1.856 1.405 1.853 1.998 1.885 1.390 1.971 1.890 1.699 5->76023 DOSE 1.877 1.832 1.897 1.995 1.885 1.856 1.405 1.853 1.998 1.885 1.390 1.971 1.890 1.699 6->76030 DOSE 1.877 1.832 1.897 1.995 1.884 1.856 1.405 1.853 1.998 1.885 1.390 1.971 1.890 1.699 7->76044 DOSE 1.877 1.832 1.897 1.995 1.884 1.856 1.407 1.853 1.998 1.885 1.390 1.971 1.890 1.699 8->76008 DOSE 1.877 1.832 1.897 1.995 1.884 1.856 1.405 1.858 1.998 1.885 1.390 1.971 1.890 1.699 9->76014 DOSE 1.877 1.832 1.897 1.995 1.884 1.856 1.405 1.853 1.999 1.885 1.390 1.971 1.890 1.699 10->76011 DOSE 1.877 1.832 1.897 1.995 1.884 1.856 1.405 1.853 1.998 1.880 1.390 1.971 1.890 1.699
Here is a class for reading this kind of file:
Dosage
import java.io.*;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
/**
* Created by vu.co.kaiyin.ReadMachDosage on 15/11/14.
*/
public class Dosage {
final String filename;
final Pattern splitPattern;
final int nColFile;
final int nCol;
final int nRow;
public Dosage(String filename) throws Exception {
this.filename = filename;
splitPattern = Pattern.compile("\\s+");
int[] dimArray = dimensions();
nColFile = dimArray[0];
nCol = dimArray[1];
nRow = dimArray[2];
}
private int[] dimensions() throws Exception {
int nColFileCounter = 0;
int nRowCounter = 0;
try(
InputStream fileStream = new FileInputStream(filename);
InputStream gzipStream = new GZIPInputStream(fileStream);
Reader reader = new InputStreamReader(gzipStream, "US-ASCII");
BufferedReader bufferedReader = new BufferedReader(reader);
) {
String line;
while ((line = bufferedReader.readLine()) != null) {
if (nRowCounter == 0) {
nColFileCounter = splitPattern.split(line).length;
}
nRowCounter++;
}
return new int[]{nColFileCounter, nColFileCounter - 2, nRowCounter};
}
}
public double[][] readCols(int start, int end) throws Exception {
// Data indices: x x 1 2 3
// Indices: 0 1 2 3 4
// Columns: 1->241 DOSE 1.1783 0.3498 1.9834 ...
if(start < 1) start = 1;
if(end > nCol) end = nCol;
start++;
end += 2; // java end index is exclusive
try(
InputStream fileStream = new FileInputStream(filename);
InputStream gzipStream = new GZIPInputStream(fileStream);
Reader reader = new InputStreamReader(gzipStream, "US-ASCII");
BufferedReader bufferedReader = new BufferedReader(reader);
) {
String line;
String[] fields;
ArrayList<double[]> dosageList = new ArrayList<>();
while ((line = bufferedReader.readLine()) != null) {
fields = splitPattern.split(line);
double[] dosage = new double[end - start];
for (int i = 2; i < end; i++) {
dosage[i - 2] = Double.parseDouble(fields[i]);
}
dosageList.add(dosage);
}
double[][] dosageMatrix = new double[dosageList.size()][];
dosageMatrix = (double[][]) dosageList.toArray(dosageMatrix);
return dosageMatrix;
}
}
public double[][] readAll() throws Exception {
return readCols(1, nCol);
}
}
TestDosage
public class TestDosage {
public static void main(String[] args) throws Exception {
String filename = "/tmp/chr22.dose.gz";
Dosage dosage = new Dosage(filename);
double[][] doseMatrix = dosage.readCols(1, 3);
PrintArray.print(doseMatrix);
double[][] doseMatrixFull = dosage.readAll();
PrintArray.print(doseMatrixFull);
}
}