Skip to content

Commit

Permalink
First commit - get 3D coords from SMILES
Browse files Browse the repository at this point in the history
  • Loading branch information
plweegie committed Dec 17, 2019
0 parents commit 09f51d2
Show file tree
Hide file tree
Showing 45 changed files with 6,070 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*.iml
.gradle
.idea/
projectFilesBackup/
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
release/
.externalNativeBuild
google-services.json
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
64 changes: 64 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.plweegie.magmolecular"
minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
pickFirst 'META-INF/kotlinx-coroutines-core.kotlin_module'
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-rc03'

implementation 'com.google.ar:core:1.14.0'
implementation 'com.google.ar.sceneform:core:1.14.0'
implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.14.0'

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.3.1'

implementation 'org.openscience.cdk:cdk-core:2.3'
implementation 'org.openscience.cdk:cdk-interfaces:2.3'
implementation 'org.openscience.cdk:cdk-smiles:2.3'
implementation 'org.openscience.cdk:cdk-data:2.3'
implementation 'org.openscience.cdk:cdk-standard:2.3'
implementation 'org.openscience.cdk:cdk-io:2.3'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

apply plugin: 'com.google.ar.sceneform.plugin'
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.plweegie.magmolecular

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.plweegie.magmolecular", appContext.packageName)
}
}
27 changes: 27 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.plweegie.magmolecular">

<uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera.ar" android:required="true" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="com.google.ar.core" android:value="required" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ar.MagMolActivity" />
</application>

</manifest>
67 changes: 67 additions & 0 deletions app/src/main/java/com/plweegie/magmolecular/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.plweegie.magmolecular

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.plweegie.magmolecular.rendering.AndroidModelBuilder3D
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.*
import org.openscience.cdk.DefaultChemObjectBuilder
import org.openscience.cdk.exception.InvalidSmilesException
import org.openscience.cdk.interfaces.IAtom
import org.openscience.cdk.interfaces.IAtomContainer
import org.openscience.cdk.smiles.SmilesParser
import kotlin.coroutines.CoroutineContext

class MainActivity : AppCompatActivity(), CoroutineScope {

private lateinit var smilesParser: SmilesParser
private lateinit var modelBuilder: AndroidModelBuilder3D
private lateinit var job: Job

override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
job = Job()

val builder = DefaultChemObjectBuilder.getInstance()
smilesParser = SmilesParser(builder)
modelBuilder = AndroidModelBuilder3D.getInstance(builder)

get_atoms_btn?.setOnClickListener {
parseSmiles()
}
}

override fun onDestroy() {
super.onDestroy()
job.cancel()
}

private fun parseSmiles() {
val smiles = smiles_field?.text.toString()
try {
val atomContainer = smilesParser.parseSmiles(smiles)

launch {
val atoms3d = get3DCoordinates(atomContainer)
atoms3d.forEach {
Log.d("COORDS", "${it.point3d.x}, ${it.point3d.y}, ${it.point3d.z}")
}
}
} catch (e: InvalidSmilesException) {
Toast.makeText(this, "Invalid SMILES", Toast.LENGTH_SHORT).show()
}
}

private suspend fun get3DCoordinates(atomContainer: IAtomContainer): List<IAtom> {
val atomContainer3D = withContext(Dispatchers.Default) {
modelBuilder.generate3DCoordinates(atomContainer, false)
}
return atomContainer3D.atoms().toList()
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/plweegie/magmolecular/ar/MagMolActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.plweegie.magmolecular.ar

import androidx.appcompat.app.AppCompatActivity
import com.google.ar.sceneform.ux.ArFragment


class MagMolActivity : AppCompatActivity() {

private lateinit var arFragment: ArFragment

}
59 changes: 59 additions & 0 deletions app/src/main/java/com/plweegie/magmolecular/ar/MagMolFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.plweegie.magmolecular.ar

import android.content.Context
import android.util.DisplayMetrics
import com.google.ar.sceneform.math.Quaternion
import com.google.ar.sceneform.math.Vector3
import com.google.ar.sceneform.ux.*


class MagMolFragment : ArFragment() {

companion object {
private val MIN_OPENGL_VERSION = 3.1
}

private val displayMetrics: DisplayMetrics by lazy {
val metrics = DisplayMetrics()
activity?.windowManager?.defaultDisplay?.getMetrics(metrics)
metrics
}

override fun onAttach(context: Context) {
super.onAttach(context)
//TODO add OpenGL check
}

override fun makeTransformationSystem(): TransformationSystem {

val transformationSystem = TransformationSystem(displayMetrics, null)
val gesturePointersUtility = GesturePointersUtility(displayMetrics)

val dragGestureRecognizer = DragGestureRecognizer(gesturePointersUtility)

dragGestureRecognizer.addOnGestureStartedListener {
val startPosition = it.position
var endPosition = Vector3.zero()

it.setGestureEventListener(object : BaseGesture.OnGestureEventListener<DragGesture> {
override fun onUpdated(gesture: DragGesture?) {
endPosition = gesture?.position
val angle = getRotationAngle(startPosition, endPosition)
gesture?.targetNode?.localRotation = Quaternion.multiply(
gesture?.targetNode?.localRotation,
Quaternion.axisAngle(Vector3.up(), angle)
)
}

override fun onFinished(gesture: DragGesture?) {}
})
}
transformationSystem.addGestureRecognizer(dragGestureRecognizer)
return transformationSystem
}

private fun getRotationAngle(startPosition: Vector3, endPosition: Vector3): Float {
val diff = endPosition.x - startPosition.x
return (10 * diff) / displayMetrics.widthPixels
}
}
Loading

0 comments on commit 09f51d2

Please sign in to comment.