The sequence repository.
Inheritance: SequenceImporter, ICommonSequenceRepository
 /// <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);
 }
 /// <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);
 }
        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="BuildingsSimilarityController"/> class.
 /// </summary>
 public BuildingsSimilarityController()
     : base("Buildings comparison")
 {
     db = new LibiadaWebEntities();
     sequenceRepository = new CommonSequenceRepository(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="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="OrderTransformerController"/> class.
 /// </summary>
 public OrderTransformerController()
     : base("Order transformation")
 {
     db = new LibiadaWebEntities();
     commonSequenceRepository = new CommonSequenceRepository(db);
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="SubsequenceImporter"/> class.
        /// </summary>
        /// <param name="features">
        /// The features.
        /// </param>
        /// <param name="sequenceId">
        /// The sequence id.
        /// </param>
        public SubsequenceImporter(List<FeatureItem> features, long sequenceId)
        {
            this.features = features;
            this.sequenceId = sequenceId;
            featureRepository = new FeatureRepository(db);
            sequenceAttributeRepository = new SequenceAttributeRepository(db);

            using (var commonSequenceRepository = new CommonSequenceRepository(db))
            {
                var parentSequence = commonSequenceRepository.ToLibiadaBaseChain(sequenceId);
                parentLength = parentSequence.GetLength();
            }

            gene = featureRepository.GetFeatureById(Aliases.Feature.Gene).Type;
            sourceLength = features[0].Location.LocationEnd;
            positionsMap = new bool[parentLength];
            allNonGenesLeafLocations = features.Where(f => f.Key != gene)
                                               .Select(f => f.Location.GetLeafLocations())
                                               .ToArray();
        }
        public ActionResult Index(
            long[] matterIds,
            int[] transformationLinkIds,
            int[] transformationIds,
            int iterationsCount,
            int[] characteristicTypeLinkIds,
            int[] notationIds,
            int[] languageIds,
            int?[] translatorIds)
        {
            return Action(() =>
            {
                var db = new LibiadaWebEntities();
                var characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db);
                var commonSequenceRepository = new CommonSequenceRepository(db);
                var mattersCharacteristics = new object[matterIds.Length];
                matterIds = matterIds.OrderBy(m => m).ToArray();
                var matters = db.Matter.Where(m => matterIds.Contains(m.Id)).ToDictionary(m => m.Id);

                for (int i = 0; i < matterIds.Length; i++)
                {
                    var matterId = matterIds[i];
                    var characteristics = new List<double>();
                    for (int k = 0; k < notationIds.Length; k++)
                    {
                        int notationId = notationIds[k];
                        long sequenceId;
                        if (matters[matterId].Nature == Nature.Literature)
                        {
                            int languageId = languageIds[k];
                            int? translatorId = translatorIds[k];

                            sequenceId = db.LiteratureSequence.Single(l => l.MatterId == matterId &&
                                                                           l.NotationId == notationId
                                                                           && l.LanguageId == languageId
                                                                           &&
                                                                           ((translatorId == null &&
                                                                             l.TranslatorId == null)
                                                                            || (translatorId == l.TranslatorId))).Id;
                        }
                        else
                        {
                            sequenceId = db.CommonSequence.Single(c => c.MatterId == matterId && c.NotationId == notationId).Id;
                        }

                        var sequence = commonSequenceRepository.ToLibiadaChain(sequenceId);
                        for (int l = 0; l < iterationsCount; l++)
                        {
                            for (int j = 0; j < transformationIds.Length; j++)
                            {
                                if (transformationIds[j] == 1)
                                {
                                    sequence = DissimilarChainFactory.Create(sequence);
                                }
                                else
                                {
                                    sequence = HighOrderFactory.Create(sequence, (LibiadaCore.Core.Link)transformationLinkIds[j]);
                                }
                            }
                        }

                        int characteristicTypeLinkId = characteristicTypeLinkIds[k];
                        var link = characteristicTypeLinkRepository.GetLibiadaLink(characteristicTypeLinkId);
                        string className = characteristicTypeLinkRepository.GetCharacteristicType(characteristicTypeLinkId).ClassName;

                        IFullCalculator calculator = CalculatorsFactory.CreateFullCalculator(className);
                        characteristics.Add(calculator.Calculate(sequence, link));
                    }

                    mattersCharacteristics[i] = new { matterName = matters[matterId].Name, characteristics };
                }

                var characteristicNames = new string[characteristicTypeLinkIds.Length];
                for (int k = 0; k < characteristicTypeLinkIds.Length; k++)
                {
                    characteristicNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicTypeLinkIds[k], notationIds[k]);
                }

                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 = ((LibiadaCore.Core.Link)transformationLinkIds[i]).GetDisplayValue();
                    transformations.Add(i, transformationIds[i] == 1 ? "dissimilar" : "higher order " + link);
                }

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

                return new Dictionary<string, object>
                           {
                               { "data", JsonConvert.SerializeObject(result) }
                           };
            });
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SequenceCheckController"/> class.
 /// </summary>
 public SequenceCheckController()
     : base("Sequence check")
 {
     db = new LibiadaWebEntities();
     commonSequenceRepository = new CommonSequenceRepository(db);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SubsequenceExtractor"/> class.
 /// </summary>
 /// <param name="db">
 /// The db.
 /// </param>
 public SubsequenceExtractor(LibiadaWebEntities db)
 {
     this.db = db;
     commonSequenceRepository = new CommonSequenceRepository(db);
 }