compara-deep-learning/read_get_gene_seq.py

137 lines
4.5 KiB
Python
Raw Permalink Normal View History

2019-07-24 03:27:54 -07:00
import json
from Bio import SeqIO
import os
import gzip
import progressbar
2019-07-24 04:54:02 -07:00
2019-07-24 03:27:54 -07:00
def read_from_multiple_lsy(lsyfl):
2019-07-24 04:54:02 -07:00
lsy = {}
2019-07-24 03:27:54 -07:00
for f in lsyfl:
2019-07-24 04:54:02 -07:00
d = {}
with open(f, "r") as file:
d = dict(json.load(file))
2019-07-24 03:27:54 -07:00
for t in d:
2019-07-24 04:54:02 -07:00
lsy[t] = d[t]
2019-07-24 03:27:54 -07:00
return lsy
2019-07-24 04:54:02 -07:00
# this function updates the given dictionary with the given keys and
# values list
def create_dict(keys, values, dictionary):
2019-07-24 03:27:54 -07:00
for i in range(len(keys)):
if keys[i] not in dictionary:
2019-07-24 04:54:02 -07:00
dictionary[keys[i]] = values[i]
2019-07-24 03:27:54 -07:00
return dictionary
2019-07-24 04:54:02 -07:00
# this function maps all the genes to their respective species.
2021-04-08 08:41:16 -07:00
# (Function: when finding the species of any gene
# we do not need to search the entire dataframe)
2019-07-24 04:54:02 -07:00
def group_seq_by_species(df, g_to_sp):
sph = list(df.homology_species)
ghsp = list(df.homology_gene_stable_id)
sp = list(df.species)
gsp = list(df.gene_stable_id)
create_dict(gsp, sp, g_to_sp)
create_dict(ghsp, sph, g_to_sp)
2019-07-24 03:27:54 -07:00
return g_to_sp
2019-07-24 04:54:02 -07:00
# this function returns the gene-id and gene-biotype from the description
# in the fasta file record.
2019-07-24 03:27:54 -07:00
def description_cleaner(description):
2019-07-24 04:54:02 -07:00
description = description.split()
t = ""
gbt = ""
2019-07-24 03:27:54 -07:00
for x in description:
try:
2019-07-24 04:54:02 -07:00
x = x.split(":")
if x[0] == "gene":
t = x[1].split(".")[0]
if x[0] == "gene_biotype":
gbt = x[1]
except BaseException:
return "aa", "aa"
return t, gbt
def read_gene_seq(dirname, s, genes_by_species):
lof = os.listdir(dirname) # list all the files in the sequences directory
ftr = []
2019-07-24 03:27:54 -07:00
for f in lof:
2019-07-24 04:54:02 -07:00
if f.split(".")[
2021-04-08 08:41:16 -07:00
# check whether the species is present in the species to read list.
# Will skip those species which are not present in the dataframe
0] in s:
2019-07-24 03:27:54 -07:00
ftr.append(f)
2019-07-24 04:54:02 -07:00
data = {}
2019-07-24 03:27:54 -07:00
for f in progressbar.progressbar(ftr):
2019-07-24 04:54:02 -07:00
species = f.split(".")[0].lower()
with gzip.open(dirname + "/" + f, "rt") as file:
record = SeqIO.parse(file, "fasta")
2019-07-24 03:27:54 -07:00
for r in record:
2019-07-24 04:54:02 -07:00
gid, gbt = description_cleaner(r.description)
2021-04-08 08:41:16 -07:00
if str(gid) not in data and str(gid) in \
genes_by_species[species] and gbt == "protein_coding":
2019-07-24 04:54:02 -07:00
data[gid] = str(r.seq)
2019-07-24 03:27:54 -07:00
return data
2019-07-24 04:54:02 -07:00
def read_gene_sequences(hdf, lsy, data_dir, fname):
2021-04-08 08:41:16 -07:00
"""The basic idea here is to create a list/dictionary of all the genes by
their species. Once the mapping is done, all the respective fasta sequence
files are read by Species and the CDNA sequences for each gene in the
species record are read and stored.
Thus we don't have to read the same file multiple times."""
2019-07-24 04:54:02 -07:00
grouped_genes = {}
gene_by_species_dict = {}
2019-07-24 03:27:54 -07:00
for df in progressbar.progressbar(hdf):
2019-07-24 04:54:02 -07:00
grouped_genes = group_seq_by_species(df, grouped_genes)
2019-07-24 03:27:54 -07:00
for i in df.homology_species.unique():
2019-07-24 04:54:02 -07:00
gene_by_species_dict[i] = []
2019-07-24 03:27:54 -07:00
for i in df.species.unique():
2019-07-24 04:54:02 -07:00
gene_by_species_dict[i] = []
2019-07-24 03:27:54 -07:00
for x in progressbar.progressbar(lsy):
try:
2019-07-24 04:54:02 -07:00
species = grouped_genes[x] # get the species
except BaseException:
2019-07-24 03:27:54 -07:00
continue
2019-07-24 04:54:02 -07:00
# check if the gene already exists in the species dict or not.
if x not in gene_by_species_dict[species]:
2019-07-24 03:27:54 -07:00
gene_by_species_dict[species].append(x)
2019-07-24 04:54:02 -07:00
xl = lsy[x]['b']
xr = lsy[x]['f']
2019-07-24 03:27:54 -07:00
for gxl in xl:
2019-07-24 04:54:02 -07:00
if gxl == "NULL_GENE":
2019-07-24 03:27:54 -07:00
break
if gxl not in gene_by_species_dict[species]:
gene_by_species_dict[species].append(gxl)
for gxr in xr:
2019-07-24 04:54:02 -07:00
if gxr == "NULL_GENE":
2019-07-24 03:27:54 -07:00
break
if gxr not in gene_by_species_dict[species]:
gene_by_species_dict[species].append(gxr)
2019-07-24 04:54:02 -07:00
# select those species only whose gene sequences we have to read.
s = [x for x in gene_by_species_dict if len(gene_by_species_dict[x]) != 0]
s = [x.capitalize() for x in s]
data = read_gene_seq(data_dir, s, gene_by_species_dict)
not_found = {}
2019-07-24 03:27:54 -07:00
for species in gene_by_species_dict:
for gene in gene_by_species_dict[species]:
try:
2019-07-24 04:54:02 -07:00
_ = data[gene]
except BaseException:
not_found[gene] = 1
2019-07-24 03:27:54 -07:00
2019-07-24 04:54:02 -07:00
with open("processed/not_found_" + fname + ".json", "w") as file:
json.dump(not_found, file)
2019-07-24 03:27:54 -07:00
return data