Example #1
0
 public patient_result_collections AddCollection(patient patient, result_files file)
 {
     // Add the patient result collection for this entry
     var collection = new patient_result_collections()
     {
         patient_id = patient.id,
         received_on = DateTime.Now,
         result_file_id = file.id
     };
     entities.patient_result_collections.Add(collection);
     entities.SaveChanges();
     return collection;
 }
Example #2
0
        public patient_result_collections AddResult(patient patient, result_files file, string name, string value, string externalId, string externalSource, DateTime resultedOn)
        {
            // First, identify the reference phenotype and if it exists
            var phenotype = (from pheno in entities.phenotypes
                             where pheno.name == name && pheno.value == value
                             select pheno).FirstOrDefault();
            if (phenotype == null)
            {
                phenotype = new phenotype()
                {
                    name = name,
                    value = value,
                    external_source = externalSource,
                    external_id = externalId
                };
                entities.phenotypes.Add(phenotype);
                entities.SaveChanges();
            }

            // Now add the patient phenotype
            var patientPhenotype = new patient_phenotypes()
            {
                phenotype = phenotype,
                resulted_on = resultedOn
            };
            entities.patient_phenotypes.Add(patientPhenotype);
            entities.SaveChanges();

            // Add the patient result collection for this entry
            var collection = patientRepo.AddCollection(patient, file);

            // Finally, link the collection to the phenotype
            var member = new patient_result_members()
            {
                collection_id = collection.id,
                member_id = patientPhenotype.id,
                member_type = Enums.ResultMemberType.Phenotype,
            };

            entities.patient_result_members.Add(member);
            entities.SaveChanges();

            return collection;
        }
Example #3
0
        public patient_result_collections AddSnps(patient patient, result_files file, DateTime resultedOn, List<SnpResult> snps)
        {
            // Create a collection
            var collection = patientRepo.AddCollection(patient, file);

            // Create patient variant entries
            List<patient_variants> variants = new List<patient_variants>();
            foreach (var snp in snps)
            {
                var variant = GetVariant(snp.RSID, "dbSNP", snp.Position, snp.Position);
                var patientVariant = new patient_variants()
                {
                    patient_id = patient.id,
                    variant_type = Enums.PatientVariantType.SNP,
                    reference_id = variant.id,
                    resulted_on = resultedOn,
                    value1 = snp.Genotype[0].ToString(),
                    value2 = snp.Genotype[1].ToString()
                };
                variants.Add(patientVariant);
            }
            entities.patient_variants.AddRange(variants);
            entities.SaveChanges();

            // Finally, link the collection to the SNPs
            foreach (var variant in variants)
            {
                var member = new patient_result_members()
                {
                    collection_id = collection.id,
                    member_id = variant.id,
                    member_type = Enums.ResultMemberType.Variant,
                };
                entities.patient_result_members.Add(member);
            }
            entities.SaveChanges();

            return collection;
        }
Example #4
0
        public patient AddPatient(string mrn, string mrnSource, string firstName, string lastName, DateTime? dateOfBirth)
        {
            var result = (from existingPatient in entities.patients
                         where existingPatient.external_source == mrnSource && existingPatient.external_id == mrn
                         select existingPatient).FirstOrDefault();
            if (result == null)
            {
                patient newPatient = new patient()
                {
                    external_source = mrnSource,
                    external_id = mrn,
                    date_of_birth = dateOfBirth,
                    first_name = firstName,
                    last_name = lastName
                };
                entities.patients.Add(newPatient);
                entities.SaveChanges();
                result = newPatient;
            }

            return result;
        }
Example #5
0
        public patient_result_collections AddStarVariants(patient patient, result_files file, DateTime resultedOn, List<StarVariantResult> stars)
        {
            // Create a collection
            var collection = patientRepo.AddCollection(patient, file);

            // Create patient variant entries
            List<patient_variants> variants = new List<patient_variants>();
            foreach (var star in stars)
            {
                var gene = AddGene(star.Gene, star.Gene, null, null, null);
                var variant = AddVariant(star.Gene, star.Result, "Star Variant", null, null, null, null, null);
                string[] splitStars = star.Result.Split(new string[]{"*"}, StringSplitOptions.RemoveEmptyEntries);
                var patientVariant = new patient_variants()
                {
                    patient_id = patient.id,
                    variant_type = Enums.PatientVariantType.StarVariant,
                    reference_id = variant.id,
                    resulted_on = resultedOn,
                    value1 = splitStars[0],
                    value2 = splitStars[1]
                };
                variants.Add(patientVariant);
            }

            entities.patient_variants.AddRange(variants);
            entities.SaveChanges();

            // Finally, link the collection to the stars
            foreach (var variant in variants)
            {
                var member = new patient_result_members()
                {
                    collection_id = collection.id,
                    member_id = variant.id,
                    member_type = Enums.ResultMemberType.Variant,
                };
                entities.patient_result_members.Add(member);
            }
            entities.SaveChanges();

            return collection;
        }
Example #6
0
        public override void LoadData(string filePath)
        {
            var vcfParser = new VCFParser(filePath);
            var header = vcfParser.Header;
            var collectionInformationList = new List<patient_variant_information>();
            var patient = new patient();
            // We pull out all of the metadata from the header (all lines) and write them as information
            // lines associated with this result.
            foreach (var headerItem in header.MetaDataInInputOrder)
            {
                if (headerItem.Key == "individual-id")
                {
                    var individualParts = headerItem.Value.Replace("<", "").Replace(">", "").Split(new char[] { ',' });
                    var individualData = individualParts.Select(x => x.Split(new char[] { '=' })).ToArray();
                    var mrnParts = individualData.FirstOrDefault(x => x[0] == "Dbxref")[1].Split(':');
                    patient = patientRepo.AddPatient(mrnParts[1], mrnParts[0],
                        individualData.FirstOrDefault(x => x[0] == "First_name")[1],
                        individualData.FirstOrDefault(x => x[0] == "Last_name")[1],
                        DateTime.Parse(individualData.FirstOrDefault(x => x[0] == "DOB")[1]));
                }
                else if (headerItem.GetType() == typeof(VCFInfoHeaderLine))
                {
                    var info = headerItem as VCFInfoHeaderLine;
                    collectionInformationList.Add(AddHeaderInformation(string.Format("VCF:{0}", headerItem.Key), CleanHeaderValue("INFO", info.ToString())));
                }
                else if (headerItem.GetType() == typeof(VCFFilterHeaderLine))
                {
                    var filter = headerItem as VCFFilterHeaderLine;
                    collectionInformationList.Add(AddHeaderInformation(string.Format("VCF:{0}", headerItem.Key), CleanHeaderValue("FILTER", filter.ToString())));
                }
                else if (headerItem.GetType() == typeof(VCFFormatHeaderLine))
                {
                    var format = headerItem as VCFFormatHeaderLine;
                    collectionInformationList.Add(AddHeaderInformation(string.Format("VCF:{0}", headerItem.Key), CleanHeaderValue("FORMAT", format.ToString())));
                }
                else
                {
                    collectionInformationList.Add(AddHeaderInformation(string.Format("VCF:{0}", headerItem.Key), headerItem.Value));
                }
            }

            var reference = header.MetaDataInInputOrder.First(x => x.Key == "reference").Value;
            DateTime? resultDate = DateTime.ParseExact(header.MetaDataInInputOrder.First(x => x.Key == "fileDate").Value,
                "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
            var patientVariants = new List<patient_variants>();
            var featureInformationList = new Dictionary<patient_variants, List<patient_variant_information>>();
            while (vcfParser.MoveNext())
            {
                var current = vcfParser.Current;
                var variant = variantRepo.AddVariant(null, current.ID, "dbSNP",
                    current.Chr, current.Start, current.End,
                    reference, current.Reference.BaseString);
                var patientVariant = new patient_variants()
                {
                    patient_id = patient.id,
                    reference_id = variant.id,
                    resulted_on = resultDate,
                    variant_type = Enums.PatientVariantType.SNP
                };
                SetVariantValues(patientVariant, current);
                patientVariants.Add(patientVariant);

                var attributeList = new List<patient_variant_information>();
                foreach (var attribute in current.Attributes)
                {
                    attributeList.Add(AddVariantInformation(string.Format("VCF:{0}", attribute.Key), attribute.Value.ToString()));
                }

                if (current.FiltersMaybeNull != null)
                {
                    foreach (var filter in current.FiltersMaybeNull)
                    {
                        attributeList.Add(AddVariantInformation("VCF:Filter", filter));
                    }
                }

                foreach (var genotype in current.Genotypes)
                {
                    attributeList.Add(AddVariantInformation("VCF:Genotype", genotype.ToMHGRString()));
                }

                attributeList.Add(AddVariantInformation("VCF:Quality", current.PhredScaledQual.ToString()));
                attributeList.Add(AddVariantInformation("VCF:Filter", string.Join(",", current.Filters.ToArray())));
                featureInformationList.Add(patientVariant, attributeList);
            }

            // Save the collection to get its ID
            var source = sourceRepo.AddSource("VCF", "VCF file");
            var file = AddResultFile(filePath, source);
            var collection = patientRepo.AddCollection(patient, file);

            // Save the collection-level header data
            collectionInformationList.ForEach(x => x.item_id = collection.id);
            variantRepo.AddPatientVariantInformationList(collectionInformationList);
            variantRepo.AddPatientVariants(patientVariants);

            // Save the individual attributes associated with each feature.
            // Must be done after the patient variants are written to DB (above), since we
            // rely on the ID being set.
            foreach (var pair in featureInformationList)
            {
                foreach (var attribute in pair.Value)
                {
                    attribute.item_id = pair.Key.id;
                }
                variantRepo.AddPatientVariantInformationList(pair.Value);
            }

            variantRepo.AddPatientVariantsToCollection(collection, patientVariants);

            featureInformationList.Clear();
            collectionInformationList.Clear();
            patientVariants.Clear();
        }