Exemplo n.º 1
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) }
                };
            }));
        }
        /// <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);
        }
Exemplo n.º 4
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);
        }
        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) }
                };
            }));
        }
Exemplo n.º 7
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) }
                };
            }));
        }
        public ActionResult Index(
            long[] matterIds,
            short[] characteristicLinkIds,
            int length,
            int step,
            bool delta,
            bool fourier,
            bool growingWindow,
            bool autocorrelation,
            Notation notation,
            Language?language,
            Translator?translator,
            PauseTreatment?pauseTreatment,
            bool?sequentialTransfer,
            ImageOrderExtractor?trajectory)
        {
            return(CreateTask(() =>
            {
                var characteristicNames = new string[characteristicLinkIds.Length];
                var partNames = new List <string> [matterIds.Length];
                var starts = new List <int> [matterIds.Length];
                var lengthes = new List <int> [matterIds.Length];
                var chains = new Chain[matterIds.Length];
                var mattersCharacteristics = new object[matterIds.Length];

                var calculators = new IFullCalculator[characteristicLinkIds.Length];
                var links = new Link[characteristicLinkIds.Length];
                matterIds = matterIds.OrderBy(m => m).ToArray();
                Dictionary <long, Matter> matters = Cache.GetInstance().Matters.Where(m => matterIds.Contains(m.Id)).ToDictionary(m => m.Id);

                for (int k = 0; k < matterIds.Length; k++)
                {
                    long matterId = matterIds[k];
                    Nature nature = Cache.GetInstance().Matters.Single(m => m.Id == matterId).Nature;

                    long sequenceId = commonSequenceRepository.GetSequenceIds(new[] { matterId },
                                                                              notation,
                                                                              language,
                                                                              translator,
                                                                              pauseTreatment,
                                                                              sequentialTransfer,
                                                                              trajectory).Single();

                    chains[k] = commonSequenceRepository.GetLibiadaChain(sequenceId);
                }

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

                for (int i = 0; i < chains.Length; i++)
                {
                    CutRule cutRule = growingWindow
                            ? (CutRule) new CutRuleWithFixedStart(chains[i].Length, step)
                            : new SimpleCutRule(chains[i].Length, step, length);

                    CutRuleIterator iterator = cutRule.GetIterator();

                    var fragments = new List <Chain>();
                    partNames[i] = new List <string>();
                    starts[i] = new List <int>();
                    lengthes[i] = new List <int>();

                    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(chains[i][start + k]);
                        }

                        fragments.Add(new Chain(fragment));

                        partNames[i].Add(fragment.ToString());
                        starts[i].Add(iterator.GetStartPosition());
                        lengthes[i].Add(fragment.Count);
                    }

                    var fragmentsData = new FragmentData[fragments.Count];
                    for (int k = 0; k < fragments.Count; k++)
                    {
                        var characteristics = new double[calculators.Length];
                        for (int j = 0; j < calculators.Length; j++)
                        {
                            characteristics[j] = calculators[j].Calculate(fragments[k], links[j]);
                        }

                        fragmentsData[k] = new FragmentData(characteristics, fragments[k].ToString(), starts[i][k], fragments[k].Length);
                    }

                    double[][] differenceData = null;
                    double[][] fourierData = null;
                    double[][] autocorrelationData = null;

                    if (delta)
                    {
                        differenceData = CalculateDifference(fragmentsData.Select(f => f.Characteristics).ToArray());
                    }

                    if (fourier)
                    {
                        fourierData = FastFourierTransform.CalculateFastFourierTransform(fragmentsData.Select(f => f.Characteristics).ToArray());
                    }

                    if (autocorrelation)
                    {
                        autocorrelationData = AutoCorrelation.CalculateAutocorrelation(fragmentsData.Select(f => f.Characteristics).ToArray());
                    }

                    mattersCharacteristics[i] = new LocalCharacteristicsData(matters[matterIds[i]].Name,
                                                                             fragmentsData,
                                                                             differenceData,
                                                                             fourierData,
                                                                             autocorrelationData);
                }

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

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

                var result = new Dictionary <string, object>
                {
                    { "characteristics", mattersCharacteristics },
                    { "notationName", notation.GetDisplayValue() },
                    { "starts", starts },
                    { "partNames", partNames },
                    { "lengthes", lengthes },
                    { "characteristicNames", characteristicNames },
                    { "matterIds", matterIds },
                    { "characteristicsList", characteristicsList },
                    { "aligners", EnumHelper.GetSelectList(typeof(Aligner)) },
                    { "distanceCalculators", EnumHelper.GetSelectList(typeof(DistanceCalculator)) },
                    { "aggregators", EnumHelper.GetSelectList(typeof(Aggregator)) }
                };

                return new Dictionary <string, string> {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }
        public ActionResult Index(
            long matterId,
            short characteristicLinkId,
            Notation notation,
            int step,
            int initialLength,
            double accuracy)
        {
            return(CreateTask(() =>
            {
                string characteristicName;
                string mattersName;
                double[] characteristics;
                Chain sequence;
                IFullCalculator calculator;
                Link link;

                using (var db = new LibiadaWebEntities())
                {
                    var commonSequenceRepository = new CommonSequenceRepository(db);
                    mattersName = db.Matter.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);

                CutRule cutRule = new CutRuleWithFixedStart(sequence.Length, step);

                Depth depthCaulc = new Depth();

                CutRuleIterator iter = cutRule.GetIterator();

                var fragments = new List <Chain>();
                var partNames = new List <string>();
                var lengthes = new List <int>();
                var teoreticalDepht = new List <double>();

                while (iter.Next())
                {
                    var fragment = new Chain(iter.GetEndPosition() - iter.GetStartPosition());

                    for (int k = 0; iter.GetStartPosition() + k < iter.GetEndPosition(); k++)
                    {
                        fragment.Set(sequence[iter.GetStartPosition() + k], k);
                    }

                    fragments.Add(fragment);
                    partNames.Add(fragment.ToString());
                    lengthes.Add(fragment.Length);

                    teoreticalDepht.Add(depthCaulc.Calculate(fragment, Link.Start));
                }

                characteristics = new double[fragments.Count];
                for (int k = 0; k < fragments.Count; k++)
                {
                    characteristics[k] = calculator.Calculate(fragments[k], link);
                    // fragmentsData[k] = new FragmentData(characteristics, fragments[k].ToString(), starts[i][k], fragments[k].Length);
                }



                // var predicted = new List<Chain>();

                //int[] startingPart = new int[initialLength];


                Chain predicted = new Chain(initialLength);

                for (int i = 0; i < initialLength; i++)
                {
                    predicted.Set(sequence[i], i);
                }

                Alphabet alphabet = sequence.Alphabet;
                IEnumerator enumerator = alphabet.GetEnumerator();
                var sequencePredictionResult = new List <SequencePredictionData>();

                for (int i = initialLength; i < sequence.Length; i++)
                {
                    Chain temp = new Chain(i + 1);
                    for (int j = 0; j < i; j++)
                    {
                        temp.Set(predicted[j], j);
                    }
                    predicted = temp;
                    double depth = 0;

                    /* do
                     * {
                     *   predicted.Set((IBaseObject)enumerator.Current, i);
                     *   depth = depthCaulc.Calculate(predicted, Link.Start);
                     *   if (System.Math.Abs(depth - teoreticalDepht.ElementAt(i)) <= accuracy)
                     *   {
                     *       break;
                     *   }
                     * } while (enumerator.MoveNext());*/
                    IBaseObject predictedLetter = null;
                    foreach (IBaseObject letter in alphabet)
                    {
                        predicted.Set(letter, i);
                        depth = depthCaulc.Calculate(predicted, Link.Start);
                        if (System.Math.Abs(depth - teoreticalDepht.ElementAt(i)) <= accuracy)
                        {
                            predictedLetter = letter;
                            break;
                        }
                    }



                    sequencePredictionResult.Add(new SequencePredictionData
                    {
                        Fragment = fragments.ElementAt(i).ToString(),
                        Predicted = /*enumerator.Current.ToString()*/ predicted.ToString(),
                        ActualCharacteristic = depth,
                        TheoreticalCharacteristic = teoreticalDepht.ElementAt(i)
                    });
                }

                /*int equal = 0;
                 * for (int i = initialLength; i < sequence.Length; i++)
                 * {
                 *  if (sequence[i] == predicted[i])
                 *  {
                 *      equal++;
                 *  }
                 * }
                 *
                 *
                 * double accuracyPercentage = equal / (sequence.Length - initialLength);*/


                // TODO: sequence prediction


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

                return new Dictionary <string, object>
                {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }
Exemplo n.º 10
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) }
                };
            }));
        }
Exemplo n.º 11
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) }
                };
            }));
        }
Exemplo n.º 12
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) }
                };
            }));
        }
        /// <summary>
        /// Calculates subsequences characteristics.
        /// </summary>
        /// <param name="characteristicIds">
        /// The ids of characteristic types, arrangement types and links as <see cref="FullCharacteristicLink"/>.
        /// </param>
        /// <param name="features">
        /// The  features ids of subsequences to extract.
        /// </param>
        /// <param name="parentSequenceId">
        /// The parent sequence id.
        /// </param>
        /// <param name="filters">
        /// Textual search filters for subsequences products.
        /// </param>
        /// <returns>
        /// The <see cref="T:SubsequenceData[]"/> .
        /// </returns>
        public static SubsequenceData[] CalculateSubsequencesCharacteristics(
            short[] characteristicIds,
            Feature[] features,
            long parentSequenceId,
            string[] filters = null)
        {
            Dictionary <long, Chain> sequences;

            long[]            subsequenceIds;
            SubsequenceData[] subsequenceData;
            Dictionary <long, Dictionary <short, double> > characteristics;
            var calculators        = new IFullCalculator[characteristicIds.Length];
            var links              = new Link[characteristicIds.Length];
            var newCharacteristics = new List <CharacteristicValue>();

            // creating local context to avoid memory overflow due to possibly big cache of characteristics
            using (var db = new LibiadaWebEntities())
            {
                var subsequenceExtractor = new SubsequenceExtractor(db);

                Subsequence[] subsequences = filters == null?
                                             subsequenceExtractor.GetSubsequences(parentSequenceId, features) :
                                                 subsequenceExtractor.GetSubsequences(parentSequenceId, features, filters);

                subsequenceData = subsequences.Select(s => new SubsequenceData(s)).ToArray();

                // converting to libiada sequences
                subsequenceIds = subsequences.Select(s => s.Id).ToArray();



                characteristics = db.CharacteristicValue
                                  .Where(c => characteristicIds.Contains(c.CharacteristicLinkId) && subsequenceIds.Contains(c.SequenceId))
                                  .ToArray()
                                  .GroupBy(c => c.SequenceId)
                                  .ToDictionary(c => c.Key, c => c.ToDictionary(ct => ct.CharacteristicLinkId, ct => ct.Value));
                if (characteristics.Count == subsequences.Length && characteristics.All(c => c.Value.Count == characteristicIds.Length))
                {
                    sequences = new Dictionary <long, Chain>();
                }
                else
                {
                    sequences = subsequenceExtractor.GetSubsequencesSequences(subsequences);
                }
            }

            var characteristicTypeLinkRepository = FullCharacteristicRepository.Instance;

            for (int k = 0; k < characteristicIds.Length; k++)
            {
                short characteristicLinkId        = characteristicIds[k];
                FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkId);
                calculators[k] = FullCalculatorsFactory.CreateCalculator(characteristic);
                links[k]       = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkId);
            }

            // cycle through subsequences
            for (int i = 0; i < subsequenceIds.Length; i++)
            {
                characteristics.TryGetValue(subsequenceIds[i], out Dictionary <short, double> sequenceDbCharacteristics);
                sequenceDbCharacteristics = sequenceDbCharacteristics ?? new Dictionary <short, double>();
                var values = new double[calculators.Length];

                // cycle through characteristics and notations
                for (int j = 0; j < calculators.Length; j++)
                {
                    short characteristicLinkId = characteristicIds[j];
                    if (!sequenceDbCharacteristics.TryGetValue(characteristicLinkId, out values[j]))
                    {
                        values[j] = calculators[j].Calculate(sequences[subsequenceIds[i]], links[j]);
                        var currentCharacteristic = new CharacteristicValue
                        {
                            SequenceId           = subsequenceIds[i],
                            CharacteristicLinkId = characteristicLinkId,
                            Value = values[j]
                        };

                        newCharacteristics.Add(currentCharacteristic);
                    }
                }

                subsequenceData[i].CharacteristicsValues = values;
            }

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

            return(subsequenceData);
        }
        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) }
                };
            }));
        }