Ejemplo n.º 1
0
#!/usr/local/bin/python3.8
import sys
import logging, sys

logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler(sys.stdout))
from lineage import Lineage

l = Lineage(output_dir='storage/app/dna/output')

var1 = sys.argv[1]
var2 = sys.argv[2]

file1 = "storage/app/dna/" + sys.argv[3]
file2 = "storage/app/dna/" + sys.argv[4]

user662 = l.create_individual(var1, file1)
user663 = l.create_individual(var2, file2)
discordant_snps = l.find_discordant_snps(user662, user663, save_output=True)
len(discordant_snps.loc[discordant_snps['chrom'] != 'MT'])
results = l.find_shared_dna([user662, user663],
                            cM_threshold=0.75,
                            snp_threshold=1100)
Ejemplo n.º 2
0
    def find_shared_dna_genes(self, progress_recorder=None):
        ind1_snps = self.individual1.get_canonical_snps()
        ind2_snps = self.individual2.get_canonical_snps()

        if not ind1_snps or not ind2_snps:
            self.delete()
            return

        with tempfile.TemporaryDirectory() as tmpdir:
            l = Lineage(output_dir=tmpdir, parallelize=False)

            ind1_snps_file = shutil.copy(
                ind1_snps.file.path,
                os.path.join(tmpdir, "ind1_snps" + ind1_snps.file_ext),
            )

            ind2_snps_file = shutil.copy(
                ind2_snps.file.path,
                os.path.join(tmpdir, "ind2_snps" + ind2_snps.file_ext),
            )

            ind1 = l.create_individual(self.individual1.name, ind1_snps_file)
            ind2 = l.create_individual(self.individual2.name, ind2_snps_file)

            shared_dna_one_chrom, shared_dna_two_chrom, shared_genes_one_chrom, shared_genes_two_chrom = l.find_shared_dna(
                ind1,
                ind2,
                cM_threshold=float(self.cM_threshold),
                snp_threshold=int(self.snp_threshold),
                shared_genes=True,
                save_output=True,
            )

            self.total_shared_segments_one_chrom = len(shared_dna_one_chrom)
            self.total_shared_segments_two_chrom = len(shared_dna_two_chrom)
            self.total_shared_cMs_one_chrom = Decimal(
                shared_dna_one_chrom["cMs"].sum())
            self.total_shared_cMs_two_chrom = Decimal(
                shared_dna_two_chrom["cMs"].sum())
            self.total_snps_one_chrom = shared_dna_one_chrom["snps"].sum()
            self.total_snps_two_chrom = shared_dna_two_chrom["snps"].sum()
            self.total_chrom_one_chrom = len(
                shared_dna_one_chrom["chrom"].unique())
            self.total_chrom_two_chrom = len(
                shared_dna_two_chrom["chrom"].unique())
            self.total_shared_genes_one_chrom = len(shared_genes_one_chrom)
            self.total_shared_genes_two_chrom = len(shared_genes_two_chrom)

            for root, dirs, files in os.walk(tmpdir):
                for file in files:
                    file_path = os.path.join(root, file)
                    if ".png" in file:
                        self.shared_dna_plot_png.name = get_relative_user_dir_file(
                            self.user.uuid, uuid4(), ".png")
                        shutil.move(file_path, self.shared_dna_plot_png.path)
                        os.chmod(self.shared_dna_plot_png.path, 0o640)

                    elif "shared_dna_one_chrom" in file:
                        self.shared_dna_one_chrom_csv = get_relative_user_dir_file(
                            self.user.uuid, uuid4())
                        compress_file(file_path,
                                      self.shared_dna_one_chrom_csv.path)

                        self.shared_dna_one_chrom_pickle = get_relative_user_dir_file(
                            self.user.uuid, uuid4(), ".pkl.gz")

                        shared_dna_one_chrom.to_pickle(
                            self.shared_dna_one_chrom_pickle.path)

                    elif "shared_genes_one_chrom" in file:
                        self.shared_genes_one_chrom_csv = get_relative_user_dir_file(
                            self.user.uuid, uuid4())
                        compress_file(file_path,
                                      self.shared_genes_one_chrom_csv.path)

                        self.shared_genes_one_chrom_pickle = get_relative_user_dir_file(
                            self.user.uuid, uuid4(), ".pkl.gz")

                        shared_genes_one_chrom.to_pickle(
                            self.shared_genes_one_chrom_pickle.path)

                    elif "shared_dna_two_chrom" in file:
                        self.shared_dna_two_chrom_csv = get_relative_user_dir_file(
                            self.user.uuid, uuid4())
                        compress_file(file_path,
                                      self.shared_dna_two_chrom_csv.path)

                        self.shared_dna_two_chrom_pickle = get_relative_user_dir_file(
                            self.user.uuid, uuid4(), ".pkl.gz")

                        shared_dna_two_chrom.to_pickle(
                            self.shared_dna_two_chrom_pickle.path)

                    elif "shared_genes_two_chrom" in file:
                        self.shared_genes_two_chrom_csv = get_relative_user_dir_file(
                            self.user.uuid, uuid4())
                        compress_file(file_path,
                                      self.shared_genes_two_chrom_csv.path)

                        self.shared_genes_two_chrom_pickle = get_relative_user_dir_file(
                            self.user.uuid, uuid4(), ".pkl.gz")

                        shared_genes_two_chrom.to_pickle(
                            self.shared_genes_two_chrom_pickle.path)

        self.setup_complete = True
        self.save()