# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:gsoc_ensembl]
#     language: python
#     name: conda-env-gsoc_ensembl-py
# ---

# +
####################################################################################################

#    Copyright 2019 <NAME> and EMBL-European Bioinformatics Institute

#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at

#        http://www.apache.org/licenses/LICENSE-2.0

#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

####################################################################################################
# -

# # Below cells : For all lincRNA data, Ensembl

import pandas as pd
from tqdm import tqdm


# %%time
def sort_file_via_chr(path):
    df = pd.read_csv(path, index_col=0)
    df['Seq region Name'] = df['Seq region Name'].map({'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12, '13': 13, '14': 14, '15': 15, '16': 16, '17': 17, '18': 18, '19': 19, '20': 20, '21': 21, '22': 22, 'X': 23, 'Y': 24 })
    df_sorted = df.sort_values(by=["Seq region Name"], ascending=True)
    di = {23: "X", 24: "Y"}
    df_sorted['Seq region Name'].replace(di, inplace=True)
    df_sorted.reset_index(drop=True, inplace=True)
    return df_sorted


# %%time
path = '/Ensembl_numeric/all_gene_data/without_M_K_G_seq_regions/all_transcript_data_without_MKG_seq.csv'
df_sorted = sort_file_via_chr(path)
df_sorted.to_csv('/dataset/Ensembl_numeric/all_gene_data/all_gene_sorted_chr/all_transcript_data_sorted_chr.csv')

# # Below cells : For all gene data, Ensembl. Removing 'M/K/G' seq region names

df = pd.read_csv('/dataset/Ensembl_numeric/all_gene_data/all_gene_data.csv', index_col=0)

df.head(4)

df['Seq region Name'].unique()


# %%time
def drop_other_seq_region_name(path):
    c = 0
    drop_index = []
    df = pd.read_csv(path, index_col=0)
    df['Seq region Name'] = df['Seq region Name'].astype(str)
    for i in tqdm(range(len(df['Seq region Name']))):
        if str(df['Seq region Name'][i]).startswith(('M', 'K', 'G')):
            drop_index.append(i)
            c = c + 1
        else:
            continue
    df.drop(drop_index, axis=0,inplace=True)
    df.reset_index(drop=True, inplace=True)
    return df, c, drop_index


# %%time
path = '/dataset/Ensembl_numeric/all_gene_data/all_exon_data.csv'
df, c, drop_index = drop_other_seq_region_name(path)
df.to_csv('/dataset/Ensembl_numeric/all_gene_data/without_M_K_G_seq_regions/all_exon_data_without_MKG_seq.csv')

df['Seq region Name'][261039]

print(c)

# +
# drop_index
# -

df.head(4)

len(df)

df['Seq region Name'].unique()

path = '/Ensembl_numeric/all_gene_data/without_M_K_G_seq_regions/all_exon_data_without_MKG_seq.csv'
df = pd.read_csv(path, index_col=0)


df['Seq region Name'].unique()

di = {1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "10", 11: "11", 12: "12", 13: "13", 14: "14", 15: "15", 16: "16", 17: "17", 18: "18", 19: "19", 21: "21", 22: "22"}
df['Seq region Name'].replace(di, inplace=True)

df['Seq region Name'].unique()

df.to_csv('/dataset/Ensembl_numeric/all_gene_data/without_M_K_G_seq_regions/all_exon_data_without_MKG_seq.csv')

len(df)


# # Below cell for sorting all exon data

# %%time
def sort_file_via_chr(path):
    df = pd.read_csv(path, index_col=0)
    di1 = {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12, '13': 13, '14': 14, '15': 15, '16': 16, '17': 17, '18': 18, '19': 19, '20': 20, '21': 21, '22': 22, 'X': 23, 'Y': 24}
    df['Seq region Name'].replace(di1, inplace=True)
#     df['Seq region Name'] = df['Seq region Name'].map({'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12, '13': 13, '14': 14, '15': 15, '16': 16, '17': 17, '18': 18, '19': 19, '20': 20, '21': 21, '22': 22, 'X': 23, 'Y': 24 })
    df_sorted = df.sort_values(by=["Seq region Name"], ascending=True)
    di = {23: "X", 24: "Y"}
    df_sorted['Seq region Name'].replace(di, inplace=True)
    df_sorted.reset_index(drop=True, inplace=True)
    return df_sorted


# %%time
path = '/dataset/Ensembl_numeric/all_gene_data/without_M_K_G_seq_regions/all_exon_data_without_MKG_seq.csv'
df_sorted = sort_file_via_chr(path)

df_sorted['Seq region Name'].unique()

len(df_sorted)

df_sorted.to_csv('/dataset/Ensembl_numeric/all_gene_data/all_gene_sorted_chr/all_exon_data_sorted_chr.csv')

path = '/all_gene_data/all_gene_sorted_chr/all_transcript_data_sorted_chr.csv'

df = pd.read_csv(path, index_col=0)

df['Seq region Name'].unique()

di = {'22': 22}
df['Seq region Name'].replace(di, inplace=True)

df['Seq region Name'].unique()
df.reset_index(drop=True, inplace=True)

di = {'X': 23, 'Y': 24}
df['Seq region Name'].replace(di, inplace=True)
df_sorted = df.sort_values(by=["Seq region Name"], ascending=True)
di = {23: "X", 24: "Y"}
df_sorted['Seq region Name'].replace(di, inplace=True)
df_sorted.reset_index(drop=True, inplace=True)
df_sorted.to_csv('/GSoC/Ensembl/1.lncRNA/dataset/Ensembl_numeric/all_gene_data/all_gene_sorted_chr/all_transcript_data_sorted_chr.csv')



StarcoderdataJupyterScriptsDedupFiltered