Package: lostruct
lostruct-pyThis is a reimplementation of lostruct from the original code:lostruct,
byJoseph Guhlinwith assistance byPeter Ralph.DemonstrationPlease see theExample NotebookInstallationLostruct-py is available onPyPipip install lostructis the easiest way to get started.UsageInput FilesInputs should be a set of markers in BCF or VCF format. Both should be indexed as appropriate (see: bcftools index). Filtering before running this analysis is strongly suggested (Allele frequency, SNPs only, missingness, etc).CitingIf you use this version, plesae cite it via ZenodoDOI: 10.5281/zenodo.3997106as well as the original paper describing the method:Li, Han, and Peter Ralph. "Local PCA shows how the effect of population structure differs along the genome." Genetics 211.1 (2019): 289-304.CyVCF2This project also usescyvcf2for fast VCF processing and should be cited:Brent S Pedersen, Aaron R Quinlan, cyvcf2: fast, flexible variant analysis with Python, Bioinformatics, Volume 33, Issue 12, 15 June 2017, Pages 1867–1869, https://doi.org/10.1093/bioinformatics/btx057Changes from Lostruct R packagePlease note numpy and R are different when it comes to row-major vs. column-major. Essentially, many things in the python version will be transposed from R.RequirementsPython >= 3.6 (may work with older versions). Developed on Python 3.8.5numbanumpycyvcf2CyVCF2 requires zlib-dev, libbz2-dev, libcurl-dev, liblzma-dev; numa requires libllvm.
These may be installed withcondaorpip, e.g. by runningpip install -r requirements.txt.ChangesSeeCHANGES.MDfor the full list.0.0.4Package name changed to lostructParallelization of get_pc_distsImplementation of fastmath parameter for get_pc_distsTestsTests were derived fromMedicago HapMap data. While the software had high correlation with lostruct R the values were determined. If values begin to deviate from the method these tests will now fail.To run tests simply do:python -m noseThe tests furthermore requireunittestandscikit-bio(and,noseto run them this way).TOXTox allows you run tests with multiple versions of the python interpreter in venvs. It is best to use pyenv to install multiple versions python to run before submitting pull requests to be certain tests complete successfully across all versions.Correlation DataTo test correlation of results between the R and Python versions we used data from the Medicago HapMap project, specifically SNPs for sister taxa chromsome 1, processed, and run with LoStruct R.Databcftools annotate chr1-filtered-set-2014Apr15.bcf -x INFO,FORMAT | bcftools view -a -i 'F_MISSING<=0.2' | bcftools view -q 0.05 -q 0.95 -m2 -M2 -a -Oz -o chr1-filtered.vcf.gzLostruct ProcessingRscript run_lostruct.R -t SNP -s 95 -k 10 -m 10 -i data/Run 21 Aug 2020, using lostruct R git hash: 444b8c64bebdf7cdd0323e7735ccadddfc1c8989This generates the mds_coords.tsv that is used in the correlation comparison. Additionally, the existing tests cover correlation.FAQ / NotesFutureCurrently the end-user is expected to save the outputs. But would be good to save it in a similar way to lostruct R-code. Please open an issue if you need this.Feature Completeness with R implementationWe are not yet feature complete with the R implementation. If something is needed please check for existing issues and comment about your need.PCA, MDS, PCoAPCoA returns the same results as lostruct's MDS implementation (cmdscale). In the example Jupyter notebook you can see the correlation is R =~ 0.998. Some examples of other methods of clustering / looking at differences are included in the notebook.Speed and MemoryNUMBA and CyVCF2 are used for speeding up processes, and the software becomes multithreaded by default. The Sparse library is used to reduce memory requirements. parse_vcf function is multithreaded. Distance calculation is not.tl;dr of belowBelow two options are offered, fastmath for get_pc_dists function, and method="fsvd" for pcoa. When using both you will see a performance increase and memory requirement decrease. Accuracy should decrease, but the absolute correlation we see with our test dataset remains ~0.998. Be aware when using fsvd the sign of the correlation may change.FastmathAdditionally, a mode implemented Numba's "fastmath" is available. For the function get_pc_dists() set fastmath=True. This results in a ~8% speed boost with very little change in the final output (correlation to R code output remains >= 0.995). This was benchmarked on the Medicago data used in the jupyter notebook using timeit, with 100 repeats with fastmath=False and Fastmath=True.get_pc_dists(result, fastmath=True)The difference with fastmath=True and leaving it off can be seenhere. Note: Downloading the file will allow you to see more detailed information, as some javascript is contained in the SVG but disabled on GitHub.If you need to limit thread usage, please seeNumba's guideVery Large DatasetsThe R implementation handles very large datasets in less memory. The problem arises with the PCoA function. A metric MDS using sklearn may work. Another alternative would be to export the data and run cmdscale in R directly.The sklearn MDS function differs from the scikit-bio function, here we focus on the scikit-bio version.There are two options in python for this as well:pcoa(method="fsvd", ...)Which reduces memory and increases speed, at the cost of some accuracy.pcoa(inplace=True, ...)Centers a distance matrix in-place, further reducing memory requirements.pcoa(number_of_dimensions=10)Returns only the first 10 dimensions (configurable) of the scaling. This has no real effect if method is default or manuially set to "eigh" as the eigenvalues and eigenvectors are all calculated, so all are calculated and this becomes a truncation.You can see the difference between method="fsvd" and method="eigh" (default)here. These are tested with a minimum of 50 rounds. Note: Downloading the file will allow you to see more detailed information, as some javascript is contained in the SVG but disabled on GitHub.Using all three techniques, correlation is maintained although the sign may change.mds = pcoa(pc_dists, method="fsvd", inplace=True, number_of_dimensions=10)
np.corrcoef(mds.samples["PC1"], mds_coords['MDS1'].to_numpy())[0][1]
-0.9978147088087447For more information please see theapplicable documentationas well as therelevant changelog. AZenodo entryis also available on this topic.ReferencesAdditional citations can be found inCITATIONS(UMAP, PHATE, Medicago HapMap).