{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# DISCLAIMER\n\nThis will be and ongoing work where I'm going to put some functions used in **RNA bioinformatics**, just for fun and to keep practicing the usage of **LLMs**","metadata":{}},{"cell_type":"markdown","source":"# Intalling Libraries ","metadata":{}},{"cell_type":"code","source":"!pip install arnie forgi openai langchain\n!conda install -y -c bioconda viennarna","metadata":{"execution":{"iopub.status.busy":"2023-09-09T20:54:49.256445Z","iopub.execute_input":"2023-09-09T20:54:49.257113Z","iopub.status.idle":"2023-09-09T20:57:10.715996Z","shell.execute_reply.started":"2023-09-09T20:54:49.257072Z","shell.execute_reply":"2023-09-09T20:57:10.714965Z"},"_kg_hide-output":true,"_kg_hide-input":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Environment","metadata":{}},{"cell_type":"code","source":"# Install Eternafold; not available via conda-forge quite yet, so we install from the pipeline build artifacts directly\n# !conda install eternafold\n!wget https://artprodeus21.artifacts.visualstudio.com/A910fa339-c7c2-46e8-a579-7ea247548706/84710dde-1620-425b-80d0-4cf5baca359d/_apis/artifact/cGlwZWxpbmVhcnRpZmFjdDovL2NvbmRhLWZvcmdlL3Byb2plY3RJZC84NDcxMGRkZS0xNjIwLTQyNWItODBkMC00Y2Y1YmFjYTM1OWQvYnVpbGRJZC83NzI5MTAvYXJ0aWZhY3ROYW1lL2NvbmRhX3BrZ3NfbGludXg1/content?format=zip\n!unzip content\\?format\\=zip\n!conda install conda_pkgs_linux/eternafold-1.3.1-h00ab1b0_0.conda","metadata":{"execution":{"iopub.status.busy":"2023-09-09T20:57:10.718322Z","iopub.execute_input":"2023-09-09T20:57:10.718735Z","iopub.status.idle":"2023-09-09T20:57:29.974383Z","shell.execute_reply.started":"2023-09-09T20:57:10.718700Z","shell.execute_reply":"2023-09-09T20:57:29.973258Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Ordinarily, the `EternaFold` conda package will automatically set necessary environment variables, but Kaggle's conda install works a little differently. Let's set them manually here using `%env`.","metadata":{}},{"cell_type":"code","source":"%env ETERNAFOLD_PATH=/opt/conda/bin/eternafold-bin\n%env ETERNAFOLD_PARAMETERS=/opt/conda/lib/eternafold-lib/parameters/EternaFoldParams.v1","metadata":{"execution":{"iopub.status.busy":"2023-09-09T20:57:29.976452Z","iopub.execute_input":"2023-09-09T20:57:29.976770Z","iopub.status.idle":"2023-09-09T20:57:29.984595Z","shell.execute_reply.started":"2023-09-09T20:57:29.976741Z","shell.execute_reply":"2023-09-09T20:57:29.982917Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Libraries","metadata":{}},{"cell_type":"code","source":"### General ###\nimport os\nimport warnings \nfrom dotenv import load_dotenv\n\n### Data Handling ###\nimport pandas as pd\n\n### Bioinformatics ###\nfrom arnie.mfe import mfe\nfrom arnie.bpps import bpps\nimport forgi.visual.mplotlib as fvm\nimport forgi.graph.bulge_graph as fgb\n\n### Viz ###\nimport matplotlib.pyplot as plt\n\n### LLMs ###\nimport openai\nfrom langchain.tools import tool\nfrom langchain.agents import AgentType\nfrom langchain.chat_models import ChatOpenAI\nfrom langchain.agents import initialize_agent\nfrom langchain.memory import ConversationBufferMemory\n\nwarnings.filterwarnings(\"ignore\", category = RuntimeWarning)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T20:57:29.986735Z","iopub.execute_input":"2023-09-09T20:57:29.987035Z","iopub.status.idle":"2023-09-09T20:57:35.035982Z","shell.execute_reply.started":"2023-09-09T20:57:29.987008Z","shell.execute_reply":"2023-09-09T20:57:35.035118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"load_dotenv(\"/kaggle/input/langchaincoursedata/.env\")\n\nos.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\")\nopenai.api_key  = os.environ[\"OPENAI_API_KEY\"]","metadata":{"execution":{"iopub.status.busy":"2023-09-09T20:57:35.037079Z","iopub.execute_input":"2023-09-09T20:57:35.037734Z","iopub.status.idle":"2023-09-09T20:57:35.046724Z","shell.execute_reply.started":"2023-09-09T20:57:35.037702Z","shell.execute_reply":"2023-09-09T20:57:35.045616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/stanford-ribonanza-rna-folding/train_data.csv\", nrows = 10)\nsequence = train[train[\"sequence_id\"] == \"8cdfeef009ea\"][\"sequence\"][0]\nsequence","metadata":{"execution":{"iopub.status.busy":"2023-09-09T20:57:35.048133Z","iopub.execute_input":"2023-09-09T20:57:35.048524Z","iopub.status.idle":"2023-09-09T20:57:35.108506Z","shell.execute_reply.started":"2023-09-09T20:57:35.048489Z","shell.execute_reply":"2023-09-09T20:57:35.107790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Tools","metadata":{}},{"cell_type":"code","source":"tools = []","metadata":{"execution":{"iopub.status.busy":"2023-09-09T20:57:35.109414Z","iopub.execute_input":"2023-09-09T20:57:35.110260Z","iopub.status.idle":"2023-09-09T20:57:35.114886Z","shell.execute_reply.started":"2023-09-09T20:57:35.110230Z","shell.execute_reply":"2023-09-09T20:57:35.114046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@tools.append\n@tool\ndef dot_bracket_notation(sequence: str):\n    \"\"\"\n    Útil para obtener la \"dot-bracket notatition\" de una molécula de ARN a partir de su secuencia.\n    \"\"\"\n    try:\n        structure = mfe(sequence, package = \"eternafold\")\n        \n        return f\"La estructura dot-bracket de la secuencia del ARN {sequence} es {structure}.\"\n    except Exception as e:\n        return f\"Ocurrió un error: {e}\"","metadata":{"execution":{"iopub.status.busy":"2023-09-09T20:57:35.116119Z","iopub.execute_input":"2023-09-09T20:57:35.116498Z","iopub.status.idle":"2023-09-09T20:57:35.132949Z","shell.execute_reply.started":"2023-09-09T20:57:35.116464Z","shell.execute_reply":"2023-09-09T20:57:35.132219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@tools.append\n@tool\ndef draw_rna(sequence):\n    \"\"\"\n    Útil para dibujar la estructura de una molécula de ARN.\n    \"\"\"\n    try:\n        structure = mfe(sequence, package = \"eternafold\")\n        plt.figure(figsize = (10, 10))\n        bg = fgb.BulgeGraph.from_fasta_text(f'>rna1\\n{structure}\\n{sequence}')[0]\n        fvm.plot_rna(bg, lighten=0.5, text_kwargs = {\"fontweight\":None})\n        plt.show()\n        \n        return f\"La estructura y secuencia del ARN han sido dibujadas.\"\n    except Exception as e:\n        return f\"Ocurrió un error: {e}\"","metadata":{"execution":{"iopub.status.busy":"2023-09-09T20:57:35.134143Z","iopub.execute_input":"2023-09-09T20:57:35.134446Z","iopub.status.idle":"2023-09-09T20:57:35.148036Z","shell.execute_reply.started":"2023-09-09T20:57:35.134421Z","shell.execute_reply":"2023-09-09T20:57:35.147360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Agent","metadata":{}},{"cell_type":"code","source":"memory = ConversationBufferMemory(memory_key = \"chat_history\", return_messages = True)\nllm = ChatOpenAI(temperature = 0)\nagent = initialize_agent(\n    tools, \n    llm, \n    agent = AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, \n    memory = memory\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T20:57:35.150194Z","iopub.execute_input":"2023-09-09T20:57:35.151365Z","iopub.status.idle":"2023-09-09T20:57:35.161486Z","shell.execute_reply.started":"2023-09-09T20:57:35.151318Z","shell.execute_reply":"2023-09-09T20:57:35.160633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Queries","metadata":{}},{"cell_type":"code","source":"agent.run(\n    f\"\"\"\n    Would you mind to compute the dot-bracket notation for the following sequence {sequence}\n    \"\"\"\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T20:57:35.164805Z","iopub.execute_input":"2023-09-09T20:57:35.165327Z","iopub.status.idle":"2023-09-09T20:57:41.781401Z","shell.execute_reply.started":"2023-09-09T20:57:35.165283Z","shell.execute_reply":"2023-09-09T20:57:41.780584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"agent.run(\n    f\"\"\"\n    Can you draw the structure of the sequence {sequence}\n    \"\"\"\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-09T20:57:41.782731Z","iopub.execute_input":"2023-09-09T20:57:41.783285Z","iopub.status.idle":"2023-09-09T20:57:49.177193Z","shell.execute_reply.started":"2023-09-09T20:57:41.783254Z","shell.execute_reply":"2023-09-09T20:57:49.176198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Can you notice that my comment was translated from spanish to english without asking for that?","metadata":{}}],"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}}