public GetCharacteristicType ( int characteristicTypeLinkId ) : CharacteristicType | ||
characteristicTypeLinkId | int | /// The characteristic type link id. /// |
return | CharacteristicType |
public ActionResult Index(long[] matterIds, int characteristicTypeLinkId, int[] characteristicTypeLinkIds, int[] featureIds) { return Action(() => { var matterNames = new string[matterIds.Length]; var remoteIds = new string[matterIds.Length]; var subsequencesCharacteristicsNames = new string[characteristicTypeLinkIds.Length]; var calculators = new IFullCalculator[characteristicTypeLinkIds.Length]; var links = new Link[characteristicTypeLinkIds.Length]; var subsequencesCharacteristicsList = new SelectListItem[characteristicTypeLinkIds.Length]; var attributeValues = new List<AttributeValue>(); IEnumerable<SelectListItem> featuresSelectList; Chain[] chains; long[] parentSequenceIds; string sequenceCharacteristicName; IFullCalculator calculator; Link link; using (var db = new LibiadaWebEntities()) { var featureRepository = new FeatureRepository(db); featuresSelectList = featureRepository.GetFeaturesById(featureIds).Select(f => new SelectListItem { Value = f.Id.ToString(), Text = f.Name, Selected = true }); var parentSequences = db.DnaSequence.Include(s => s.Matter) .Where(s => s.NotationId == Aliases.Notation.Nucleotide && matterIds.Contains(s.MatterId)) .Select(s => new { s.Id, MatterName = s.Matter.Name, s.RemoteId }) .ToDictionary(s => s.Id); parentSequenceIds = parentSequences.Keys.ToArray(); for (int n = 0; n < parentSequenceIds.Length; n++) { matterNames[n] = parentSequences[parentSequenceIds[n]].MatterName; remoteIds[n] = parentSequences[parentSequenceIds[n]].RemoteId; } var commonSequenceRepository = new CommonSequenceRepository(db); chains = commonSequenceRepository.GetNucleotideChains(matterIds); var characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db); sequenceCharacteristicName = characteristicTypeLinkRepository.GetCharacteristicName(characteristicTypeLinkId); calculator = CalculatorsFactory.CreateFullCalculator(characteristicTypeLinkRepository.GetCharacteristicType(characteristicTypeLinkId).ClassName); link = characteristicTypeLinkRepository.GetLibiadaLink(characteristicTypeLinkId); for (int k = 0; k < characteristicTypeLinkIds.Length; k++) { links[k] = characteristicTypeLinkRepository.GetLibiadaLink(characteristicTypeLinkIds[k]); string className = characteristicTypeLinkRepository.GetCharacteristicType(characteristicTypeLinkIds[k]).ClassName; calculators[k] = CalculatorsFactory.CreateFullCalculator(className); subsequencesCharacteristicsNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicTypeLinkIds[k]); subsequencesCharacteristicsList[k] = new SelectListItem { Value = k.ToString(), Text = subsequencesCharacteristicsNames[k], Selected = false }; } } var characteristics = SequencesCharacteristicsCalculator.Calculate(chains, calculator, link, characteristicTypeLinkId); var sequenceData = new SequenceData[parentSequenceIds.Length]; for (int i = 0; i < parentSequenceIds.Length; i++) { // all subsequence calculations var subsequencesData = SubsequencesCharacteristicsCalculator.CalculateSubsequencesCharacteristics( characteristicTypeLinkIds, featureIds, parentSequenceIds[i], calculators, links, attributeValues); sequenceData[i] = new SequenceData(matterIds[i], matterNames[i], remoteIds[i], characteristics[i], subsequencesData); } // sorting organisms by their characteristic sequenceData = sequenceData.OrderBy(r => r.Characteristic).ToArray(); var resultData = new Dictionary<string, object> { { "result", sequenceData }, { "subsequencesCharacteristicsNames", subsequencesCharacteristicsNames }, { "subsequencesCharacteristicsList", subsequencesCharacteristicsList }, { "sequenceCharacteristicName", sequenceCharacteristicName }, { "features", featuresSelectList.ToDictionary(f => f.Value) }, { "attributes", EnumExtensions.ToArray<Attribute>().ToDictionary(a => (byte)a, a => a.GetDisplayValue()) }, { "attributeValues", attributeValues.Select(sa => new { attribute = sa.AttributeId, value = sa.Value }) } }; return new Dictionary<string, object> { { "data", JsonConvert.SerializeObject(resultData) } }; }); }
public ActionResult Index( int[] transformationLinkIds, int[] transformationIds, int iterationsCount, int[] characteristicTypeLinkIds, string[] customSequences, bool localFile, HttpPostedFileBase[] file) { return Action(() => { var db = new LibiadaWebEntities(); var characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db); int sequencesCount = localFile ? Request.Files.Count : customSequences.Length; var sequences = new string[sequencesCount]; var names = new string[sequencesCount]; for (int i = 0; i < sequencesCount; i++) { if (localFile) { var sequenceStream = FileHelper.GetFileStream(file[i]); var fastaSequence = NcbiHelper.GetFastaSequence(sequenceStream); sequences[i] = fastaSequence.ConvertToString(); names[i] = fastaSequence.ID; } else { sequences[i] = customSequences[i]; names[i] = "Custom sequence " + (i + 1) + ". Length: " + customSequences[i].Length; } } var characteristics = new double[sequences.Length, characteristicTypeLinkIds.Length]; for (int j = 0; j < sequences.Length; j++) { for (int k = 0; k < characteristicTypeLinkIds.Length; k++) { var sequence = new Chain(sequences[j]); for (int l = 0; l < iterationsCount; l++) { for (int w = 0; w < transformationIds.Length; w++) { if (transformationIds[w] == 1) { sequence = DissimilarChainFactory.Create(sequence); } else { sequence = HighOrderFactory.Create(sequence, (Link)transformationLinkIds[w]); } } } var link = characteristicTypeLinkRepository.GetLibiadaLink(characteristicTypeLinkIds[k]); string className = characteristicTypeLinkRepository.GetCharacteristicType(characteristicTypeLinkIds[k]).ClassName; var calculator = CalculatorsFactory.CreateFullCalculator(className); characteristics[j, k] = calculator.Calculate(sequence, link); } } var characteristicNames = characteristicTypeLinkIds.Select(c => characteristicTypeLinkRepository.GetCharacteristicName(c)).ToArray(); var characteristicsList = new SelectListItem[characteristicTypeLinkIds.Length]; for (int i = 0; i < characteristicNames.Length; i++) { characteristicsList[i] = new SelectListItem { Value = i.ToString(), Text = characteristicNames[i], Selected = false }; } var transformations = new Dictionary<int, string>(); for (int i = 0; i < transformationIds.Length; i++) { var link = ((Link)transformationLinkIds[i]).GetDisplayValue(); transformations.Add(i, transformationIds[i] == 1 ? "dissimilar" : "higher order " + link); } var result = new Dictionary<string, object> { { "characteristics", characteristics }, { "characteristicNames", characteristicNames }, { "characteristicsList", characteristicsList }, { "transformationsList", transformations }, { "iterationsCount", iterationsCount } }; return new Dictionary<string, object> { { "data", JsonConvert.SerializeObject(result) } }; }); }
public ActionResult Index( long[] matterIds, int[] transformationLinkIds, int[] transformationIds, int iterationsCount, int[] characteristicTypeLinkIds, int[] notationIds, int[] languageIds, int?[] translatorIds) { return Action(() => { var db = new LibiadaWebEntities(); var characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db); var commonSequenceRepository = new CommonSequenceRepository(db); var mattersCharacteristics = new object[matterIds.Length]; matterIds = matterIds.OrderBy(m => m).ToArray(); var matters = db.Matter.Where(m => matterIds.Contains(m.Id)).ToDictionary(m => m.Id); for (int i = 0; i < matterIds.Length; i++) { var matterId = matterIds[i]; var characteristics = new List<double>(); for (int k = 0; k < notationIds.Length; k++) { int notationId = notationIds[k]; long sequenceId; if (matters[matterId].Nature == Nature.Literature) { int languageId = languageIds[k]; int? translatorId = translatorIds[k]; sequenceId = db.LiteratureSequence.Single(l => l.MatterId == matterId && l.NotationId == notationId && l.LanguageId == languageId && ((translatorId == null && l.TranslatorId == null) || (translatorId == l.TranslatorId))).Id; } else { sequenceId = db.CommonSequence.Single(c => c.MatterId == matterId && c.NotationId == notationId).Id; } var sequence = commonSequenceRepository.ToLibiadaChain(sequenceId); for (int l = 0; l < iterationsCount; l++) { for (int j = 0; j < transformationIds.Length; j++) { if (transformationIds[j] == 1) { sequence = DissimilarChainFactory.Create(sequence); } else { sequence = HighOrderFactory.Create(sequence, (LibiadaCore.Core.Link)transformationLinkIds[j]); } } } int characteristicTypeLinkId = characteristicTypeLinkIds[k]; var link = characteristicTypeLinkRepository.GetLibiadaLink(characteristicTypeLinkId); string className = characteristicTypeLinkRepository.GetCharacteristicType(characteristicTypeLinkId).ClassName; IFullCalculator calculator = CalculatorsFactory.CreateFullCalculator(className); characteristics.Add(calculator.Calculate(sequence, link)); } mattersCharacteristics[i] = new { matterName = matters[matterId].Name, characteristics }; } var characteristicNames = new string[characteristicTypeLinkIds.Length]; for (int k = 0; k < characteristicTypeLinkIds.Length; k++) { characteristicNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicTypeLinkIds[k], notationIds[k]); } var characteristicsList = new SelectListItem[characteristicTypeLinkIds.Length]; for (int i = 0; i < characteristicNames.Length; i++) { characteristicsList[i] = new SelectListItem { Value = i.ToString(), Text = characteristicNames[i], Selected = false }; } var transformations = new Dictionary<int, string>(); for (int i = 0; i < transformationIds.Length; i++) { var link = ((LibiadaCore.Core.Link)transformationLinkIds[i]).GetDisplayValue(); transformations.Add(i, transformationIds[i] == 1 ? "dissimilar" : "higher order " + link); } var result = new Dictionary<string, object> { { "characteristics", mattersCharacteristics }, { "characteristicNames", characteristicNames }, { "characteristicsList", characteristicsList }, { "transformationsList", transformations }, { "iterationsCount", iterationsCount } }; return new Dictionary<string, object> { { "data", JsonConvert.SerializeObject(result) } }; }); }
public ActionResult Index( long[] matterIds, int characteristicTypeLinkId, int[] featureIds, string maxPercentageDifference, double characteristicValueFrom, double characteristicValueTo) { return Action(() => { Dictionary<int, string> features; var attributeValues = new List<AttributeValue>(); var characteristics = new SubsequenceData[matterIds.Length][]; string characteristicName; long[] parentSequenceIds; var matterNames = new string[matterIds.Length]; IFullCalculator calculator; Link link; int mattersCount = matterIds.Length; int[] subsequencesCount = new int[mattersCount]; using (var db = new LibiadaWebEntities()) { var featureRepository = new FeatureRepository(db); features = featureRepository.Features.ToDictionary(f => f.Id, f => f.Name); var parentSequences = db.DnaSequence.Include(s => s.Matter) .Where(s => s.NotationId == Aliases.Notation.Nucleotide && matterIds.Contains(s.MatterId)) .Select(s => new { s.Id, MatterName = s.Matter.Name }) .ToDictionary(s => s.Id); parentSequenceIds = parentSequences.Keys.ToArray(); for (int n = 0; n < parentSequenceIds.Length; n++) { matterNames[n] = parentSequences[parentSequenceIds[n]].MatterName; } var characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db); characteristicName = characteristicTypeLinkRepository.GetCharacteristicName(characteristicTypeLinkId); string className = characteristicTypeLinkRepository.GetCharacteristicType(characteristicTypeLinkId).ClassName; calculator = CalculatorsFactory.CreateFullCalculator(className); link = characteristicTypeLinkRepository.GetLibiadaLink(characteristicTypeLinkId); } // cycle through matters; first level of characteristics array for (int i = 0; i < parentSequenceIds.Length; i++) { var subsequencesData = SubsequencesCharacteristicsCalculator.CalculateSubsequencesCharacteristics( new[] { characteristicTypeLinkId }, featureIds, parentSequenceIds[i], new[] { calculator }, new[] { link }, attributeValues); subsequencesCount[i] = subsequencesData.Length; subsequencesData = subsequencesData.Where(c => (characteristicValueFrom == 0 && characteristicValueTo == 0) || (c.CharacteristicsValues[0] >= characteristicValueFrom && c.CharacteristicsValues[0] <= characteristicValueTo)). OrderBy(c => c.CharacteristicsValues[0]).ToArray(); characteristics[i] = subsequencesData; } double decimalDifference = double.Parse(maxPercentageDifference, CultureInfo.InvariantCulture) / 100; var similarities = new object[mattersCount, mattersCount]; var equalElements = new List<SubsequenceComparisonData>(); int comparisonNumber = 0; for (int i = 0; i < characteristics.Length; i++) { for (int j = 0; j < characteristics.Length; j++) { comparisonNumber++; double similarSequencesCharacteristicValueFirst = 0; var similarSequencesCharacteristicValueSecond = new Dictionary<int, double>(); double similarFirstSequencesCharacteristicValue = 0; double similarSecondSequencesCharacteristicValue = 0; int secondArrayStartPosition = 0; double differenceSum = 0; int equalElementsCountFromFirst = 0; var equalElementsCountFromSecond = new Dictionary<int, int>(); bool equalFound = false; int equalPairsCount = 0; for (int k = 0; k < characteristics[i].Length; k++) { double first = characteristics[i][k].CharacteristicsValues[0]; for (int l = secondArrayStartPosition; l < characteristics[j].Length; l++) { double second = characteristics[j][l].CharacteristicsValues[0]; double difference = calculateAverageDifference(first, second); bool nextElementInSecondArrayIsEqual = false; if (l < characteristics[j].Length - 1) { nextElementInSecondArrayIsEqual = calculateAverageDifference(second, characteristics[j][l + 1].CharacteristicsValues[0]) <= decimalDifference; } if (difference <= decimalDifference) { equalFound = true; equalPairsCount++; if (!equalElementsCountFromSecond.ContainsKey(l)) { equalElementsCountFromSecond.Add(l, 1); differenceSum += difference; } if (!similarSequencesCharacteristicValueSecond.ContainsKey(l)) { similarSequencesCharacteristicValueSecond.Add(l, second); } if (i != j) { equalElements.Add(new SubsequenceComparisonData { Difference = difference, FirstMatterId = i, SecondMatterId = j, FirstSubsequenceId = k, SecondSubsequenceId = l, }); } similarFirstSequencesCharacteristicValue += first; similarSecondSequencesCharacteristicValue += second; if (!nextElementInSecondArrayIsEqual) { break; } } else if (second < first) { secondArrayStartPosition++; } } if (equalFound) { equalElementsCountFromFirst++; similarSequencesCharacteristicValueFirst += first; } } double differenceSecondFinal = equalElementsCountFromSecond.Sum(s => s.Value); double differenceFinal = equalElementsCountFromFirst < differenceSecondFinal ? equalElementsCountFromFirst * 2d : differenceSecondFinal * 2d; double formula1 = differenceFinal / (subsequencesCount[i] + subsequencesCount[j]); double formula2 = 0; if (equalPairsCount != 0 && formula1 != 0) { formula2 = (differenceSum / equalPairsCount) / formula1; } double similarSequencesCharacteristicValueSecondFinal = similarSequencesCharacteristicValueSecond.Sum(s => s.Value); double similarSequencesCharacteristicValue = similarSequencesCharacteristicValueFirst < similarSequencesCharacteristicValueSecondFinal ? similarSequencesCharacteristicValueFirst * 2d : similarSequencesCharacteristicValueSecondFinal * 2d; double formula3 = similarSequencesCharacteristicValue * 100d / (characteristics[i].Sum(c => c.CharacteristicsValues[0]) + characteristics[j].Sum(c => c.CharacteristicsValues[0])); similarities[i, j] = new { formula1 = Math.Round(formula1 * 100d, 3), formula2 = Math.Round(formula2, 3), formula3 = Math.Round(formula3, 3) }; } } var result = new Dictionary<string, object> { { "mattersNames", matterNames }, { "characteristicName", characteristicName }, { "similarities", similarities }, { "characteristics", characteristics }, { "equalElements", equalElements.OrderBy(e => e.Difference).ToList() }, { "features", features }, { "attributeValues", attributeValues.Select(sa => new { attribute = sa.AttributeId, value = sa.Value }) }, { "attributes", EnumExtensions.ToArray<LibiadaWeb.Attribute>().ToDictionary(a => (byte)a, a => a.GetDisplayValue()) } }; return new Dictionary<string, object> { { "data", JsonConvert.SerializeObject(result) } }; }); }
public ActionResult Index(long[] matterIds, int[] characteristicTypeLinkIds, int[] featureIds, string[] filters) { return Action(() => { Dictionary<int, string> features; var attributeValues = new List<AttributeValue>(); var characteristics = new Dictionary<string, SubsequenceData[]>(matterIds.Length); var matterNames = new string[matterIds.Length]; var characteristicNames = new string[characteristicTypeLinkIds.Length]; long[] parentSequenceIds; var calculators = new IFullCalculator[characteristicTypeLinkIds.Length]; var links = new LibiadaCore.Core.Link[characteristicTypeLinkIds.Length]; using (var db = new LibiadaWebEntities()) { var featureRepository = new FeatureRepository(db); features = featureRepository.Features.ToDictionary(f => f.Id, f => f.Name); var parentSequences = db.DnaSequence.Include(s => s.Matter) .Where(s => s.NotationId == Aliases.Notation.Nucleotide && matterIds.Contains(s.MatterId)) .Select(s => new { s.Id, MatterName = s.Matter.Name }) .ToDictionary(s => s.Id); parentSequenceIds = parentSequences.Keys.ToArray(); for (int n = 0; n < parentSequenceIds.Length; n++) { matterNames[n] = parentSequences[parentSequenceIds[n]].MatterName; } var characteristicTypeLinkRepository = new CharacteristicTypeLinkRepository(db); for (int k = 0; k < characteristicTypeLinkIds.Length; k++) { var characteristicTypeLinkId = characteristicTypeLinkIds[k]; characteristicNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicTypeLinkId); string className = characteristicTypeLinkRepository.GetCharacteristicType(characteristicTypeLinkId).ClassName; calculators[k] = CalculatorsFactory.CreateFullCalculator(className); links[k] = characteristicTypeLinkRepository.GetLibiadaLink(characteristicTypeLinkId); } } // cycle through matters; first level of characteristics array for (int i = 0; i < parentSequenceIds.Length; i++) { // all subsequence calculations var subsequencesData = SubsequencesCharacteristicsCalculator.CalculateSubsequencesCharacteristics( characteristicTypeLinkIds, featureIds, parentSequenceIds[i], calculators, links, attributeValues, filters); subsequencesData = subsequencesData.OrderByDescending(s => s.CharacteristicsValues[0]).ToArray(); characteristics[matterNames[i]] = subsequencesData; } return new Dictionary<string, object> { { "characteristics", characteristics }, { "matterNames", matterNames }, { "characteristicNames", characteristicNames }, { "features", features }, { "attributes", EnumExtensions.ToArray<LibiadaWeb.Attribute>().ToDictionary(a => (byte)a, a => a.GetDisplayValue()) }, { "attributeValues", attributeValues.Select(sa => new { attribute = (byte)sa.AttributeId, value = sa.Value }) } }; }); }