update plot lib form multiple chrom coverage

master
tforest 2022-02-14 10:20:04 +01:00
parent 7a08bb7b41
commit 257c713458
4 changed files with 59 additions and 13 deletions

View File

@ -1,3 +1 @@
from frst import sfs_tools
from frst import customgraphics
from frst import vcf_utils
from frst import sfs_tools, customgraphics, vcf_utils, sfs_tools

View File

@ -9,8 +9,14 @@ FOREST Thomas (thomas.forest@college-de-france.fr)
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
import gc
import time
import datetime
import pandas as pd
# custom libs
from frst import vcf_utils
def heatmap(data, row_labels=None, col_labels=None, ax=None,
cbar_kw={}, cbarlabel="", **kwargs):
"""
@ -145,8 +151,17 @@ def plot_matrix(mat, legend=None, color_scale_type="YlGn", cbarlabel = "qt", tit
fig.tight_layout()
plt.show()
def plot(x, y, outfile = None, outfolder = None, ylab=None, xlab=None, title=None):
plt.plot(x, y)
def plot(x, y, outfile = None, outfolder = None, ylab=None, xlab=None,
title=None, label = None, show=True):
if x:
fig, = plt.plot(x, y)
else:
# x is optional
fig, = plt.plot(y)
if label:
# if legend
fig.set_label(label)
plt.legend()
if ylab:
plt.ylabel(ylab)
if xlab:
@ -156,7 +171,8 @@ def plot(x, y, outfile = None, outfolder = None, ylab=None, xlab=None, title=Non
if outfile:
plt.savefig(outfile)
else:
plt.show()
if show == True:
plt.show()
def scatter(x, y, ylab=None, xlab=None, title=None):
plt.scatter(x, y)
@ -178,14 +194,38 @@ def barplot(x, y, ylab=None, xlab=None, title=None):
plt.title(title)
plt.show()
def plot_chrom_continuity(vcf_entries, chr_id, outfile = None, outfolder = None):
def plot_chrom_continuity(vcf_entries, chr_id, x=None, y=None, outfile = None,
outfolder = None, returned=False, show=True, label=True):
chr_name = list(vcf_entries.keys())[chr_id]
if label:
label = chr_name
chr_entries = vcf_entries[chr_name]
genotyped_pos = vcf_utils.genotyping_continuity_plot(chr_entries)
plot(genotyped_pos[0], genotyped_pos[1], ylab = "genotyped pos.",
xlab = "pos. in ref.",
title = "Genotyped pos in chr "+str(chr_id+1)+":'"+chr_name+"'",
outfile = outfile, outfolder = outfolder)
if returned:
# if we do not want to plot while executing
# useful for storing the x,y coords in a variable for ex.
return genotyped_pos
else:
# to plot on the fly
plot(x, y=genotyped_pos[1], ylab = "genotyped pos.",
xlab = "pos. in ref.",
title = "Genotyped pos in chr "+str(chr_id+1)+":'"+chr_name+"'",
outfile = outfile, outfolder = outfolder, show=show, label=label)
def plot_whole_karyotype(recent_variants, mem_clean = False):
coords = []
for chr in range(len(recent_variants)):
x, y = vcf_utils.customgraphics.plot_chrom_continuity(recent_variants, chr_id = chr, show = False, returned = True)
coords.append([x, y])
if mem_clean:
start = time.time()
del x
del y
gc.collect()
end = time.time()
print("Cleaned mem. in", str(datetime.timedelta(seconds=end - start)))
# maybe add a clean of recent_variants in extreme cases, before building the plots
return coords
def plot_chrom_coverage(vcf_entries, chr_id):
chr_name = list(vcf_entries.keys())[chr_id]

View File

@ -19,8 +19,7 @@ import matplotlib.pyplot as plt
def sfs_from_vcf(n, vcf_file, folded = True, diploid = True, phased = False, verbose = False):
"""
Multiplication de deux nombres entiers.
Cette fonction ne sert pas à grand chose.
Generates a Site Frequency Spectrum from a gzipped VCF file format.
Parameters
----------

View File

@ -18,6 +18,9 @@ from frst import customgraphics
import json
import time
import datetime
import gc
import pandas as pd
def parse_vcf(vcf_file, phased=False, stop_at=None, chr_starts_with="*"):
start = time.time()
@ -199,6 +202,12 @@ def compute_coverage(vcf_entries, verbose=False):
coords[1].append(y)
return coords
def free(obj):
""" Free the object and call the garbage collector explicitely
"""
del obj
gc.collect()
if __name__ == "__main__":
# check args
if len(sys.argv) !=2: