Beispiel #1
0
        /// <summary>
        /// The calculate characteristic.
        /// </summary>
        /// <param name="characteristicLinkId">
        /// The characteristic type and link id.
        /// </param>
        /// <param name="subsequences">
        /// The subsequences.
        /// </param>
        /// <returns>
        /// The <see cref="List{Subsequence}"/>.
        /// </returns>
        private List <double> CalculateCharacteristic(short characteristicLinkId, Subsequence[] subsequences)
        {
            var characteristics    = new List <double>();
            var newCharacteristics = new List <CharacteristicValue>();
            FullCharacteristic fullCharacteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId);
            IFullCalculator    calculator         = FullCalculatorsFactory.CreateCalculator(fullCharacteristic);
            Link link                    = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId);
            var  subsequenceIds          = subsequences.Select(s => s.Id);
            var  existingCharacteristics = db.CharacteristicValue
                                           .Where(cv => cv.CharacteristicLinkId == characteristicLinkId && subsequenceIds.Contains(cv.SequenceId))
                                           .ToDictionary(cv => cv.SequenceId);
            Dictionary <long, Chain> sequences = subsequenceExtractor.GetSubsequencesSequences(subsequences);

            for (int i = 0; i < subsequences.Length; i++)
            {
                if (existingCharacteristics.ContainsKey(subsequences[i].Id))
                {
                    characteristics.Add(existingCharacteristics[subsequences[i].Id].Value);
                }
                else
                {
                    double value = calculator.Calculate(sequences[subsequences[i].Id], link);
                    var    currentCharacteristic = new CharacteristicValue
                    {
                        SequenceId           = subsequences[i].Id,
                        CharacteristicLinkId = characteristicLinkId,
                        Value = value
                    };
                    newCharacteristics.Add(currentCharacteristic);
                    characteristics.Add(value);
                }
            }

            db.CharacteristicValue.AddRange(newCharacteristics);
            db.SaveChanges();

            return(characteristics);
        }
        /// <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;
        }
        public ActionResult Index(
            long[] matterIds,
            short[] characteristicLinkIds,
            Notation[] notations,
            Language[] languages,
            int clustersCount,
            ClusterizationType clusterizationType,
            double equipotencyWeight        = 1,
            double normalizedDistanceWeight = 1,
            double distanceWeight           = 1,
            double bandwidth    = 0,
            int maximumClusters = 2)
        {
            return(CreateTask(() =>
            {
                var characteristicNames = new string[characteristicLinkIds.Length];
                var mattersCharacteristics = new object[matterIds.Length];
                var characteristics = new double[matterIds.Length][];
                matterIds = matterIds.OrderBy(m => m).ToArray();
                Dictionary <long, string> matters = db.Matter.Where(m => matterIds.Contains(m.Id)).ToDictionary(m => m.Id, m => m.Name);

                for (int j = 0; j < matterIds.Length; j++)
                {
                    long matterId = matterIds[j];
                    characteristics[j] = new double[characteristicLinkIds.Length];
                    for (int i = 0; i < characteristicLinkIds.Length; i++)
                    {
                        Notation notation = notations[i];
                        long sequenceId = db.Matter.Single(m => m.Id == matterId).Sequence.Single(c => c.Notation == notation).Id;

                        int characteristicLinkId = characteristicLinkIds[i];
                        if (db.CharacteristicValue.Any(c => c.SequenceId == sequenceId && c.CharacteristicLinkId == characteristicLinkId))
                        {
                            characteristics[j][i] = db.CharacteristicValue.Single(c => c.SequenceId == sequenceId && c.CharacteristicLinkId == characteristicLinkId).Value;
                        }
                        else
                        {
                            Chain tempChain = commonSequenceRepository.GetLibiadaChain(sequenceId);

                            Link link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId);
                            FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId);
                            IFullCalculator calculator = FullCalculatorsFactory.CreateCalculator(characteristic);
                            characteristics[j][i] = calculator.Calculate(tempChain, link);
                        }
                    }
                }

                for (int k = 0; k < characteristicLinkIds.Length; k++)
                {
                    characteristicNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkIds[k], notations[k]);
                }

                var clusterizationParams = new Dictionary <string, double>
                {
                    { "clustersCount", clustersCount },
                    { "equipotencyWeight", equipotencyWeight },
                    { "normalizedDistanceWeight", normalizedDistanceWeight },
                    { "distanceWeight", distanceWeight },
                    { "bandwidth", bandwidth },
                    { "maximumClusters", maximumClusters }
                };

                IClusterizator clusterizator = ClusterizatorsFactory.CreateClusterizator(clusterizationType, clusterizationParams);
                int[] clusterizationResult = clusterizator.Cluster(clustersCount, characteristics);
                for (int i = 0; i < clusterizationResult.Length; i++)
                {
                    mattersCharacteristics[i] = new
                    {
                        MatterName = matters[matterIds[i]],
                        cluster = clusterizationResult[i] + 1,
                        Characteristics = characteristics[i]
                    };
                }

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

                var result = new Dictionary <string, object>
                {
                    { "characteristicNames", characteristicNames },
                    { "characteristics", mattersCharacteristics },
                    { "characteristicsList", characteristicsList },
                    { "clustersCount", clusterizationResult.Distinct().Count() }
                };

                return new Dictionary <string, object>
                {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }
        public ActionResult Index(int length, int alphabetCardinality, bool generateStrict, short[] characteristicLinkIds)
        {
            return(CreateTask(() =>
            {
                var orderGenerator = new OrderGenerator();
                var orders = generateStrict ?
                             orderGenerator.StrictGenerateOrders(length, alphabetCardinality) :
                             orderGenerator.GenerateOrders(length, alphabetCardinality);

                var characteristics = new double[orders.Count][];
                var sequencesCharacteristics = new List <SequenceCharacteristics>();
                for (int i = 0; i < orders.Count; i++)
                {
                    sequencesCharacteristics.Add(new SequenceCharacteristics());
                }
                for (int j = 0; j < orders.Count; j++)
                {
                    var sequence = new Chain(orders[j].Select(Convert.ToInt16).ToArray());
                    characteristics[j] = new double[characteristicLinkIds.Length];
                    for (int k = 0; k < characteristicLinkIds.Length; k++)
                    {
                        Link link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkIds[k]);
                        FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkIds[k]);
                        IFullCalculator calculator = FullCalculatorsFactory.CreateCalculator(characteristic);

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

                    sequencesCharacteristics[j] = new SequenceCharacteristics
                    {
                        MatterName = String.Join(",", orders[j].Select(n => n.ToString()).ToArray()),
                        Characteristics = characteristics[j]
                    };
                }

                var characteristicNames = new string[characteristicLinkIds.Length];
                var characteristicsList = new SelectListItem[characteristicLinkIds.Length];
                for (int k = 0; k < characteristicLinkIds.Length; k++)
                {
                    characteristicNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkIds[k]);
                    characteristicsList[k] = new SelectListItem
                    {
                        Value = k.ToString(),
                        Text = characteristicNames[k],
                        Selected = false
                    };
                }

                sequencesCharacteristics.RemoveAll(el => el.Characteristics.Any(v => Double.IsInfinity(v) ||
                                                                                Double.IsNaN(v) ||
                                                                                Double.IsNegativeInfinity(v) ||
                                                                                Double.IsPositiveInfinity(v)));
                var index = new int[characteristicsList.Length];
                for (int i = 0; i < index.Length; i++)
                {
                    index[i] = i;
                }
                var result = new Dictionary <string, object>
                {
                    { "characteristics", sequencesCharacteristics.ToArray() },
                    { "characteristicNames", characteristicNames },
                    { "characteristicsList", characteristicsList },
                    { "characteristicsIndex", index }
                };

                return new Dictionary <string, object>
                {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }
Beispiel #5
0
        public ActionResult Index(short[] characteristicLinkIds, string[] customSequences, bool localFile, string fileType, bool toLower, bool removePunctuation)
        {
            return(CreateTask(() =>
            {
                int sequencesCount = localFile ? Request.Files.Count : customSequences.Length;
                var sequencesNames = new string[sequencesCount];
                var sequences = new Chain[sequencesCount];
                if (localFile)
                {
                    for (int i = 0; i < sequencesCount; i++)
                    {
                        Stream sequenceStream = FileHelper.GetFileStream(Request.Files[i]);
                        sequencesNames[i] = Request.Files[i].FileName;

                        switch (fileType)
                        {
                        case "text":
                            using (var sr = new StreamReader(sequenceStream))
                            {
                                string stringTextSequence = sr.ReadToEnd();
                                if (toLower)
                                {
                                    stringTextSequence = stringTextSequence.ToLower();
                                }
                                if (removePunctuation)
                                {
                                    stringTextSequence = Regex.Replace(stringTextSequence, @"[^\w\s]", "");
                                }
                                sequences[i] = new Chain(stringTextSequence);
                            }
                            break;

                        case "image":
                            var image = Image.Load(sequenceStream);
                            var sequence = ImageProcessor.ProcessImage(image, new IImageTransformer[0], new IMatrixTransformer[0], new LineOrderExtractor());
                            var alphabet = new Alphabet {
                                NullValue.Instance()
                            };
                            var incompleteAlphabet = sequence.Alphabet;
                            for (int j = 0; j < incompleteAlphabet.Cardinality; j++)
                            {
                                alphabet.Add(incompleteAlphabet[j]);
                            }

                            sequences[i] = new Chain(sequence.Building, alphabet);
                            break;

                        case "genetic":
                            ISequence fastaSequence = NcbiHelper.GetFastaSequence(sequenceStream);
                            var stringSequence = fastaSequence.ConvertToString();
                            sequences[i] = new Chain(stringSequence);
                            sequencesNames[i] = fastaSequence.ID;
                            break;

                        case "wavFile":
                            var reader = new BinaryReader(Request.Files[i].InputStream);

                            int chunkID = reader.ReadInt32();
                            int fileSize = reader.ReadInt32();
                            int riffType = reader.ReadInt32();
                            int fmtID = reader.ReadInt32();
                            int fmtSize = reader.ReadInt32();
                            int fmtCode = reader.ReadInt16();
                            int channels = reader.ReadInt16();
                            int sampleRate = reader.ReadInt32();
                            int fmtAvgBPS = reader.ReadInt32();
                            int fmtBlockAlign = reader.ReadInt16();
                            int bitDepth = reader.ReadInt16();

                            if (fmtSize == 18)
                            {
                                // Read any extra values
                                int fmtExtraSize = reader.ReadInt16();
                                reader.ReadBytes(fmtExtraSize);
                            }

                            int dataID = reader.ReadInt32();
                            int dataSize = reader.ReadInt32();
                            byte[] byteArray = reader.ReadBytes(dataSize);
                            var shortArray = new short[byteArray.Length / 2];
                            Buffer.BlockCopy(byteArray, 0, shortArray, 0, byteArray.Length);
                            //shortArray = Amplitude(shortArray, 20);
                            shortArray = Sampling(shortArray, 50);
                            //shortArray = shortArray.Select(s => (short)(s / 10)).ToArray();
                            sequences[i] = new Chain(shortArray);
                            break;

                        default:
                            throw new ArgumentException("Unknown file type", nameof(fileType));
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < sequencesCount; i++)
                    {
                        sequences[i] = new Chain(customSequences[i]);
                        sequencesNames[i] = $"Custom sequence {i + 1}. Length: {customSequences[i].Length}";
                    }
                }

                var sequencesCharacteristics = new SequenceCharacteristics[sequences.Length];
                for (int j = 0; j < sequences.Length; j++)
                {
                    var characteristics = new double[characteristicLinkIds.Length];
                    for (int k = 0; k < characteristicLinkIds.Length; k++)
                    {
                        Link link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkIds[k]);
                        FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkIds[k]);
                        IFullCalculator calculator = FullCalculatorsFactory.CreateCalculator(characteristic);

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

                    sequencesCharacteristics[j] = new SequenceCharacteristics
                    {
                        MatterName = sequencesNames[j],
                        Characteristics = characteristics
                    };
                }

                var characteristicNames = new string[characteristicLinkIds.Length];
                var characteristicsList = new SelectListItem[characteristicLinkIds.Length];
                for (int k = 0; k < characteristicLinkIds.Length; k++)
                {
                    characteristicNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkIds[k]);
                    characteristicsList[k] = new SelectListItem
                    {
                        Value = k.ToString(),
                        Text = characteristicNames[k],
                        Selected = false
                    };
                }

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

                return new Dictionary <string, object>
                {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }
Beispiel #6
0
        public ActionResult Index(
            OrderTransformation[] transformationsSequence,
            int iterationsCount,
            short[] characteristicLinkIds,
            string[] customSequences,
            bool localFile,
            HttpPostedFileBase[] file)
        {
            return(CreateTask(() =>
            {
                var characteristicTypeLinkRepository = FullCharacteristicRepository.Instance;
                int sequencesCount = localFile ? Request.Files.Count : customSequences.Length;
                var sequences = new string[sequencesCount];
                var sequencesNames = new string[sequencesCount];

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

                var sequencesCharacteristics = new SequenceCharacteristics[sequences.Length];
                for (int j = 0; j < sequences.Length; j++)
                {
                    var characteristics = new double[characteristicLinkIds.Length];
                    for (int k = 0; k < characteristicLinkIds.Length; k++)
                    {
                        var sequence = new Chain(sequences[j]);
                        for (int l = 0; l < iterationsCount; l++)
                        {
                            for (int w = 0; w < transformationsSequence.Length; w++)
                            {
                                sequence = transformationsSequence[w] == OrderTransformation.Dissimilar ? DissimilarChainFactory.Create(sequence)
                                                                     : HighOrderFactory.Create(sequence, EnumExtensions.GetLink(transformationsSequence[w]));
                            }
                        }

                        Link link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkIds[k]);
                        FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkIds[k]);
                        IFullCalculator calculator = FullCalculatorsFactory.CreateCalculator(characteristic);

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

                    sequencesCharacteristics[j] = new SequenceCharacteristics
                    {
                        MatterName = sequencesNames[j],
                        Characteristics = characteristics
                    };
                }

                string[] characteristicNames = characteristicLinkIds.Select(c => characteristicTypeLinkRepository.GetCharacteristicName(c)).ToArray();

                var characteristicsList = new SelectListItem[characteristicLinkIds.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 < transformationsSequence.Length; i++)
                {
                    transformations.Add(i, transformationsSequence[i].GetDisplayValue());
                }

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

                return new Dictionary <string, object>
                {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }
Beispiel #7
0
        public ActionResult Index(
            long[] matterIds,
            OrderTransformation[] transformationsSequence,
            int iterationsCount,
            short[] characteristicLinkIds,
            Notation[] notations,
            Language[] languages,
            Translator?[] translators,
            PauseTreatment[] pauseTreatments,
            bool[] sequentialTransfers)
        {
            return(CreateTask(() =>
            {
                var db = new LibiadaWebEntities();
                var characteristicTypeLinkRepository = FullCharacteristicRepository.Instance;
                var commonSequenceRepository = new CommonSequenceRepository(db);
                var mattersCharacteristics = new object[matterIds.Length];
                matterIds = matterIds.OrderBy(m => m).ToArray();
                Dictionary <long, Matter> matters = db.Matter.Where(m => matterIds.Contains(m.Id)).ToDictionary(m => m.Id);

                for (int i = 0; i < matterIds.Length; i++)
                {
                    long matterId = matterIds[i];
                    var characteristics = new double[characteristicLinkIds.Length];
                    for (int k = 0; k < characteristicLinkIds.Length; k++)
                    {
                        Notation notation = notations[k];
                        long sequenceId;
                        switch (matters[matterId].Nature)
                        {
                        case Nature.Literature:
                            Language language = languages[k];
                            Translator translator = translators[k] ?? Translator.NoneOrManual;
                            sequenceId = db.LiteratureSequence.Single(l => l.MatterId == matterId &&
                                                                      l.Notation == notation &&
                                                                      l.Language == language &&
                                                                      l.Translator == translator).Id;
                            break;

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

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

                        Chain sequence = commonSequenceRepository.GetLibiadaChain(sequenceId);
                        for (int l = 0; l < iterationsCount; l++)
                        {
                            for (int j = 0; j < transformationsSequence.Length; j++)
                            {
                                sequence = transformationsSequence[j] == OrderTransformation.Dissimilar ? DissimilarChainFactory.Create(sequence)
                                                                     : HighOrderFactory.Create(sequence, EnumExtensions.GetLink(transformationsSequence[j]));
                            }
                        }

                        int characteristicLinkId = characteristicLinkIds[k];
                        Link link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId);
                        FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId);

                        IFullCalculator calculator = FullCalculatorsFactory.CreateCalculator(characteristic);
                        characteristics[k] = calculator.Calculate(sequence, link);
                    }

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

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

                var characteristicsList = new SelectListItem[characteristicLinkIds.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 < transformationsSequence.Length; i++)
                {
                    transformations.Add(i, transformationsSequence[i].GetDisplayValue());
                }

                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) }
                };
            }));
        }
Beispiel #8
0
        public ActionResult Index(
            long[] matterIds,
            OrderTransformation[] transformationsSequence,
            int iterationsCount,
            short characteristicLinkId,
            Notation notation,
            Language?language,
            Translator?translator,
            PauseTreatment?pauseTreatment,
            bool?sequentialTransfer)
        {
            return(CreateTask(() =>
            {
                var db = new LibiadaWebEntities();
                var characteristicTypeLinkRepository = FullCharacteristicRepository.Instance;
                var commonSequenceRepository = new CommonSequenceRepository(db);
                var mattersCharacteristics = new object[matterIds.Length];
                matterIds = matterIds.OrderBy(m => m).ToArray();
                Dictionary <long, Matter> matters = db.Matter.Where(m => matterIds.Contains(m.Id)).ToDictionary(m => m.Id);

                for (int i = 0; i < matterIds.Length; i++)
                {
                    long matterId = matterIds[i];
                    long sequenceId;
                    switch (matters[matterId].Nature)
                    {
                    case Nature.Literature:
                        sequenceId = db.LiteratureSequence.Single(l => l.MatterId == matterId &&
                                                                  l.Notation == notation &&
                                                                  l.Language == language &&
                                                                  l.Translator == translator).Id;
                        break;

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

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

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

                    Chain sequence = commonSequenceRepository.GetLibiadaChain(sequenceId);

                    var characteristics = new double[transformationsSequence.Length * iterationsCount];
                    for (int j = 0; j < iterationsCount; j++)
                    {
                        for (int k = 0; k < transformationsSequence.Length; k++)
                        {
                            sequence = transformationsSequence[k] == OrderTransformation.Dissimilar ? DissimilarChainFactory.Create(sequence)
                                                                 : HighOrderFactory.Create(sequence, transformationsSequence[k].GetLink());
                            characteristics[transformationsSequence.Length * j + k] = calculator.Calculate(sequence, link);
                        }
                    }

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

                string characteristicName = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkId, notation);


                var result = new Dictionary <string, object>
                {
                    { "characteristics", mattersCharacteristics },
                    { "characteristicName", characteristicName },
                    { "transformationsList", transformationsSequence.Select(ts => ts.GetDisplayValue()) },
                    { "iterationsCount", iterationsCount }
                };

                return new Dictionary <string, object>
                {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }
        public ActionResult Index(int length, int alphabetCardinality, int generateStrict, short[] characteristicLinkIds)
        {
            // TODO: Reafctor all of this
            return(CreateTask(() =>
            {
                var orderGenerator = new OrderGenerator();
                var orders = new List <int[]>();
                switch (generateStrict)
                {
                case 0:
                    orders = orderGenerator.StrictGenerateOrders(length, alphabetCardinality);
                    break;

                case 1:
                    orders = orderGenerator.GenerateOrders(length, alphabetCardinality);
                    break;

                default: throw new ArgumentException("Invalid type of generate");
                }

                var characteristics = new double[orders.Count][];
                var sequencesCharacteristics = new List <SequenceCharacteristics>();
                for (int i = 0; i < orders.Count; i++)
                {
                    sequencesCharacteristics.Add(new SequenceCharacteristics());
                }
                for (int j = 0; j < orders.Count; j++)
                {
                    var sequence = new Chain(orders[j].Select(Convert.ToInt16).ToArray());
                    characteristics[j] = new double[characteristicLinkIds.Length];
                    for (int k = 0; k < characteristicLinkIds.Length; k++)
                    {
                        Link characteristicLink = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkIds[k]);
                        FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkIds[k]);
                        IFullCalculator calculator = FullCalculatorsFactory.CreateCalculator(characteristic);

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

                    sequencesCharacteristics[j] = new SequenceCharacteristics
                    {
                        MatterName = String.Join(",", orders[j].Select(n => n.ToString()).ToArray()),
                        Characteristics = characteristics[j]
                    };
                }
                sequencesCharacteristics.RemoveAll(el => el.Characteristics.Any(v => Double.IsInfinity(v) ||
                                                                                Double.IsNaN(v) ||
                                                                                Double.IsNegativeInfinity(v) ||
                                                                                Double.IsPositiveInfinity(v)));



                var characteristicNames = new string[characteristicLinkIds.Length];
                var characteristicsList = new SelectListItem[characteristicLinkIds.Length];
                for (int k = 0; k < characteristicLinkIds.Length; k++)
                {
                    characteristicNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkIds[k]);
                    characteristicsList[k] = new SelectListItem
                    {
                        Value = k.ToString(),
                        Text = characteristicNames[k],
                        Selected = false
                    };
                }
                var index = new int[characteristicsList.Length];
                for (int i = 0; i < index.Length; i++)
                {
                    index[i] = i;
                }

                var resultIntervals = new Dictionary <string, Dictionary <Dictionary <int, int>, Dictionary <int[], SequenceCharacteristics> > >();
                foreach (var link in EnumExtensions.ToArray <Link>())
                {
                    if (link == Link.NotApplied)
                    {
                        continue;
                    }
                    var accordance = new Dictionary <Dictionary <int, int>, Dictionary <int[], SequenceCharacteristics> >();
                    for (int j = 0; j < orders.Count; j++)
                    {
                        var order = orders[j];
                        var sequence = new Chain(order.Select(Convert.ToInt16).ToArray());
                        var fullIntervals = new Dictionary <int, int>();
                        var alphabet = sequence.Alphabet.ToList();
                        foreach (var el in alphabet)
                        {
                            var congIntervals = sequence.CongenericChain(el).GetArrangement(link);
                            foreach (var interval in congIntervals)
                            {
                                if (fullIntervals.Any(e => e.Key == interval))
                                {
                                    fullIntervals[interval]++;
                                }
                                else
                                {
                                    fullIntervals.Add(interval, 1);
                                }
                            }
                        }
                        if (accordance.Keys.Any(intervals => intervals.All(i1 => fullIntervals.Any(i2 => i2.Key == i1.Key && i2.Value == i1.Value))))
                        {
                            accordance[accordance.Keys.First(intervals => intervals.All(i1 => fullIntervals.Any(i2 => i2.Key == i1.Key && i2.Value == i1.Value)))].Add(order, sequencesCharacteristics.First(el => el.MatterName.SequenceEqual(String.Join(",", orders[j].Select(n => n.ToString()).ToArray()))));
                        }
                        else
                        {
                            accordance.Add(fullIntervals, new Dictionary <int[], SequenceCharacteristics> {
                                {
                                    order,
                                    sequencesCharacteristics.First(el => el.MatterName.SequenceEqual(String.Join(",", orders[j].Select(n => n.ToString()).ToArray())))
                                }
                            });
                        }
                    }
                    resultIntervals.Add(EnumExtensions.GetDisplayValue <Link>(link), accordance);
                }


                var list = EnumHelper.GetSelectList(typeof(Link));
                list.RemoveAt(0);
                var result = new Dictionary <string, object>
                {
                    { "result", resultIntervals.Select(r => new
                        {
                            link = r.Key,
                            accordance = r.Value.Select(d => new {
                                distributionIntervals = d.Key.Select(pair => new
                                {
                                    interval = pair.Key,
                                    count = pair.Value
                                }).ToArray(),
                                orders = d.Value.Select(o => new
                                {
                                    order = o.Key,
                                    characteristics = o.Value
                                })
                            })
                        }) },
                    { "linkList", list },
                    { "characteristicNames", characteristicNames },
                    { "characteristicsList", characteristicsList },
                    { "characteristicsIndex", index }
                };

                return new Dictionary <string, string> {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }
        public ActionResult Index(
            long[] matterIds,
            OrderTransformation[] transformationsSequence,
            int iterationsCount,
            short[] characteristicLinkIds,
            Notation[] notations,
            Language?[] languages,
            Translator?[] translators,
            PauseTreatment?[] pauseTreatments,
            bool?[] sequentialTransfers,
            ImageOrderExtractor?[] trajectories)
        {
            return(CreateTask(() =>
            {
                Dictionary <long, string> mattersNames = Cache.GetInstance().Matters.Where(m => matterIds.Contains(m.Id)).ToDictionary(m => m.Id, m => m.Name);
                Chain[][] sequences = new Chain[matterIds.Length][];

                using (var db = new LibiadaWebEntities())
                {
                    var commonSequenceRepository = new CommonSequenceRepository(db);
                    long[][] sequenceIds = commonSequenceRepository.GetSequenceIds(matterIds,
                                                                                   notations,
                                                                                   languages,
                                                                                   translators,
                                                                                   pauseTreatments,
                                                                                   sequentialTransfers,
                                                                                   trajectories);
                    for (int i = 0; i < matterIds.Length; i++)
                    {
                        sequences[i] = new Chain[characteristicLinkIds.Length];
                        for (int j = 0; j < characteristicLinkIds.Length; j++)
                        {
                            sequences[i][j] = commonSequenceRepository.GetLibiadaChain(sequenceIds[i][j]);
                        }
                    }
                }

                var characteristicTypeLinkRepository = FullCharacteristicRepository.Instance;
                var sequencesCharacteristics = new SequenceCharacteristics[matterIds.Length];
                matterIds = matterIds.OrderBy(m => m).ToArray();

                for (int i = 0; i < matterIds.Length; i++)
                {
                    long matterId = matterIds[i];
                    var characteristics = new double[characteristicLinkIds.Length];
                    for (int j = 0; j < characteristicLinkIds.Length; j++)
                    {
                        Notation notation = notations[j];


                        Chain sequence = sequences[i][j];
                        for (int l = 0; l < iterationsCount; l++)
                        {
                            for (int k = 0; k < transformationsSequence.Length; k++)
                            {
                                sequence = transformationsSequence[k] == OrderTransformation.Dissimilar ? DissimilarChainFactory.Create(sequence)
                                                                     : HighOrderFactory.Create(sequence, EnumExtensions.GetLink(transformationsSequence[k]));
                            }
                        }

                        int characteristicLinkId = characteristicLinkIds[j];
                        Link link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId);
                        FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId);

                        IFullCalculator calculator = FullCalculatorsFactory.CreateCalculator(characteristic);
                        characteristics[j] = calculator.Calculate(sequence, link);
                    }

                    sequencesCharacteristics[i] = new SequenceCharacteristics
                    {
                        MatterName = mattersNames[matterId],
                        Characteristics = characteristics
                    };
                }

                var characteristicNames = new string[characteristicLinkIds.Length];
                var characteristicsList = new SelectListItem[characteristicLinkIds.Length];
                for (int k = 0; k < characteristicLinkIds.Length; k++)
                {
                    characteristicNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkIds[k], notations[k]);
                    characteristicsList[k] = new SelectListItem
                    {
                        Value = k.ToString(),
                        Text = characteristicNames[k],
                        Selected = false
                    };
                }

                var transformations = transformationsSequence.Select(ts => ts.GetDisplayValue());

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

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