use of org.biojava.nbio.structure.io.mmtf.MmtfStructureWriter in project mmtf-spark by sbl-sdsc.
the class MmtfImporter method toStructureDataInterface.
/**
* Parses PDB-formatted input stream and return structure data.
*
* @param inputStream PDB-formatted input stream
* @param structureId id to be assigned to the structure
* @return structure data
* @throws IOException
*/
private static AdapterToStructureData toStructureDataInterface(InputStream inputStream, String structureId) throws IOException {
// standardize PDB formatting
InputStream pdbIs = standardizePdbInputStream(inputStream);
// parse PDB and generate BioJava Structure object
PDBFileParser parser = new PDBFileParser();
parser.getFileParsingParameters().setCreateAtomBonds(true);
Structure struct = parser.parsePDBFile(pdbIs);
struct.setPDBCode(structureId);
// where the entity info is null when there is only one polymer chain.
for (EntityInfo info : struct.getEntityInfos()) {
if (info.getType() == null) {
for (String chainId : info.getChainIds()) {
Chain c = struct.getChain(chainId);
if (c.getAtomSequence().length() > 0) {
info.setType(EntityType.POLYMER);
}
}
}
}
pdbIs.close();
// convert to MMTF
AdapterToStructureData writerToEncoder = new AdapterToStructureData();
// TODO get version number
writerToEncoder.setMmtfProducer("mmtf-spark 0.2.0");
new MmtfStructureWriter(struct, writerToEncoder);
return writerToEncoder;
}
use of org.biojava.nbio.structure.io.mmtf.MmtfStructureWriter 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.mmtf.MmtfStructureWriter 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