Package org.rcsb.mmtf.spark.utils
Class SparkUtils
- java.lang.Object
-
- org.rcsb.mmtf.spark.utils.SparkUtils
-
public class SparkUtils extends java.lang.ObjectA class of Spark utility methods- Author:
- Anthony Bradley
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringCHANGELOG_LOCATIONThe URL to find the Changelog for MMTF.static java.lang.StringFULL_LOCATIONThe URL to find the Hadoop sequence file for the full version of MMTF.static java.lang.StringREDUCED_LOCATIONThe URL to find the Hadoop sequence file for the reduced version of MMTF.
-
Constructor Summary
Constructors Constructor Description SparkUtils()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidcombineDirToFile(java.io.File dir, java.lang.String header)Join a Spark output dir of text files into a single text file.static <T> org.apache.spark.sql.Dataset<org.apache.spark.sql.Row>convertToDataFrame(org.apache.spark.api.java.JavaRDD<T> javaRDD, java.lang.Class<T> clazz)Convert aJavaRDDto a Dataframe (Spark 2.0.0 Dataset(Row) = Dataframe) of the same type.static <T> org.apache.spark.sql.Dataset<T>convertToDataset(org.apache.spark.api.java.JavaRDD<T> javaRDD, java.lang.Class<T> clazz)Convert aJavaRDDto aDatasetof the same type.static voiddownloadPdb(java.lang.String inputPath)Function to download the PDB and place it on the file system.static voidfilePath(java.lang.String filePath)Set the file path of the Hadoop file to read.static java.util.Map<java.lang.String,java.util.Set<java.lang.String>>findUpdates(java.lang.String changelogLocation, java.util.Date lastUpdatedDate)Find the updates from the change logs using.AtomSelectObjectgenerateAtomSelectObject(java.util.List<java.lang.String> atomNameList, java.util.List<java.lang.String> groupNameList, boolean charged, java.util.List<java.lang.String> elementNameList, java.lang.String groupType)Utility function to generate anAtomSelectObject.static javax.vecmath.Point3dgetCalpha(org.rcsb.mmtf.api.StructureDataInterface structureDataInterface, int groupType, int atomCounter)Get the calpha as aPoint3d.static SegmentDataRDDgetCalphaChains(java.lang.String[] inputIds)Get the Calpha chains for a few structures as a SegmentDataRDD.static org.apache.spark.api.java.JavaPairRDD<java.lang.Integer,java.lang.Integer>getComparisonMatrix(int numMembers)Get aJavaPairRDDof Integers to do a half matrix comparison.static org.apache.spark.SparkConfgetConf()Get theSparkConffor this run.static byte[]getDataAsByteArray(java.lang.String pdbCode)Helper function to get the data for a PDB id as an gzip compressed byte array.static java.lang.StringgetFilePath()Get the path to the Hadoop sequence file to read.static org.apache.spark.api.java.JavaPairRDD<java.lang.String,org.rcsb.mmtf.api.StructureDataInterface>getFromList(java.util.List<java.lang.String> pdbIdList)Generate aJavaPairRDDof String (PDB ID) and ValueStructureDataInterfacefrom a list of PDB idsstatic java.lang.StringgetFullPdbFile()Get the path for where the full PDB file will be stored.static java.lang.StringgetHadoopBase()Get the base path of where to store Hadoop data.static <K extends java.lang.Comparable<K>,V>
org.apache.spark.api.java.JavaPairRDD<scala.Tuple2<K,V>,scala.Tuple2<K,V>>getHalfCartesian(org.apache.spark.api.java.JavaPairRDD<K,V> inputRDD)Get a half cartesian - using the first part of the input RDD as a string key.static <K extends java.lang.Comparable<K>,V>
org.apache.spark.api.java.JavaPairRDD<scala.Tuple2<K,V>,scala.Tuple2<K,V>>getHalfCartesian(org.apache.spark.api.java.JavaPairRDD<K,V> inputRDD, int numPartitions)Get a half cartesian - using the first part of the input RDD as a string key.static <T> org.apache.spark.api.java.JavaRDD<T>getJavaRdd(org.apache.spark.sql.Dataset<T> atomDataset, java.lang.Class<T> class1)Get aJavaRDDfrom aDataset.static org.apache.spark.api.java.JavaPairRDD<java.lang.String,org.rcsb.mmtf.dataholders.MmtfStructure>getMmtfStructureRdd(java.lang.String filePath)Get aJavaPairRDDofStringMmtfStructurefrom a file pathstatic org.apache.spark.api.java.JavaSparkContextgetSparkContext()Get theJavaSparkContextfor this run.static org.apache.spark.api.java.JavaSparkContextgetSparkContext(org.apache.spark.SparkConf conf)Get theJavaSparkContextfor this run.static org.apache.spark.sql.SparkSessiongetSparkSession()Get theSparkSessionfor this runstatic org.apache.spark.sql.SQLContextgetSqlContext()Get theSQLContextfor producing datasets and data frames.static org.apache.spark.api.java.JavaPairRDD<java.lang.String,org.rcsb.mmtf.api.StructureDataInterface>getStructureDataRdd(java.lang.String filePath)Get anJavaPairRDDofStringStructureDataInterfacefrom a file path.static StructureDataRDDgetStructureObj(java.lang.String filePath)Get theStructureDataRDDfrom a file path.static byte[]gzipCompress(byte[] byteArray)Compress a byte array using Gzip.static voidjoinAndSave(org.apache.spark.api.java.JavaPairRDD<org.apache.hadoop.io.Text,org.apache.hadoop.io.BytesWritable> datasetOne, org.apache.spark.api.java.JavaPairRDD<org.apache.hadoop.io.Text,org.apache.hadoop.io.BytesWritable> datasetTwo, java.lang.String outputUri)Join two RDDs together into one larger one and save it as a new filestatic org.apache.spark.api.java.JavaPairRDD<org.apache.hadoop.io.Text,org.apache.hadoop.io.BytesWritable>joinDatasets(org.apache.spark.api.java.JavaPairRDD<org.apache.hadoop.io.Text,org.apache.hadoop.io.BytesWritable> datasetOne, org.apache.spark.api.java.JavaPairRDD<org.apache.hadoop.io.Text,org.apache.hadoop.io.BytesWritable> datasetTwo)Join two RDDs together into one larger one.static java.util.DateparseDate(java.lang.String releaseDate)Parse the date as it is stored in the String.static org.apache.spark.api.java.JavaPairRDD<java.lang.String,byte[]>readHadoopFile(java.lang.String filePath)Read a hadoop sequence file to a Strign byte[] pair.StructureDataRDDselectSubset(java.lang.String filePath, float subsetToChose)Select a subset of the PDB in a fast and efficient way.StructureDataRDDselectSubset(java.lang.String filePath, java.util.List<java.lang.String> subsetToChose)Select a subset of the PDB in a fast and efficient way.static voidshutdown()Gently shutdown at the end of a run.static voidupdatePdb(java.lang.String filePath)Update the PDB data from when it was last updated.static java.lang.StringwriteDate(java.util.Date releaseDate)Write the date out as a string using the format we are using.
-
-
-
Field Detail
-
FULL_LOCATION
public static final java.lang.String FULL_LOCATION
The URL to find the Hadoop sequence file for the full version of MMTF.- See Also:
- Constant Field Values
-
REDUCED_LOCATION
public static final java.lang.String REDUCED_LOCATION
The URL to find the Hadoop sequence file for the reduced version of MMTF.- See Also:
- Constant Field Values
-
CHANGELOG_LOCATION
public static final java.lang.String CHANGELOG_LOCATION
The URL to find the Changelog for MMTF.- See Also:
- Constant Field Values
-
-
Method Detail
-
convertToDataset
public static <T> org.apache.spark.sql.Dataset<T> convertToDataset(org.apache.spark.api.java.JavaRDD<T> javaRDD, java.lang.Class<T> clazz)Convert aJavaRDDto aDatasetof the same type.- Type Parameters:
T- the type of the RDD- Parameters:
javaRDD- the inputJavaRDDclazz- the input class of the RDD- Returns:
- the converted dataset
-
convertToDataFrame
public static <T> org.apache.spark.sql.Dataset<org.apache.spark.sql.Row> convertToDataFrame(org.apache.spark.api.java.JavaRDD<T> javaRDD, java.lang.Class<T> clazz)Convert aJavaRDDto a Dataframe (Spark 2.0.0 Dataset(Row) = Dataframe) of the same type.- Type Parameters:
T- the type of the RDD- Parameters:
javaRDD- the inputJavaRDDclazz- the input class of the RDD- Returns:
- the converted dataframe
-
getStructureDataRdd
public static org.apache.spark.api.java.JavaPairRDD<java.lang.String,org.rcsb.mmtf.api.StructureDataInterface> getStructureDataRdd(java.lang.String filePath)
Get anJavaPairRDDofStringStructureDataInterfacefrom a file path.- Parameters:
filePath- the input path to the hadoop sequence file- Returns:
- the
JavaPairRDDofStringStructureDataInterface
-
getMmtfStructureRdd
public static org.apache.spark.api.java.JavaPairRDD<java.lang.String,org.rcsb.mmtf.dataholders.MmtfStructure> getMmtfStructureRdd(java.lang.String filePath)
Get aJavaPairRDDofStringMmtfStructurefrom a file path- Parameters:
filePath- the input path to the hadoop sequence file- Returns:
- the
JavaPairRDDofStringMmtfStructure
-
selectSubset
public StructureDataRDD selectSubset(java.lang.String filePath, java.util.List<java.lang.String> subsetToChose)
Select a subset of the PDB in a fast and efficient way.- Parameters:
filePath- the input Hadoop file to readsubsetToChose- the list of PDB ids to chose- Returns:
- the
StructureDataRDDof the selected data
-
selectSubset
public StructureDataRDD selectSubset(java.lang.String filePath, float subsetToChose)
Select a subset of the PDB in a fast and efficient way.- Parameters:
filePath- the input Hadoop file to readsubsetToChose- the fraction to chose- Returns:
- the
StructureDataRDDof the selected data
-
readHadoopFile
public static org.apache.spark.api.java.JavaPairRDD<java.lang.String,byte[]> readHadoopFile(java.lang.String filePath)
Read a hadoop sequence file to a Strign byte[] pair.- Parameters:
filePath- the input path of the hadoop file- Returns:
- the
JavaPairRDDof the PDB code and the byte array of the data.
-
getStructureObj
public static StructureDataRDD getStructureObj(java.lang.String filePath)
Get theStructureDataRDDfrom a file path.- Parameters:
filePath- the input file path- Returns:
- the
StructureDataRDDobject
-
getConf
public static org.apache.spark.SparkConf getConf()
Get theSparkConffor this run.- Returns:
- the
SparkConffor this run
-
getSparkContext
public static org.apache.spark.api.java.JavaSparkContext getSparkContext()
Get theJavaSparkContextfor this run.- Returns:
- the
JavaSparkContextfor this run
-
getSqlContext
public static org.apache.spark.sql.SQLContext getSqlContext()
Get theSQLContextfor producing datasets and data frames.- Returns:
- the
SQLContext.
-
getSparkSession
public static org.apache.spark.sql.SparkSession getSparkSession()
Get theSparkSessionfor this run- Returns:
- the
SparkSessionfor this run
-
getSparkContext
public static org.apache.spark.api.java.JavaSparkContext getSparkContext(org.apache.spark.SparkConf conf)
Get theJavaSparkContextfor this run.- Parameters:
conf- theSparkConfto use to setup the context- Returns:
- the
JavaSparkContextfor this run
-
shutdown
public static void shutdown()
Gently shutdown at the end of a run.
-
filePath
public static void filePath(java.lang.String filePath)
Set the file path of the Hadoop file to read.- Parameters:
filePath- the input file path to read
-
getCalpha
public static javax.vecmath.Point3d getCalpha(org.rcsb.mmtf.api.StructureDataInterface structureDataInterface, int groupType, int atomCounter)Get the calpha as aPoint3d.- Parameters:
structureDataInterface- theStructureDataInterfaceto readgroupType- the integer specifying the grouptypeatomCounter- the atom count at the start of this group- Returns:
- the point3d object specifying the calpha of this point
-
gzipCompress
public static byte[] gzipCompress(byte[] byteArray) throws java.io.IOExceptionCompress a byte array using Gzip.- Parameters:
byteArray- the input byte array- Returns:
- the compressed byte array
- Throws:
java.io.IOException- an error reading from the URL
-
getFilePath
public static java.lang.String getFilePath()
Get the path to the Hadoop sequence file to read.- Returns:
- the
Stringpath of the Hadoop sequence file to read.
-
downloadPdb
public static void downloadPdb(java.lang.String inputPath) throws java.io.FileNotFoundException, java.io.IOExceptionFunction to download the PDB and place it on the file system.- Parameters:
inputPath- the input path to read from- Throws:
java.io.IOException- an error reading from the URLjava.io.FileNotFoundException- an error transferring files
-
getFullPdbFile
public static java.lang.String getFullPdbFile()
Get the path for where the full PDB file will be stored.- Returns:
- the
Stringdescribing where the full PDB data is
-
getHadoopBase
public static java.lang.String getHadoopBase()
Get the base path of where to store Hadoop data.- Returns:
- the
Stringof the path of where Hadoop data should be
-
getCalphaChains
public static SegmentDataRDD getCalphaChains(java.lang.String[] inputIds) throws java.io.IOException
Get the Calpha chains for a few structures as a SegmentDataRDD.- Parameters:
inputIds- the list of input ids as strings- Returns:
- the
SegmentDataRDDof the calpha chains given the ids. - Throws:
java.io.IOException- due to reading from the MMTF url
-
getDataAsByteArray
public static byte[] getDataAsByteArray(java.lang.String pdbCode) throws java.io.IOExceptionHelper function to get the data for a PDB id as an gzip compressed byte array. Data is retrieved from the REST service. This should be moved to mmtf for the next release.- Parameters:
pdbCode- the input PDB id- Returns:
- the gzip compressed byte array for this structure
- Throws:
java.io.IOException- due to retrieving data from the URL
-
generateAtomSelectObject
public AtomSelectObject generateAtomSelectObject(java.util.List<java.lang.String> atomNameList, java.util.List<java.lang.String> groupNameList, boolean charged, java.util.List<java.lang.String> elementNameList, java.lang.String groupType)
Utility function to generate anAtomSelectObject. Mainly for application to the Python API.- Parameters:
atomNameList- the list of atoms to considergroupNameList- the list of groups to consider (e.g. LYS)charged- whether to consider charged atoms only (true)elementNameList- the list of elements to considergroupType- a string defining the type of group- Returns:
- an atom select object of the appropriate type.
-
getComparisonMatrix
public static org.apache.spark.api.java.JavaPairRDD<java.lang.Integer,java.lang.Integer> getComparisonMatrix(int numMembers)
Get aJavaPairRDDof Integers to do a half matrix comparison. i.e. all comparisons where i!=j and i is greather than j- Parameters:
numMembers- the total number of members to compare- Returns:
- the
JavaPairRDDof the comparisons
-
combineDirToFile
public static void combineDirToFile(java.io.File dir, java.lang.String header) throws java.io.IOExceptionJoin a Spark output dir of text files into a single text file.- Parameters:
dir- the file the partitions are written toheader- the header for the top of the file- Throws:
java.io.IOException- an error joining the files
-
getHalfCartesian
public static <K extends java.lang.Comparable<K>,V> org.apache.spark.api.java.JavaPairRDD<scala.Tuple2<K,V>,scala.Tuple2<K,V>> getHalfCartesian(org.apache.spark.api.java.JavaPairRDD<K,V> inputRDD)
Get a half cartesian - using the first part of the input RDD as a string key.- Type Parameters:
K- the type of the valueV- the type of the value- Parameters:
inputRDD- the input rdd - keys are- Returns:
- the RDD of all non-repated comparisons
-
getHalfCartesian
public static <K extends java.lang.Comparable<K>,V> org.apache.spark.api.java.JavaPairRDD<scala.Tuple2<K,V>,scala.Tuple2<K,V>> getHalfCartesian(org.apache.spark.api.java.JavaPairRDD<K,V> inputRDD, int numPartitions)Get a half cartesian - using the first part of the input RDD as a string key.- Type Parameters:
K- the type of the valueV- the type of the value- Parameters:
inputRDD- the input rdd - keys arenumPartitions- the number of partitions to repartition- Returns:
- the RDD of all non-repated comparisons
-
getFromList
public static org.apache.spark.api.java.JavaPairRDD<java.lang.String,org.rcsb.mmtf.api.StructureDataInterface> getFromList(java.util.List<java.lang.String> pdbIdList) throws java.io.IOExceptionGenerate aJavaPairRDDof String (PDB ID) and ValueStructureDataInterfacefrom a list of PDB ids- Parameters:
pdbIdList- the list of PDB ids- Returns:
- the
JavaPairRDDof String andStructureDataInterface - Throws:
java.io.IOException- due to reading from the URL
-
getJavaRdd
public static <T> org.apache.spark.api.java.JavaRDD<T> getJavaRdd(org.apache.spark.sql.Dataset<T> atomDataset, java.lang.Class<T> class1)Get aJavaRDDfrom aDataset.- Type Parameters:
T- the type of the dataset- Parameters:
atomDataset- the dataset to convertclass1- the class of the dataset- Returns:
- the
JavaRDDfromn the dataset
-
parseDate
public static java.util.Date parseDate(java.lang.String releaseDate) throws java.text.ParseExceptionParse the date as it is stored in the String.- Parameters:
releaseDate- the input date as a string- Returns:
- the parsed date
- Throws:
java.text.ParseException- an error in parsing the date
-
writeDate
public static java.lang.String writeDate(java.util.Date releaseDate)
Write the date out as a string using the format we are using.- Parameters:
releaseDate- the Date being considered- Returns:
- the String representation of that date
-
findUpdates
public static java.util.Map<java.lang.String,java.util.Set<java.lang.String>> findUpdates(java.lang.String changelogLocation, java.util.Date lastUpdatedDate)Find the updates from the change logs using. Only those after the date of the initial input will be used.- Parameters:
changelogLocation- the location of the change log informationlastUpdatedDate- the date of the last update- Returns:
- the REMOVED and ADDED sets of PDB codes to be used in the update
-
updatePdb
public static void updatePdb(java.lang.String filePath) throws java.io.IOExceptionUpdate the PDB data from when it was last updated.- Parameters:
filePath- the input Hadoop sequence folder path- Throws:
java.io.IOException- an error writing the final output file
-
joinAndSave
public static void joinAndSave(org.apache.spark.api.java.JavaPairRDD<org.apache.hadoop.io.Text,org.apache.hadoop.io.BytesWritable> datasetOne, org.apache.spark.api.java.JavaPairRDD<org.apache.hadoop.io.Text,org.apache.hadoop.io.BytesWritable> datasetTwo, java.lang.String outputUri)Join two RDDs together into one larger one and save it as a new file- Parameters:
datasetOne- the firstJavaPairRDDto joindatasetTwo- the secondJavaPairRDDto joinoutputUri- the output URI to write to
-
joinDatasets
public static org.apache.spark.api.java.JavaPairRDD<org.apache.hadoop.io.Text,org.apache.hadoop.io.BytesWritable> joinDatasets(org.apache.spark.api.java.JavaPairRDD<org.apache.hadoop.io.Text,org.apache.hadoop.io.BytesWritable> datasetOne, org.apache.spark.api.java.JavaPairRDD<org.apache.hadoop.io.Text,org.apache.hadoop.io.BytesWritable> datasetTwo)Join two RDDs together into one larger one.- Parameters:
datasetOne- the firstJavaPairRDDto joindatasetTwo- the secondJavaPairRDDto join- Returns:
- the
JavaPairRDDof the joined datasets
-
-