public ActionResult Index(long[] matterIds, short[] characteristicLinkIds, Feature[] features) { return(CreateTask(() => { var sequencesData = new SequenceData[matterIds.Length]; long[] parentSequenceIds; var matterNames = new string[matterIds.Length]; var remoteIds = new string[matterIds.Length]; var subsequencesCharacteristicsNames = new string[characteristicLinkIds.Length]; var subsequencesCharacteristicsList = new SelectListItem[characteristicLinkIds.Length]; using (var db = new LibiadaWebEntities()) { var parentSequences = db.DnaSequence.Include(s => s.Matter) .Where(s => s.Notation == Notation.Nucleotides && matterIds.Contains(s.MatterId)) .Select(s => new { s.Id, MatterName = s.Matter.Name, s.RemoteId }) .ToDictionary(s => s.Id); parentSequenceIds = parentSequences.Keys.ToArray(); for (int n = 0; n < parentSequenceIds.Length; n++) { matterNames[n] = parentSequences[parentSequenceIds[n]].MatterName; remoteIds[n] = parentSequences[parentSequenceIds[n]].RemoteId; } } FullCharacteristicRepository characteristicTypeLinkRepository = FullCharacteristicRepository.Instance; for (int k = 0; k < characteristicLinkIds.Length; k++) { subsequencesCharacteristicsNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkIds[k]); subsequencesCharacteristicsList[k] = new SelectListItem { Value = k.ToString(), Text = subsequencesCharacteristicsNames[k], Selected = false }; } // TODO: Maybe AttributesValueCache should be created in the Subsequences calculator var attributeValuesCache = new AttributeValueCacheManager(); for (int i = 0; i < parentSequenceIds.Length; i++) { var subsequencesData = CalculateSubsequencesCharacteristics(characteristicLinkIds, features, parentSequenceIds[i]); attributeValuesCache.FillAttributeValues(subsequencesData); sequencesData[i] = new SequenceData(matterIds[i], matterNames[i], remoteIds[i], default, subsequencesData);
public ActionResult Index( long[] matterIds, short characteristicLinkId, Notation notation, Feature[] features, string maxDifference, string excludeType) { return(CreateTask(() => { if (matterIds.Length != 2) { throw new ArgumentException("Count of selected matters must be 2.", nameof(matterIds)); } long firstMatterId = matterIds[0]; long firstParentSequenceId = db.CommonSequence.Single(c => c.MatterId == firstMatterId && c.Notation == notation).Id; SubsequenceData[] firstSequenceSubsequences = CalculateSubsequencesCharacteristics( new[] { characteristicLinkId }, features, firstParentSequenceId); List <double> firstSequenceCharacteristics = firstSequenceSubsequences.Select(s => s.CharacteristicsValues[0]).ToList(); Dictionary <long, AttributeValue[]> firstDbSubsequencesAttributes = sequenceAttributeRepository.GetAttributes(firstSequenceSubsequences.Select(s => s.Id)); var firstSequenceAttributes = new List <AttributeValue[]>(); foreach (SubsequenceData subsequence in firstSequenceSubsequences) { firstDbSubsequencesAttributes.TryGetValue(subsequence.Id, out AttributeValue[] attributes); attributes = attributes ?? new AttributeValue[0]; firstSequenceAttributes.Add(attributes); } long secondMatterId = matterIds[1]; long secondParentSequenceId = db.CommonSequence.Single(c => c.MatterId == secondMatterId && c.Notation == notation).Id; SubsequenceData[] secondSequenceSubsequences = CalculateSubsequencesCharacteristics( new[] { characteristicLinkId }, features, secondParentSequenceId); List <double> secondSequenceCharacteristics = secondSequenceSubsequences.Select(s => s.CharacteristicsValues[0]).ToList(); Dictionary <long, AttributeValue[]> secondDbSubsequencesAttributes = sequenceAttributeRepository.GetAttributes(secondSequenceSubsequences.Select(s => s.Id)); var secondSequenceAttributes = new List <AttributeValue[]>(); foreach (SubsequenceData subsequence in secondSequenceSubsequences) { secondDbSubsequencesAttributes.TryGetValue(subsequence.Id, out AttributeValue[] attributes); attributes = attributes ?? new AttributeValue[0]; secondSequenceAttributes.Add(attributes); } double difference = double.Parse(maxDifference, CultureInfo.InvariantCulture); var similarSubsequences = new List <(int, int)>(); for (int i = 0; i < firstSequenceCharacteristics.Count; i++) { for (int j = 0; j < secondSequenceCharacteristics.Count; j++) { if (Math.Abs(firstSequenceCharacteristics[i] - secondSequenceCharacteristics[j]) <= difference) { similarSubsequences.Add((i, j)); if (excludeType == "Exclude") { firstSequenceCharacteristics[i] = double.NaN; secondSequenceCharacteristics[j] = double.NaN; } } } } var characteristicName = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkId, notation); var similarity = similarSubsequences.Count * 200d / (firstSequenceSubsequences.Length + secondSequenceSubsequences.Length); var firstSequenceSimilarity = similarSubsequences.Count * 100d / firstSequenceSubsequences.Length; var secondSequenceSimilarity = similarSubsequences.Count * 100d / secondSequenceSubsequences.Length; var result = new Dictionary <string, object> { { "firstSequenceName", Cache.GetInstance().Matters.Single(m => m.Id == firstMatterId).Name }, { "secondSequenceName", Cache.GetInstance().Matters.Single(m => m.Id == secondMatterId).Name }, { "characteristicName", characteristicName }, { "similarSubsequences", similarSubsequences }, { "similarity", similarity }, { "features", features.ToDictionary(f => (byte)f, f => f.GetDisplayValue()) }, { "firstSequenceSimilarity", firstSequenceSimilarity }, { "secondSequenceSimilarity", secondSequenceSimilarity }, { "firstSequenceSubsequences", firstSequenceSubsequences }, { "secondSequenceSubsequences", secondSequenceSubsequences }, { "firstSequenceAttributes", firstSequenceAttributes }, { "secondSequenceAttributes", secondSequenceAttributes } }; string json = JsonConvert.SerializeObject(result); return new Dictionary <string, string> { { "data", json } }; })); }
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) } }; })); }
public ActionResult Index( long[] matterIds, short characteristicLinkId, short subsequencesCharacteristicLinkId, Feature[] features, string maxPercentageDifference, string[] filters) { return(CreateTask(() => { double percentageDifference = double.Parse(maxPercentageDifference, CultureInfo.InvariantCulture) / 100; var attributeValuesCache = new AttributeValueCacheManager(); var characteristics = new SubsequenceData[matterIds.Length][]; long[] parentSequenceIds; var matterNames = new string[matterIds.Length]; int mattersCount = matterIds.Length; Dictionary <string, object> characteristicsTypesData; using (var db = new LibiadaWebEntities()) { // Sequences characteristic var geneticSequenceRepository = new GeneticSequenceRepository(db); long[] chains = geneticSequenceRepository.GetNucleotideSequenceIds(matterIds); // Sequences characteristic matterIds = OrderMatterIds(matterIds, characteristicLinkId, chains); // Subsequences characteristics var parentSequences = db.DnaSequence.Include(s => s.Matter) .Where(s => s.Notation == Notation.Nucleotides && matterIds.Contains(s.MatterId)) .Select(s => new { s.Id, s.MatterId, MatterName = s.Matter.Name }) .ToDictionary(s => s.Id); parentSequenceIds = parentSequences .OrderBy(ps => Array.IndexOf(matterIds, ps.Value.MatterId)) .Select(ps => ps.Key) .ToArray(); for (int n = 0; n < parentSequenceIds.Length; n++) { matterNames[n] = parentSequences[parentSequenceIds[n]].MatterName; } var viewDataHelper = new ViewDataHelper(db); characteristicsTypesData = viewDataHelper.GetCharacteristicsData(CharacteristicCategory.Full); } FullCharacteristicRepository fullCharacteristicRepository = FullCharacteristicRepository.Instance; string sequenceCharacteristicName = fullCharacteristicRepository.GetCharacteristicName(characteristicLinkId); string characteristicName = fullCharacteristicRepository.GetCharacteristicName(subsequencesCharacteristicLinkId); var characteristicValueSubsequences = new Dictionary <double, List <(int matterIndex, int subsequenceIndex)> >(); // cycle through matters for (int i = 0; i < mattersCount; i++) { SubsequenceData[] subsequencesData = CalculateSubsequencesCharacteristics( new[] { subsequencesCharacteristicLinkId }, features, parentSequenceIds[i], filters); characteristics[i] = subsequencesData; attributeValuesCache.FillAttributeValues(subsequencesData); for (int j = 0; j < subsequencesData.Length; j++) { double value = subsequencesData[j].CharacteristicsValues[0]; if (characteristicValueSubsequences.TryGetValue(value, out List <(int matterIndex, int subsequenceIndex)> matterAndSubsequenceIdsList)) { matterAndSubsequenceIdsList.Add((i, j)); }
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[] matterIds, short characteristicLinkId, Notation notation, Feature[] features, string validationType, bool cyclicShift, bool sort) { return(CreateTask(() => { if (matterIds.Length != 2) { throw new ArgumentException("Count of selected matters must be 2.", nameof(matterIds)); } string firstMatterName; string secondMatterName; long firstParentId; long secondParentId; using (var db = new LibiadaWebEntities()) { long firstMatterId = matterIds[0]; firstMatterName = Cache.GetInstance().Matters.Single(m => m.Id == firstMatterId).Name; firstParentId = db.CommonSequence.Single(c => c.MatterId == firstMatterId && c.Notation == notation).Id; long secondMatterId = matterIds[1]; secondMatterName = Cache.GetInstance().Matters.Single(m => m.Id == secondMatterId).Name; secondParentId = db.CommonSequence.Single(c => c.MatterId == secondMatterId && c.Notation == notation).Id; } double[] firstSequenceCharacteristics = CalculateSubsequencesCharacteristics(firstParentId, characteristicLinkId, features); double[] secondSequenceCharacteristics = CalculateSubsequencesCharacteristics(secondParentId, characteristicLinkId, features); if (sort) { firstSequenceCharacteristics = firstSequenceCharacteristics.OrderByDescending(v => v).ToArray(); secondSequenceCharacteristics = secondSequenceCharacteristics.OrderByDescending(v => v).ToArray(); } List <double> longer; List <double> shorter; if (firstSequenceCharacteristics.Length >= secondSequenceCharacteristics.Length) { longer = firstSequenceCharacteristics.ToList(); shorter = secondSequenceCharacteristics.ToList(); } else { longer = secondSequenceCharacteristics.ToList(); shorter = firstSequenceCharacteristics.ToList(); } if (!cyclicShift) { int count = longer.Count; for (int i = 0; i < count; i++) { longer.Add(0); } } var distanceCalculator = GetDistanceCalculator(validationType); var distances = new List <double>(); int optimalRotation = CalculateMeasureForRotation(longer, shorter, distances, distanceCalculator); string characteristicName = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkId, notation); var result = new Dictionary <string, object> { { "firstSequenceName", firstMatterName }, { "secondSequenceName", secondMatterName }, { "characteristicName", characteristicName }, { "features", features.ConvertAll(p => p.GetDisplayValue()) }, { "optimalRotation", optimalRotation }, { "distances", distances.Select(el => new { Value = el }) }, { "validationType", validationType }, { "cyclicShift", cyclicShift }, { "sort", sort } }; return new Dictionary <string, string> { { "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) } }; })); }