首页 > 解决方案 > 如何根据 kotlin 中的名称替换列表中的项目?

问题描述

我正在 Android Studio 中制作一个应用程序,它获取 DNA 并将其转录为 mRNA。我也想显示相应的氨基酸,但我有点卡住了。到目前为止,我所能做的就是从 mRNA 序列中列出一个列表,并将它们分成 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)
        }
    }
}

我想用正确的氨基酸替换列表中的每个项目,例如,而不是输出是: [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>```

标签: androidkotlin

解决方案


您需要做的是,指定从 mRna 密码子到氨基酸的映射,一种方法是定义一个函数

// 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

从有效且权威的来源获取映射,例如这个看起来不错,但请进行研究。


推荐阅读