Как заменить элемент в списке на основе имени в kotlin? - PullRequest
0 голосов
/ 02 февраля 2020

Я подаю заявку в Android Studio, которая берет ДНК и транскрибирует ее в мРНК. Я также хочу отобразить соответствующие аминокислоты, но я немного застрял. Пока все, что я могу сделать, это составить список из последовательности мРНК и разделить их на группы по 3. Как показывает мой код:

package com.example.dnadecode

import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import java.nio.channels.NonReadableChannelException

class MainActivity : AppCompatActivity() {

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

        val button = findViewById<Button>(R.id.button)
        val button2 = findViewById<Button>(R.id.button2)
        val dnainput = findViewById<EditText>(R.id.dnainput)
        val dnaoutput = findViewById<TextView>(R.id.textView5)
        val button3 = findViewById<Button>(R.id.button3)
        val amino = findViewById<TextView>(R.id.amino)

        button.setOnClickListener {
            val ok = dnainput.text
            var okstr = ok.toString().toUpperCase()
            val result = okstr.replace(oldChar = 'G', newChar = '_').replace(oldChar = 'C', newChar = 'G').replace(oldChar = '_', newChar = 'C').replace(oldChar = 'A', newChar = 'U').replace(oldChar = 'T', newChar = 'A')
            val ogamino = result.replace("\\s".toRegex(), "")
            val newamino = ogamino.chunked(3)
            amino.text = newamino.toString()
            dnaoutput.text = result.toString()
        }
        button2.setOnClickListener {
            dnainput.text.clear()
            dnaoutput.text = ""
            amino.text = ""
        }
        button3.setOnClickListener {
            val openURL = Intent(Intent.ACTION_VIEW)
            openURL.data = Uri.parse("https://saucygames05.github.io/DNADecoder/help.html")
            startActivity(openURL)
        }
    }
}

Я хочу заменить каждый элемент в списке на правильный аминокислота, так, например, вместо того, чтобы th-вывод был: [AUG, GAA, UCC] Я хочу, чтобы он отображал: [Met, Glu, Ser] и то же самое для всех других возможных комбинаций, которые указывают, какая аминокислота используется. Это мой первый раз, когда я использую kotlin, поэтому я только отошел от своих знаний о python и немного о java. Мой XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:configChanges="orientation"
    android:screenOrientation="portrait"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="87dp"
        android:layout_height="25dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="24dp"
        android:text="DNA Code:"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="307dp"
        android:layout_height="47dp"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginEnd="24dp"
        android:layout_marginRight="24dp"
        android:text="Translate!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.785"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.203" />

    <EditText
        android:id="@+id/dnainput"
        android:layout_width="329dp"
        android:layout_height="53dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="40dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="180dp"
        android:text="mRNA Sequence:"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="24dp"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/textView3" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginBottom="16dp"
        android:text="Clear"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:text="help"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginBottom="180dp"
        android:text="Amino Acids:"
        app:layout_constraintBottom_toTopOf="@+id/button2"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/amino"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="24dp"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/textView6" />

</androidx.constraintlayout.widget.ConstraintLayout>```

1 Ответ

1 голос
/ 02 февраля 2020

Что вам нужно сделать, это указать отображение кодона мРна в аминокислоты, одним из способов будет определение функции

// Pass mRna codon and get amino acid, this is not complete list
// please refer some authorotative reference to get all the mappings, 
// once you have the mappings then use the below function to codify them
private fun mapToAmino(mRna: String): String{
    return when(mRna){
        // List all the codons that map to a specific amino acid
        "UUU","UUC" -> "Phe" 
        "UUA","UUG" -> "Leu"
        //These six map to "Ser" so list them as comma separated
        "UCU","UCC","UCA","UCG","AGU","AGC" -> "Ser"
        else -> "Not found"
    }
}

Затем в вашем activity вы можете выполнить следующее, чтобы получить list аминокислот.

val result = okstr.replace(oldChar = 'G', newChar = '_').replace(oldChar = 'C', newChar = 'G').replace(oldChar = '_', newChar = 'C').replace(oldChar = 'A', newChar = 'U').replace(oldChar = 'T', newChar = 'A')
val ogamino = result.replace("\\s".toRegex(), "")
val newamino = ogamino.chunked(3).map { mapToAmino(it) }
// newAmino is a list of all amino acids, given that you defined the mappings correctly

Получите сопоставления из действительного и авторитетного источника, например этот хорошо выглядит, но, пожалуйста, сделайте свое исследование.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...