public void MinLengthPartialFalseInGetIdFromFileTest()
        {
            var txtReader              = new StreamReader($"{SystemData.ProjectFolderPathForNcbiHelper}nuccore_result2.txt");
            var textFromFile           = txtReader.ReadToEnd();
            var result                 = NcbiHelper.GetIdsFromNcbiSearchResults(textFromFile, false, minLength: 1000);
            int expectedSequencesCount = 415;

            Assert.AreEqual(expectedSequencesCount, result.Length);
        }
        public void GetIDFromFileTest()
        {
            var txtReader              = new StreamReader($"{SystemData.ProjectFolderPathForNcbiHelper}nuccore_result.txt");
            var textFromFile           = txtReader.ReadToEnd();
            var result                 = NcbiHelper.GetIdsFromNcbiSearchResults(textFromFile, true);
            int expectedSequencesCount = 2111;

            Assert.AreEqual(expectedSequencesCount, result.Length);
        }
        public void IncludePartialInGetIdFromFileTest()
        {
            var txtReader              = new StreamReader($"{SystemData.ProjectFolderPathForNcbiHelper}nuccore_result2.txt");
            var textFromFile           = txtReader.ReadToEnd();
            var result                 = NcbiHelper.GetIdsFromNcbiSearchResults(textFromFile, false);
            int expectedSequencesCount = 1447;
            int partialSequences       = 823;

            expectedSequencesCount -= partialSequences;
            Assert.AreEqual(expectedSequencesCount, result.Length);
        }
Exemple #4
0
        public ActionResult Index(
            bool importGenes,
            bool importPartial,
            bool filterMinLength,
            int minLength,
            bool filterMaxLength,
            int maxLength)
        {
            return(CreateTask(() =>
            {
                var genBankSearchResultsStream = FileHelper.GetFileStream(Request.Files[0]);
                string searchResults = FileHelper.ReadSequenceFromStream(genBankSearchResultsStream);
                string[] accessions;
                if (filterMinLength)
                {
                    accessions = filterMaxLength ?
                                 NcbiHelper.GetIdsFromNcbiSearchResults(searchResults, importPartial, minLength, maxLength) :
                                 NcbiHelper.GetIdsFromNcbiSearchResults(searchResults, importPartial, minLength);
                }
                else
                {
                    accessions = filterMaxLength ?
                                 NcbiHelper.GetIdsFromNcbiSearchResults(searchResults, importPartial, 1, maxLength) :
                                 NcbiHelper.GetIdsFromNcbiSearchResults(searchResults, importPartial);
                }
                accessions = accessions.Distinct().Select(a => a.Split('.')[0]).ToArray();
                var importResults = new List <MatterImportResult>(accessions.Length);

                using (var db = new LibiadaWebEntities())
                {
                    var matterRepository = new MatterRepository(db);
                    var dnaSequenceRepository = new GeneticSequenceRepository(db);

                    var(existingAccessions, accessionsToImport) = dnaSequenceRepository.SplitAccessionsIntoExistingAndNotImported(accessions);

                    importResults.AddRange(existingAccessions.ConvertAll(existingAccession => new MatterImportResult
                    {
                        MatterName = existingAccession,
                        Result = "Sequence already exists",
                        Status = "Exists"
                    }));

                    foreach (string accession in accessionsToImport)
                    {
                        var importResult = new MatterImportResult()
                        {
                            MatterName = accession
                        };

                        try
                        {
                            ISequence bioSequence = NcbiHelper.DownloadGenBankSequence(accession);
                            GenBankMetadata metadata = NcbiHelper.GetMetadata(bioSequence);
                            importResult.MatterName = metadata.Version.CompoundAccession;

                            Matter matter = matterRepository.CreateMatterFromGenBankMetadata(metadata);

                            importResult.SequenceType = matter.SequenceType.GetDisplayValue();
                            importResult.Group = matter.Group.GetDisplayValue();
                            importResult.MatterName = matter.Name;
                            importResult.AllNames = $"Common name = {metadata.Source.CommonName}, "
                                                    + $"Species = {metadata.Source.Organism.Species}, "
                                                    + $"Definition = {metadata.Definition}, "
                                                    + $"Saved matter name = {importResult.MatterName}";

                            var sequence = new CommonSequence
                            {
                                Matter = matter,
                                Notation = Notation.Nucleotides,
                                RemoteDb = RemoteDb.GenBank,
                                RemoteId = metadata.Version.CompoundAccession
                            };
                            bool partial = metadata.Definition.ToLower().Contains("partial");
                            dnaSequenceRepository.Create(sequence, bioSequence, partial);

                            (importResult.Result, importResult.Status) = importGenes ?
                                                                         ImportFeatures(metadata, sequence) :
                                                                         ("Successfully imported sequence", "Success");
                        }
                        catch (Exception exception)
                        {
                            importResult.Status = "Error";
                            importResult.Result = $"Error: {exception.Message}";
                            while (exception.InnerException != null)
                            {
                                exception = exception.InnerException;
                                importResult.Result += $" {exception.Message}";
                            }

                            foreach (var dbEntityEntry in db.ChangeTracker.Entries())
                            {
                                if (dbEntityEntry.Entity != null)
                                {
                                    dbEntityEntry.State = EntityState.Detached;
                                }
                            }
                        }
                        finally
                        {
                            importResults.Add(importResult);
                        }
                    }

                    string[] names = importResults.Select(r => r.MatterName).ToArray();

                    // removing matters for which adding of sequence failed
                    Matter[] orphanMatters = db.Matter
                                             .Include(m => m.Sequence)
                                             .Where(m => names.Contains(m.Name) && m.Sequence.Count == 0)
                                             .ToArray();

                    if (orphanMatters.Length > 0)
                    {
                        db.Matter.RemoveRange(orphanMatters);
                        db.SaveChanges();
                    }
                }

                var result = new Dictionary <string, object> {
                    { "result", importResults }
                };

                return new Dictionary <string, string> {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }