Beispiel #1
0
        /// <summary>
        /// Adds new task to db and tasks list.
        /// </summary>
        /// <param name="action">
        /// The action.
        /// </param>
        /// <param name="taskType">
        /// The task Type.
        /// </param>
        public long CreateTask(Func <Dictionary <string, string> > action, TaskType taskType)
        {
            CalculationTask databaseTask;
            Task            task;

            using (var db = new LibiadaWebEntities())
            {
                databaseTask = new CalculationTask
                {
                    Created     = DateTime.Now,
                    Description = taskType.GetDisplayValue(),
                    Status      = TaskState.InQueue,
                    UserId      = AccountHelper.GetUserId(),
                    TaskType    = taskType
                };

                db.CalculationTask.Add(databaseTask);
                db.SaveChanges();
            }

            lock (tasks)
            {
                task = new Task(databaseTask.Id, action, databaseTask.UserId, taskType);
                lock (task)
                {
                    tasks.Add(task);
                    signalrHub.Send(TaskEvent.AddTask, task.TaskData);
                }
            }

            ManageTasks();
            return(task.TaskData.Id);
        }
Beispiel #2
0
        /// <summary>
        /// TExtracts alphabet elements ids for given sequence.
        /// </summary>
        /// <param name="db">
        /// Database connection.
        /// </param>
        /// <param name="sequenceId">
        /// The sequence id.
        /// </param>
        /// <returns>
        /// The <see cref="long[]"/>.
        /// </returns>
        public static long[] GetAlphabetElementIds(this LibiadaWebEntities db, long sequenceId)
        {
            var          dbConnection = (NpgsqlConnection)db.Database.Connection;
            const string Query        = "SELECT alphabet FROM chain WHERE id = @id";
            var          id           = new NpgsqlParameter <long>("id", NpgsqlDbType.Bigint)
            {
                TypedValue = sequenceId
            };

            long[] result;
            dbConnection.Open();
            using (var command = new NpgsqlCommand(Query, dbConnection))
            {
                command.Parameters.Add(id);
                using (NpgsqlDataReader reader = command.ExecuteReader())
                {
                    if (!reader.Read())
                    {
                        dbConnection.Close();
                        throw new ArgumentException($"sequence id {sequenceId} does not exist", nameof(sequenceId));
                    }

                    result = (long[])reader[0];
                }
            }
            dbConnection.Close();
            return(result);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="LocalCalculationController"/> class.
 /// </summary>
 public LocalCalculationController()
     : base("Local calculation")
 {
     db = new LibiadaWebEntities();
     commonSequenceRepository = new CommonSequenceRepository(db);
     characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db);
 }
Beispiel #4
0
 /// <summary>
 /// Finds user by it's id.
 /// </summary>
 /// <param name="id">
 /// The user id.
 /// </param>
 /// <returns>
 /// The <see cref="Models.Account.ApplicationUser"/>.
 /// </returns>
 private static AspNetUser GetUserById(int id)
 {
     using (var db = new LibiadaWebEntities())
     {
         return(db.AspNetUsers.Find(id));
     }
 }
Beispiel #5
0
        /// <summary>
        /// Send push notification to subscribers.
        /// </summary>
        /// <param name="userId">
        /// User id for sending the push notification.
        /// </param>
        /// <param name="data">
        /// Data dictionary containing push notification elements.
        /// </param>
        public static void Send(int userId, Dictionary <string, string> data)
        {
            using (var db = new LibiadaWebEntities())
            {
                var subscribers = db.AspNetPushNotificationSubscribers.Where(s => s.UserId == userId);

                if (subscribers.Count() != 0)
                {
                    foreach (var subscriber in subscribers)
                    {
                        var endpoint = subscriber.Endpoint;
                        var p256dh   = subscriber.P256dh;
                        var auth     = subscriber.Auth;
                        var payload  = JsonConvert.SerializeObject(data);

                        var subscription = new PushSubscription(endpoint, p256dh, auth);
                        var options      = new Dictionary <string, object>();
                        options["TTL"] = 3600;

                        var publicKey  = ConfigurationManager.AppSettings["PublicVapidKey"];
                        var privateKey = ConfigurationManager.AppSettings["PrivateVapidKey"];
                        options["vapidDetails"] = new VapidDetails(subject, publicKey, privateKey);
                        var webPushClient = new WebPushClient();
                        try
                        {
                            webPushClient.SendNotification(subscription, payload, options);
                        }
                        catch (WebPushException exception)
                        {
                            Debug.WriteLine("Http STATUS code: {0}", exception.StatusCode);
                        }
                    }
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// Deletes the task by id.
        /// </summary>
        /// <param name="id">
        /// The task id.
        /// </param>
        public void DeleteTask(long id)
        {
            lock (tasks)
            {
                Task task = tasks.Single(t => t.TaskData.Id == id);
                if (task.TaskData.UserId == AccountHelper.GetUserId() || AccountHelper.IsAdmin())
                {
                    lock (task)
                    {
                        if ((task.SystemTask != null) && (!task.SystemTask.IsCompleted))
                        {
                            CancellationTokenSource cancellationTokenSource = task.CancellationTokenSource;
                            cancellationTokenSource.Cancel();
                            cancellationTokenSource.Dispose();
                        }

                        tasks.Remove(task);

                        using (var db = new LibiadaWebEntities())
                        {
                            CalculationTask databaseTask = db.CalculationTask.Find(id);
                            db.CalculationTask.Remove(databaseTask);
                            db.SaveChanges();
                        }

                        signalrHub.Send(TaskEvent.DeleteTask, task.TaskData);
                    }
                }
            }
        }
        public ActionResult Index(long matterId)
        {
            return Action(() =>
            {
                string matterName;
                Subsequence[] sequenceSubsequences;
                using (var db = new LibiadaWebEntities())
                {
                    long parentSequenceId = db.DnaSequence.Single(d => d.MatterId == matterId).Id;
                    DnaSequence parentSequence = db.DnaSequence.Single(c => c.Id == parentSequenceId);

                    var features = NcbiHelper.GetFeatures(parentSequence.RemoteId);
                    using (var subsequenceImporter = new SubsequenceImporter(features, parentSequenceId))
                    {
                        subsequenceImporter.CreateSubsequences();
                    }

                    matterName = db.Matter.Single(m => m.Id == matterId).Name;
                    sequenceSubsequences = db.Subsequence.Where(s => s.SequenceId == parentSequenceId)
                                                         .Include(s => s.Position)
                                                         .Include(s => s.Feature)
                                                         .Include(s => s.SequenceAttribute)
                                                         .ToArray();
                }

                return new Dictionary<string, object>
                                     {
                                         { "matterName", matterName },
                                         { "genes", sequenceSubsequences }
                                     };
            });
        }
Beispiel #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SequenceTransformerController"/> class.
 /// </summary>
 public SequenceTransformerController()
 {
     db = new LibiadaWebEntities();
     dnaSequenceRepository    = new GeneticSequenceRepository(db);
     commonSequenceRepository = new CommonSequenceRepository(db);
     elementRepository        = new ElementRepository(db);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SequenceTransformerController"/> class.
 /// </summary>
 public SequenceTransformerController()
 {
     db = new LibiadaWebEntities();
     dnaSequenceRepository = new DnaSequenceRepository(db);
     commonSequenceRepository = new CommonSequenceRepository(db);
     elementRepository = new ElementRepository(db);
 }
Beispiel #10
0
        /// <summary>
        /// Deletes the task by id.
        /// </summary>
        /// <param name="id">
        /// The task id.
        /// </param>
        public void DeleteTask(long id)
        {
            lock (tasks)
            {
                Task task = tasks.Single(t => t.TaskData.Id == id);
                if (task.TaskData.UserId == AccountHelper.GetUserId() || AccountHelper.IsAdmin())
                {
                    lock (task)
                    {
                        if (task.Thread != null && task.Thread.IsAlive)
                        {
                            task.Thread.Abort();
                        }

                        tasks.Remove(task);

                        using (var db = new LibiadaWebEntities())
                        {
                            CalculationTask databaseTask = db.CalculationTask.Find(id);
                            db.CalculationTask.Remove(databaseTask);
                            db.SaveChanges();
                        }

                        signalrHub.Send(TaskEvent.DeleteTask, task.TaskData);
                    }
                }
            }
        }
        /// <summary>
        /// The edit.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="System.Threading.Tasks.Task"/>.
        /// </returns>
        public async Task <ActionResult> Edit(long?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var db = new LibiadaWebEntities())
            {
                Matter matter = await db.Matter.FindAsync(id);

                if (matter == null)
                {
                    return(HttpNotFound());
                }

                var groups        = EnumExtensions.ToArray <Group>().ToSelectListWithNature();
                var sequenceTypes = EnumExtensions.ToArray <SequenceType>().ToSelectListWithNature();
                var natures       = EnumHelper.GetSelectList(typeof(Nature), matter.Nature);
                var data          = new Dictionary <string, object>
                {
                    { "natures", natures },
                    { "groups", groups },
                    { "sequenceTypes", sequenceTypes },
                    { "matter", new StringedMatter(matter, db.CommonSequence.Count(c => c.MatterId == matter.Id)) }
                };

                ViewBag.data = JsonConvert.SerializeObject(data);

                return(View(matter));
            }
        }
        /// <summary>
        /// The edit.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        public async Task <ActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            SequenceGroup sequenceGroup = await db.SequenceGroup.FindAsync(id);

            if (sequenceGroup == null)
            {
                return(HttpNotFound());
            }

            using (var db = new LibiadaWebEntities())
            {
                var viewDataHelper   = new ViewDataHelper(db);
                var viewData         = viewDataHelper.FillViewData(1, int.MaxValue);
                var matterRepository = new MatterRepository(db);
                var matterIds        = sequenceGroup.Matters.Select(m => m.Id);
                bool Selected(Matter m) => matterIds.Contains(m.Id);

                viewData["matters"]            = matterRepository.GetMatterSelectList(db.Matter, Selected);
                viewData["sequenceGroupTypes"] = EnumExtensions.ToArray <SequenceGroupType>().ToSelectListWithNature();
                ViewBag.data = JsonConvert.SerializeObject(viewData);
            }

            return(View(sequenceGroup));
        }
        /// <summary>
        /// The edit.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="System.Threading.Tasks.Task"/>.
        /// </returns>
        public ActionResult Edit(long?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var db = new LibiadaWebEntities())
            {
                Matter matter = db.Matter.Include(m => m.Multisequence).SingleOrDefault(m => m.Id == id);
                if (matter == null)
                {
                    return(HttpNotFound());
                }
                var data = new Dictionary <string, object>
                {
                    { "natures", EnumHelper.GetSelectList(typeof(Nature), matter.Nature) },
                    { "groups", EnumExtensions.ToArray <Group>().ToSelectListWithNature() },
                    { "sequenceTypes", EnumExtensions.ToArray <SequenceType>().ToSelectListWithNature() },
                    { "sequencesCount", db.CommonSequence.Count(c => c.MatterId == matter.Id) },
                    { "matter", matter },
                    { "multisequences", db.Multisequence.ToList() },
                    { "nature", ((byte)matter.Nature).ToString() },
                    { "group", ((byte)matter.Group).ToString() },
                    { "sequenceType", ((byte)matter.SequenceType).ToString() }
                };

                ViewBag.data = JsonConvert.SerializeObject(data, new JsonSerializerSettings()
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                });

                return(View(matter));
            }
        }
        public async Task <ActionResult> Edit(
            [Bind(Include = "Id,Name,Nature,Description,Group,SequenceType,MultisequenceId,MultisequenceNumber,CollectionCountry,CollectionDate")] Matter matter)
        {
            using (var db = new LibiadaWebEntities())
            {
                if (ModelState.IsValid)
                {
                    db.Entry(matter).State = EntityState.Modified;
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }

                var data = new Dictionary <string, object>
                {
                    { "natures", EnumHelper.GetSelectList(typeof(Nature), matter.Nature) },
                    { "groups", EnumExtensions.ToArray <Group>().ToSelectListWithNature() },
                    { "sequenceTypes", EnumExtensions.ToArray <SequenceType>().ToSelectListWithNature() },
                    { "sequencesCount", db.CommonSequence.Count(c => c.MatterId == matter.Id) },
                    { "matter", matter },
                    { "multisequences", db.Multisequence.ToList() },
                    { "nature", ((byte)matter.Nature).ToString() },
                    { "group", ((byte)matter.Group).ToString() },
                    { "sequenceType", ((byte)matter.SequenceType).ToString() }
                };

                ViewBag.data = JsonConvert.SerializeObject(data);
                return(View(matter));
            }
        }
Beispiel #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SubsequencesSimilarityController"/> class.
 /// </summary>
 public SubsequencesSimilarityController() : base(TaskType.SubsequencesSimilarity)
 {
     db = new LibiadaWebEntities();
     subsequenceExtractor             = new SubsequenceExtractor(db);
     characteristicTypeLinkRepository = FullCharacteristicRepository.Instance;
     sequenceAttributeRepository      = new SequenceAttributeRepository(db);
 }
        /// <summary>
        /// The index.
        /// </summary>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult Index()
        {
            var db = new LibiadaWebEntities();
            var viewDataHelper = new ViewDataHelper(db);

            Func<CharacteristicType, bool> filter;
            if (UserHelper.IsAdmin())
            {
                filter = c => c.FullSequenceApplicable;
            }
            else
            {
                filter = c => c.FullSequenceApplicable && Aliases.UserAvailableCharacteristics.Contains((Aliases.CharacteristicType)c.Id);
            }

            var data = new Dictionary<string, object>
                {
                    { "characteristicTypes", viewDataHelper.GetCharacteristicTypes(filter) }
                };

            var transformationLinks = new[] { Link.Start, Link.End, Link.CycleStart, Link.CycleEnd };
            transformationLinks = transformationLinks.OrderBy(n => (int)n).ToArray();
            data.Add("transformationLinks", transformationLinks.ToSelectList());

            var operations = new List<SelectListItem> { new SelectListItem { Text = "Dissimilar", Value = 1.ToString() }, new SelectListItem { Text = "Higher order", Value = 2.ToString() } };
            data.Add("operations", operations);

            ViewBag.data = JsonConvert.SerializeObject(data);
            return View();
        }
        public ActionResult Index(long[] matterIds)
        {
            return(CreateTask(() =>
            {
                string[] matterNames;
                var results = new string[matterIds.Length];
                var statuses = new string[matterIds.Length];
                using (var db = new LibiadaWebEntities())
                {
                    matterNames = db.Matter
                                  .Where(m => matterIds.Contains(m.Id))
                                  .OrderBy(m => m.Id)
                                  .Select(m => m.Name)
                                  .ToArray();
                    var parentSequences = db.DnaSequence
                                          .Where(c => matterIds.Contains(c.MatterId))
                                          .OrderBy(c => c.MatterId)
                                          .ToArray();

                    for (int i = 0; i < parentSequences.Length; i++)
                    {
                        try
                        {
                            DnaSequence parentSequence = parentSequences[i];
                            using (var subsequenceImporter = new SubsequenceImporter(parentSequence))
                            {
                                subsequenceImporter.CreateSubsequences();
                            }

                            int featuresCount = db.Subsequence.Count(s => s.SequenceId == parentSequence.Id &&
                                                                     s.Feature != Feature.NonCodingSequence);
                            int nonCodingCount = db.Subsequence.Count(s => s.SequenceId == parentSequence.Id &&
                                                                      s.Feature == Feature.NonCodingSequence);

                            statuses[i] = "Success";
                            results[i] = $"Successfully imported {featuresCount} features and {nonCodingCount} non coding subsequences";
                        }
                        catch (Exception exception)
                        {
                            statuses[i] = "Error";
                            results[i] = exception.Message;
                            while (exception.InnerException != null)
                            {
                                results[i] += $" {exception.InnerException.Message}";

                                exception = exception.InnerException;
                            }
                        }
                    }
                }

                return new Dictionary <string, object>
                {
                    { "matterNames", matterNames },
                    { "results", results },
                    { "status", statuses }
                };
            }));
        }
Beispiel #18
0
        public ActionResult Index(
            long matterId,
            short characteristicLinkId,
            Notation notation,
            int step,
            int initialLength,
            string accuracy)
        {
            return(CreateTask(() =>
            {
                string characteristicName;
                string matterName;
                double[] characteristics;
                Chain sequence;
                IFullCalculator calculator;
                Link link;

                using (var db = new LibiadaWebEntities())
                {
                    var commonSequenceRepository = new CommonSequenceRepository(db);
                    matterName = Cache.GetInstance().Matters.Single(m => matterId == m.Id).Name;
                    var sequenceId = db.CommonSequence.Single(c => matterId == c.MatterId && c.Notation == notation).Id;
                    sequence = commonSequenceRepository.GetLibiadaChain(sequenceId);

                    var characteristicTypeLinkRepository = FullCharacteristicRepository.Instance;
                    characteristicName = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkId, notation);

                    FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId);
                    calculator = FullCalculatorsFactory.CreateCalculator(characteristic);
                    link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId);
                }

                // characteristics = SequencesCharacteristicsCalculator.Calculate( new[] { sequenceId }, characteristicLinkId);

                AverageRemoteness averageRemotenessCalc = new AverageRemoteness();
                double averageRemoteness = averageRemotenessCalc.Calculate(sequence, Link.Start);
                Alphabet alphabet = sequence.Alphabet;
                var doubleAccuracy = double.Parse(accuracy);

                List <SequencePredictionData> sequencePredictionResult;
                Chain chain;
                (sequencePredictionResult, chain) = Predict(averageRemotenessCalc, sequence, initialLength, alphabet, averageRemoteness, doubleAccuracy);

                var matching = FindPercentageOfMatching(sequence, chain, initialLength) * 100;


                var result = new Dictionary <string, object>
                {
                    { "result", sequencePredictionResult },
                    { "matterName", matterName },
                    { "matching", matching }
                };

                return new Dictionary <string, string>
                {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }
Beispiel #19
0
 /// <summary>
 /// The index.
 /// </summary>
 /// <returns>
 /// The <see cref="System.Threading.Tasks.Task"/>.
 /// </returns>
 public async Task <ActionResult> Index()
 {
     using (var db = new LibiadaWebEntities())
     {
         var musicSequence = db.MusicSequence.Where(m => m.Notation == Notation.FormalMotifs).Include(m => m.Matter);
         return(View(await musicSequence.ToListAsync()));
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SubsequencesSimilarityController"/> class.
 /// </summary>
 public SubsequencesSimilarityController()
     : base("Subsequences similarity")
 {
     db = new LibiadaWebEntities();
     subsequenceExtractor = new SubsequenceExtractor(db);
     characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db);
     sequenceAttributeRepository = new SequenceAttributeRepository(db);
 }
 /// <summary>
 /// The index.
 /// </summary>
 /// <returns>
 /// The <see cref="ActionResult"/>.
 /// </returns>
 public ActionResult Index()
 {
     using (var db = new LibiadaWebEntities())
     {
         ViewBag.matterId = new SelectList(Cache.GetInstance().Matters.ToArray(), "id", "name");
         return(View());
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SequencesAlignmentController"/> class.
 /// </summary>
 public SequencesAlignmentController()
     : base("Sequences alignment")
 {
     db = new LibiadaWebEntities();
     subsequenceExtractor = new SubsequenceExtractor(db);
     characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db);
     featureRepository = new FeatureRepository(db);
 }
Beispiel #23
0
 /// <summary>
 /// Retrieves attributes values of given subsequences from database.
 /// </summary>
 /// <param name="subsequenceIds">
 /// Ids of subsequences which attributes values are to be retrieved.
 /// </param>
 /// <returns>
 /// Dictionary containing subsequences ids as keys and attributes values as values.
 /// </returns>
 private Dictionary <long, AttributeValue[]> GetSubsequencesAttributesValues(long[] subsequenceIds)
 {
     using (var db = new LibiadaWebEntities())
     {
         var sequenceAttributeRepository = new SequenceAttributeRepository(db);
         return(sequenceAttributeRepository.GetAttributes(subsequenceIds));
     }
 }
 /// <summary>
 /// The index.
 /// </summary>
 /// <returns>
 /// The <see cref="System.Threading.Tasks.Task"/>.
 /// </returns>
 public async Task <ActionResult> Index()
 {
     using (var db = new LibiadaWebEntities())
     {
         var commonSequence = db.CommonSequence.Include(c => c.Matter);
         return(View(await commonSequence.ToListAsync()));
     }
 }
        /// <summary>
        /// Calculation method.
        /// </summary>
        /// <param name="chainCharacteristicsIds">
        /// Dictionary with chain ids as a key
        /// and characteristicLink ids array as value.
        /// </param>
        /// <returns>
        /// The <see cref="T:double[][]"/>.
        /// </returns>
        public static Dictionary <long, Dictionary <short, double> > Calculate(Dictionary <long, short[]> chainCharacteristicsIds)
        {
            var newCharacteristics = new List <CharacteristicValue>();
            var allCharacteristics = new Dictionary <long, Dictionary <short, double> >();

            using (var db = new LibiadaWebEntities())
            {
                short[] characteristicLinkIds            = chainCharacteristicsIds.SelectMany(c => c.Value).Distinct().ToArray();
                var     characteristicTypeLinkRepository = FullCharacteristicRepository.Instance;
                var     calculators = new Dictionary <short, LinkedFullCalculator>();
                foreach (short characteristicLinkId in characteristicLinkIds)
                {
                    Link link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId);
                    FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId);
                    calculators.Add(characteristicLinkId, new LinkedFullCalculator(characteristic, link));
                }

                var    commonSequenceRepository = new CommonSequenceRepository(db);
                long[] sequenceIds = chainCharacteristicsIds.Keys.ToArray();
                foreach (long sequenceId in sequenceIds)
                {
                    short[] sequenceCharacteristicLinkIds      = chainCharacteristicsIds[sequenceId];
                    Dictionary <short, double> characteristics = db.CharacteristicValue
                                                                 .Where(c => sequenceId == c.SequenceId && sequenceCharacteristicLinkIds.Contains(c.CharacteristicLinkId))
                                                                 .ToDictionary(ct => ct.CharacteristicLinkId, ct => ct.Value);

                    allCharacteristics.Add(sequenceId, characteristics);

                    if (characteristics.Count < sequenceCharacteristicLinkIds.Length)
                    {
                        Chain sequence = commonSequenceRepository.GetLibiadaChain(sequenceId);

                        foreach (short sequenceCharacteristicLinkId in sequenceCharacteristicLinkIds)
                        {
                            if (!characteristics.ContainsKey(sequenceCharacteristicLinkId))
                            {
                                LinkedFullCalculator calculator = calculators[sequenceCharacteristicLinkId];
                                double characteristicValue      = calculator.Calculate(sequence);
                                var    characteristic           = new CharacteristicValue
                                {
                                    SequenceId           = sequenceId,
                                    CharacteristicLinkId = sequenceCharacteristicLinkId,
                                    Value = characteristicValue
                                };

                                characteristics.Add(sequenceCharacteristicLinkId, characteristicValue);
                                newCharacteristics.Add(characteristic);
                            }
                        }
                    }
                }

                var characteristicRepository = new CharacteristicRepository(db);
                characteristicRepository.TrySaveCharacteristicsToDatabase(newCharacteristics);

                return(allCharacteristics);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ViewDataHelper"/> class.
 /// </summary>
 /// <param name="db">
 /// The db.
 /// </param>
 public ViewDataHelper(LibiadaWebEntities db)
 {
     this.db = db;
     matterRepository = new MatterRepository(db);
     notationRepository = new NotationRepository(db);
     featureRepository = new FeatureRepository(db);
     characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db);
     remoteDbRepository = new RemoteDbRepository(db);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SequencesMattersController"/> class.
 /// </summary>
 protected SequencesMattersController()
     : base("Sequence upload")
 {
     Db = new LibiadaWebEntities();
     dnaSequenceRepository = new DnaSequenceRepository(Db);
     literatureSequenceRepository = new LiteratureSequenceRepository(Db);
     musicSequenceRepository = new MusicSequenceRepository(Db);
     dataSequenceRepository = new DataSequenceRepository(Db);
 }
Beispiel #28
0
        /// <summary>
        /// The create.
        /// </summary>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult Create()
        {
            using (var db = new LibiadaWebEntities())
            {
                var viewDataHelper = new ViewDataHelper(db);
                ViewBag.data = JsonConvert.SerializeObject(viewDataHelper.FillMatterCreationViewData());
            }

            return(View());
        }
Beispiel #29
0
 /// <summary>
 /// The index.
 /// </summary>
 /// <returns>
 /// The <see cref="ActionResult"/>.
 /// </returns>
 public ActionResult Index()
 {
     using (var db = new LibiadaWebEntities())
     {
         var viewDataHelper = new ViewDataHelper(db);
         Dictionary <string, object> viewData = viewDataHelper.GetCharacteristicsData(CharacteristicCategory.Full);
         ViewBag.data = JsonConvert.SerializeObject(viewData);
         return(View());
     }
 }
Beispiel #30
0
        /// <summary>
        /// The index.
        /// </summary>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult Index()
        {
            using (var db = new LibiadaWebEntities())
            {
                var viewDataHelper = new ViewDataHelper(db);
                ViewBag.data = JsonConvert.SerializeObject(viewDataHelper.FillSubsequencesViewData(1, int.MaxValue, "Calculate"));
            }

            return(View());
        }
        /// <summary>
        /// The index.
        /// </summary>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult Index()
        {
            using (var db = new LibiadaWebEntities())
            {
                var viewDataHelper = new ViewDataHelper(db);
                ViewBag.data = JsonConvert.SerializeObject(viewDataHelper.FillSubsequencesViewData(1, int.MaxValue, "Calculate"));
            }

            return View();
        }
Beispiel #32
0
        /// <summary>
        /// Extracts sequence length from database.
        /// </summary>
        /// <param name="db">
        /// The db.
        /// </param>
        /// <param name="sequenceId">
        /// The sequence id.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int GetSequenceLength(this LibiadaWebEntities db, long sequenceId)
        {
            const string Query = "SELECT cardinality(building) FROM chain WHERE id = @id";
            var          id    = new NpgsqlParameter <long>("id", NpgsqlDbType.Bigint)
            {
                TypedValue = sequenceId
            };

            return(db.Database.SqlQuery <int>(Query, id).First());
        }
Beispiel #33
0
 /// <summary>
 /// The index.
 /// </summary>
 /// <returns>
 /// The <see cref="ActionResult"/>.
 /// </returns>
 public ActionResult Index()
 {
     using (var db = new LibiadaWebEntities())
     {
         var viewDataHelper = new ViewDataHelper(db);
         var viewData       = viewDataHelper.FillViewData(CharacteristicCategory.Full, 1, int.MaxValue, "Calculate");
         ViewBag.data = JsonConvert.SerializeObject(viewData);
         return(View());
     }
 }
Beispiel #34
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SequenceMixerController"/> class.
 /// </summary>
 public SequenceMixerController()
 {
     db = new LibiadaWebEntities();
     sequenceRepository           = new CommonSequenceRepository(db);
     dnaSequenceRepository        = new GeneticSequenceRepository(db);
     musicSequenceRepository      = new MusicSequenceRepository(db);
     literatureSequenceRepository = new LiteratureSequenceRepository(db);
     dataSequenceRepository       = new DataSequenceRepository(db);
     elementRepository            = new ElementRepository(db);
 }
        /// <summary>
        /// Calculation method.
        /// </summary>
        /// <param name="chains">
        /// The chains.
        /// </param>
        /// <param name="calculators">
        /// The calculators.
        /// </param>
        /// <param name="links">
        /// The links.
        /// </param>
        /// <param name="characteristicTypeLinkIds">
        /// The characteristic type link ids.
        /// </param>
        /// <returns>
        /// The <see cref="T:double[][]"/>.
        /// </returns>
        public static double[][] Calculate(Chain[][] chains, IFullCalculator[] calculators, Link[] links, int[] characteristicTypeLinkIds)
        {
            var newCharacteristics = new List<Characteristic>();
            var characteristics = new double[chains.Length][];
            var sequenceIds = chains.SelectMany(c => c).Select(c => c.Id).Distinct();

            Dictionary<long, Dictionary<int, double>> dbCharacteristics;
            using (var db = new LibiadaWebEntities())
            {
                dbCharacteristics = db.Characteristic
                                              .Where(c => characteristicTypeLinkIds.Contains(c.CharacteristicTypeLinkId) && sequenceIds.Contains(c.SequenceId))
                                              .ToArray()
                                              .GroupBy(c => c.SequenceId)
                                              .ToDictionary(c => c.Key, c => c.ToDictionary(ct => ct.CharacteristicTypeLinkId, ct => ct.Value));
            }

            for (int i = 0; i < chains.Length; i++)
            {
                characteristics[i] = new double[calculators.Length];

                for (int j = 0; j < calculators.Length; j++)
                {
                    long sequenceId = chains[i][j].Id;
                    chains[i][j].FillIntervalManagers();

                    Dictionary<int, double> sequenceDbCharacteristics;
                    if (!dbCharacteristics.TryGetValue(sequenceId, out sequenceDbCharacteristics))
                    {
                        sequenceDbCharacteristics = new Dictionary<int, double>();
                    }

                    if (!sequenceDbCharacteristics.TryGetValue(characteristicTypeLinkIds[j], out characteristics[i][j]))
                    {
                        characteristics[i][j] = calculators[j].Calculate(chains[i][j], links[j]);
                        var currentCharacteristic = new Characteristic
                        {
                            SequenceId = sequenceId,
                            CharacteristicTypeLinkId = characteristicTypeLinkIds[j],
                            Value = characteristics[i][j]
                        };

                        newCharacteristics.Add(currentCharacteristic);
                    }
                }
            }

            // trying to save calculated characteristics to database
            using (var db = new LibiadaWebEntities())
            {
                var characteristicRepository = new CharacteristicRepository(db);
                characteristicRepository.TrySaveCharacteristicsToDatabase(newCharacteristics);
            }

            return characteristics;
        }
        /// <summary>
        /// The get subsequence characteristic.
        /// </summary>
        /// <param name="subsequenceId">
        /// The subsequence id.
        /// </param>
        /// <param name="characteristicLinkId">
        /// The characteristic type link id.
        /// </param>
        /// <param name="windowSize">
        /// The window size.
        /// </param>
        /// <param name="step">
        /// The step.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public string GetSubsequenceCharacteristic(
            long subsequenceId,
            short characteristicLinkId,
            int windowSize,
            int step)
        {
            Chain           chain;
            IFullCalculator calculator;
            Link            link;

            using (var db = new LibiadaWebEntities())
            {
                var characteristicTypeLinkRepository = FullCharacteristicRepository.Instance;

                FullCharacteristic characteristic =
                    characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId);
                calculator = FullCalculatorsFactory.CreateCalculator(characteristic);
                link       = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId);

                var subsequenceExtractor = new SubsequenceExtractor(db);

                Subsequence subsequence = db.Subsequence.Single(s => s.Id == subsequenceId);
                chain = subsequenceExtractor.GetSubsequenceSequence(subsequence);
            }

            CutRule cutRule = new SimpleCutRule(chain.Length, step, windowSize);

            CutRuleIterator iterator = cutRule.GetIterator();

            var fragments = new List <Chain>();

            while (iterator.Next())
            {
                int start = iterator.GetStartPosition();
                int end   = iterator.GetEndPosition();

                var fragment = new List <IBaseObject>();
                for (int k = 0; start + k < end; k++)
                {
                    fragment.Add(chain[start + k]);
                }

                fragments.Add(new Chain(fragment));
            }

            var characteristics = new double[fragments.Count];

            for (int k = 0; k < fragments.Count; k++)
            {
                characteristics[k] = calculator.Calculate(fragments[k], link);
            }

            return(JsonConvert.SerializeObject(characteristics));
        }
        /// <summary>
        /// Calculation method.
        /// </summary>
        /// <param name="chainIds">
        /// The chains ids.
        /// </param>
        /// <param name="characteristicLinkIds">
        /// The characteristic type link ids.
        /// </param>
        /// <param name="rotate">
        /// The rotate flag.
        /// </param>
        /// <param name="complementary">
        /// The complementary flag.
        /// </param>
        /// <param name="rotationLength">
        /// The rotation length.
        /// </param>
        /// <returns>
        /// The <see cref="T:double[][]"/>.
        /// </returns>
        public static double[][] Calculate(long[][] chainIds, short[] characteristicLinkIds, bool rotate, bool complementary, uint?rotationLength)
        {
            var links           = new Link[characteristicLinkIds.Length];
            var calculators     = new IFullCalculator[characteristicLinkIds.Length];
            var characteristics = new double[chainIds.Length][];

            long[] sequenceIds = chainIds.SelectMany(c => c).Distinct().ToArray();
            var    sequences   = new Dictionary <long, Chain>();

            using (var db = new LibiadaWebEntities())
            {
                var commonSequenceRepository = new CommonSequenceRepository(db);
                for (int i = 0; i < sequenceIds.Length; i++)
                {
                    sequences.Add(sequenceIds[i], commonSequenceRepository.GetLibiadaChain(sequenceIds[i]));
                }

                var characteristicTypeLinkRepository = FullCharacteristicRepository.Instance;
                for (int k = 0; k < characteristicLinkIds.Length; k++)
                {
                    links[k] = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkIds[k]);
                    FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkIds[k]);
                    calculators[k] = FullCalculatorsFactory.CreateCalculator(characteristic);
                }
            }

            for (int i = 0; i < chainIds.Length; i++)
            {
                characteristics[i] = new double[calculators.Length];

                for (int j = 0; j < calculators.Length; j++)
                {
                    long sequenceId = chainIds[i][j];
                    var  sequence   = (Chain)sequences[sequenceId].Clone();
                    if (complementary)
                    {
                        var       sourceSequence        = new Sequence(Alphabets.DNA, sequence.ToString());
                        ISequence complementarySequence = sourceSequence.GetReverseComplementedSequence();
                        sequence = new Chain(complementarySequence.ConvertToString());
                    }

                    if (rotate)
                    {
                        int[] building = sequence.Building.Rotate(rotationLength ?? 0);
                        List <IBaseObject> newSequence = building.Select(t => new ValueInt(t)).ToList <IBaseObject>();
                        sequence = new Chain(newSequence);
                    }

                    characteristics[i][j] = calculators[j].Calculate(sequence, links[j]);
                }
            }

            return(characteristics);
        }
 /// <summary>
 /// The create.
 /// </summary>
 /// <returns>
 /// The <see cref="ActionResult"/>.
 /// </returns>
 public ActionResult Create()
 {
     using (var db = new LibiadaWebEntities())
     {
         var viewDataHelper = new ViewDataHelper(db);
         var viewData       = viewDataHelper.FillViewData(1, int.MaxValue);
         viewData["sequenceGroupTypes"] = EnumExtensions.ToArray <SequenceGroupType>().ToSelectListWithNature();
         ViewBag.data = JsonConvert.SerializeObject(viewData);
         return(View());
     }
 }
        public void Create(ImageSequence sequence, LibiadaWebEntities db)
        {
            if (sequence.Id == default)
            {
                sequence.Id = db.GetNewElementId();
            }

            var parameters = new List <NpgsqlParameter>
            {
                new NpgsqlParameter <long>("id", NpgsqlDbType.Bigint)
                {
                    TypedValue = sequence.Id
                },
                new NpgsqlParameter <byte>("notation", NpgsqlDbType.Smallint)
                {
                    TypedValue = (byte)sequence.Notation
                },
                new NpgsqlParameter <long>("matter_id", NpgsqlDbType.Bigint)
                {
                    TypedValue = sequence.MatterId
                },
                new NpgsqlParameter <byte[]>("image_transformations", NpgsqlDbType.Array | NpgsqlDbType.Smallint)
                {
                    TypedValue = Array.Empty <byte>()
                },
                new NpgsqlParameter <byte[]>("matrix_transformations", NpgsqlDbType.Array | NpgsqlDbType.Smallint)
                {
                    TypedValue = Array.Empty <byte>()
                },
                new NpgsqlParameter <byte>("order_extractor", NpgsqlDbType.Smallint)
                {
                    TypedValue = (byte)sequence.OrderExtractor
                },
            };

            const string Query = @"INSERT INTO image_sequence (
                                        id,
                                        notation,
                                        matter_id,
                                        image_transformations,
                                        matrix_transformations,
                                        order_extractor
                                    ) VALUES (
                                        @id,
                                        @notation,
                                        @matter_id,
                                        @image_transformations,
                                        @matrix_transformations,
                                        @order_extractor
                                    );";

            db.ExecuteCommand(Query, parameters.ToArray());
        }
Beispiel #40
0
        /// <summary>
        /// Removes not finished and not started tasks from database
        /// on task manager initialization.
        /// </summary>
        private void RemoveGarbageFromDb()
        {
            using (var db = new LibiadaWebEntities())
            {
                var tasksToDelete = db.CalculationTask
                                    .Where(t => t.Status != TaskState.Completed && t.Status != TaskState.Error)
                                    .ToArray();

                db.CalculationTask.RemoveRange(tasksToDelete);
                db.SaveChanges();
            }
        }
        public ActionResult Index(long[] matterIds)
        {
            return Action(() =>
                {
                    string[] matterNames;
                    string[] results = new string[matterIds.Length];
                    string[] statuses = new string[matterIds.Length];
                    using (var db = new LibiadaWebEntities())
                    {
                        matterNames = db.Matter.Where(m => matterIds.Contains(m.Id)).OrderBy(m => m.Id).Select(m => m.Name).ToArray();
                        var parentSequences = db.DnaSequence.Where(c => matterIds.Contains(c.MatterId)).OrderBy(c => c.MatterId);
                        long[] parentSequenceIds = parentSequences.Select(c => c.Id).ToArray();
                        string[] parentRemoteIds = parentSequences.Select(c => c.RemoteId).ToArray();
                        var features = NcbiHelper.GetFeatures(parentRemoteIds);

                        for (int i = 0; i < matterIds.Length; i++)
                        {
                            try
                            {
                                var parentSequenceId = parentSequenceIds[i];
                                using (var subsequenceImporter = new SubsequenceImporter(features[i], parentSequenceId))
                                {
                                    subsequenceImporter.CreateSubsequences();
                                }

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

                                statuses[i] = "Success";
                                results[i] = "Successfully imported " + featuresCount + " features and " + nonCodingCount + " non coding subsequences";
                            }
                            catch (Exception exception)
                            {
                                statuses[i] = "Error";
                                results[i] = exception.Message;
                                if (exception.InnerException != null)
                                {
                                    results[i] += " " + exception.InnerException.Message;
                                }
                            }
                        }
                    }

                    return new Dictionary<string, object>
                           {
                               { "matterNames", matterNames },
                               { "results", results },
                               { "status", statuses }
                           };
                });
        }
        /// <summary>
        /// The index.
        /// </summary>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult Index()
        {
            var db = new LibiadaWebEntities();
            var genesSequenceIds = db.Subsequence.Select(s => s.SequenceId).Distinct();
            var matterIds = db.DnaSequence.Include(c => c.Matter).Where(c => !string.IsNullOrEmpty(c.RemoteId) &&
                                                          !genesSequenceIds.Contains(c.Id) &&
                                                          (c.Matter.SequenceType == SequenceType.CompleteGenome
                                                        || c.Matter.SequenceType == SequenceType.MitochondrionGenome
                                                        || c.Matter.SequenceType == SequenceType.Plasmid)).Select(c => c.MatterId).ToList();

            var viewDataHelper = new ViewDataHelper(db);
            var data = viewDataHelper.GetMattersData(1, 1, m => matterIds.Contains(m.Id), "Import");
            data.Add("nature", (byte)Nature.Genetic);
            ViewBag.data = JsonConvert.SerializeObject(data);
            return View();
        }
        /// <summary>
        /// The index.
        /// </summary>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult Index()
        {
            var db = new LibiadaWebEntities();
            var viewDataHelper = new ViewDataHelper(db);

            Func<CharacteristicType, bool> filter;
            if (UserHelper.IsAdmin())
            {
                filter = c => c.FullSequenceApplicable;
            }
            else
            {
                filter = c => c.FullSequenceApplicable && Aliases.UserAvailableCharacteristics.Contains((Aliases.CharacteristicType)c.Id);
            }

            ViewBag.data = JsonConvert.SerializeObject(viewDataHelper.FillViewData(filter, 1, int.MaxValue, "Calculate"));
            return View();
        }
 /// <summary>
 /// The execute custom sql command with parameters.
 /// </summary>
 /// <param name="db">
 /// The db.
 /// </param>
 /// <param name="query">
 /// The query.
 /// </param>
 /// <param name="parameters">
 /// The parameters.
 /// </param>
 public static void ExecuteCommand(LibiadaWebEntities db, string query, object[] parameters)
 {
     db.Database.ExecuteSqlCommand(query, parameters);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ElementRepository"/> class.
 /// </summary>
 /// <param name="db">
 /// The db.
 /// </param>
 public ElementRepository(LibiadaWebEntities db)
 {
     this.db = db;
 }
 /// <summary>
 /// Gets building of sequence by id..
 /// </summary>
 /// <param name="db">
 /// Database connection.
 /// </param>
 /// <param name="sequenceId">
 /// The sequence id.
 /// </param>
 /// <returns>
 /// The <see cref="T:int[]"/>.
 /// </returns>
 public static int[] GetBuilding(LibiadaWebEntities db, long sequenceId)
 {
     const string Query = "SELECT unnest(building) FROM chain WHERE id = @id";
     return db.Database.SqlQuery<int>(Query, new NpgsqlParameter("@id", sequenceId)).ToArray();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SequenceAttributeRepository"/> class.
 /// </summary>
 /// <param name="db">
 /// The db.
 /// </param>
 public SequenceAttributeRepository(LibiadaWebEntities db)
 {
     this.db = db;
     attributeRepository = new AttributeRepository();
 }
        public ActionResult Index(long[] matterIds, int characteristicTypeLinkId, int[] characteristicTypeLinkIds, int[] featureIds)
        {
            return Action(() =>
                {
                    var matterNames = new string[matterIds.Length];
                    var remoteIds = new string[matterIds.Length];
                    var subsequencesCharacteristicsNames = new string[characteristicTypeLinkIds.Length];
                    var calculators = new IFullCalculator[characteristicTypeLinkIds.Length];
                    var links = new Link[characteristicTypeLinkIds.Length];
                    var subsequencesCharacteristicsList = new SelectListItem[characteristicTypeLinkIds.Length];
                    var attributeValues = new List<AttributeValue>();
                    IEnumerable<SelectListItem> featuresSelectList;
                    Chain[] chains;
                    long[] parentSequenceIds;
                    string sequenceCharacteristicName;
                    IFullCalculator calculator;
                    Link link;

                    using (var db = new LibiadaWebEntities())
                    {
                        var featureRepository = new FeatureRepository(db);
                        featuresSelectList = featureRepository.GetFeaturesById(featureIds).Select(f => new SelectListItem { Value = f.Id.ToString(), Text = f.Name, Selected = true });

                        var parentSequences = db.DnaSequence.Include(s => s.Matter)
                                                .Where(s => s.NotationId == Aliases.Notation.Nucleotide && matterIds.Contains(s.MatterId))
                                                .Select(s => new { s.Id, MatterName = s.Matter.Name, s.RemoteId })
                                                .ToDictionary(s => s.Id);
                        parentSequenceIds = parentSequences.Keys.ToArray();

                        for (int n = 0; n < parentSequenceIds.Length; n++)
                        {
                            matterNames[n] = parentSequences[parentSequenceIds[n]].MatterName;
                            remoteIds[n] = parentSequences[parentSequenceIds[n]].RemoteId;
                        }

                        var commonSequenceRepository = new CommonSequenceRepository(db);
                        chains = commonSequenceRepository.GetNucleotideChains(matterIds);

                        var characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db);

                        sequenceCharacteristicName = characteristicTypeLinkRepository.GetCharacteristicName(characteristicTypeLinkId);
                        calculator = CalculatorsFactory.CreateFullCalculator(characteristicTypeLinkRepository.GetCharacteristicType(characteristicTypeLinkId).ClassName);
                        link = characteristicTypeLinkRepository.GetLibiadaLink(characteristicTypeLinkId);

                        for (int k = 0; k < characteristicTypeLinkIds.Length; k++)
                        {
                            links[k] = characteristicTypeLinkRepository.GetLibiadaLink(characteristicTypeLinkIds[k]);
                            string className = characteristicTypeLinkRepository.GetCharacteristicType(characteristicTypeLinkIds[k]).ClassName;
                            calculators[k] = CalculatorsFactory.CreateFullCalculator(className);
                            subsequencesCharacteristicsNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicTypeLinkIds[k]);
                            subsequencesCharacteristicsList[k] = new SelectListItem
                            {
                                Value = k.ToString(),
                                Text = subsequencesCharacteristicsNames[k],
                                Selected = false
                            };
                        }
                    }

                    var characteristics = SequencesCharacteristicsCalculator.Calculate(chains, calculator, link, characteristicTypeLinkId);

                    var sequenceData = new SequenceData[parentSequenceIds.Length];

                    for (int i = 0; i < parentSequenceIds.Length; i++)
                    {
                        // all subsequence calculations
                        var subsequencesData = SubsequencesCharacteristicsCalculator.CalculateSubsequencesCharacteristics(
                            characteristicTypeLinkIds,
                            featureIds,
                            parentSequenceIds[i],
                            calculators,
                            links,
                            attributeValues);
                        sequenceData[i] = new SequenceData(matterIds[i], matterNames[i], remoteIds[i], characteristics[i], subsequencesData);
                    }

                    // sorting organisms by their characteristic
                    sequenceData = sequenceData.OrderBy(r => r.Characteristic).ToArray();

                    var resultData = new Dictionary<string, object>
                                 {
                                     { "result", sequenceData },
                                     { "subsequencesCharacteristicsNames", subsequencesCharacteristicsNames },
                                     { "subsequencesCharacteristicsList", subsequencesCharacteristicsList },
                                     { "sequenceCharacteristicName", sequenceCharacteristicName },
                                     { "features", featuresSelectList.ToDictionary(f => f.Value) },
                                     { "attributes", EnumExtensions.ToArray<Attribute>().ToDictionary(a => (byte)a, a => a.GetDisplayValue()) },
                                     { "attributeValues", attributeValues.Select(sa => new { attribute = sa.AttributeId, value = sa.Value }) }
                                 };

                    return new Dictionary<string, object>
                    {
                        { "data", JsonConvert.SerializeObject(resultData) }
                    };
                });
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MatterRepository"/> class.
 /// </summary>
 /// <param name="db">
 /// The db.
 /// </param>
 public MatterRepository(LibiadaWebEntities db)
 {
     this.db = db;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SequenceElementsController"/> class.
 /// </summary>
 public SequenceElementsController()
 {
     db = new LibiadaWebEntities();
     elementRepository = new ElementRepository(db);
     sequenceRepository = new CommonSequenceRepository(db);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="BuildingsSimilarityController"/> class.
 /// </summary>
 public BuildingsSimilarityController()
     : base("Buildings comparison")
 {
     db = new LibiadaWebEntities();
     sequenceRepository = new CommonSequenceRepository(db);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="CharacteristicTypeLinkRepository"/> class.
 /// </summary>
 /// <param name="db">
 /// The db.
 /// </param>
 public CharacteristicTypeLinkRepository(LibiadaWebEntities db)
 {
     characteristicTypeLinks = db.CharacteristicTypeLink.Include(ctl => ctl.CharacteristicType).Include(ctl => ctl.Link).ToList();
     notations = db.Notation.ToList();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DataSequenceRepository"/> class.
 /// </summary>
 /// <param name="db">
 /// The db.
 /// </param>
 public DataSequenceRepository(LibiadaWebEntities db)
     : base(db)
 {
 }
        public ActionResult Index(
            int[] transformationLinkIds,
            int[] transformationIds,
            int iterationsCount,
            int[] characteristicTypeLinkIds,
            string[] customSequences,
            bool localFile,
            HttpPostedFileBase[] file)
        {
            return Action(() =>
            {
                var db = new LibiadaWebEntities();
                var characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db);
                int sequencesCount = localFile ? Request.Files.Count : customSequences.Length;
                var sequences = new string[sequencesCount];
                var names = new string[sequencesCount];

                for (int i = 0; i < sequencesCount; i++)
                {
                    if (localFile)
                    {
                        var sequenceStream = FileHelper.GetFileStream(file[i]);
                        var fastaSequence = NcbiHelper.GetFastaSequence(sequenceStream);
                        sequences[i] = fastaSequence.ConvertToString();
                        names[i] = fastaSequence.ID;
                    }
                    else
                    {
                        sequences[i] = customSequences[i];
                        names[i] = "Custom sequence " + (i + 1) + ". Length: " + customSequences[i].Length;
                    }
                }

                var characteristics = new double[sequences.Length, characteristicTypeLinkIds.Length];

                for (int j = 0; j < sequences.Length; j++)
                {
                    for (int k = 0; k < characteristicTypeLinkIds.Length; k++)
                    {
                        var sequence = new Chain(sequences[j]);
                        for (int l = 0; l < iterationsCount; l++)
                        {
                            for (int w = 0; w < transformationIds.Length; w++)
                            {
                                if (transformationIds[w] == 1)
                                {
                                    sequence = DissimilarChainFactory.Create(sequence);
                                }
                                else
                                {
                                    sequence = HighOrderFactory.Create(sequence, (Link)transformationLinkIds[w]);
                                }
                            }
                        }

                        var link = characteristicTypeLinkRepository.GetLibiadaLink(characteristicTypeLinkIds[k]);
                        string className = characteristicTypeLinkRepository.GetCharacteristicType(characteristicTypeLinkIds[k]).ClassName;
                        var calculator = CalculatorsFactory.CreateFullCalculator(className);

                        characteristics[j, k] = calculator.Calculate(sequence, link);
                    }
                }

                var characteristicNames = characteristicTypeLinkIds.Select(c => characteristicTypeLinkRepository.GetCharacteristicName(c)).ToArray();

                var characteristicsList = new SelectListItem[characteristicTypeLinkIds.Length];
                for (int i = 0; i < characteristicNames.Length; i++)
                {
                    characteristicsList[i] = new SelectListItem
                    {
                        Value = i.ToString(),
                        Text = characteristicNames[i],
                        Selected = false
                    };
                }

                var transformations = new Dictionary<int, string>();
                for (int i = 0; i < transformationIds.Length; i++)
                {
                    var link = ((Link)transformationLinkIds[i]).GetDisplayValue();
                    transformations.Add(i, transformationIds[i] == 1 ? "dissimilar" : "higher order " + link);
                }

                var result = new Dictionary<string, object>
                                 {
                                     { "characteristics", characteristics },
                                     { "characteristicNames", characteristicNames },
                                     { "characteristicsList", characteristicsList },
                                     { "transformationsList", transformations },
                                     { "iterationsCount", iterationsCount }
                                 };

                return new Dictionary<string, object>
                           {
                               { "data", JsonConvert.SerializeObject(result) }
                           };
            });
        }
        /// <summary>
        /// Calculation method.
        /// </summary>
        /// <param name="chains">
        /// The chains.
        /// </param>
        /// <param name="calculator">
        /// The calculator.
        /// </param>
        /// <param name="link">
        /// The link.
        /// </param>
        /// <param name="characteristicTypeLinkId">
        /// The characteristic type link id.
        /// </param>
        /// <returns>
        /// The <see cref="T:double[]"/>.
        /// </returns>
        public static double[] Calculate(Chain[] chains, IFullCalculator calculator, Link link, int characteristicTypeLinkId)
        {
            var newCharacteristics = new List<Characteristic>();
            var characteristics = new double[chains.Length];
            var sequenceIds = chains.Select(c => c.Id);

            Dictionary<long, double> dbCharacteristics;
            using (var db = new LibiadaWebEntities())
            {
                dbCharacteristics = db.Characteristic
                                              .Where(c => characteristicTypeLinkId == c.CharacteristicTypeLinkId && sequenceIds.Contains(c.SequenceId))
                                              .ToArray()
                                              .GroupBy(c => c.SequenceId)
                                              .ToDictionary(c => c.Key, c => c.Single().Value);
            }

            for (int i = 0; i < chains.Length; i++)
            {
                chains[i].FillIntervalManagers();

                long sequenceId = chains[i].Id;

                if (!dbCharacteristics.TryGetValue(sequenceId, out characteristics[i]))
                {
                    characteristics[i] = calculator.Calculate(chains[i], link);
                    var currentCharacteristic = new Characteristic
                    {
                        SequenceId = sequenceId,
                        CharacteristicTypeLinkId = characteristicTypeLinkId,
                        Value = characteristics[i]
                    };

                    newCharacteristics.Add(currentCharacteristic);
                }
            }

            // trying to save calculated characteristics to database
            using (var db = new LibiadaWebEntities())
            {
                var characteristicRepository = new CharacteristicRepository(db);
                characteristicRepository.TrySaveCharacteristicsToDatabase(newCharacteristics);
            }

            return characteristics;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SequenceImporter"/> class.
 /// </summary>
 /// <param name="db">
 /// The db.
 /// </param>
 protected SequenceImporter(LibiadaWebEntities db)
 {
     Db = db;
     MatterRepository = new MatterRepository(db);
     ElementRepository = new ElementRepository(db);
 }
        public ActionResult Index(
            long[] matterIds,
            int[] characteristicTypeLinkIds,
            int[] notationIds,
            int[] languageIds,
            int?[] translatorIds,
            bool rotate,
            bool complementary,
            uint? rotationLength)
        {
            return Action(() =>
                {
                    var sequencesCharacteristics = new SequenceCharacteristics[matterIds.Length];
                    var characteristicNames = new string[characteristicTypeLinkIds.Length];
                    var characteristicsList = new SelectListItem[characteristicTypeLinkIds.Length];
                    var links = new Link[characteristicTypeLinkIds.Length];
                    var calculators = new IFullCalculator[characteristicTypeLinkIds.Length];
                    Dictionary<long, string> mattersNames;
                    double[][] characteristics;
                    Chain[][] chains;

                    using (var db = new LibiadaWebEntities())
                    {
                        mattersNames = db.Matter.Where(m => matterIds.Contains(m.Id)).ToDictionary(m => m.Id, m => m.Name);

                        var commonSequenceRepository = new CommonSequenceRepository(db);
                        chains = commonSequenceRepository.GetChains(matterIds, notationIds, languageIds, translatorIds);

                        var characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db);
                        for (int k = 0; k < characteristicTypeLinkIds.Length; k++)
                        {
                            links[k] = characteristicTypeLinkRepository.GetLibiadaLink(characteristicTypeLinkIds[k]);
                            string className = characteristicTypeLinkRepository.GetCharacteristicType(characteristicTypeLinkIds[k]).ClassName;
                            calculators[k] = CalculatorsFactory.CreateFullCalculator(className);
                            characteristicNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicTypeLinkIds[k], notationIds[k]);
                            characteristicsList[k] = new SelectListItem
                            {
                                Value = k.ToString(),
                                Text = characteristicNames[k],
                                Selected = false
                            };
                        }
                    }

                    if (!rotate && !complementary)
                    {
                        characteristics = SequencesCharacteristicsCalculator.Calculate(chains, calculators, links, characteristicTypeLinkIds);
                    }
                    else
                    {
                        characteristics = SequencesCharacteristicsCalculator.Calculate(chains, calculators, links, rotate, complementary, rotationLength);
                    }

                    for (int i = 0; i < matterIds.Length; i++)
                    {
                        sequencesCharacteristics[i] = new SequenceCharacteristics
                        {
                            MatterName = mattersNames[matterIds[i]],
                            Characteristics = characteristics[i]
                        };
                    }

                    var result = new Dictionary<string, object>
                                            {
                                                    { "characteristics", sequencesCharacteristics },
                                                    { "characteristicNames", characteristicNames },
                                                    { "characteristicsList", characteristicsList }
                                            };

                    return new Dictionary<string, object>
                           {
                               { "data", JsonConvert.SerializeObject(result) }
                           };
                });
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="OrderTransformerController"/> class.
 /// </summary>
 public OrderTransformerController()
     : base("Order transformation")
 {
     db = new LibiadaWebEntities();
     commonSequenceRepository = new CommonSequenceRepository(db);
 }
        public ActionResult Index(
            long[] matterIds,
            int characteristicTypeLinkId,
            int[] featureIds,
            string maxPercentageDifference,
            double characteristicValueFrom,
            double characteristicValueTo)
        {
            return Action(() =>
            {
                Dictionary<int, string> features;
                var attributeValues = new List<AttributeValue>();
                var characteristics = new SubsequenceData[matterIds.Length][];
                string characteristicName;

                long[] parentSequenceIds;
                var matterNames = new string[matterIds.Length];
                IFullCalculator calculator;
                Link link;

                int mattersCount = matterIds.Length;
                int[] subsequencesCount = new int[mattersCount];

                using (var db = new LibiadaWebEntities())
                {
                    var featureRepository = new FeatureRepository(db);
                    features = featureRepository.Features.ToDictionary(f => f.Id, f => f.Name);

                    var parentSequences = db.DnaSequence.Include(s => s.Matter)
                                            .Where(s => s.NotationId == Aliases.Notation.Nucleotide && matterIds.Contains(s.MatterId))
                                            .Select(s => new { s.Id, MatterName = s.Matter.Name })
                                            .ToDictionary(s => s.Id);
                    parentSequenceIds = parentSequences.Keys.ToArray();

                    for (int n = 0; n < parentSequenceIds.Length; n++)
                    {
                        matterNames[n] = parentSequences[parentSequenceIds[n]].MatterName;
                    }

                    var characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db);

                    characteristicName = characteristicTypeLinkRepository.GetCharacteristicName(characteristicTypeLinkId);
                    string className = characteristicTypeLinkRepository.GetCharacteristicType(characteristicTypeLinkId).ClassName;
                    calculator = CalculatorsFactory.CreateFullCalculator(className);
                    link = characteristicTypeLinkRepository.GetLibiadaLink(characteristicTypeLinkId);
                }

                // cycle through matters; first level of characteristics array
                for (int i = 0; i < parentSequenceIds.Length; i++)
                {
                    var subsequencesData = SubsequencesCharacteristicsCalculator.CalculateSubsequencesCharacteristics(
                            new[] { characteristicTypeLinkId },
                            featureIds,
                            parentSequenceIds[i],
                            new[] { calculator },
                            new[] { link },
                            attributeValues);

                    subsequencesCount[i] = subsequencesData.Length;

                    subsequencesData = subsequencesData.Where(c => (characteristicValueFrom == 0 && characteristicValueTo == 0)
                        || (c.CharacteristicsValues[0] >= characteristicValueFrom && c.CharacteristicsValues[0] <= characteristicValueTo)).
                        OrderBy(c => c.CharacteristicsValues[0]).ToArray();

                    characteristics[i] = subsequencesData;
                }

                double decimalDifference = double.Parse(maxPercentageDifference, CultureInfo.InvariantCulture) / 100;

                var similarities = new object[mattersCount, mattersCount];

                var equalElements = new List<SubsequenceComparisonData>();
                int comparisonNumber = 0;

                for (int i = 0; i < characteristics.Length; i++)
                {
                    for (int j = 0; j < characteristics.Length; j++)
                    {
                        comparisonNumber++;

                        double similarSequencesCharacteristicValueFirst = 0;
                        var similarSequencesCharacteristicValueSecond = new Dictionary<int, double>();

                        double similarFirstSequencesCharacteristicValue = 0;
                        double similarSecondSequencesCharacteristicValue = 0;

                        int secondArrayStartPosition = 0;
                        double differenceSum = 0;

                        int equalElementsCountFromFirst = 0;
                        var equalElementsCountFromSecond = new Dictionary<int, int>();
                        bool equalFound = false;

                        int equalPairsCount = 0;

                        for (int k = 0; k < characteristics[i].Length; k++)
                        {
                            double first = characteristics[i][k].CharacteristicsValues[0];

                            for (int l = secondArrayStartPosition; l < characteristics[j].Length; l++)
                            {
                                double second = characteristics[j][l].CharacteristicsValues[0];

                                double difference = calculateAverageDifference(first, second);
                                bool nextElementInSecondArrayIsEqual = false;

                                if (l < characteristics[j].Length - 1)
                                {
                                    nextElementInSecondArrayIsEqual = calculateAverageDifference(second, characteristics[j][l + 1].CharacteristicsValues[0]) <= decimalDifference;
                                }

                                if (difference <= decimalDifference)
                                {
                                    equalFound = true;
                                    equalPairsCount++;

                                    if (!equalElementsCountFromSecond.ContainsKey(l))
                                    {
                                        equalElementsCountFromSecond.Add(l, 1);
                                        differenceSum += difference;
                                    }

                                    if (!similarSequencesCharacteristicValueSecond.ContainsKey(l))
                                    {
                                        similarSequencesCharacteristicValueSecond.Add(l, second);
                                    }

                                    if (i != j)
                                    {
                                        equalElements.Add(new SubsequenceComparisonData
                                        {
                                            Difference = difference,
                                            FirstMatterId = i,
                                            SecondMatterId = j,
                                            FirstSubsequenceId = k,
                                            SecondSubsequenceId = l,
                                        });
                                    }

                                    similarFirstSequencesCharacteristicValue += first;
                                    similarSecondSequencesCharacteristicValue += second;

                                    if (!nextElementInSecondArrayIsEqual)
                                    {
                                        break;
                                    }
                                }
                                else if (second < first)
                                {
                                    secondArrayStartPosition++;
                                }
                            }

                            if (equalFound)
                            {
                                equalElementsCountFromFirst++;
                                similarSequencesCharacteristicValueFirst += first;
                            }
                        }

                        double differenceSecondFinal = equalElementsCountFromSecond.Sum(s => s.Value);
                        double differenceFinal = equalElementsCountFromFirst < differenceSecondFinal ? equalElementsCountFromFirst * 2d : differenceSecondFinal * 2d;

                        double formula1 = differenceFinal / (subsequencesCount[i] + subsequencesCount[j]);

                        double formula2 = 0;
                        if (equalPairsCount != 0 && formula1 != 0)
                        {
                            formula2 = (differenceSum / equalPairsCount) / formula1;
                        }

                        double similarSequencesCharacteristicValueSecondFinal = similarSequencesCharacteristicValueSecond.Sum(s => s.Value);
                        double similarSequencesCharacteristicValue = similarSequencesCharacteristicValueFirst < similarSequencesCharacteristicValueSecondFinal ?
                                            similarSequencesCharacteristicValueFirst * 2d : similarSequencesCharacteristicValueSecondFinal * 2d;

                        double formula3 = similarSequencesCharacteristicValue * 100d / (characteristics[i].Sum(c => c.CharacteristicsValues[0]) + characteristics[j].Sum(c => c.CharacteristicsValues[0]));

                        similarities[i, j] = new
                        {
                            formula1 = Math.Round(formula1 * 100d, 3),
                            formula2 = Math.Round(formula2, 3),
                            formula3 = Math.Round(formula3, 3)
                        };
                    }
                }

                var result = new Dictionary<string, object>
                {
                    { "mattersNames", matterNames },
                    { "characteristicName", characteristicName },
                    { "similarities", similarities },
                    { "characteristics", characteristics },
                    { "equalElements", equalElements.OrderBy(e => e.Difference).ToList() },
                    { "features", features },
                    { "attributeValues", attributeValues.Select(sa => new { attribute = sa.AttributeId, value = sa.Value }) },
                    { "attributes", EnumExtensions.ToArray<LibiadaWeb.Attribute>().ToDictionary(a => (byte)a, a => a.GetDisplayValue()) }
                };

                return new Dictionary<string, object>
                           {
                               { "data", JsonConvert.SerializeObject(result) }
                           };
            });
        }
        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 }
                           };
            });
        }