use of org.biojava.nbio.structure.io.FileParsingParameters in project mmtf-spark by sbl-sdsc.
the class MmtfImporter method importMmcifFiles.
/**
* Reads uncompressed and compressed mmCIF files recursively from a given
* directory path. This methods reads files with the .cif or .cif.gz
* extension.
*
* @param path
* Path to .cif files
* @param sc
* Spark context
* @return structure data as keyword/value pairs
*/
public static JavaPairRDD<String, StructureDataInterface> importMmcifFiles(String path, JavaSparkContext sc) {
FileParsingParameters params = new FileParsingParameters();
params.setCreateAtomBonds(true);
return sc.parallelize(getFiles(path)).mapToPair(new PairFunction<File, String, StructureDataInterface>() {
private static final long serialVersionUID = -7815663658405168429L;
public Tuple2<String, StructureDataInterface> call(File f) throws Exception {
InputStream is = null;
String path = f.getName();
// TODO debugging
System.out.println(path);
if (path.endsWith(".cif") || path.endsWith((".cif.gz"))) {
try {
is = new FileInputStream(f);
if (path.endsWith(".cif.gz")) {
is = new GZIPInputStream(is);
}
// parse .cif file
MMCIFFileReader mmcifReader = new MMCIFFileReader();
mmcifReader.setFileParsingParameters(params);
Structure struc = mmcifReader.getStructure(is);
is.close();
// convert to mmtf
AdapterToStructureData writerToEncoder = new AdapterToStructureData();
new MmtfStructureWriter(struc, writerToEncoder);
return new Tuple2<String, StructureDataInterface>(path.substring(0, path.indexOf(".cif")), writerToEncoder);
} catch (Exception e) {
System.out.println("WARNING: cannot parse: " + path + ". Skipping this entry!");
return null;
}
} else {
return null;
}
}
}).filter(t -> t != null);
}
use of org.biojava.nbio.structure.io.FileParsingParameters in project mmtf-spark by sbl-sdsc.
the class MmtfImporter method getFromMmcifUrl.
/**
* Reads a mmCIF file from a URL.
*
* @param url URL for mmCIF file
* @return
* @throws IOException
*/
private static AdapterToStructureData getFromMmcifUrl(String url, String structureId) throws IOException {
FileParsingParameters params = new FileParsingParameters();
params.setCreateAtomBonds(true);
URL u = new URL(url);
InputStream is = null;
try {
is = u.openStream();
} catch (IOException e) {
return null;
}
try {
if (url.endsWith(".gz")) {
is = new GZIPInputStream(is);
}
} catch (Exception e) {
return null;
}
try {
// parse .cif file
MMCIFFileReader mmcifReader = new MMCIFFileReader();
mmcifReader.setFileParsingParameters(params);
Structure struc = mmcifReader.getStructure(is);
is.close();
// convert to mmtf
AdapterToStructureData writerToEncoder = new AdapterToStructureData();
new MmtfStructureWriter(struc, writerToEncoder);
return writerToEncoder;
} catch (Exception e) {
System.out.println("WARNING: cannot parse: " + url + ". Skipping this entry!");
return null;
}
}
Aggregations