diff --git a/docs/analysis_guide/TAD_calling.ipynb b/docs/analysis_guide/TAD_calling.ipynb index a45047f..63904e2 100644 --- a/docs/analysis_guide/TAD_calling.ipynb +++ b/docs/analysis_guide/TAD_calling.ipynb @@ -35,9 +35,9 @@ ], "source": [ "resolution = 10000\n", - "coolfile=f'./HFF_MicroC.mcool::resolutions/{resolution}'\n", - "clr=cooler.Cooler(coolfile)\n", - "windows = [3*resolution, 5*resolution, 10*resolution, 25*resolution]\n", + "coolfile = f\"./HFF_MicroC.mcool::resolutions/{resolution}\"\n", + "clr = cooler.Cooler(coolfile)\n", + "windows = [3 * resolution, 5 * resolution, 10 * resolution, 25 * resolution]\n", "insulation_table = insulation(clr, windows, verbose=True)" ] }, @@ -296,11 +296,17 @@ "metadata": {}, "outputs": [], "source": [ - "df = insulation_table[['chrom', 'start', 'end', \n", - " 'log2_insulation_score_30000', \n", - " 'log2_insulation_score_50000', \n", - " 'log2_insulation_score_100000', \n", - " 'log2_insulation_score_250000']]" + "df = insulation_table[\n", + " [\n", + " \"chrom\",\n", + " \"start\",\n", + " \"end\",\n", + " \"log2_insulation_score_30000\",\n", + " \"log2_insulation_score_50000\",\n", + " \"log2_insulation_score_100000\",\n", + " \"log2_insulation_score_250000\",\n", + " ]\n", + "]" ] }, { @@ -355,18 +361,21 @@ "ten.add(pos=\"bottom\", height=1.5, hspace=0.05)\n", "ten.add(pos=\"bottom\", height=1, hspace=0.07)\n", "\n", - "tc.pl.mapC(\n", - " ax=ten.axs(0),\n", - " mat=HFF,\n", - " label=\"HFF\",\n", - " map_type=\"rec\",\n", - " height=150\n", - ")\n", + "tc.pl.mapC(ax=ten.axs(0), mat=HFF, label=\"HFF\", map_type=\"rec\", height=150)\n", "\n", - "tc.pl.bdgmat_track(ax=ten.axs(1), bed=df, regions=regions, style='line', label='insulation score')\n", - "tc.pl.scale_track(ax=ten.axs(1), region=regions, scale_adjust=\"Mb\", tick_pos=\"bottom\", ratio2ax=1, space=0)\n", + "tc.pl.bdgmat_track(\n", + " ax=ten.axs(1), bed=df, regions=regions, style=\"line\", label=\"insulation score\"\n", + ")\n", + "tc.pl.scale_track(\n", + " ax=ten.axs(1),\n", + " region=regions,\n", + " scale_adjust=\"Mb\",\n", + " tick_pos=\"bottom\",\n", + " ratio2ax=1,\n", + " space=0,\n", + ")\n", "\n", - "#tc.savefig('outfile.pdf')" + "# tc.savefig('outfile.pdf')" ] } ], diff --git a/docs/analysis_guide/compartment_calling.ipynb b/docs/analysis_guide/compartment_calling.ipynb index 28ce6e0..337fc7b 100644 --- a/docs/analysis_guide/compartment_calling.ipynb +++ b/docs/analysis_guide/compartment_calling.ipynb @@ -27,12 +27,12 @@ ], "source": [ "import cooler\n", - "import pandas as pd\n", "import cooltools\n", + "import pandas as pd\n", "\n", "resolution = 100000\n", - "coolfile=f'/Users/yuanzan/Downloads/HFF_MicroC.mcool::resolutions/{resolution}'\n", - "clr=cooler.Cooler(coolfile)" + "coolfile = f\"/Users/yuanzan/Downloads/HFF_MicroC.mcool::resolutions/{resolution}\"\n", + "clr = cooler.Cooler(coolfile)" ] }, { @@ -197,15 +197,16 @@ "source": [ "## fasta sequence is required for calculating binned profile of GC conent\n", "import bioframe\n", + "\n", "bins = clr.bins()[:]\n", "### Unified chromosome Naming\n", - "bins['chrom'] = bins['chrom'].str.lstrip('chr')\n", + "bins[\"chrom\"] = bins[\"chrom\"].str.lstrip(\"chr\")\n", "\n", - "hg38_genome = bioframe.load_fasta('/Users/yuanzan/Documents/ref/GRCh38.105.fa');\n", + "hg38_genome = bioframe.load_fasta(\"/Users/yuanzan/Documents/ref/GRCh38.105.fa\")\n", "## note the next command may require installing pysam\n", - "gc_cov = bioframe.frac_gc(bins[['chrom', 'start', 'end']], hg38_genome)\n", - "gc_cov['chrom'] = 'chr' + gc_cov['chrom'].astype(str)\n", - "gc_cov.to_csv('hg38_gc_cov_100kb.tsv',index=False,sep='\\t')\n", + "gc_cov = bioframe.frac_gc(bins[[\"chrom\", \"start\", \"end\"]], hg38_genome)\n", + "gc_cov[\"chrom\"] = \"chr\" + gc_cov[\"chrom\"].astype(str)\n", + "gc_cov.to_csv(\"hg38_gc_cov_100kb.tsv\", index=False, sep=\"\\t\")\n", "display(gc_cov)" ] }, @@ -271,11 +272,14 @@ } ], "source": [ - "view_df = pd.DataFrame({'chrom': clr.chromnames,\n", - " 'start': 0,\n", - " 'end': clr.chromsizes.values,\n", - " 'name': clr.chromnames}\n", - " )\n", + "view_df = pd.DataFrame(\n", + " {\n", + " \"chrom\": clr.chromnames,\n", + " \"start\": 0,\n", + " \"end\": clr.chromsizes.values,\n", + " \"name\": clr.chromnames,\n", + " }\n", + ")\n", "display(view_df)" ] }, @@ -360,14 +364,14 @@ "source": [ "# obtain first 3 eigenvectors\n", "cis_eigs = cooltools.eigs_cis(\n", - " clr,\n", - " gc_cov,\n", - " view_df=view_df,\n", - " n_eigs=3,\n", - " )\n", + " clr,\n", + " gc_cov,\n", + " view_df=view_df,\n", + " n_eigs=3,\n", + ")\n", "# cis_eigs[0] returns eigenvalues, here we focus on eigenvectors\n", "display(cis_eigs[0])\n", - "eigenvector_track = cis_eigs[1][['chrom','start','end','E1']]" + "eigenvector_track = cis_eigs[1][[\"chrom\", \"start\", \"end\", \"E1\"]]" ] }, { @@ -376,7 +380,7 @@ "metadata": {}, "outputs": [], "source": [ - "eigenvector_track.to_csv('eigenvector_track.bdg', sep=\"\\t\", index=False, header=False)" + "eigenvector_track.to_csv(\"eigenvector_track.bdg\", sep=\"\\t\", index=False, header=False)" ] }, { @@ -432,34 +436,26 @@ "ten.add(pos=\"bottom\", height=0.4, hspace=0.09)\n", "\n", "\n", - "tc.pl.mapC(\n", - " ax=ten.axs(0),\n", - " mat=HFF,\n", - " label=\"HFF\",\n", - " map_type=\"tri\",\n", - " ax_on=False\n", - ")\n", + "tc.pl.mapC(ax=ten.axs(0), mat=HFF, label=\"HFF\", map_type=\"tri\", ax_on=False)\n", "\n", "tc.pl.bed_track(\n", - " ax=ten.axs(1),\n", - " bed=eigenvector_track,\n", - " regions=regions,\n", - " style=\"bar\",\n", - " label=\"Eign1\"\n", + " ax=ten.axs(1), bed=eigenvector_track, regions=regions, style=\"bar\", label=\"Eign1\"\n", ")\n", "\n", "\n", "tc.pl.bed_track(\n", - " ax=ten.axs(2),\n", - " bed=gc_cov,\n", - " regions=regions,\n", - " style=\"rec\",\n", - " label=\"GC\",\n", - " cmap='Blues'\n", + " ax=ten.axs(2), bed=gc_cov, regions=regions, style=\"rec\", label=\"GC\", cmap=\"Blues\"\n", ")\n", "\n", "\n", - "tc.pl.scale_track(ax=ten.axs(2), region=regions, scale_adjust=\"Mb\", tick_pos=\"bottom\", ratio2ax=2, space=0)\n" + "tc.pl.scale_track(\n", + " ax=ten.axs(2),\n", + " region=regions,\n", + " scale_adjust=\"Mb\",\n", + " tick_pos=\"bottom\",\n", + " ratio2ax=2,\n", + " space=0,\n", + ")" ] } ], diff --git a/docs/cli/cli-4C.ipynb b/docs/cli/cli-4C.ipynb index caeb2ef..fa7ca4f 100644 --- a/docs/cli/cli-4C.ipynb +++ b/docs/cli/cli-4C.ipynb @@ -94,8 +94,9 @@ "outputs": [], "source": [ "import pandas as pd\n", - "import trackc as tc\n", - "import pyBigWig" + "import pyBigWig\n", + "\n", + "import trackc as tc" ] }, { @@ -109,7 +110,9 @@ "regions = [\"chr8:127000000-129200000\", \"chr14:96500000-99300000\"]\n", "MYC_TSS = \"chr8:127735434-127735435\"\n", "\n", - "AML_1360_H3K27ac = pyBigWig.open(\"../../../trackc_data/tutorials/4C/GSM4604189_H3K27ac.bw\")" + "AML_1360_H3K27ac = pyBigWig.open(\n", + " \"../../../trackc_data/tutorials/4C/GSM4604189_H3K27ac.bw\"\n", + ")" ] }, { @@ -210,8 +213,7 @@ ], "source": [ "gene_bed12 = pd.read_table(\n", - " \"../../../trackc_data/tutorials/4C/Homo_sapiens.GRCh38.105.chr.bed13\", \n", - " header=None\n", + " \"../../../trackc_data/tutorials/4C/Homo_sapiens.GRCh38.105.chr.bed13\", header=None\n", ")\n", "gene_bed12 = gene_bed12[gene_bed12[12] == \"protein_coding\"]\n", "gene_bed12[0] = \"chr\" + gene_bed12[0]\n", @@ -258,7 +260,7 @@ "fruitpunch3 = LinearSegmentedColormap.from_list(\n", " \"fruitpunch3\", [(0, \"white\"), (0.02, \"w\"), (0.25, \"r\"), (1, \"#0E3858\")], N=100\n", ")\n", - "#CF3F35\n", + "# CF3F35\n", "\n", "aml = tc.tl.extractContactRegions(clr=AML_1360, row_regions=regions)\n", "tc.pl.mapC(\n", diff --git a/docs/cli/heatmap-1.ipynb b/docs/cli/heatmap-1.ipynb index acfe578..8a61657 100644 --- a/docs/cli/heatmap-1.ipynb +++ b/docs/cli/heatmap-1.ipynb @@ -50,10 +50,10 @@ "outputs": [], "source": [ "import cooler\n", - "import trackc as tc\n", - "\n", "from matplotlib.colors import LinearSegmentedColormap, ListedColormap\n", "\n", + "import trackc as tc\n", + "\n", "fruitpunch3 = LinearSegmentedColormap.from_list(\n", " \"fruitpunch3\", [(0, \"white\"), (0.2, \"r\"), (1, \"#0E3858\")], N=100\n", ")" @@ -102,7 +102,9 @@ } ], "source": [ - "clr = cooler.Cooler(\"/Users/yuanzan/Documents/RD/GSM4417639_SK-N-DZ_C6BC81F2_b38d5.mcool::/resolutions/5120000\")\n", + "clr = cooler.Cooler(\n", + " \"/Users/yuanzan/Documents/RD/GSM4417639_SK-N-DZ_C6BC81F2_b38d5.mcool::/resolutions/5120000\"\n", + ")\n", "clr.chromsizes" ] }, @@ -153,7 +155,7 @@ " tick_fontsize=10,\n", ")\n", "\n", - "#tc.savefig('inter-chrom.pdf')" + "# tc.savefig('inter-chrom.pdf')" ] } ], diff --git a/docs/cli/heatmap-2.ipynb b/docs/cli/heatmap-2.ipynb index 8e9e329..e20cd56 100644 --- a/docs/cli/heatmap-2.ipynb +++ b/docs/cli/heatmap-2.ipynb @@ -79,6 +79,7 @@ ], "source": [ "import cooler\n", + "\n", "import trackc as tc\n", "\n", "ten = tc.tenon(figsize=(4, 1))\n", @@ -87,21 +88,18 @@ "\n", "clr = \"./GSM4417639_SK-N-DZ_C6BC81F2_b38d5.mcool::/resolutions/5120000\"\n", "\n", - "mat = tc.tl.extractContactRegions(\n", - " clr=clr,\n", - " row_regions=['chr6'],\n", - " col_regions=['chr8']\n", - ")\n", + "mat = tc.tl.extractContactRegions(clr=clr, row_regions=[\"chr6\"], col_regions=[\"chr8\"])\n", "\n", - "tc.pl.mapC(ax=ten.axs(0), \n", - " mat=mat.cmat, \n", - " map_type=\"squ\", \n", - " cmap='Blues', \n", - " symmetric=True,\n", - " maxrange= 1000,\n", - " aspect=1,\n", - " )\n", - "#tc.savefig('chr6_chr8_trans.pdf')" + "tc.pl.mapC(\n", + " ax=ten.axs(0),\n", + " mat=mat.cmat,\n", + " map_type=\"squ\",\n", + " cmap=\"Blues\",\n", + " symmetric=True,\n", + " maxrange=1000,\n", + " aspect=1,\n", + ")\n", + "# tc.savefig('chr6_chr8_trans.pdf')" ] } ], diff --git a/docs/cli/heatmap-3.ipynb b/docs/cli/heatmap-3.ipynb index 30794db..776ce11 100644 --- a/docs/cli/heatmap-3.ipynb +++ b/docs/cli/heatmap-3.ipynb @@ -94,30 +94,23 @@ "ten.add(pos=\"bottom\", height=2)\n", "ten.add(pos=\"bottom\", height=0.6)\n", "\n", - "regions = ['chr6:0-25600000', 'chr8:122880000-143360000']\n", - "clr = '/Users/yuanzan/Documents/RD/GSM4417639_SK-N-DZ_C6BC81F2_b38d5.mcool::/resolutions/5120000'\n", + "regions = [\"chr6:0-25600000\", \"chr8:122880000-143360000\"]\n", + "clr = \"/Users/yuanzan/Documents/RD/GSM4417639_SK-N-DZ_C6BC81F2_b38d5.mcool::/resolutions/5120000\"\n", "\n", - "mat = tc.tl.extractContactRegions(\n", - " clr=clr,\n", - " row_regions=regions\n", - ")\n", - "\n", - "tc.pl.mapC(ax=ten.axs(0), \n", - " mat=mat.cmat,\n", - " map_type=\"tri\",\n", - " ax_on=False,\n", - " cmap='PuBu',\n", - " maxrange=10000,\n", - " )\n", + "mat = tc.tl.extractContactRegions(clr=clr, row_regions=regions)\n", "\n", - "tc.pl.multi_scale_track(\n", - " ax=ten.axs(1),\n", - " regions=regions,\n", - " scale_adjust= 'Mb',\n", - " intervals=2\n", + "tc.pl.mapC(\n", + " ax=ten.axs(0),\n", + " mat=mat.cmat,\n", + " map_type=\"tri\",\n", + " ax_on=False,\n", + " cmap=\"PuBu\",\n", + " maxrange=10000,\n", ")\n", "\n", - "#tc.savefig('trans_interactions.pdf')" + "tc.pl.multi_scale_track(ax=ten.axs(1), regions=regions, scale_adjust=\"Mb\", intervals=2)\n", + "\n", + "# tc.savefig('trans_interactions.pdf')" ] } ], diff --git a/docs/cli/heatmap-4.ipynb b/docs/cli/heatmap-4.ipynb index 34beaa5..5981d27 100644 --- a/docs/cli/heatmap-4.ipynb +++ b/docs/cli/heatmap-4.ipynb @@ -170,7 +170,7 @@ "ten.add(pos=\"bottom\", height=1)\n", "ten.add(pos=\"bottom\", height=1, hspace=0.3)\n", "\n", - "region = '18:10000000-14500000'\n", + "region = \"18:10000000-14500000\"\n", "clr = \"./github/seqyuan/trackc_data/examples/BxPC3.chr18.mcool::/resolutions/25000\"\n", "\n", "height = 50\n", @@ -179,34 +179,31 @@ "mat2 = tc.tl.extractCisContact(clr=clr, region=region, extend=height)\n", "\n", "\n", - "tc.pl.scale_track(ax=ten.axs(0), \n", - " region=region,\n", - " scale_adjust='Mb',\n", - " tick_pos='top',\n", - " ratio2ax=0.4\n", - " )\n", + "tc.pl.scale_track(\n", + " ax=ten.axs(0), region=region, scale_adjust=\"Mb\", tick_pos=\"top\", ratio2ax=0.4\n", + ")\n", "\n", - "tc.pl.mapC(ax=ten.axs(0), \n", - " mat=mat2, \n", - " map_type=\"rec\", \n", - " height=height, \n", - " label='rec-sym', \n", - " symmetric=True)\n", + "tc.pl.mapC(\n", + " ax=ten.axs(0),\n", + " mat=mat2,\n", + " map_type=\"rec\",\n", + " height=height,\n", + " label=\"rec-sym\",\n", + " symmetric=True,\n", + ")\n", "\n", - "tc.pl.mapC(ax=ten.axs(1), \n", - " mat=mat, \n", - " cmap='PuBu',\n", - " map_type=\"tri\", \n", - " height=height, \n", - " label='tri-mat')\n", + "tc.pl.mapC(\n", + " ax=ten.axs(1), mat=mat, cmap=\"PuBu\", map_type=\"tri\", height=height, label=\"tri-mat\"\n", + ")\n", "\n", - "tc.pl.mapC(ax=ten.axs(2), \n", - " mat2=mat2, \n", - " cmap='cividis',\n", - " map_type=\"rec\", \n", - " height=height, \n", - " label='rec-mat2')\n", - "\n" + "tc.pl.mapC(\n", + " ax=ten.axs(2),\n", + " mat2=mat2,\n", + " cmap=\"cividis\",\n", + " map_type=\"rec\",\n", + " height=height,\n", + " label=\"rec-mat2\",\n", + ")" ] } ], diff --git a/docs/cli/heatmap-5.ipynb b/docs/cli/heatmap-5.ipynb index 727a04f..aabdce5 100644 --- a/docs/cli/heatmap-5.ipynb +++ b/docs/cli/heatmap-5.ipynb @@ -129,19 +129,21 @@ "ten = tc.tenon(figsize=(4, 1))\n", "ten.add(pos=\"bottom\", height=4)\n", "\n", - "region = '18:64000000-71000000'\n", + "region = \"18:64000000-71000000\"\n", "clr1 = \"./github/seqyuan/trackc_data/examples/HPDE6C7.chr18.mcool::/resolutions/25000\"\n", "clr2 = \"./github/seqyuan/trackc_data/examples/BxPC3.chr18.mcool::/resolutions/25000\"\n", "\n", "mat = tc.tl.extractCisContact(clr=clr1, region=region)\n", "mat2 = tc.tl.extractCisContact(clr=clr2, region=region)\n", "\n", - "tc.pl.mapC(ax=ten.axs(0), \n", - " mat=mat, \n", - " mat2=mat2, \n", - " cmap=[tc.pa.cmaps.fruitpunch, 'PuBu'],\n", - " map_type=\"squ\", \n", - " label=['Normal', 'Tumor'])\n" + "tc.pl.mapC(\n", + " ax=ten.axs(0),\n", + " mat=mat,\n", + " mat2=mat2,\n", + " cmap=[tc.pa.cmaps.fruitpunch, \"PuBu\"],\n", + " map_type=\"squ\",\n", + " label=[\"Normal\", \"Tumor\"],\n", + ")" ] } ], diff --git a/docs/cli/heatmap-6.ipynb b/docs/cli/heatmap-6.ipynb index e436680..83966e0 100644 --- a/docs/cli/heatmap-6.ipynb +++ b/docs/cli/heatmap-6.ipynb @@ -89,9 +89,10 @@ "metadata": {}, "outputs": [], "source": [ - "import trackc as tc\n", "from matplotlib.colors import LinearSegmentedColormap, ListedColormap\n", "\n", + "import trackc as tc\n", + "\n", "cmap1 = LinearSegmentedColormap.from_list(\n", " \"cmap1\", [(0, \"white\"), (0.03, \"w\"), (0.4, \"r\"), (1, \"#CF3F35\")], N=100\n", ")" @@ -129,36 +130,37 @@ "ten = tc.tenon(figsize=(6, 1))\n", "ten.add(pos=\"bottom\", height=2)\n", "\n", - "region = '18:64000000-71000000'\n", + "region = \"18:64000000-71000000\"\n", "mat1 = tc.tl.extractCisContact(\n", - " clr='../../../trackc_data/examples/HPDE6C7.chr18.mcool::/resolutions/25000',\n", - " region=region\n", + " clr=\"../../../trackc_data/examples/HPDE6C7.chr18.mcool::/resolutions/25000\",\n", + " region=region,\n", ")\n", "mat2 = tc.tl.extractCisContact(\n", - " clr='../../../trackc_data/examples/BxPC3.chr18.mcool::/resolutions/25000',\n", - " region=region\n", + " clr=\"../../../trackc_data/examples/BxPC3.chr18.mcool::/resolutions/25000\",\n", + " region=region,\n", ")\n", "\n", - "tc.pl.mapC(ax=ten.axs(0), \n", - " mat=mat1,\n", - " mat2=mat2,\n", - " map_type=\"tri\",\n", - " height=100,\n", - " label=['Normal', 'Tumor'],\n", - " ax_on=False,\n", - " cmap=[cmap1, tc.pa.cmaps.fruitpunch2]\n", - " )\n", + "tc.pl.mapC(\n", + " ax=ten.axs(0),\n", + " mat=mat1,\n", + " mat2=mat2,\n", + " map_type=\"tri\",\n", + " height=100,\n", + " label=[\"Normal\", \"Tumor\"],\n", + " ax_on=False,\n", + " cmap=[cmap1, tc.pa.cmaps.fruitpunch2],\n", + ")\n", "\n", "tc.pl.scale_track(\n", " ax=ten.axs(0),\n", " region=region,\n", - " scale_adjust= 'Mb',\n", - " tick_pos= 'bottom',\n", - " ratio2ax= 0.5,\n", - " space=0.05\n", + " scale_adjust=\"Mb\",\n", + " tick_pos=\"bottom\",\n", + " ratio2ax=0.5,\n", + " space=0.05,\n", ")\n", "\n", - "#tc.savefig('compare_heatmap.pdf')" + "# tc.savefig('compare_heatmap.pdf')" ] } ], diff --git a/docs/conf.py b/docs/conf.py index b0c7f33..336ff88 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -16,10 +16,10 @@ import warnings from datetime import date -#from sphinx_gallery.notebook import add_code_cell, add_markdown_cell +# from sphinx_gallery.notebook import add_code_cell, add_markdown_cell # import sphinx_gallery -#from sphinx_gallery.sorting import FileNameSortKey +# from sphinx_gallery.sorting import FileNameSortKey HERE = os.path.abspath(os.path.dirname(__file__)) package_dir = os.path.join(HERE, "..", "src") @@ -132,6 +132,7 @@ master_doc = "index" html_theme = "pydata_sphinx_theme" autosummary_generate = True +autodoc_mock_imports = ["pyBigWig"] html_show_sphinx = False pygments_style = "sphinx" highlight_language = "python3" @@ -161,8 +162,8 @@ def setup(app): "logo": { "text": None, "alt_text": None, - #"image_light": "_static/logo/trackc-low-resolution-logo-color-on-transparent-background.png", - #"image_dark": "_static/logo/trackc-low-resolution-logo-color-on-transparent-background.png" + # "image_light": "_static/logo/trackc-low-resolution-logo-color-on-transparent-background.png", + # "image_dark": "_static/logo/trackc-low-resolution-logo-color-on-transparent-background.png" }, #'switcher': dict( # json_url='https://sphinx-gallery.github.io/dev/_static/switcher.json', @@ -190,12 +191,14 @@ def setup(app): # The name of an image file (relative to this directory) to place at the top # of the sidebar. -''' +""" +html_logo = ( + "_static/logo/trackc-low-resolution-logo-color-on-transparent-background.png" +) +""" html_logo = ( "_static/logo/trackc-low-resolution-logo-color-on-transparent-background.png" ) -''' -html_logo = "_static/logo/trackc-low-resolution-logo-color-on-transparent-background.png" # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. @@ -370,15 +373,13 @@ def setup(app): nbsphinx_thumbnails = { - 'cli/ectopic_interactions-1': '_static/guides/ectopic-i-1.png', - 'cli/ectopic_interactions-2': '_static/guides/ectopic-i-2.png', - 'cli/ectopic_interactions-3': '_static/guides/ectopic-i-3.png', - 'analysis_guide/genebed12/bed12': '_static/analysis/bedtools.swiss.png', - 'analysis_guide/bedGraph2bw': '_static/analysis/bdg2bw.png', - 'analysis_guide/subset_bigwig': '_static/analysis/bedtools.swiss.png', - 'analysis_guide/extract_cool': '_static/analysis/cooler_logo.png', - 'analysis_guide/mcool': '_static/analysis/cooler_logo.png', - 'gallery/embryos': '_static/guides/embryos.png', + "cli/ectopic_interactions-1": "_static/guides/ectopic-i-1.png", + "cli/ectopic_interactions-2": "_static/guides/ectopic-i-2.png", + "cli/ectopic_interactions-3": "_static/guides/ectopic-i-3.png", + "analysis_guide/genebed12/bed12": "_static/analysis/bedtools.swiss.png", + "analysis_guide/bedGraph2bw": "_static/analysis/bdg2bw.png", + "analysis_guide/subset_bigwig": "_static/analysis/bedtools.swiss.png", + "analysis_guide/extract_cool": "_static/analysis/cooler_logo.png", + "analysis_guide/mcool": "_static/analysis/cooler_logo.png", + "gallery/embryos": "_static/guides/embryos.png", } - - diff --git a/docs/gallery/genome_denovo_scaffolding.ipynb b/docs/gallery/genome_denovo_scaffolding.ipynb index 43125ce..f13a64d 100755 --- a/docs/gallery/genome_denovo_scaffolding.ipynb +++ b/docs/gallery/genome_denovo_scaffolding.ipynb @@ -13,9 +13,11 @@ "metadata": {}, "outputs": [], "source": [ - "import trackc as tc\n", + "import time\n", + "\n", "import cooler\n", - "import time" + "\n", + "import trackc as tc" ] }, { @@ -24,7 +26,7 @@ "metadata": {}, "outputs": [], "source": [ - "clr = cooler.Cooler('contig_bin.mcool::/resolutions/500000')" + "clr = cooler.Cooler(\"contig_bin.mcool::/resolutions/500000\")" ] }, { @@ -85,24 +87,40 @@ "source": [ "import pandas as pd\n", "\n", + "\n", "def agp2regions(agp_order):\n", " agp_order = agp_order.query('Contig_start != \"contig\"')\n", " minus_strand_i = agp_order.query('Orientation==\"-\"').index\n", "\n", - " agp_order['base0_start'] = agp_order['Contig_start'].astype(int) - 1\n", - " agp_order['base0_end'] = agp_order['Contig_end'].astype(int) - 1\n", + " agp_order[\"base0_start\"] = agp_order[\"Contig_start\"].astype(int) - 1\n", + " agp_order[\"base0_end\"] = agp_order[\"Contig_end\"].astype(int) - 1\n", "\n", - " agp_order.loc[minus_strand_i, 'base0_start'] = agp_order.loc[minus_strand_i, 'Contig_end'].astype(int) - 1\n", - " agp_order.loc[minus_strand_i, 'base0_end'] = agp_order.loc[minus_strand_i, 'Contig_start'].astype(int) - 1\n", + " agp_order.loc[minus_strand_i, \"base0_start\"] = (\n", + " agp_order.loc[minus_strand_i, \"Contig_end\"].astype(int) - 1\n", + " )\n", + " agp_order.loc[minus_strand_i, \"base0_end\"] = (\n", + " agp_order.loc[minus_strand_i, \"Contig_start\"].astype(int) - 1\n", + " )\n", "\n", - " agp_order = agp_order.sort_values(by=['Chromosome', 'Order'])\n", - " agp_order['region'] = agp_order['Contig_ID'] + \":\" + agp_order['base0_start'].astype(str) + \"-\" + agp_order['base0_end'].astype(str)\n", + " agp_order = agp_order.sort_values(by=[\"Chromosome\", \"Order\"])\n", + " agp_order[\"region\"] = (\n", + " agp_order[\"Contig_ID\"]\n", + " + \":\"\n", + " + agp_order[\"base0_start\"].astype(str)\n", + " + \"-\"\n", + " + agp_order[\"base0_end\"].astype(str)\n", + " )\n", " return agp_order\n", "\n", - "reorder = pd.read_table('/Volumes/work/my_project/trackc_data/reorder_assembly.build.ScafInChr.agp')\n", + "\n", + "reorder = pd.read_table(\n", + " \"/Volumes/work/my_project/trackc_data/reorder_assembly.build.ScafInChr.agp\"\n", + ")\n", "reorder = agp2regions(reorder)\n", "\n", - "draft_order = pd.read_table('/Volumes/work/my_project/trackc_data/assembly.build.ScafInChr.agp')\n", + "draft_order = pd.read_table(\n", + " \"/Volumes/work/my_project/trackc_data/assembly.build.ScafInChr.agp\"\n", + ")\n", "draft_order = agp2regions(draft_order)" ] }, @@ -325,10 +343,10 @@ "\n", "draft_mat = tc.tl.extractContactRegions(\n", " clr=clr,\n", - " row_regions=draft_order['region'].to_list(),\n", + " row_regions=draft_order[\"region\"].to_list(),\n", ")\n", "\n", - "time.time()- start_time" + "time.time() - start_time" ] }, { @@ -352,9 +370,9 @@ "\n", "redorder_mat = tc.tl.extractContactRegions(\n", " clr=clr,\n", - " row_regions=reorder['region'].to_list(),\n", + " row_regions=reorder[\"region\"].to_list(),\n", ")\n", - "time.time()- start_time" + "time.time() - start_time" ] }, { @@ -388,12 +406,14 @@ "source": [ "ten = tc.tenon(figsize=(7, 1))\n", "ten.add(pos=\"bottom\", height=7)\n", - "tc.pl.mapC(ax=ten.axs(0), \n", - " mat=draft_mat.cmat, \n", - " mat2=redorder_mat.cmat,\n", - " label=['draft contig', 'redorder contig'],\n", - " cmap=['PuBu', tc.pa.fruitpunch],\n", - " map_type=\"squ\")\n" + "tc.pl.mapC(\n", + " ax=ten.axs(0),\n", + " mat=draft_mat.cmat,\n", + " mat2=redorder_mat.cmat,\n", + " label=[\"draft contig\", \"redorder contig\"],\n", + " cmap=[\"PuBu\", tc.pa.fruitpunch],\n", + " map_type=\"squ\",\n", + ")" ] }, { @@ -403,22 +423,21 @@ "outputs": [], "source": [ "def chrom_ticks(ax, ticks, ticklabels, xmax):\n", - " ax.spines['bottom'].set_visible(False)\n", - " ax.spines['right'].set_visible(False)\n", - " ax.spines['left'].set_visible(False)\n", + " ax.spines[\"bottom\"].set_visible(False)\n", + " ax.spines[\"right\"].set_visible(False)\n", + " ax.spines[\"left\"].set_visible(False)\n", "\n", " ax.xaxis.tick_top()\n", - " ax.xaxis.set_label_position('top')\n", - " ax.xaxis.set_ticks_position('top')\n", + " ax.xaxis.set_label_position(\"top\")\n", + " ax.xaxis.set_ticks_position(\"top\")\n", "\n", " # 显示 X 轴刻度线和标签\n", - " ax.xaxis.set_tick_params(which='both', direction='in', length=30)\n", + " ax.xaxis.set_tick_params(which=\"both\", direction=\"in\", length=30)\n", " ax.yaxis.set_visible(False)\n", "\n", " ax.set_xticks(ticks)\n", - " _ = ax.set_xticklabels(ticklabels, rotation=45, ha='left', va='top')\n", - " ax.set_xlim(0, xmax)\n", - " " + " _ = ax.set_xticklabels(ticklabels, rotation=45, ha=\"left\", va=\"top\")\n", + " ax.set_xlim(0, xmax)" ] }, { @@ -495,28 +514,34 @@ "ten.add(pos=\"bottom\", height=3.5)\n", "\n", "\n", - "tc.pl.mapC(ax=ten.axs(0), \n", - " mat=redorder_mat.cmat,\n", - " label='redorder contig',\n", - " map_type=\"tri\")\n", + "tc.pl.mapC(\n", + " ax=ten.axs(0), mat=redorder_mat.cmat, label=\"redorder contig\", map_type=\"tri\"\n", + ")\n", "\n", - "chrom_ticks(ten.axs(1), reorder_chromsizes['start'], reorder_chromsizes.index, reorder_chromsizes['End'].sum())\n", + "chrom_ticks(\n", + " ten.axs(1),\n", + " reorder_chromsizes[\"start\"],\n", + " reorder_chromsizes.index,\n", + " reorder_chromsizes[\"End\"].sum(),\n", + ")\n", "\n", "\n", "tc.pl.zoomin(\n", " ax=ten.axs(2),\n", - " raw_regions=reorder['region'].to_list(),\n", - " zoomin_regions=draft_order['region'].to_list(),\n", + " raw_regions=reorder[\"region\"].to_list(),\n", + " zoomin_regions=draft_order[\"region\"].to_list(),\n", " line_on=True,\n", " fill=True,\n", " alpha=0.5,\n", ")\n", "\n", - "tc.pl.mapC(ax=ten.axs(3), \n", - " mat2=draft_mat.cmat,\n", - " label='draft contig',\n", - " cmap='PuBu',\n", - " map_type=\"tri\")\n", + "tc.pl.mapC(\n", + " ax=ten.axs(3),\n", + " mat2=draft_mat.cmat,\n", + " label=\"draft contig\",\n", + " cmap=\"PuBu\",\n", + " map_type=\"tri\",\n", + ")\n", "\n", "# tc.savefig('reorder_contig.pdf')" ] diff --git a/docs/gallery/long_range_interact.ipynb b/docs/gallery/long_range_interact.ipynb index cf8f011..81b8095 100644 --- a/docs/gallery/long_range_interact.ipynb +++ b/docs/gallery/long_range_interact.ipynb @@ -36,6 +36,7 @@ "outputs": [], "source": [ "import cooler\n", + "\n", "import trackc as tc" ] }, diff --git a/docs/gallery/neo_domain.ipynb b/docs/gallery/neo_domain.ipynb index b37e951..a2f1f3b 100644 --- a/docs/gallery/neo_domain.ipynb +++ b/docs/gallery/neo_domain.ipynb @@ -35,7 +35,9 @@ "\n", "import trackc as tc\n", "\n", - "BxPC3_chr18_25k = cooler.Cooler( \"../../../trackc_data/examples/BxPC3.chr18.mcool::/resolutions/25000\")\n", + "BxPC3_chr18_25k = cooler.Cooler(\n", + " \"../../../trackc_data/examples/BxPC3.chr18.mcool::/resolutions/25000\"\n", + ")\n", "\n", "zoom_regions = [\"18:47400000-48280000\", \"18:75280000-74030000\"]\n", "neo_domain_regions = [\"18:47950000-48280000\", \"18:75280000-74850000\"]\n", diff --git a/docs/gallery/scaffold_contig_reorder.ipynb b/docs/gallery/scaffold_contig_reorder.ipynb index fe1e00a..a177c72 100644 --- a/docs/gallery/scaffold_contig_reorder.ipynb +++ b/docs/gallery/scaffold_contig_reorder.ipynb @@ -13,9 +13,11 @@ "metadata": {}, "outputs": [], "source": [ - "import trackc as tc\n", + "import time\n", + "\n", "import cooler\n", - "import time" + "\n", + "import trackc as tc" ] }, { @@ -24,7 +26,7 @@ "metadata": {}, "outputs": [], "source": [ - "clr = cooler.Cooler('contig_bin_interaction.mcool::/resolutions/500000')" + "clr = cooler.Cooler(\"contig_bin_interaction.mcool::/resolutions/500000\")" ] }, { @@ -305,9 +307,10 @@ "source": [ "chrom_contigs = reorder.query('Chromosome==\"chr04\"')\n", "display(chrom_contigs)\n", - "order1 = chrom_contigs['region'].to_list()\n", + "order1 = chrom_contigs[\"region\"].to_list()\n", "\n", "import random\n", + "\n", "order2 = random.sample(order1, len(order1))" ] }, @@ -343,7 +346,7 @@ "start_time = time.time()\n", "r1_mat = tc.tl.extractContactRegions(clr=clr, row_regions=order1)\n", "r2_mat = tc.tl.extractContactRegions(clr=clr, row_regions=order2)\n", - "time.time()- start_time\n", + "time.time() - start_time\n", "\n", "ten = tc.tenon(figsize=(6, 1))\n", "ten.add(pos=\"bottom\", height=6)\n", @@ -352,14 +355,18 @@ "ten.add(pos=\"bottom\", height=3)\n", "\n", "\n", - "tc.pl.mapC(ax=ten.axs(0), \n", - " mat=r1_mat.cmat,\n", - " mat2=r2_mat.cmat,\n", - " label=['chr4 contig order', 'scaffold4 draft contig order'],\n", - " cmap=['PuBu', tc.pa.fruitpunch],\n", - " map_type=\"squ\")\n", + "tc.pl.mapC(\n", + " ax=ten.axs(0),\n", + " mat=r1_mat.cmat,\n", + " mat2=r2_mat.cmat,\n", + " label=[\"chr4 contig order\", \"scaffold4 draft contig order\"],\n", + " cmap=[\"PuBu\", tc.pa.fruitpunch],\n", + " map_type=\"squ\",\n", + ")\n", "\n", - "tc.pl.multi_scale_track(ten.axs(1), regions=order2, scale_adjust=\"Mb\", intervals=7, tick_rotation=0)\n", + "tc.pl.multi_scale_track(\n", + " ten.axs(1), regions=order2, scale_adjust=\"Mb\", intervals=7, tick_rotation=0\n", + ")\n", "\n", "tc.pl.zoomin(\n", " ax=ten.axs(2),\n", @@ -370,7 +377,9 @@ " alpha=0.5,\n", ")\n", "\n", - "tc.pl.multi_scale_track(ten.axs(3), regions=order1, scale_adjust=\"Mb\", intervals=7, tick_rotation=0)\n", + "tc.pl.multi_scale_track(\n", + " ten.axs(3), regions=order1, scale_adjust=\"Mb\", intervals=7, tick_rotation=0\n", + ")\n", "\n", "\n", "# tc.savefig('reorder_contig.pdf')" diff --git a/docs/track_types/bdgmat.ipynb b/docs/track_types/bdgmat.ipynb index 159feef..8a0ce31 100644 --- a/docs/track_types/bdgmat.ipynb +++ b/docs/track_types/bdgmat.ipynb @@ -86,10 +86,11 @@ } ], "source": [ - "import trackc as tc\n", "import pandas as pd\n", "\n", - "df = pd.read_table('../../../trackc_data/tutorials/bedGraphMatrix/insulations.txt')\n", + "import trackc as tc\n", + "\n", + "df = pd.read_table(\"../../../trackc_data/tutorials/bedGraphMatrix/insulations.txt\")\n", "display(df.head(2))" ] }, @@ -122,11 +123,17 @@ } ], "source": [ - "df_bdgmat = insulation_table[['chrom', 'start', 'end', \n", - " 'log2_insulation_score_50000', \n", - " 'log2_insulation_score_100000', \n", - " 'log2_insulation_score_250000']]\n", - "coolfile = '/Users/yuanzan/Downloads/HFF_MicroC.mcool::resolutions/10000'\n", + "df_bdgmat = insulation_table[\n", + " [\n", + " \"chrom\",\n", + " \"start\",\n", + " \"end\",\n", + " \"log2_insulation_score_50000\",\n", + " \"log2_insulation_score_100000\",\n", + " \"log2_insulation_score_250000\",\n", + " ]\n", + "]\n", + "coolfile = \"/Users/yuanzan/Downloads/HFF_MicroC.mcool::resolutions/10000\"\n", "\n", "regions = \"chr2:10500000-13200000\"\n", "HFF = tc.tl.extractCisContact(clr=coolfile, region=regions, extend=150)\n", @@ -135,18 +142,26 @@ "ten.add(pos=\"bottom\", height=1.5, hspace=0.05)\n", "ten.add(pos=\"bottom\", height=1, hspace=0.07)\n", "\n", - "tc.pl.mapC(\n", - " ax=ten.axs(0),\n", - " mat=HFF,\n", - " label=\"HFF\",\n", - " map_type=\"rec\",\n", - " height=150\n", - ")\n", + "tc.pl.mapC(ax=ten.axs(0), mat=HFF, label=\"HFF\", map_type=\"rec\", height=150)\n", "\n", - "tc.pl.bdgmat_track(ax=ten.axs(1), bed=df_bdgmat, regions=regions, style='line', label='insulation score', tick_fontsize=9)\n", - "tc.pl.scale_track(ax=ten.axs(1), region=regions, scale_adjust=\"Mb\", tick_pos=\"bottom\", ratio2ax=1, space=0)\n", + "tc.pl.bdgmat_track(\n", + " ax=ten.axs(1),\n", + " bed=df_bdgmat,\n", + " regions=regions,\n", + " style=\"line\",\n", + " label=\"insulation score\",\n", + " tick_fontsize=9,\n", + ")\n", + "tc.pl.scale_track(\n", + " ax=ten.axs(1),\n", + " region=regions,\n", + " scale_adjust=\"Mb\",\n", + " tick_pos=\"bottom\",\n", + " ratio2ax=1,\n", + " space=0,\n", + ")\n", "\n", - "#tc.savefig('outfile.pdf')" + "# tc.savefig('outfile.pdf')" ] } ], diff --git a/docs/track_types/bedGraph_matrix.ipynb b/docs/track_types/bedGraph_matrix.ipynb index e266c33..1256fc8 100644 --- a/docs/track_types/bedGraph_matrix.ipynb +++ b/docs/track_types/bedGraph_matrix.ipynb @@ -7,7 +7,9 @@ "outputs": [], "source": [ "import os\n", + "\n", "import pandas as pd\n", + "\n", "import trackc as tc" ] }, @@ -70,7 +72,7 @@ ], "source": [ "df = pd.read_table(\"./trackc_data/tutorials/bedGraphMatrix/HFF_MicroC.chr2is.txt\")\n", - "coolfile = '/Users/yuanzan/Downloads/HFF_MicroC.mcool::resolutions/10000'\n", + "coolfile = \"/Users/yuanzan/Downloads/HFF_MicroC.mcool::resolutions/10000\"\n", "\n", "regions = \"chr2:10500000-13200000\"\n", "HFF = tc.tl.extractCisContact(clr=coolfile, region=regions, extend=150)\n", @@ -88,11 +90,13 @@ " height=150,\n", ")\n", "\n", - "tc.pl.bdgmat_track(ax=ten.axs(1), bed=df, regions=regions, cmap='RdBu_r')\n", - "tc.pl.bdgmat_track(ax=ten.axs(2), bed=df, regions=regions, style='line')\n", - "tc.pl.scale_track(ax=ten.axs(2), region=regions, scale_adjust=\"Mb\", tick_pos=\"bottom\", ratio2ax=1)\n", + "tc.pl.bdgmat_track(ax=ten.axs(1), bed=df, regions=regions, cmap=\"RdBu_r\")\n", + "tc.pl.bdgmat_track(ax=ten.axs(2), bed=df, regions=regions, style=\"line\")\n", + "tc.pl.scale_track(\n", + " ax=ten.axs(2), region=regions, scale_adjust=\"Mb\", tick_pos=\"bottom\", ratio2ax=1\n", + ")\n", "\n", - "#tc.savefig('outfile.pdf')" + "# tc.savefig('outfile.pdf')" ] } ], diff --git a/trackc/__init__.py b/trackc/__init__.py index e5f61c3..974b999 100644 --- a/trackc/__init__.py +++ b/trackc/__init__.py @@ -8,16 +8,7 @@ plt.rcParams["pdf.fonttype"] = 42 plt.rcParams["svg.fonttype"] = "none" -from trackc import tl, pl, pa +from trackc import pa, pl, tl from trackc.gs import make_spec, savefig, tenon -__all__ = ["tl", "pl", "pa", "make_spec", "tenon"] - -""" -import sys -sys.modules.update({f'{__name__}.{m}': globals()[m] for m in ['tl', 'pl', 'pa']}) -from ._utils import annotate_doc_types -annotate_doc_types(sys.modules[__name__], 'trackc') -del sys, annotate_doc_types -""" - +__all__ = ["tl", "pl", "pa", "make_spec", "savefig", "tenon"] diff --git a/trackc/_utils.py b/trackc/_utils.py index e494e2b..83f5f69 100644 --- a/trackc/_utils.py +++ b/trackc/_utils.py @@ -1,16 +1,16 @@ """Utility functions and classes""" -import logging - -import colorlog -import sys import inspect +import logging +import sys import warnings -import pandas as pd -from typing import Union, Callable, Optional, Mapping, Any, Dict, Tuple -from types import ModuleType, MethodType -from weakref import WeakSet from functools import partial, wraps +from types import MethodType, ModuleType +from typing import Any, Callable, Dict, Mapping, Optional, Tuple, Union +from weakref import WeakSet + +import colorlog +import pandas as pd def get_logger(level=logging.INFO): @@ -43,39 +43,40 @@ def get_logger(level=logging.INFO): LOGGER = get_logger() + def _getdoc(c_or_f: Union[Callable, type]) -> Optional[str]: - if getattr(c_or_f, '__doc__', None) is None: + if getattr(c_or_f, "__doc__", None) is None: return None doc = inspect.getdoc(c_or_f) - if isinstance(c_or_f, type) and hasattr(c_or_f, '__init__'): + if isinstance(c_or_f, type) and hasattr(c_or_f, "__init__"): sig = inspect.signature(c_or_f.__init__) else: sig = inspect.signature(c_or_f) def type_doc(name: str): param: inspect.Parameter = sig.parameters[name] - cls = getattr(param.annotation, '__qualname__', repr(param.annotation)) + cls = getattr(param.annotation, "__qualname__", repr(param.annotation)) if param.default is not param.empty: - return f'{cls}, optional (default: {param.default!r})' + return f"{cls}, optional (default: {param.default!r})" else: return cls - return '\n'.join( - f'{line} : {type_doc(line)}' if line.strip() in sig.parameters else line - for line in doc.split('\n') + return "\n".join( + f"{line} : {type_doc(line)}" if line.strip() in sig.parameters else line + for line in doc.split("\n") ) - def _one_of_ours(obj, root: str): return ( hasattr(obj, "__name__") and not obj.__name__.split(".")[-1].startswith("_") and getattr( - obj, '__module__', getattr(obj, '__qualname__', obj.__name__) + obj, "__module__", getattr(obj, "__qualname__", obj.__name__) ).startswith(root) ) + def _descend_classes_and_funcs(mod: ModuleType, root: str, encountered=None): if encountered is None: encountered = WeakSet() @@ -93,7 +94,6 @@ def _descend_classes_and_funcs(mod: ModuleType, root: str, encountered=None): yield from _descend_classes_and_funcs(obj, root, encountered) - def annotate_doc_types(mod: ModuleType, root: str): for c_or_f in _descend_classes_and_funcs(mod, root): c_or_f.getdoc = partial(_getdoc, c_or_f) diff --git a/trackc/pl/bed.py b/trackc/pl/bed.py index 24459d4..3faef7f 100644 --- a/trackc/pl/bed.py +++ b/trackc/pl/bed.py @@ -1,19 +1,19 @@ +import sys from typing import Optional, Sequence, Union import matplotlib.pyplot as plt import numpy as np -import sys import pandas as pd from matplotlib import cm from matplotlib.axes import Axes from matplotlib.collections import PatchCollection -from matplotlib.colors import Colormap +from matplotlib.colors import Colormap, TwoSlopeNorm from matplotlib.patches import Polygon from trackc.pl.bigwig import _make_multi_region_ax from trackc.pl.links import _plot_loop_arc from trackc.tl._getRegionsCmat import GenomeRegion -from matplotlib.colors import TwoSlopeNorm + def bed_track( ax: Optional[Axes] = None, @@ -94,12 +94,16 @@ def bed_track( if isinstance(primary_col, list) == False: primary_col = [primary_col] if len(primary_col) < line_GenomeRegions.shape[0]: - repeat_times = (line_GenomeRegions.shape[0] + len(primary_col) - 1) // len(primary_col) + repeat_times = (line_GenomeRegions.shape[0] + len(primary_col) - 1) // len( + primary_col + ) primary_col = (primary_col * repeat_times)[: line_GenomeRegions.shape[0]] if isinstance(secondary_col, list) == False: secondary_col = [secondary_col] if len(secondary_col) < line_GenomeRegions.shape[0]: - repeat_times = (line_GenomeRegions.shape[0] + len(secondary_col) - 1) // len(secondary_col) + repeat_times = (line_GenomeRegions.shape[0] + len(secondary_col) - 1) // len( + secondary_col + ) secondary_col = (secondary_col * repeat_times)[: line_GenomeRegions.shape[0]] if isinstance(cmap, list) == False: @@ -144,17 +148,17 @@ def bed_track( max_y = None max_len = 0 - chromos = bed['chrom'].unique() + chromos = bed["chrom"].unique() for ix, row in line_GenomeRegions.iterrows(): raw_chr = row["chrom"] if row["chrom"] not in chromos: - if row["chrom"].startswith('chr'): - row["chrom"] = row["chrom"].lstrip('chr') + if row["chrom"].startswith("chr"): + row["chrom"] = row["chrom"].lstrip("chr") else: - row["chrom"] = 'chr' + row["chrom"] + row["chrom"] = "chr" + row["chrom"] if row["chrom"] not in chromos: - print(f'{raw_chr} not in beg chroms!') + print(f"{raw_chr} not in beg chroms!") sys.exit(0) bed2plot = bed[ @@ -163,7 +167,6 @@ def bed_track( & (bed["start"] <= row["fetch_end"]) ] - if style in ["line", "bar"] or bed.shape[1] >= 4: if min_y == None: min_y = bed2plot["score"].min(skipna=True, numeric_only=True) @@ -191,15 +194,15 @@ def bed_track( for ix, row in line_GenomeRegions.iterrows(): raw_chr = row["chrom"] if row["chrom"] not in chromos: - if row["chrom"].startswith('chr'): - row["chrom"] = row["chrom"].lstrip('chr') + if row["chrom"].startswith("chr"): + row["chrom"] = row["chrom"].lstrip("chr") else: - row["chrom"] = 'chr' + row["chrom"] + row["chrom"] = "chr" + row["chrom"] if row["chrom"] not in chromos: - print(f'{raw_chr} not in beg chroms!') + print(f"{raw_chr} not in beg chroms!") sys.exit(0) - + bed2plot = bed[ (bed["chrom"] == row["chrom"]) & (bed["end"] >= row["fetch_start"]) @@ -217,7 +220,7 @@ def bed_track( needReverse=row["isReverse"], style="line", pri_col=primary_col[ix], - #5sec_col=secondary_col[ix], + # 5sec_col=secondary_col[ix], alpha=alpha, ) @@ -351,9 +354,9 @@ def _plot_bed_tri( # 因为 y 大到一定程度超过临界数值后颜色就会饱和不变(不使用循环colormap)。 norm = plt.Normalize(vmin, vmax) - if vmin<0 and vmax>0: + if vmin < 0 and vmax > 0: norm = TwoSlopeNorm(vmin=vmin, vcenter=0, vmax=vmax) - + # matplotlib.colors.Normalize 对象,可以作为参数传入到绘图方法里 # 也可给其传入数值直接计算归一化的结果 norm_y = norm(bed["score"]) @@ -412,9 +415,9 @@ def _plot_bed_rec( # 因为 y 大到一定程度超过临界数值后颜色就会饱和不变(不使用循环colormap)。 norm = plt.Normalize(vmin, vmax) - if vmin<0 and vmax>0: + if vmin < 0 and vmax > 0: norm = TwoSlopeNorm(vmin=vmin, vcenter=0, vmax=vmax) - + # matplotlib.colors.Normalize 对象,可以作为参数传入到绘图方法里 # 也可给其传入数值直接计算归一化的结果 norm_y = norm(bed["score"]) @@ -447,7 +450,7 @@ def _plot_bed_rec( xlim_e = start ax.set_xlim(xlim_s, xlim_e) if cname != None: - #sm = cm.ScalarMappable(norm=norm, cmap=map_vir) + # sm = cm.ScalarMappable(norm=norm, cmap=map_vir) sm = cm.ScalarMappable(norm=norm, cmap=map_vir) cax = mainAX.inset_axes([1.01, 0, 0.01, 0.9]) cb = plt.colorbar(sm, ax=mainAX, cax=cax, label=score_label) @@ -456,14 +459,22 @@ def _plot_bed_rec( def _plot_bed_bar_l( - ax, bed, start, end, needReverse, style="bar", pri_col="#3271B2", sec_col="#FBD23C", alpha=1 + ax, + bed, + start, + end, + needReverse, + style="bar", + pri_col="#3271B2", + sec_col="#FBD23C", + alpha=1, ): plot_bottom_line = True if style == "bar": - bed_pos = bed.query('score>=0') - bed_neg = bed.query('score<0') - if bed_neg.shape[0] >0 : - plot_bottom_line=False + bed_pos = bed.query("score>=0") + bed_neg = bed.query("score<0") + if bed_neg.shape[0] > 0: + plot_bottom_line = False ax.bar( x=bed_pos["start"], width=bed_pos["end"] - bed_pos["start"], @@ -483,7 +494,11 @@ def _plot_bed_bar_l( if style == "line": ax.plot( - bed["start"], bed["score"], color=pri_col, alpha=alpha, solid_capstyle="butt" + bed["start"], + bed["score"], + color=pri_col, + alpha=alpha, + solid_capstyle="butt", ) xlim_s = start @@ -495,10 +510,10 @@ def _plot_bed_bar_l( spines = ["top", "bottom", "left", "right"] if needReverse == True: - if plot_bottom_line==True: + if plot_bottom_line == True: del spines[0] else: - if plot_bottom_line==True: + if plot_bottom_line == True: del spines[1] """ diff --git a/trackc/pl/bedgraph_matrix.py b/trackc/pl/bedgraph_matrix.py index 80514dd..96589b0 100644 --- a/trackc/pl/bedgraph_matrix.py +++ b/trackc/pl/bedgraph_matrix.py @@ -1,31 +1,35 @@ import sys -from matplotlib.axes import Axes -from typing import Union, Optional, Sequence -from mpl_toolkits.axes_grid1.inset_locator import inset_axes -from matplotlib.colors import Colormap +from typing import Optional, Sequence, Union + import matplotlib.pyplot as plt import pandas as pd -from matplotlib.colors import LogNorm #,CenteredNorm, SymLogNorm,PowerNorm, Normalize -from trackc.tl._getRegionsCmat import GenomeRegion +from matplotlib.axes import Axes +from matplotlib.colors import LogNorm # ,CenteredNorm, SymLogNorm,PowerNorm, Normalize +from matplotlib.colors import Colormap +from mpl_toolkits.axes_grid1.inset_locator import inset_axes + from trackc.pa import fruitpunch, trackcl_9, trackcl_11, zeileis_28 +from trackc.tl._getRegionsCmat import GenomeRegion + -def bdgmat_track(ax: Optional[Axes] = None, - bed: Union[pd.DataFrame, str, None] = None, - regions: Union[Sequence[str], str, None] = None, - style: Union[str, None] = 'heatmap', - logdata: bool = False, - minrange: Optional[float] = None, - maxrange: Optional[float] = None, - color: Union[Sequence[str], None] = 'silver', - cmap: Union[Colormap, str, None] = fruitpunch, - alpha: Union[float, None] = 1, - label: Union[str, None] = None, - label_fontsize: Union[int, None] = 8, - ticks_on: bool=True, - tick_fontsize: Optional[int] = 7, - tick_fl: Optional[str] = "%0.2f", - line_lg_ncol: Optional[int] = 1 - ): +def bdgmat_track( + ax: Optional[Axes] = None, + bed: Union[pd.DataFrame, str, None] = None, + regions: Union[Sequence[str], str, None] = None, + style: Union[str, None] = "heatmap", + logdata: bool = False, + minrange: Optional[float] = None, + maxrange: Optional[float] = None, + color: Union[Sequence[str], None] = "silver", + cmap: Union[Colormap, str, None] = fruitpunch, + alpha: Union[float, None] = 1, + label: Union[str, None] = None, + label_fontsize: Union[int, None] = 8, + ticks_on: bool = True, + tick_fontsize: Optional[int] = 7, + tick_fl: Optional[str] = "%0.2f", + line_lg_ncol: Optional[int] = 1, +): """\ Plot bedGraph matrix track, support for multiple or reverse genome regions. bedgraph matrix file like bedgraph, columns from 4th are values @@ -59,24 +63,26 @@ def bdgmat_track(ax: Optional[Axes] = None, """ if isinstance(regions, list): - line_GenomeRegions = pd.concat([GenomeRegion(i).GenomeRegion2df() for i in regions]) + line_GenomeRegions = pd.concat( + [GenomeRegion(i).GenomeRegion2df() for i in regions] + ) else: line_GenomeRegions = GenomeRegion(regions).GenomeRegion2df() - #axs = _make_multi_region_ax(ax, line_GenomeRegions) + # axs = _make_multi_region_ax(ax, line_GenomeRegions) line_GenomeRegions = line_GenomeRegions.reset_index() - if isinstance(bed, str)==True: - bed=pd.read_table(bed, sep="\t", header=0) + if isinstance(bed, str) == True: + bed = pd.read_table(bed, sep="\t", header=0) - if style=="line": - if isinstance(color, list)==False: + if style == "line": + if isinstance(color, list) == False: color = [color] - score_columns = bed.shape[1]-3 + score_columns = bed.shape[1] - 3 if len(color) < score_columns: - #repeat_times = (line_GenomeRegions.shape[0] + len(color) - 1) // len(color) - #color = (color * repeat_times)[:score_columns] - print('color sizes less than plots, use defaults') + # repeat_times = (line_GenomeRegions.shape[0] + len(color) - 1) // len(color) + # color = (color * repeat_times)[:score_columns] + print("color sizes less than plots, use defaults") if score_columns <= 9: color = trackcl_9 elif score_columns <= 11: @@ -86,100 +92,118 @@ def bdgmat_track(ax: Optional[Axes] = None, else: pass + bed["chrom"] = bed["chrom"].astype(str) - bed['chrom'] = bed['chrom'].astype(str) - df2plot = pd.DataFrame() bed = bed.fillna(0) - chromos = bed['chrom'].unique() - for ix, row in line_GenomeRegions.iterrows(): + chromos = bed["chrom"].unique() + for ix, row in line_GenomeRegions.iterrows(): raw_chr = row["chrom"] if row["chrom"] not in chromos: - if row["chrom"].startswith('chr'): - row["chrom"] = row["chrom"].lstrip('chr') + if row["chrom"].startswith("chr"): + row["chrom"] = row["chrom"].lstrip("chr") else: - row["chrom"] = 'chr' + row["chrom"] + row["chrom"] = "chr" + row["chrom"] if row["chrom"] not in chromos: - print(f'{raw_chr} not in begmat chroms!') - #sys.exit(0) + print(f"{raw_chr} not in begmat chroms!") + # sys.exit(0) return - bed2plot = bed[(bed['chrom']==row['chrom']) & (bed['end']>=row['fetch_start']) & (bed['start']<=row['fetch_end'])] - if row['isReverse']==True: + bed2plot = bed[ + (bed["chrom"] == row["chrom"]) + & (bed["end"] >= row["fetch_start"]) + & (bed["start"] <= row["fetch_end"]) + ] + if row["isReverse"] == True: bed2plot = bed2plot.iloc[::-1] - if df2plot.shape[0]==0: + if df2plot.shape[0] == 0: df2plot = bed2plot else: df2plot = pd.concat([df2plot, bed2plot], axis=0) - - df2plot = df2plot.iloc[:,3:] - if minrange==None: + + df2plot = df2plot.iloc[:, 3:] + if minrange == None: minrange = df2plot.min().min() - if maxrange==None: + if maxrange == None: maxrange = df2plot.max().max() norm = None if logdata: - norm=LogNorm() + norm = LogNorm() clim = (minrange, maxrange) - - if style=="heatmap": + if style == "heatmap": df2plot = df2plot.T - caxes = ax.pcolormesh(df2plot, cmap=cmap, clim=clim, edgecolor='none', norm=norm, snap=True, linewidth=.001) + caxes = ax.pcolormesh( + df2plot, + cmap=cmap, + clim=clim, + edgecolor="none", + norm=norm, + snap=True, + linewidth=0.001, + ) _colorbar(ax, caxes, tick_font_size=tick_fontsize) ax.set_xticks([]) # 隐藏刻度 ax.set_xticklabels([]) # 隐藏刻度标签 - #if - ax.set_yticks([i+0.5 for i in range(0, df2plot.shape[0])]) # 隐藏刻度 + # if + ax.set_yticks([i + 0.5 for i in range(0, df2plot.shape[0])]) # 隐藏刻度 if ticks_on: ax.set_yticklabels(df2plot.index, fontsize=tick_fontsize) # 隐藏刻度标签 else: ax.set_yticklabels([]) ax.set_yticks([]) - elif style=="line": - #print(df2plot) - #import sys - #sys.exit(1) + elif style == "line": + # print(df2plot) + # import sys + # sys.exit(1) df2plot = df2plot.reset_index() - del df2plot['index'] + del df2plot["index"] line = ax.plot(df2plot) if len(color) < df2plot.shape[1]: repeat_times = (df2plot.shape[1] + len(color) - 1) // len(color) - color = (color * repeat_times) + color = color * repeat_times - for i,v in enumerate(line): + for i, v in enumerate(line): v.set_label(df2plot.columns[i]) v.set(color=color[i]) - ax.set_xlim(0, df2plot.shape[0]-1) - print(f'bdg_mat minrange:{minrange} maxrange:{maxrange}') + ax.set_xlim(0, df2plot.shape[0] - 1) + print(f"bdg_mat minrange:{minrange} maxrange:{maxrange}") ax.set_xticklabels([]) ax.set_xticks([]) ax.set_ylim([minrange, maxrange]) ax.set_yticks([minrange, maxrange]) - ax.set_yticklabels([f'{tick_fl % minrange}', f'{tick_fl % maxrange}'], fontsize=tick_fontsize) # 隐藏刻度标签 + ax.set_yticklabels( + [f"{tick_fl % minrange}", f"{tick_fl % maxrange}"], fontsize=tick_fontsize + ) # 隐藏刻度标签 if ticks_on: - ax.legend(loc='best', bbox_to_anchor=(1, 0, 0, 1), fontsize=tick_fontsize, ncol=line_lg_ncol) + ax.legend( + loc="best", + bbox_to_anchor=(1, 0, 0, 1), + fontsize=tick_fontsize, + ncol=line_lg_ncol, + ) ax.set_ylabel(label, fontsize=label_fontsize) -def _colorbar(axm,im, width="3%", height="100%", tick_font_size=7): - axins = inset_axes(axm, - width=width, # width = 5% of parent_bbox width - height=height, # height : 50% - loc='lower left', - bbox_to_anchor=(1.01, 0, 1, 1), - bbox_transform=axm.transAxes, - borderpad=0, - ) - cbar=plt.colorbar(im, cax=axins, orientation='vertical') + +def _colorbar(axm, im, width="3%", height="100%", tick_font_size=7): + axins = inset_axes( + axm, + width=width, # width = 5% of parent_bbox width + height=height, # height : 50% + loc="lower left", + bbox_to_anchor=(1.01, 0, 1, 1), + bbox_transform=axm.transAxes, + borderpad=0, + ) + cbar = plt.colorbar(im, cax=axins, orientation="vertical") cbar.set_ticks([im.get_array().min(), im.get_array().max()]) cbar.ax.tick_params(labelsize=tick_font_size) cbar.ax.yaxis.tick_right() - cbar.ax.spines['top'].set_color('none') - cbar.ax.spines['right'].set_color('none') - cbar.ax.spines['bottom'].set_color('none') - cbar.ax.spines['left'].set_color('none') - + cbar.ax.spines["top"].set_color("none") + cbar.ax.spines["right"].set_color("none") + cbar.ax.spines["bottom"].set_color("none") + cbar.ax.spines["left"].set_color("none") diff --git a/trackc/pl/bigwig.py b/trackc/pl/bigwig.py index 2b861c3..2c69749 100644 --- a/trackc/pl/bigwig.py +++ b/trackc/pl/bigwig.py @@ -1,15 +1,16 @@ +import sys +import warnings from typing import Any, Callable, List, Mapping, Optional, Sequence, Tuple, Union -import numpy as np -import sys import pandas as pd -from matplotlib.axes import Axes import pyBigWig -from trackc._utils import LOGGER +from matplotlib.axes import Axes + from trackc.tl._getRegionsCmat import GenomeRegion -import warnings + warnings.filterwarnings("ignore") + def _make_multi_region_ax(ax, lineGenomeRegions): lineGenomeRegions["len"] = ( lineGenomeRegions["fetch_end"] - lineGenomeRegions["fetch_start"] @@ -125,13 +126,17 @@ def bw_track( if isinstance(primary_col, list) == False: primary_col = [primary_col] if len(primary_col) < line_GenomeRegions.shape[0]: - repeat_times = (line_GenomeRegions.shape[0] + len(primary_col) - 1) // len(primary_col) + repeat_times = (line_GenomeRegions.shape[0] + len(primary_col) - 1) // len( + primary_col + ) primary_col = (primary_col * repeat_times)[: line_GenomeRegions.shape[0]] if isinstance(secondary_col, list) == False: secondary_col = [secondary_col] if len(secondary_col) < line_GenomeRegions.shape[0]: - repeat_times = (line_GenomeRegions.shape[0] + len(secondary_col) - 1) // len(secondary_col) + repeat_times = (line_GenomeRegions.shape[0] + len(secondary_col) - 1) // len( + secondary_col + ) secondary_col = (secondary_col * repeat_times)[: line_GenomeRegions.shape[0]] min_y = 0 @@ -142,41 +147,47 @@ def bw_track( bins = int(row["len"] / binsize) if row["chrom"] not in bw.chroms(): raw_chr = row["chrom"] - if row["chrom"].startswith('chr'): - row["chrom"] = row["chrom"].lstrip('chr') + if row["chrom"].startswith("chr"): + row["chrom"] = row["chrom"].lstrip("chr") else: - row["chrom"] = 'chr' + row["chrom"] + row["chrom"] = "chr" + row["chrom"] if row["chrom"] not in bw.chroms(): - print(f'{raw_chr} not in bigwig chroms!') + print(f"{raw_chr} not in bigwig chroms!") return - plot_list = bw.stats(row["chrom"], int(row["fetch_start"]), int(row["fetch_end"]), type=summary_type, nBins=bins) + plot_list = bw.stats( + row["chrom"], + int(row["fetch_start"]), + int(row["fetch_end"]), + type=summary_type, + nBins=bins, + ) plot_list = [0 if v is None else v for v in plot_list] if style == "line": axs[i].plot(range(0, bins), plot_list, color=primary_col[i], alpha=alpha) else: - plotvalues = pd.DataFrame({'v':plot_list}, index=range(0, bins)) - pos_value = plotvalues.query('v>=0') - neg_value = plotvalues.query('v<0') - if neg_value.shape[0]>0: - plot_bottom_line = False + plotvalues = pd.DataFrame({"v": plot_list}, index=range(0, bins)) + pos_value = plotvalues.query("v>=0") + neg_value = plotvalues.query("v<0") + if neg_value.shape[0] > 0: + plot_bottom_line = False axs[i].bar( x=pos_value.index, - height=pos_value['v'], + height=pos_value["v"], width=1, - #bottom=[0] * (pos_value.shape[0]), + # bottom=[0] * (pos_value.shape[0]), bottom=0, color=primary_col[i], align="edge", edgecolor=None, alpha=alpha, ) - + axs[i].bar( x=neg_value.index, - height=neg_value['v'], + height=neg_value["v"], width=1, - #bottom=[0] * (neg_value.shape[0]), + # bottom=[0] * (neg_value.shape[0]), bottom=0, color=secondary_col[i], align="edge", @@ -225,11 +236,15 @@ def bw_track( else: ax.set_yticks([vmin, 0, vmax]) - ax.set_yticklabels([f'{tick_fl % vmin}', '0', f'{tick_fl % vmax}'], fontsize=tick_fontsize) + ax.set_yticklabels( + [f"{tick_fl % vmin}", "0", f"{tick_fl % vmax}"], fontsize=tick_fontsize + ) if invert_y: ax.set_yticks([vmax, 0, vmin]) - ax.set_yticklabels([f'{tick_fl % vmax}', '0', f'{tick_fl % vmin}'], fontsize=tick_fontsize) - + ax.set_yticklabels( + [f"{tick_fl % vmax}", "0", f"{tick_fl % vmin}"], fontsize=tick_fontsize + ) + ax.set_ylabel( label, fontsize=label_fontsize, @@ -241,12 +256,12 @@ def bw_track( if ax_on == False: spines = ["top", "bottom", "left", "right"] if invert_y == True: - if plot_bottom_line==True: + if plot_bottom_line == True: del spines[0] else: del spines[2] else: - if plot_bottom_line==True: + if plot_bottom_line == True: del spines[1] else: del spines[2] @@ -254,6 +269,3 @@ def bw_track( ax.spines[i].set_visible(False) ax.set_xticks([]) ax.set_xticklabels("") - - - diff --git a/trackc/pl/gene.py b/trackc/pl/gene.py index 913e63e..2ab388f 100644 --- a/trackc/pl/gene.py +++ b/trackc/pl/gene.py @@ -3,10 +3,9 @@ import pandas as pd from matplotlib.axes import Axes +from trackc.pl.bigwig import _make_multi_region_ax from trackc.tl._getRegionsCmat import GenomeRegion -from .bigwig import _make_multi_region_ax - def gene_track( ax: Optional[Axes] = None, @@ -125,16 +124,16 @@ def gene_track( bed12["blockStarts"] = bed12["blockStarts"].str.rstrip(",") bed12["chrom"] = bed12["chrom"].astype(str) chrom_names = bed12["chrom"].unique() - + for ix, row in line_GenomeRegions.iterrows(): if row["chrom"] not in chrom_names: raw_chr = row["chrom"] - if row["chrom"].startswith('chr'): - row["chrom"] = row["chrom"].lstrip('chr') + if row["chrom"].startswith("chr"): + row["chrom"] = row["chrom"].lstrip("chr") else: - row["chrom"] = 'chr' + row["chrom"] + row["chrom"] = "chr" + row["chrom"] if row["chrom"] not in chrom_names: - print(f'{raw_chr} not in bigwig chroms!') + print(f"{raw_chr} not in bigwig chroms!") return if track_type == "gene": diff --git a/trackc/pl/links.py b/trackc/pl/links.py index 8e5612f..c00b721 100644 --- a/trackc/pl/links.py +++ b/trackc/pl/links.py @@ -4,10 +4,9 @@ import pandas as pd from matplotlib.axes import Axes +from trackc.pl.bigwig import _make_multi_region_ax from trackc.tl._getRegionsCmat import GenomeRegion -from .bigwig import _make_multi_region_ax - def _two_degree_bc(x_l=10, x_r=90, y_lr=0, y2=10, dots_num=100): """ @@ -171,18 +170,19 @@ def links_track( data["chr1"] = data["chr1"].astype(str) data["chr2"] = data["chr2"].astype(str) - - chromos = data['chr1'].unique() - chrchrom = line_GenomeRegions.loc[0, 'chrom'] + chromos = data["chr1"].unique() + chrchrom = line_GenomeRegions.loc[0, "chrom"] if chrchrom not in chromos: - if chrchrom.startswith('chr'): - chrchrom = chrchrom.lstrip('chr') + if chrchrom.startswith("chr"): + chrchrom = chrchrom.lstrip("chr") if chrchrom in chromos: - line_GenomeRegions['chrom'] = line_GenomeRegions['chrom'].str.lstrip('chr') + line_GenomeRegions["chrom"] = line_GenomeRegions["chrom"].str.lstrip( + "chr" + ) else: - chrchrom = 'chr' + chrchrom + chrchrom = "chr" + chrchrom if chrchrom in chromos: - line_GenomeRegions['chrom'] = 'chr' + line_GenomeRegions['chrom'] + line_GenomeRegions["chrom"] = "chr" + line_GenomeRegions["chrom"] for ix, row in line_GenomeRegions.iterrows(): loop_bed_plot = data[data["chr1"] == row["chrom"]] diff --git a/trackc/pl/vhighlight.py b/trackc/pl/vhighlight.py index 951813d..935aa85 100644 --- a/trackc/pl/vhighlight.py +++ b/trackc/pl/vhighlight.py @@ -3,7 +3,7 @@ import bioframe as bf from matplotlib.axes import Axes -from .zoomin import _region_pos +from trackc.pl.zoomin import _region_pos def vhighlight( diff --git a/trackc/pl/zoomin.py b/trackc/pl/zoomin.py index bcf3881..ecdc852 100644 --- a/trackc/pl/zoomin.py +++ b/trackc/pl/zoomin.py @@ -80,10 +80,9 @@ def zoomin( if colors == None: colors = trackcl_11 - if len(colors) < zoomin_GRs.shape[0]: - colors = colors * (int(zoomin_GRs.shape[0]/len(colors)) + 1) - print(len(colors)) + colors = colors * (int(zoomin_GRs.shape[0] / len(colors)) + 1) + print(len(colors)) for i, row in zoomin_GRs.iterrows(): x = row[["ps", "pe", "ore", "ors"]] y = [0, 0, 1, 1] diff --git a/trackc/scripts/cli.py b/trackc/scripts/cli.py index ba40e93..5d3c534 100644 --- a/trackc/scripts/cli.py +++ b/trackc/scripts/cli.py @@ -1,68 +1,93 @@ #!/Users/yuanzan/anaconda3/bin/python3 # -*- coding: utf-8 -*- +import warnings + import click import yaml -import warnings warnings.filterwarnings("ignore") import trackc as tc - from trackc.scripts.gtf2bed12 import gtf2bed from trackc.scripts.trackc_cli import cli as track -#from trackc_cli import track -#from trackc.scripts.tadscore import tadScore + +# from trackc_cli import track +# from trackc.scripts.tadscore import tadScore @click.group() def main(): pass -@main.command(name="cli", short_help="plot trackc by use yaml format configfile, yaml track type parameters is the same as API") -@click.argument('config', metavar='') -@click.option('--regions', '-r', default=None, - help="genome regions, \ - eg.:\'18:47950000-48280000 18:75280000-74850000\'") - -@click.option('--outfile', '-o', default='trackc.pdf', - help='output filename. default=trackc.pdf') - -@click.option('--basefigsize', '-s', default='6,1', - help='base figsize: width,height. default=\"6,1\"\ - The height option in the config.yml is relative to the base figsize height') +@main.command( + name="cli", + short_help="plot trackc by use yaml format configfile, yaml track type parameters is the same as API", +) +@click.argument("config", metavar="") +@click.option( + "--regions", + "-r", + default=None, + help="genome regions, \ + eg.:'18:47950000-48280000 18:75280000-74850000'", +) +@click.option( + "--outfile", "-o", default="trackc.pdf", help="output filename. default=trackc.pdf" +) +@click.option( + "--basefigsize", + "-s", + default="6,1", + help='base figsize: width,height. default="6,1"\ + The height option in the config.yml is relative to the base figsize height', +) def cli(config, regions, outfile, basefigsize): track(config, regions, outfile, basefigsize) - -@main.command(name="gtf2bed", short_help="Converts the gene GTF to bed12 or bed13 format") -@click.argument('gtf', metavar='') -#, help="input file. GTF format gene annotation." -@click.option('--outfile', '-o', default='gene.bed', - help='File name of gene annotation bed12 format.') -@click.option('--sep', '-s', default=';', - help='tags of GTF sepration by which str') -@click.option('--gene_name_tag', '-nt', default='gene_name', - help='tags of GTF gene_name') -@click.option('--gene_id_tag', '-it', default='gene_id', - help='tags of GTF gene_id') -@click.option('--gene_biotype_tag', '-bt', default='gene_biotype', - help='tags of GTF gene_biotype') -@click.option('--biotype2bed13', '-bed13', is_flag=True, show_default=True, default=True, - help='if this arg is set, the out file columns 13 will add gene\'s biotype') - -def mode2(gtf, outfile, sep, gene_name_tag, gene_id_tag, gene_biotype_tag, biotype2bed13): - gtf2bed(gtf, outfile, sep, gene_name_tag, gene_id_tag, gene_biotype_tag, biotype2bed13) - -#@main.command() -#@click.argument('mcool', metavar='name.mcool::/resolutions/10000') - -#def tadscore(mcool): +@main.command( + name="gtf2bed", short_help="Converts the gene GTF to bed12 or bed13 format" +) +@click.argument("gtf", metavar="") +# , help="input file. GTF format gene annotation." +@click.option( + "--outfile", + "-o", + default="gene.bed", + help="File name of gene annotation bed12 format.", +) +@click.option("--sep", "-s", default=";", help="tags of GTF sepration by which str") +@click.option( + "--gene_name_tag", "-nt", default="gene_name", help="tags of GTF gene_name" +) +@click.option("--gene_id_tag", "-it", default="gene_id", help="tags of GTF gene_id") +@click.option( + "--gene_biotype_tag", "-bt", default="gene_biotype", help="tags of GTF gene_biotype" +) +@click.option( + "--biotype2bed13", + "-bed13", + is_flag=True, + show_default=True, + default=True, + help="if this arg is set, the out file columns 13 will add gene's biotype", +) +def mode2( + gtf, outfile, sep, gene_name_tag, gene_id_tag, gene_biotype_tag, biotype2bed13 +): + gtf2bed( + gtf, outfile, sep, gene_name_tag, gene_id_tag, gene_biotype_tag, biotype2bed13 + ) + + +# @main.command() +# @click.argument('mcool', metavar='name.mcool::/resolutions/10000') + +# def tadscore(mcool): # tadScore() -if __name__ == '__main__': +if __name__ == "__main__": main() - diff --git a/trackc/scripts/gtf2bed12.py b/trackc/scripts/gtf2bed12.py index bd9f2cc..6da1f27 100644 --- a/trackc/scripts/gtf2bed12.py +++ b/trackc/scripts/gtf2bed12.py @@ -38,8 +38,23 @@ def tostr(self, biotype2bed13): blockSizes = blockSizes + "," + "{0}".format(int(tmp[1]) - int(tmp[0])) blockStarts = blockStarts + "," + tmp[0] - obj_str = ( - "{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}".format( + obj_str = "{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}".format( + self.chrom, + self.start, + self.end, + self.name, + self.score, + self.strand, + self.thickStart, + self.thickEnd, + self.itemRgb, + blockCount, + blockSizes, + blockStarts, + self.gene_biotype, + ) + if biotype2bed13 == True: + obj_str = "{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}".format( self.chrom, self.start, self.end, @@ -52,25 +67,6 @@ def tostr(self, biotype2bed13): blockCount, blockSizes, blockStarts, - self.gene_biotype, - ) - ) - if biotype2bed13 == True: - obj_str = ( - "{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}".format( - self.chrom, - self.start, - self.end, - self.name, - self.score, - self.strand, - self.thickStart, - self.thickEnd, - self.itemRgb, - blockCount, - blockSizes, - blockStarts, - ) ) return obj_str @@ -88,7 +84,9 @@ def _bedcol9_2dic(bedcol9, split=";"): return col9dic -def gtf2bed(gtf, outfile, sep, gene_name_tag, gene_id_tag, gene_biotype_tag, biotype2bed13): +def gtf2bed( + gtf, outfile, sep, gene_name_tag, gene_id_tag, gene_biotype_tag, biotype2bed13 +): gene = None gtf_hand = open(gtf, "r") gtf2bed = open(outfile, "w") diff --git a/trackc/scripts/trackc_cli.py b/trackc/scripts/trackc_cli.py index e7439d8..6e3c146 100644 --- a/trackc/scripts/trackc_cli.py +++ b/trackc/scripts/trackc_cli.py @@ -1,6 +1,5 @@ import sys -#import click import pandas as pd import yaml @@ -95,6 +94,8 @@ def hicContactMap(Paras, ax): tc.pl.mapC(ax=ax, mat=mat, mat2=mat2, **Paras["mapC"]) else: tc.pl.mapC(ax=ax, mat=mat, **Paras["mapC"]) + + """ @click.command() @@ -117,6 +118,8 @@ def hicContactMap(Paras, ax): The height option in the config.yml is relative to the base figsize height', ) """ + + def cli(config, regions, outfile, basefigsize): conf = _get_yaml_data(config) fs = basefigsize.split(",") @@ -150,8 +153,8 @@ def cli(config, regions, outfile, basefigsize): ax = ten.axs(axs_pre.loc[tt["ax"], "rank_u"]) ax.set_axis_off() continue - - print('ax: ', tt["ax"]) + + print("ax: ", tt["ax"]) paras = tt["track_para"] if track_type in regions_type: diff --git a/trackc/tl/_getRegionsCmat.py b/trackc/tl/_getRegionsCmat.py index 281ca25..8edd9b7 100644 --- a/trackc/tl/_getRegionsCmat.py +++ b/trackc/tl/_getRegionsCmat.py @@ -272,11 +272,11 @@ def extractCisContact( if genome_region.chrom not in clr.chromsizes: print(genome_region.chrom, " is not a chrom in the cool matrix") - if genome_region.chrom.startswith('chr'): - genome_region.chrom = genome_region.chrom.lstrip('chr') + if genome_region.chrom.startswith("chr"): + genome_region.chrom = genome_region.chrom.lstrip("chr") else: - genome_region.chrom = 'chr' + genome_region.chrom - print (f'{genome_region.chrom} instead') + genome_region.chrom = "chr" + genome_region.chrom + print(f"{genome_region.chrom} instead") maxChromL = clr.chromsizes[genome_region.chrom]