public ActionResult Index(
            string searchQuery,
            bool importGenes,
            bool importPartial,
            bool filterMinLength,
            int minLength,
            bool filterMaxLength,
            int maxLength)
        {
            return(CreateTask(() =>
            {
                string searchResults;
                string[] accessions;
                List <NuccoreObject> nuccoreObjects;

                if (filterMinLength)
                {
                    searchResults = filterMaxLength ?
                                    NcbiHelper.FormatNcbiSearchTerm(searchQuery, minLength, maxLength: maxLength) :
                                    NcbiHelper.FormatNcbiSearchTerm(searchQuery, minLength);
                }
                else
                {
                    searchResults = filterMaxLength ?
                                    NcbiHelper.FormatNcbiSearchTerm(searchQuery, minLength: 1, maxLength: maxLength) :
                                    NcbiHelper.FormatNcbiSearchTerm(searchQuery);
                }
                nuccoreObjects = NcbiHelper.ExecuteESummaryRequest(searchResults, importPartial);
                accessions = nuccoreObjects.Select(no => no.AccessionVersion.Split('.')[0]).Distinct().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) }
                };
            }));
        }
        public ActionResult Index(long matterId,
                                  Notation notation,
                                  Language?language,
                                  Translator?translator,
                                  PauseTreatment?pauseTreatment,
                                  bool?sequentialTransfer,
                                  int scrambling)
        {
            Matter matter = db.Matter.Single(m => m.Id == matterId);
            long   sequenceId;

            switch (matter.Nature)
            {
            case Nature.Literature:
                sequenceId = db.LiteratureSequence.Single(l => l.MatterId == matterId &&
                                                          l.Notation == notation &&
                                                          l.Language == language &&
                                                          l.Translator == translator).Id;
                break;

            case Nature.Music:
                sequenceId = db.MusicSequence.Single(m => m.MatterId == matterId &&
                                                     m.Notation == notation &&
                                                     m.PauseTreatment == pauseTreatment &&
                                                     m.SequentialTransfer == sequentialTransfer).Id;
                break;

            default:
                sequenceId = db.CommonSequence.Single(c => c.MatterId == matterId && c.Notation == notation).Id;
                break;
            }

            BaseChain chain = sequenceRepository.GetLibiadaBaseChain(sequenceId);

            for (int i = 0; i < scrambling; i++)
            {
                int firstIndex  = randomGenerator.Next(chain.Length);
                int secondIndex = randomGenerator.Next(chain.Length);

                IBaseObject firstElement  = chain[firstIndex];
                IBaseObject secondElement = chain[secondIndex];
                chain[firstIndex]  = secondElement;
                chain[secondIndex] = firstElement;
            }

            var resultMatter = new Matter
            {
                Nature = matter.Nature,
                Name   = $"{matter.Name} {scrambling} mixes"
            };

            db.Matter.Add(resultMatter);
            db.SaveChanges();

            var result = new CommonSequence
            {
                Notation = notation,
                MatterId = resultMatter.Id
            };

            long[] alphabet = elementRepository.ToDbElements(chain.Alphabet, notation, false);

            switch (matter.Nature)
            {
            case Nature.Genetic:
                DnaSequence dnaSequence = db.DnaSequence.Single(c => c.Id == sequenceId);

                dnaSequenceRepository.Create(result, dnaSequence.Partial, alphabet, chain.Building);
                break;

            case Nature.Music:
                musicSequenceRepository.Create(result, alphabet, chain.Building);
                break;

            case Nature.Literature:
                LiteratureSequence sequence = db.LiteratureSequence.Single(c => c.Id == sequenceId);

                literatureSequenceRepository.Create(result, sequence.Original, sequence.Language, sequence.Translator, alphabet, chain.Building);
                break;

            case Nature.MeasurementData:
                dataSequenceRepository.Create(result, alphabet, chain.Building);
                break;

            default:
                throw new InvalidEnumArgumentException(nameof(matter.Nature), (int)matter.Nature, typeof(Nature));
            }

            return(RedirectToAction("Index", "Matters"));
        }
        public ActionResult Index(string[] accessions, bool importGenes)
        {
            return Action(() =>
            {
                var matterNames = new string[accessions.Length];
                var savedMatterNames = new string[accessions.Length];
                var results = new string[accessions.Length];
                var statuses = new string[accessions.Length];

                using (var db = new LibiadaWebEntities())
                {
                    var existingAccessions = db.DnaSequence.Select(d => d.RemoteId).Distinct().ToArray();
                    var dnaSequenceRepository = new DnaSequenceRepository(db);
                    var bioSequences = NcbiHelper.GetGenBankSequences(accessions);

                    for (int i = 0; i < accessions.Length; i++)
                    {
                        string accession = accessions[i];
                        matterNames[i] = accession;
                        if (existingAccessions.Contains(accession) || existingAccessions.Contains(accession + ".1"))
                        {
                            results[i] = "Sequence already exists";
                            statuses[i] = "Exist";
                            continue;
                        }

                        try
                        {
                            var metadata = NcbiHelper.GetMetadata(bioSequences[i]);
                            if (existingAccessions.Contains(metadata.Version.CompoundAccession))
                            {
                                results[i] = "Sequence already exists";
                                statuses[i] = "Exist";
                                continue;
                            }

                            savedMatterNames[i] = NcbiHelper.ExtractSequenceName(metadata) + " | " + metadata.Version.CompoundAccession;
                            matterNames[i] = "Common name=" + metadata.Source.CommonName +
                                             ", Species=" + metadata.Source.Organism.Species +
                                             ", Definition=" + metadata.Definition +
                                             ", Saved matter name=" + savedMatterNames[i];

                            var matter = new Matter
                                             {
                                                 Name = savedMatterNames[i],
                                                 Nature = Nature.Genetic,
                                                 Group = GroupRepository.ExtractSequenceGroup(savedMatterNames[i]),
                                                 SequenceType = SequenceTypeRepsitory.ExtractSequenceGroup(savedMatterNames[i])
                                             };

                            var sequence = new CommonSequence
                                               {
                                                   Matter = matter,
                                                   NotationId = Aliases.Notation.Nucleotide,
                                                   RemoteDbId = Aliases.RemoteDb.RemoteDbNcbi,
                                                   RemoteId = metadata.Version.CompoundAccession
                                               };

                            dnaSequenceRepository.Create(sequence, bioSequences[i], metadata.Definition.ToLower().Contains("partial"));
                            if (importGenes)
                            {
                                try
                                {
                                    using (var subsequenceImporter = new SubsequenceImporter(metadata.Features.All, sequence.Id))
                                    {
                                        subsequenceImporter.CreateSubsequences();
                                    }

                                    var nonCodingCount = db.Subsequence.Count(s => s.SequenceId == sequence.Id && s.FeatureId == Aliases.Feature.NonCodingSequence);
                                    var featuresCount = db.Subsequence.Count(s => s.SequenceId == sequence.Id && s.FeatureId != Aliases.Feature.NonCodingSequence);

                                    statuses[i] = "Success";
                                    results[i] = "Successfully imported  sequence and " + featuresCount + " features and " + nonCodingCount + " non coding subsequences";
                                }
                                catch (Exception exception)
                                {
                                    results[i] = "successfully imported sequence but failed to import genes: " + exception.Message;
                                    statuses[i] = "Error";

                                    if (exception.InnerException != null)
                                    {
                                        results[i] += " " + exception.InnerException.Message;
                                    }
                                }
                            }
                            else
                            {
                                results[i] = "successfully imported sequence";
                                statuses[i] = "Success";
                            }
                        }
                        catch (Exception exception)
                        {
                            results[i] = "Error:" + exception.Message + (exception.InnerException == null ? string.Empty : exception.InnerException.Message);
                            statuses[i] = "Error";
                        }
                    }

                    // removing matters for whitch adding of sequence failed
                    var orphanMatters = db.Matter.Include(m => m.Sequence).Where(m => savedMatterNames.Contains(m.Name) && m.Sequence.Count == 0).ToArray();

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

                return new Dictionary<string, object>
                           {
                               { "matterNames", matterNames },
                               { "results", results },
                               { "status", statuses }
                           };
            });
        }
示例#4
0
        public ActionResult Create(
            [Bind(Include = "Id,Notation,RemoteDb,RemoteId,Description,Matter,MatterId")] CommonSequence commonSequence,
            bool localFile,
            Language?language,
            bool?original,
            Translator?translator,
            bool?partial,
            int?precision)
        {
            return(CreateTask(() =>
            {
                var db = new LibiadaWebEntities();
                try
                {
                    if (!ModelState.IsValid)
                    {
                        throw new Exception("Model state is invalid");
                    }

                    Stream sequenceStream;
                    Nature nature = commonSequence.Notation.GetNature();
                    if (nature == Nature.Genetic && !localFile)
                    {
                        sequenceStream = NcbiHelper.GetFastaFileStream(commonSequence.RemoteId);
                    }
                    else
                    {
                        sequenceStream = FileHelper.GetFileStream(Request.Files[0]);
                    }

                    switch (nature)
                    {
                    case Nature.Genetic:
                        ISequence bioSequence = NcbiHelper.GetFastaSequence(sequenceStream);
                        var dnaSequenceRepository = new GeneticSequenceRepository(db);
                        dnaSequenceRepository.Create(commonSequence, bioSequence, partial ?? false);
                        break;

                    case Nature.Music:
                        var musicSequenceRepository = new MusicSequenceRepository(db);
                        musicSequenceRepository.Create(commonSequence, sequenceStream);
                        break;

                    case Nature.Literature:
                        var literatureSequenceRepository = new LiteratureSequenceRepository(db);
                        literatureSequenceRepository.Create(commonSequence, sequenceStream, language ?? Language.Russian, original ?? true, translator ?? Translator.NoneOrManual);
                        break;

                    case Nature.MeasurementData:
                        var dataSequenceRepository = new DataSequenceRepository(db);
                        dataSequenceRepository.Create(commonSequence, sequenceStream, precision ?? 0);
                        break;

                    case Nature.Image:
                        var matterRepository = new MatterRepository(db);
                        int fileSize = Request.Files[0].ContentLength;
                        var file = new byte[fileSize];
                        Request.Files[0].InputStream.Read(file, 0, fileSize);
                        var matter = new Matter
                        {
                            Nature = Nature.Image,
                            SequenceType = commonSequence.Matter.SequenceType,
                            Name = commonSequence.Matter.Name,
                            Source = file,
                            Group = commonSequence.Matter.Group
                        };
                        matterRepository.SaveToDatabase(matter);
                        break;

                    default:
                        throw new InvalidEnumArgumentException(nameof(nature), (int)nature, typeof(Nature));
                    }
                    string multisequenceName = db.Multisequence.SingleOrDefault(ms => ms.Id == commonSequence.Matter.MultisequenceId).Name;
                    var result = new ImportResult(commonSequence, language, original, translator, partial, precision, multisequenceName);

                    return new Dictionary <string, string> {
                        { "data", JsonConvert.SerializeObject(result) }
                    };
                }
                catch (Exception)
                {
                    long matterId = commonSequence.MatterId;
                    if (matterId != 0)
                    {
                        List <Matter> orphanMatter = db.Matter
                                                     .Include(m => m.Sequence)
                                                     .Where(m => m.Id == matterId && m.Sequence.Count == 0)
                                                     .ToList();

                        if (orphanMatter.Count > 0)
                        {
                            db.Matter.Remove(orphanMatter[0]);
                            db.SaveChanges();
                        }
                    }

                    throw;
                }
                finally
                {
                    Dispose(true);
                }
            }));
        }
示例#5
0
        /// <summary>
        /// Starts a new thread with the given task.
        /// </summary>
        /// <param name="task">
        /// The task.
        /// </param>
        private void ExecuteTaskAction(Task task)
        {
            try
            {
                Func <Dictionary <string, object> > actionToCall;
                lock (task)
                {
                    actionToCall          = task.Action;
                    task.TaskData.Started = DateTime.Now;
                    using (var db = new LibiadaWebEntities())
                    {
                        CalculationTask databaseTask = db.CalculationTask.Single(t => t.Id == task.TaskData.Id);

                        databaseTask.Started         = DateTime.Now;
                        databaseTask.Status          = TaskState.InProgress;
                        db.Entry(databaseTask).State = EntityState.Modified;
                        db.SaveChanges();
                    }

                    signalrHub.Send(TaskEvent.ChangeStatus, task.TaskData);
                }

                // executing action
                Dictionary <string, object> result = actionToCall();

                lock (task)
                {
                    task.TaskData.Completed     = DateTime.Now;
                    task.TaskData.ExecutionTime = task.TaskData.Completed - task.TaskData.Started;
                    task.Result             = result;
                    task.TaskData.TaskState = TaskState.Completed;
                    using (var db = new LibiadaWebEntities())
                    {
                        CalculationTask databaseTask = db.CalculationTask.Single(t => (t.Id == task.TaskData.Id));

                        databaseTask.Completed = DateTime.Now;
                        databaseTask.Status    = TaskState.Completed;
                        if (result.ContainsKey("data"))
                        {
                            databaseTask.Result = result["data"].ToString();
                        }

                        if (result.ContainsKey("additionalData"))
                        {
                            databaseTask.AdditionalResultData = JsonConvert.SerializeObject(result["additionalData"]);
                        }

                        db.Entry(databaseTask).State = EntityState.Modified;
                        db.SaveChanges();
                    }

                    signalrHub.Send(TaskEvent.ChangeStatus, task.TaskData);
                }
            }
            catch (Exception e)
            {
                string errorMessage = e.Message;
                string stackTrace   = e.StackTrace;

                while (e.InnerException != null)
                {
                    e             = e.InnerException;
                    errorMessage += $"{Environment.NewLine} {e.Message}";
                }

                lock (task)
                {
                    task.TaskData.Completed     = DateTime.Now;
                    task.TaskData.ExecutionTime = task.TaskData.Completed - task.TaskData.Started;
                    task.TaskData.TaskState     = TaskState.Error;
                    task.Result = new Dictionary <string, object>
                    {
                        { "Error", true },
                        { "ErrorMessage", errorMessage },
                        { "StackTrace", stackTrace }
                    };

                    using (var db = new LibiadaWebEntities())
                    {
                        CalculationTask databaseTask = db.CalculationTask.Single(t => (t.Id == task.TaskData.Id));

                        databaseTask.Completed       = DateTime.Now;
                        databaseTask.Status          = TaskState.Error;
                        databaseTask.Result          = JsonConvert.SerializeObject(task.Result);
                        db.Entry(databaseTask).State = EntityState.Modified;
                        db.SaveChanges();
                    }

                    signalrHub.Send(TaskEvent.ChangeStatus, task.TaskData);
                }
            }

            ManageTasks();
        }
示例#6
0
 /// <summary>
 /// Adds given matter to database.
 /// </summary>
 /// <param name="matter">
 /// The matter.
 /// </param>
 /// <returns>
 /// The <see cref="long"/>.
 /// </returns>
 public long CreateMatter(Matter matter)
 {
     db.Matter.Add(matter);
     db.SaveChanges();
     return(matter.Id);
 }