{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#default_exp liftover"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#hide\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Liftover module\n",
    "\n",
    "> Liftover genodata and sumstat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#hide\n",
    "from nbdev.showdoc import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "import gzip\n",
    "from liftover import get_lifter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "class Liftover:\n",
    "    def __init__(self,fr='hg19',to='hg38'):\n",
    "        self.fr,self.to = fr,to\n",
    "        self.chainmap = get_lifter(fr, to)\n",
    "        #self.fasta \n",
    "              \n",
    "    def variants_liftover(self,chrom,pos):\n",
    "        if len(chrom) == 1:\n",
    "            chrom = [chrom]*len(pos)\n",
    "        lchr,lpos = [],[]\n",
    "        for c,p in zip(chrom,pos):\n",
    "            new_c,new_p = self.chrpos_liftover(c,p)\n",
    "            lchr.append(new_c)\n",
    "            lpos.append(new_p)\n",
    "        return lchr,lpos\n",
    "    \n",
    "    def chrpos_liftover(self,chrom,pos):\n",
    "        try:\n",
    "            if str(chrom) in ['X','chrX','23']:\n",
    "                new_c,new_p,_ = self.chainmap['X'][pos][0]\n",
    "                return 23,new_p\n",
    "            elif str(chrom) in ['Y','chrY','24']:\n",
    "                new_c,new_p,_ = self.chainmap['Y'][pos][0]\n",
    "                return 24,new_p\n",
    "            elif str(chrom) in ['M','chrM','25','MT']:\n",
    "                new_c,new_p,_ = self.chainmap['M'][pos][0]\n",
    "                return 25,new_p\n",
    "            else:\n",
    "                new_c,new_p,_ = self.chainmap[int(chrom)][pos][0]\n",
    "            return int(new_c[3:]),new_p\n",
    "        except:\n",
    "            return 0,0\n",
    "           \n",
    "        #The function to liftover bim\n",
    "    def bim_liftover(self,bim):\n",
    "        new_bim = bim.copy()\n",
    "        lchr,lpos = self.variants_liftover(bim.chrom,bim.pos)\n",
    "        new_bim.chrom =lchr\n",
    "        new_bim.pos = lpos\n",
    "        new_bim.snp = 'chr'+new_bim[['chrom','pos','a0','a1']].astype(str).agg(':'.join, axis=1)\n",
    "        return new_bim\n",
    "\n",
    "    \n",
    "    def sumstat_liftover(self,ss):\n",
    "        new_ss = ss.copy()\n",
    "        lchr,lpos = self.variants_liftover(ss.CHR,ss.POS)\n",
    "        new_ss.CHR =lchr\n",
    "        new_ss.POS = lpos\n",
    "        new_ss.SNP = 'chr'+new_ss[['CHR','POS','REF','ALT']].astype(str).agg(':'.join, axis=1)\n",
    "        return new_ss\n",
    "    \n",
    "    def vcf_liftover(self,vcf,vcf_out=None,remove_missing = True):\n",
    "        if vcf_out is None:\n",
    "            vcf_out = vcf[:-7]+'_'+self.fr+'To'+self.to+vcf[-7:]\n",
    "        count_fail,total= 0,0\n",
    "        with gzip.open(vcf, 'rt') as ifile:\n",
    "            with gzip.open(vcf_out,'wt') as ofile:\n",
    "                for line in ifile:\n",
    "                    if line.startswith(\"#\"):\n",
    "                        ofile.write(line)\n",
    "                    else:\n",
    "                        variant = [x for x in line.split('\\t')]\n",
    "                        new_c,new_p = self.chrpos_liftover(variant[0],int(variant[1]))\n",
    "                        total +=1\n",
    "                        if new_c == 0:\n",
    "                            count_fail +=1\n",
    "                            if remove_missing:\n",
    "                                continue\n",
    "                        variant[0] = str(new_c)\n",
    "                        variant[1] = str(new_p)\n",
    "                        variant[2] = 'chr'+':'.join(variant[:2]+variant[3:5])\n",
    "                        ofile.write('\\t'.join(variant))\n",
    "            ofile.close()       \n",
    "        ifile.close()\n",
    "        if remove_missing:\n",
    "            print(\"Total number SNPs \",total,\". Removing SNPs failed to liftover \", count_fail)\n",
    "        else:\n",
    "            print(\"Total number SNPs \",total,\". The number of SNPs failed to liftover \", count_fail,\". Their chr and pos is replaced with 0, 0\")\n",
    "\n",
    "\n",
    "        \n",
    "    def region_liftover(self,region):\n",
    "        imp_cs,imp_start = self.chrpos_liftover(region[0],region[1])\n",
    "        imp_ce,imp_end = self.chrpos_liftover(region[0],region[2])\n",
    "        if imp_cs !=imp_ce:\n",
    "            raise ValueError('After liftover, the region is not in the same chromosome anymore.')\n",
    "        return imp_cs,imp_start,imp_end\n",
    "    \n",
    "    def df_liftover(self):\n",
    "        pass\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "424"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geno.bim.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from LDtools.genodata import *\n",
    "from LDtools.sumstat import Sumstat\n",
    "from LDtools.liftover import Liftover\n",
    "def main(input_path,output_path,fr='hg19',to='hg38',remove_missing=True):\n",
    "    lf = Liftover(fr,to)\n",
    "    print(\"liftover from \" + fr +\"to\" +to)\n",
    "    print(\"Removing SNPs failed to liftover is\", remove_missing)\n",
    "    #file type detection, sumstats, plink, vcf,gvcf, >>>future bgen\n",
    "    input_path = Path(input_path)\n",
    "    input_suffixes = set(input_path.suffixes)\n",
    "    output_path = Path(output_path)\n",
    "    if not input_path.exists(): print(\"The file is not exist:\", input_path)\n",
    "    if input_path.suffix in ['.bim','.bed','.fam']:\n",
    "        geno = Genodata(str(input_path.with_suffix('.bed')))\n",
    "        new_bim = lf.bim_liftover(geno.bim)\n",
    "        idx = new_bim.chrom == 0\n",
    "        if remove_missing:\n",
    "            geno.bim = new_bim\n",
    "            geno.extractbyidx(~idx)\n",
    "            geno.export_plink(output_path.with_suffix('.bed'))\n",
    "            print(\"Total number SNPs \",new_bim.shape[0],\". Removing SNPs failed to liftover \", sum(idx))\n",
    "        else:\n",
    "            write_bim(output_path.with_suffix('.bim'),new_bim)\n",
    "            print(\"Total number SNPs \",new_bim.shape[0],\". The number of SNPs failed to liftover \", sum(idx),\". Their chr and pos is replaced with 0, 0\")\n",
    "    elif len(input_suffixes.intersection(['.gvcf','.vcf']))>0:\n",
    "        lf.vcf_liftover(input_path,output_path,remove_missing)\n",
    "    else:\n",
    "        print(\"This file is considered as sumstat format file\")\n",
    "        sums = Sumstat(input_path)\n",
    "        new_sums = lf.sumstat_liftover(sums.ss)\n",
    "        idx = new_sums.CHR == 0\n",
    "        if remove_missing:\n",
    "            new_sums[~idx].to_csv(output_path, compression='gzip', sep = \"\\t\", header = True, index = False)\n",
    "            print(\"Total number SNPs \",new_sums.shape[0],\". Removing SNPs failed to liftover \", sum(idx))\n",
    "        else:\n",
    "            new_sums.to_csv(output_path, compression='gzip', sep = \"\\t\", header = True, index = False)\n",
    "            print(\"Total number SNPs \",new_sums.shape[0],\". The number of SNPs failed to liftover \", sum(idx),\". Their chr and pos is replaced with 0, 0\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [],
   "source": [
    "from glob import glob"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = Path('data/GH.AR.SAD.P1.001.0_X3547_S42_1180478_GVCF.hard-filtered.gvcf.gz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmp.suffix[1:] in ['bim','bed','fam']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Path('test.bed')"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmp.with_suffix('.bed')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.Test liftover vcf and gvcf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "lf = Liftover('hg19','hg38')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lf.chainmap[22][50549067]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [],
   "source": [
    "vcf ='data/GH.AR.SAD.P1.001.0_X3547_S42_1180478_GVCF.hard-filtered.gvcf.gz'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "liftover from hg19tohg38\n",
      "Removing SNPs failed to liftover is True\n",
      "Total number SNPs  93816694 . The number of SNPs failed to liftover  59995\n"
     ]
    }
   ],
   "source": [
    "main(vcf,'data/new_hg19_hg38_test.gvcf.gz',remove_missing=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 272626, 313413)"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lf.region_liftover([5,272741,1213528-900000])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lf.vcf_liftover(vcf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0006394917305442463"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "59995/93816694"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.Test liftover plink"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "from LDtools.genodata import Genodata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "geno = Genodata('/mnt/mfs/statgen/guangyou/imputation/genome/othergenes/UKB_exome_othergenes.bed')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "liftover from hg19tohg38\n",
      "Removing SNPs failed to liftover is True\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/mnt/mfs/statgen/yin/Github/LDtools/LDtools/genodata.py:83: PerformanceWarning: Slicing is producing a large chunk. To accept the large\n",
      "chunk and silence this warning, set the option\n",
      "    >>> with dask.config.set(**{'array.slicing.split_large_chunks': False}):\n",
      "    ...     array[indexer]\n",
      "\n",
      "To avoid creating the large chunks, set the option\n",
      "    >>> with dask.config.set(**{'array.slicing.split_large_chunks': True}):\n",
      "    ...     array[indexer]\n",
      "  geno = geno[idx,:]\n",
      "Writing BED: 100%|██████████| 1/1 [00:15<00:00, 15.11s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing FAM... "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "done.\n",
      "Writing BIM... done.\n",
      "Total number SNPs  1413 . Removing SNPs failed to liftover  0\n"
     ]
    }
   ],
   "source": [
    "main('/mnt/mfs/statgen/guangyou/imputation/genome/othergenes/UKB_exome_othergenes.bed','test2.bed')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "liftover from hg19tohg38\n",
      "Removing SNPs failed to liftover is True\n",
      "This file is considered as sumstat format file\n"
     ]
    }
   ],
   "source": [
    "main('MWE_region_extraction/100521_UKBB_Hearing_aid_f3393_expandedwhite_15601cases_237318ctrl_500k_PC1_PC2_f3393.regenie.snp_stats.gz','test_sumstats.sumstats.gz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bim:      chrom                  snp   cm        pos a0 a1     i\n",
       "0         1    chr1:55039741:G:C  0.0   55039741  G  C     0\n",
       "1         1    chr1:55039742:G:A  0.0   55039742  G  A     1\n",
       "2         1    chr1:55039749:G:C  0.0   55039749  G  C     2\n",
       "3         1    chr1:55039750:T:C  0.0   55039750  T  C     3\n",
       "4         1    chr1:55039753:T:C  0.0   55039753  T  C     4\n",
       "...     ...                  ...  ...        ... .. ..   ...\n",
       "1408     11  chr11:116832956:T:C  0.0  116832956  T  C  1408\n",
       "1409     11  chr11:116832976:C:G  0.0  116832976  C  G  1409\n",
       "1410     11  chr11:116832977:T:G  0.0  116832977  T  G  1410\n",
       "1411     11  chr11:116832978:T:G  0.0  116832978  T  G  1411\n",
       "1412     11  chr11:116832980:T:C  0.0  116832980  T  C  1412\n",
       "\n",
       "[1413 rows x 7 columns] \n",
       " fam:            fid      iid father mother gender trait       i\n",
       "0       1000019  1000019      0      0      2    -9       0\n",
       "1       1000078  1000078      0      0      2    -9       1\n",
       "2       1000081  1000081      0      0      1    -9       2\n",
       "3       1000198  1000198      0      0      2    -9       3\n",
       "4       1000210  1000210      0      0      1    -9       4\n",
       "...         ...      ...    ...    ...    ...   ...     ...\n",
       "168201  6025295  6025295      0      0      1    -9  168201\n",
       "168202  6025319  6025319      0      0      2    -9  168202\n",
       "168203  6025346  6025346      0      0      2    -9  168203\n",
       "168204  6025363  6025363      0      0      1    -9  168204\n",
       "168205  6025411  6025411      0      0      2    -9  168205\n",
       "\n",
       "[168206 rows x 7 columns] \n",
       " bed:dask.array<transpose, shape=(1413, 168206), dtype=float32, chunksize=(1024, 1024), chunktype=numpy.ndarray>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geno"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "geno.extractbyidx(~(geno.bim.chrom == 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "Writing BED:   0%|          | 0/1 [00:00<?, ?it/s]\u001b[A\n",
      "Writing BED: 100%|██████████| 1/1 [00:02<00:00,  2.26s/it]\u001b[A"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing FAM... "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "done.\n",
      "Writing BIM... done.\n"
     ]
    }
   ],
   "source": [
    "geno.export_plink('test1.bed')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Test liftover sumstat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from LDtools.sumstat import Sumstat\n",
    "from LDtools.liftover import Liftover\n",
    "def gwas_liftover(input_path,output_path,output_unmapped,output_mapped,fr='hg19',to='hg38',remove_missing=False):\n",
    "    lf = Liftover(fr,to)\n",
    "    print(\"reading GWAS sumstat\")\n",
    "    sums = Sumstat(input_path)\n",
    "    print(\"liftover from\" + fr +\"to\" +to)\n",
    "    sums1 = lf.sumstat_liftover(sums.ss)\n",
    "    if remove_missing:\n",
    "        sums1[sums1.CHR == 0].to_csv(output_unmapped, compression='gzip', sep = \"\\t\", header = True, index = False)\n",
    "        sums1[sums1.CHR != 0].to_csv(output_mapped, compression='gzip', sep = \"\\t\", header = True, index = False)\n",
    "    else:\n",
    "        sums1.to_csv(output_path, compression='gzip', sep = \"\\t\", header = True, index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gwas_liftover(input_file,output_path=None,fr='hg19',to='hg38',remove_missing=False):\n",
    "    if output_path is None:\n",
    "        output_path = os.path.dirname(input_file)+'/'\n",
    "    basename = os.path.basename(input_file)\n",
    "    lf = Liftover('hg19','hg38')\n",
    "    print(\"reading GWAS sumstat\")\n",
    "    sums = Sumstat(input_path)\n",
    "    print(\"liftover from\" + fr +\"to\" +to)\n",
    "    sums1 = lf.sumstat_liftover(sums.ss)\n",
    "    if remove_missing:\n",
    "        sums1[sums1.CHR == 0].to_csv(output_unmapped, compression='gzip', sep = \"\\t\", header = True, index = False)\n",
    "        sums1[sums1.CHR != 0].to_csv(output_mapped, compression='gzip', sep = \"\\t\", header = True, index = False)\n",
    "    else:\n",
    "        sums1.to_csv(output_path, compression='gzip', sep = \"\\t\", header = True, index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sumstats_lifted = f'{cwd}/{_input:bnn}.hg38.sumstats.gz',\n",
    "sumstats_unmapped = f'{cwd}/{_input:bnn}.hg38.sumstats_unmapped.gz',\n",
    "sumstats_mapped = f'{cwd}/{_input:bnn}.hg38.sumstats_mapped.gz'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = os.path.basename(input_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('100521_UKBB_Combined_f2247_f2257_expandedwhite_93258cases_237318ctrl_500k_PC1_PC2_f2247_f2257.regenie.snp_stats',\n",
       " '.gz')"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.path.splitext(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/home/dmc2245/UKBiobank/results/REGENIE_results/results_imputed_data/2021_10_07_combined_500K/'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.path.dirname(input_path)+'/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "sums = Sumstat(input_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "lf = Liftover('hg19','hg38')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CHR</th>\n",
       "      <th>POS</th>\n",
       "      <th>REF</th>\n",
       "      <th>ALT</th>\n",
       "      <th>SNP</th>\n",
       "      <th>BETA</th>\n",
       "      <th>SE</th>\n",
       "      <th>P</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>13259</td>\n",
       "      <td>G</td>\n",
       "      <td>A</td>\n",
       "      <td>chr1:13259:G:A</td>\n",
       "      <td>0.434586</td>\n",
       "      <td>0.175780</td>\n",
       "      <td>0.014801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>17569</td>\n",
       "      <td>C</td>\n",
       "      <td>A</td>\n",
       "      <td>chr1:17569:C:A</td>\n",
       "      <td>-0.030568</td>\n",
       "      <td>0.795968</td>\n",
       "      <td>0.969366</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>17641</td>\n",
       "      <td>G</td>\n",
       "      <td>A</td>\n",
       "      <td>chr1:17641:G:A</td>\n",
       "      <td>-0.078881</td>\n",
       "      <td>0.108663</td>\n",
       "      <td>0.467883</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>30741</td>\n",
       "      <td>C</td>\n",
       "      <td>A</td>\n",
       "      <td>chr1:30741:C:A</td>\n",
       "      <td>-1.599610</td>\n",
       "      <td>0.990472</td>\n",
       "      <td>0.044798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>57222</td>\n",
       "      <td>T</td>\n",
       "      <td>C</td>\n",
       "      <td>chr1:57222:T:C</td>\n",
       "      <td>0.031666</td>\n",
       "      <td>0.121422</td>\n",
       "      <td>0.794253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>58396</td>\n",
       "      <td>T</td>\n",
       "      <td>C</td>\n",
       "      <td>chr1:58396:T:C</td>\n",
       "      <td>0.366266</td>\n",
       "      <td>0.172004</td>\n",
       "      <td>0.035663</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>62157</td>\n",
       "      <td>G</td>\n",
       "      <td>A</td>\n",
       "      <td>chr1:62157:G:A</td>\n",
       "      <td>-0.147251</td>\n",
       "      <td>0.296105</td>\n",
       "      <td>0.618983</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1</td>\n",
       "      <td>62595</td>\n",
       "      <td>C</td>\n",
       "      <td>T</td>\n",
       "      <td>chr1:62595:C:T</td>\n",
       "      <td>0.356993</td>\n",
       "      <td>0.171623</td>\n",
       "      <td>0.040096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1</td>\n",
       "      <td>69487</td>\n",
       "      <td>G</td>\n",
       "      <td>A</td>\n",
       "      <td>chr1:69487:G:A</td>\n",
       "      <td>-0.559373</td>\n",
       "      <td>0.853882</td>\n",
       "      <td>0.512407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1</td>\n",
       "      <td>69569</td>\n",
       "      <td>T</td>\n",
       "      <td>C</td>\n",
       "      <td>chr1:69569:T:C</td>\n",
       "      <td>0.232690</td>\n",
       "      <td>0.216585</td>\n",
       "      <td>0.282662</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   CHR    POS REF ALT             SNP      BETA        SE         P\n",
       "0    1  13259   G   A  chr1:13259:G:A  0.434586  0.175780  0.014801\n",
       "1    1  17569   C   A  chr1:17569:C:A -0.030568  0.795968  0.969366\n",
       "2    1  17641   G   A  chr1:17641:G:A -0.078881  0.108663  0.467883\n",
       "3    1  30741   C   A  chr1:30741:C:A -1.599610  0.990472  0.044798\n",
       "4    1  57222   T   C  chr1:57222:T:C  0.031666  0.121422  0.794253\n",
       "5    1  58396   T   C  chr1:58396:T:C  0.366266  0.172004  0.035663\n",
       "6    1  62157   G   A  chr1:62157:G:A -0.147251  0.296105  0.618983\n",
       "7    1  62595   C   T  chr1:62595:C:T  0.356993  0.171623  0.040096\n",
       "8    1  69487   G   A  chr1:69487:G:A -0.559373  0.853882  0.512407\n",
       "9    1  69569   T   C  chr1:69569:T:C  0.232690  0.216585  0.282662"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lf.sumstat_liftover(sums.ss[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def main(input_path,output_path,remove_missing):\n",
    "    sums = read_regenie(input_path)\n",
    "    sums1 = sumstat_liftover(sums)\n",
    "    if remove_missing:\n",
    "        sums1[sums1.CHR == 0].to_csv(output_path, sep = \"\\t\", header = True, index = False)\n",
    "    else:\n",
    "        sums1.to_csv(output_path, sep = \"\\t\", header = True, index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_path = '/home/dmc2245/UKBiobank/results/REGENIE_results/results_imputed_data/2021_10_07_combined_500K/100521_UKBB_Combined_f2247_f2257_expandedwhite_93258cases_237318ctrl_500k_PC1_PC2_f2247_f2257.regenie.snp_stats.gz'\n",
    "output_path = ''\n",
    "remove_missing = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "main(input_path,output_path,remove_missing)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "sequence item 0: expected str instance, int found",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m/tmp/1967998.1.high_mem.q/ipykernel_39087/731612175.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m':'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'1'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: sequence item 0: expected str instance, int found"
     ]
    }
   ],
   "source": [
    "':'.join([1,'1'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## test chr22"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this region [22, 50519304, 50549676] has 669 SNPs in Sumstat\n"
     ]
    }
   ],
   "source": [
    "region = [22,50519304,50549676]\n",
    "exome_sumstats = Sumstat('/home/dmc2245/UKBiobank/results/REGENIE_results/results_imputed_data/010522_f2247_hearing_diff_200K_imputed/090321_UKBB_Hearing_difficulty_f2247_expandedwhite_45502cases_96601ctrl_PC1_2_f2247.regenie.snp_stats.gz')\n",
    "exome_sumstats.extractbyregion(region)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('1.2. LiftOver the region')\n",
    "hg38toimpref = Liftover('hg38','hg19')\n",
    "imp_region = hg38toimpref.region_liftover(region)\n",
    "imput_sumstats.extractbyregion(imp_region)\n",
    "\n",
    "print('1.3. Regional SNPs Liftover')\n",
    "impreftohg38 = Liftover(imp_ref,'hg38') #oppsite with hg38toimpref\n",
    "impreftohg38."
   ]
  }
 ],
 "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
