{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9a264fcd-74f7-4fd9-8bc1-e9a2053f8682",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ebf78598-df67-404e-9d31-743f79afbc26",
   "metadata": {},
   "outputs": [],
   "source": [
    "#augmented files\n",
    "train_filename = './summits_to_topics_wochr2.fa'\n",
    "test_filename = './summits_to_topics_chr2.fa'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "67e678ab-e84e-43e3-95da-7ac2c9c8fbf3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def readfile(filename):\n",
    "    ids = []\n",
    "    ids_d = {}\n",
    "    seqs = {}\n",
    "    classes = {}\n",
    "    f = open(filename, 'r')\n",
    "    lines = f.readlines()\n",
    "    f.close()\n",
    "    seq = []\n",
    "    for line in lines:\n",
    "        if line[0] == '>':\n",
    "            ids.append(line[1:].rstrip('\\n'))\n",
    "            if line[1:].rstrip('\\n').split('_')[0] not in seqs:\n",
    "                seqs[line[1:].rstrip('\\n').split('_')[0]] = []\n",
    "            if line[1:].rstrip('\\n').split('_')[0] not in ids_d:\n",
    "                ids_d[line[1:].rstrip('\\n').split('_')[0]] = line[1:].rstrip('\\n').split('_')[0]\n",
    "            if line[1:].rstrip('\\n').split('_')[0] not in classes:\n",
    "                classes[line[1:].rstrip('\\n').split('_')[0]] = np.zeros(NUM_CLASSES)\n",
    "            \n",
    "            classes[line[1:].rstrip('\\n').split('_')[0]][int(line[1:].rstrip('\\n').split('_')[1])-1] = 1        \n",
    "            if seq != []: seqs[ids[-2].split('_')[0]]= (\"\".join(seq))\n",
    "            seq = []\n",
    "        else:\n",
    "            seq.append(line.rstrip('\\n').upper())\n",
    "    if seq != []:\n",
    "        seqs[ids[-1].split('_')[0]]=(\"\".join(seq))\n",
    "\n",
    "    return ids,ids_d,seqs,classes\n",
    "\n",
    "def one_hot_encode_along_row_axis(sequence):\n",
    "    to_return = np.zeros((1,len(sequence),4), dtype=np.int8)\n",
    "    seq_to_one_hot_fill_in_array(zeros_array=to_return[0],\n",
    "                                 sequence=sequence, one_hot_axis=1)\n",
    "    return to_return\n",
    "\n",
    "def seq_to_one_hot_fill_in_array(zeros_array, sequence, one_hot_axis):\n",
    "    assert one_hot_axis==0 or one_hot_axis==1\n",
    "    if (one_hot_axis==0):\n",
    "        assert zeros_array.shape[1] == len(sequence)\n",
    "    elif (one_hot_axis==1): \n",
    "        assert zeros_array.shape[0] == len(sequence)\n",
    "    for (i,char) in enumerate(sequence):\n",
    "        if (char==\"A\" or char==\"a\"):\n",
    "            char_idx = 0\n",
    "        elif (char==\"C\" or char==\"c\"):\n",
    "            char_idx = 1\n",
    "        elif (char==\"G\" or char==\"g\"):\n",
    "            char_idx = 2\n",
    "        elif (char==\"T\" or char==\"t\"):\n",
    "            char_idx = 3\n",
    "        elif (char==\"N\" or char==\"n\"):\n",
    "            continue #leave that pos as all 0's\n",
    "        else:\n",
    "            raise RuntimeError(\"Unsupported character: \"+str(char))\n",
    "        if (one_hot_axis==0):\n",
    "            zeros_array[char_idx,i] = 1\n",
    "        elif (one_hot_axis==1):\n",
    "            zeros_array[i,char_idx] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2e168b04-bac7-43f3-aca6-f006a0f65c9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "NUM_CLASSES = 24\n",
    "selected_classes = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24])-1\n",
    "\n",
    "train_ids, train_ids_d, train_seqs, train_classes = readfile(train_filename)\n",
    "X_train = np.array([one_hot_encode_along_row_axis(train_seqs[id]) for id in train_ids_d]).squeeze(axis=1)\n",
    "y_train = np.array([train_classes[id] for id in train_ids_d])\n",
    "y_train=y_train[:,selected_classes]\n",
    "X_train = X_train[y_train.sum(axis=1)>0]\n",
    "y_train = y_train[y_train.sum(axis=1)>0]\n",
    "X_train_rc = X_train[:,::-1,::-1]\n",
    "train_data=[X_train, X_train_rc]\n",
    "\n",
    "test_ids, test_ids_d, test_seqs, test_classes = readfile(test_filename)\n",
    "X_test = np.array([one_hot_encode_along_row_axis(test_seqs[id]) for id in test_ids_d]).squeeze(axis=1)\n",
    "y_test = np.array([test_classes[id] for id in test_ids_d])\n",
    "y_test=y_test[:,selected_classes]\n",
    "X_test = X_test[y_test.sum(axis=1)>0]\n",
    "y_test = y_test[y_test.sum(axis=1)>0]\n",
    "\n",
    "X_test_rc = X_test[:,::-1,::-1]\n",
    "test_data=[X_test, X_test_rc]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9501d5cd-ab6f-4f3b-be6e-ea50348cc554",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(696654, 500, 4)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "328c939c-7719-4252-8ab3-89c48754e19f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(696654, 24)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b7fe5525-a257-4d31-8796-53245f5e5f2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(58086, 500, 4)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "574a4679-b4b2-4845-810d-a5d61958d593",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(58086, 24)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5629131-0e69-4874-bb77-f54d4cd91ff9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
