2009-03-16 06:37:20 +08:00
|
|
|
package org.broadinstitute.sting.gatk.refdata;
|
|
|
|
|
|
2009-07-17 05:03:47 +08:00
|
|
|
import org.apache.log4j.Logger;
|
2009-08-12 06:10:20 +08:00
|
|
|
import org.broadinstitute.sting.utils.StingException;
|
2009-07-17 05:03:47 +08:00
|
|
|
|
2009-08-12 06:10:20 +08:00
|
|
|
import java.io.*;
|
2009-05-21 23:23:22 +08:00
|
|
|
import java.lang.reflect.Method;
|
2010-04-01 06:39:56 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
2009-03-16 06:37:20 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class for representing arbitrary reference ordered data sets
|
2009-08-12 06:10:20 +08:00
|
|
|
* <p/>
|
2009-03-16 06:37:20 +08:00
|
|
|
* User: mdepristo
|
|
|
|
|
* Date: Feb 27, 2009
|
|
|
|
|
* Time: 10:47:14 AM
|
|
|
|
|
* To change this template use File | Settings | File Templates.
|
|
|
|
|
*/
|
2010-04-01 06:39:56 +08:00
|
|
|
public class ReferenceOrderedData<ROD extends ReferenceOrderedDatum> implements Iterable<ReferenceOrderedDatum> {
|
2009-04-04 00:41:33 +08:00
|
|
|
private String name;
|
2009-03-16 06:37:20 +08:00
|
|
|
private File file = null;
|
2009-09-22 00:55:22 +08:00
|
|
|
// private String fieldDelimiter;
|
2009-08-12 06:10:20 +08:00
|
|
|
|
|
|
|
|
/** Header object returned from the datum */
|
2009-09-22 00:55:22 +08:00
|
|
|
// private Object header = null;
|
2009-08-12 06:10:20 +08:00
|
|
|
|
2009-03-16 06:37:20 +08:00
|
|
|
private Class<ROD> type = null; // runtime type information for object construction
|
|
|
|
|
|
2009-08-12 06:10:20 +08:00
|
|
|
/** our log, which we want to capture anything from this class */
|
|
|
|
|
private static Logger logger = Logger.getLogger(ReferenceOrderedData.class);
|
|
|
|
|
|
|
|
|
|
/**
|
2010-01-13 02:50:39 +08:00
|
|
|
* given an existing file, open it and append all the valid triplet lines to an existing list
|
2009-08-12 06:10:20 +08:00
|
|
|
*
|
|
|
|
|
* @param rodTripletList the list of existing triplets
|
|
|
|
|
* @param filename the file to attempt to extract ROD triplets from
|
|
|
|
|
*/
|
|
|
|
|
protected static void extractRodsFromFile(List<String> rodTripletList, String filename) {
|
|
|
|
|
BufferedReader str;
|
|
|
|
|
try {
|
|
|
|
|
str = new BufferedReader(new FileReader(new File(filename)));
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
throw new StingException("Unable to load the ROD input file " + filename,e);
|
|
|
|
|
}
|
|
|
|
|
String line = "NO LINES READ IN";
|
|
|
|
|
try {
|
|
|
|
|
while ((line = str.readLine()) != null) {
|
|
|
|
|
if (line.matches(".+,.+,.+")) rodTripletList.add(line.trim());
|
|
|
|
|
else logger.warn("the following file line didn't parsing into a triplet -> " + line);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new StingException("Failed reading the input rod file " + filename + " last line read was " + line,e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-10 06:04:59 +08:00
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Constructors
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------
|
2009-04-04 00:41:33 +08:00
|
|
|
public ReferenceOrderedData(final String name, File file, Class<ROD> type ) {
|
2009-11-09 12:25:39 +08:00
|
|
|
this.name = name;
|
2009-03-16 06:37:20 +08:00
|
|
|
this.file = file;
|
|
|
|
|
this.type = type;
|
2009-09-22 00:55:22 +08:00
|
|
|
// this.header = initializeROD(name, file, type);
|
|
|
|
|
// this.fieldDelimiter = newROD(name, type).delimiterRegex();
|
2009-03-16 06:37:20 +08:00
|
|
|
}
|
|
|
|
|
|
2009-04-04 03:54:54 +08:00
|
|
|
public String getName() { return name; }
|
|
|
|
|
|
2009-11-09 12:25:39 +08:00
|
|
|
public File getFile() { return file; }
|
|
|
|
|
|
|
|
|
|
public Class<ROD> getType() { return type; }
|
|
|
|
|
|
2009-05-20 07:26:17 +08:00
|
|
|
/**
|
2009-05-24 04:50:28 +08:00
|
|
|
* Special equals override to see if this ROD is compatible with the given
|
|
|
|
|
* name and type. 'Compatible' means that this ROD has the name that's passed
|
|
|
|
|
* in and its data can fit into the container specified by type.
|
2009-08-12 06:10:20 +08:00
|
|
|
*
|
2009-05-20 07:26:17 +08:00
|
|
|
* @param name Name to check.
|
|
|
|
|
* @param type Type to check.
|
2009-08-12 06:10:20 +08:00
|
|
|
*
|
2009-05-20 07:26:17 +08:00
|
|
|
* @return True if these parameters imply this rod. False otherwise.
|
|
|
|
|
*/
|
2009-08-12 06:10:20 +08:00
|
|
|
public boolean matches(String name, Class<? extends ReferenceOrderedDatum> type) {
|
2009-05-24 04:50:28 +08:00
|
|
|
return this.name.equals(name) && type.isAssignableFrom(this.type);
|
2009-05-20 07:26:17 +08:00
|
|
|
}
|
|
|
|
|
|
2010-04-01 06:39:56 +08:00
|
|
|
public Iterator<ReferenceOrderedDatum> iterator() {
|
2010-02-25 06:11:53 +08:00
|
|
|
Iterator<ReferenceOrderedDatum> it;
|
2009-05-21 23:23:22 +08:00
|
|
|
try {
|
2009-08-12 06:10:20 +08:00
|
|
|
Method m = type.getDeclaredMethod("createIterator", String.class, java.io.File.class);
|
2010-02-25 06:11:53 +08:00
|
|
|
it = (Iterator<ReferenceOrderedDatum>) m.invoke(null, name, file);
|
2009-08-12 06:10:20 +08:00
|
|
|
} catch (java.lang.NoSuchMethodException e) {
|
2009-09-22 00:55:22 +08:00
|
|
|
it = new RODRecordIterator(file,name,type);
|
2009-08-12 06:10:20 +08:00
|
|
|
} catch (java.lang.NullPointerException e) {
|
2009-05-21 23:23:22 +08:00
|
|
|
throw new RuntimeException(e);
|
2009-08-12 06:10:20 +08:00
|
|
|
} catch (java.lang.SecurityException e) {
|
2009-05-21 23:23:22 +08:00
|
|
|
throw new RuntimeException(e);
|
2009-08-12 06:10:20 +08:00
|
|
|
} catch (java.lang.IllegalAccessException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
} catch (java.lang.IllegalArgumentException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
} catch (java.lang.reflect.InvocationTargetException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
2009-09-22 00:55:22 +08:00
|
|
|
// return new RODIterator<ROD>(it);
|
2010-04-01 06:39:56 +08:00
|
|
|
return it;
|
2009-08-12 06:10:20 +08:00
|
|
|
}
|
2009-03-16 06:37:20 +08:00
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Manipulations of all of the data
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
public static void write(ArrayList<ReferenceOrderedDatum> data, File output) throws IOException {
|
|
|
|
|
final FileWriter out = new FileWriter(output);
|
|
|
|
|
|
2009-08-12 06:10:20 +08:00
|
|
|
for (ReferenceOrderedDatum rec : data) {
|
2009-03-16 06:37:20 +08:00
|
|
|
out.write(rec.repl() + "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-22 00:55:22 +08:00
|
|
|
|
2009-03-16 06:37:20 +08:00
|
|
|
}
|