Skip to content

Commit c952cbd

Browse files
committed
Added option to show individual sample names, even when the population is provided
1 parent ace8472 commit c952cbd

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

structure_threader/plotter/structplot.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,9 @@ def _parse_indfile(self, indfile):
601601
self._sort_qvals_pop(indarray=indarray)
602602
indarray = indarray[indarray[:, 1].argsort()]
603603

604+
self.indv = indarray[:, 0]
605+
self.number_indv = len(self.indv)
606+
604607
# Set self.pops attribute
605608
pop_counts = Counter(indarray[:, 1])
606609
pop_sums = np.cumsum([np.count_nonzero(indarray == x)
@@ -797,13 +800,15 @@ def plotk(self, kvals, output_dir):
797800
with open(filepath, "w") as fh:
798801
fh.write(ploty_html(pdiv))
799802

800-
def plotk_static(self, kval, output_dir, bw=False):
803+
def plotk_static(self, kval, output_dir, bw=False, use_ind=False):
801804
"""
802805
Generates a structure plot in svg format.
803806
:param kval: (int) Must match the K value from self.kvals
804807
:param output_dir: (string) Path of the plot file
805-
:param bw: If True, plots will be generated with patterns instead of
808+
:param bw: (bool) If True, plots will be generated with patterns instead of
806809
colors to distinguish k groups.
810+
:param show_ind: (bool) If True, and if individual labels were provided
811+
with the --ind option, use those labels instead of population labels
807812
"""
808813

809814
qvalues = self.kvals[kval].qvals
@@ -854,8 +859,7 @@ def plotk_static(self, kval, output_dir, bw=False):
854859
former_q = former_q + qvalues[:, i]
855860

856861
# Annotate population info
857-
if self.pops:
858-
862+
if self.pops and not use_ind:
859863
pop_lines = list(OrderedDict.fromkeys(
860864
[x for y in self.pops_xrange for x in y]))[1:-1]
861865

@@ -872,7 +876,7 @@ def plotk_static(self, kval, output_dir, bw=False):
872876

873877
for pos in range(self.number_indv):
874878
axe.text(pos, -0.05, self.indv[pos],
875-
rotation=45, va="top", ha="right", fontsize=10)
879+
rotation=45, va="top", ha="right", fontsize=8)
876880

877881
for axis in ["top", "bottom", "left", "right"]:
878882
axe.spines[axis].set_linewidth(2)
@@ -897,7 +901,7 @@ def plotk_static(self, kval, output_dir, bw=False):
897901

898902

899903
def main(result_files, fmt, outdir, bestk=None, popfile=None, indfile=None,
900-
filter_k=None, bw=False):
904+
filter_k=None, bw=False, use_ind=False):
901905
"""
902906
Wrapper function that generates one plot for each K value.
903907
:return:
@@ -919,7 +923,7 @@ def main(result_files, fmt, outdir, bestk=None, popfile=None, indfile=None,
919923

920924
if k >= 1 and k in filter_k:
921925
klist.plotk([k], outdir)
922-
klist.plotk_static(k, outdir, bw=bw)
926+
klist.plotk_static(k, outdir, bw=bw, use_ind=use_ind)
923927

924928
# If a sequence of multiple bestk is provided, plot all files in a single
925929
# plot

structure_threader/structure_threader.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def create_plts(resultsdir, wrapped_prog, Ks, bestk, arg):
254254
for i in plt_list]
255255

256256
sp.main(plt_files, wrapped_prog, outdir, bestk=bestk, popfile=arg.popfile,
257-
indfile=arg.indfile, bw=arg.blacknwhite)
257+
indfile=arg.indfile, bw=arg.blacknwhite, use_ind=arg.use_ind)
258258

259259

260260
def maverick_merger(outdir, k_list, no_tests):
@@ -428,6 +428,11 @@ def argument_parser(args):
428428
action="store_const", const=True,
429429
help="Set this flag to draw greyscale plots"
430430
" instead of colored ones.")
431+
plot_opts.add_argument("--use-ind-labels", dest="use_ind",
432+
action="store_const", const=True,
433+
help="Use the individual labels in the "
434+
"structure plot instead of population"
435+
" labels")
431436

432437
# ####################### PLOT ARGUMENTS ##################################
433438
# Group definitions
@@ -463,6 +468,11 @@ def argument_parser(args):
463468
action="store_const", const=True,
464469
help="Set this flag to draw greyscale plots"
465470
" instead of colored ones.")
471+
extra_opts.add_argument("--use-ind-labels", dest="use_ind",
472+
action="store_const", const=True,
473+
help="Use the individual labels in the "
474+
"structure plot instead of population"
475+
" labels")
466476

467477
sort_opts_ex.add_argument("--pop", dest="popfile", type=str,
468478
required=False,
@@ -607,7 +617,8 @@ def main():
607617
bestk = [int(x) for x in arg.bestk]
608618

609619
sp.main(infiles, arg.format, arg.outpath, bestk, popfile=arg.popfile,
610-
indfile=arg.indfile, filter_k=bestk, bw=arg.blacknwhite)
620+
indfile=arg.indfile, filter_k=bestk, bw=arg.blacknwhite,
621+
use_ind=arg.use_ind)
611622

612623

613624
if __name__ == "__main__":

0 commit comments

Comments
 (0)