private async Task AssignToChannel(OligoSynthesisActivity currentSynthesisActivity, IEnumerable <OligoSynthesisActivity> otherSynthesisActivities) { IEnumerable <ChannelConfigurationItemViewModel> allChannels = await channelConfigurationService.GetChannelConfigurations(); if (allChannels.Any(x => x.ChannelNumber == currentSynthesisActivity.ChannelNumber && currentSynthesisActivity.DNASequence.StartsWith(x.StartNucleotide))) { return; } currentSynthesisActivity.ChannelNumber = 0; IEnumerable <ChannelConfigurationItemViewModel> availableChannels = allChannels.Where(x => currentSynthesisActivity.DNASequence.StartsWith(x.StartNucleotide)); foreach (ChannelConfigurationItemViewModel availableChannelConfiguration in availableChannels) { if (otherSynthesisActivities.All(x => x.ChannelNumber != availableChannelConfiguration.ChannelNumber)) { currentSynthesisActivity.ChannelNumber = availableChannelConfiguration.ChannelNumber; currentSynthesisActivity.ChannelApiFunction = unitOfWork.GetById <HardwareFunction>(availableChannelConfiguration.HardwareFunction.Id); } } if (currentSynthesisActivity.ChannelNumber < 1) { throw new Exception(string.Format(AppResources.ChannelNotConfigured, currentSynthesisActivity.DNASequence[0])); } }
public OligoSynthesisActivity CreateOligoSynthesisActivity() { OligoSynthesisActivity oligoSynthesisActivity = new OligoSynthesisActivity() { DNASequence = "GG", OligoSynthesisProcess = CreateOligoSynthesisProcess(), SynthesisCycle = CreateSynthesisCycle(), ChannelApiFunction = CreateHardwareFunction(), }; return(GetOrCreate(() => oligoSynthesisActivity)); }
public async Task CreateOrUpdateSynthesisActivity(SynthesisActivityItemViewModel item) { OligoSynthesisActivity entity; OligoSynthesisProcess currentOligoSynthesisProcess = await GetCurrentOligoSynthesisProcess(); if (string.IsNullOrWhiteSpace(item.Id)) { entity = new OligoSynthesisActivity(); entity.OligoSynthesisProcess = currentOligoSynthesisProcess; } else { entity = currentOligoSynthesisProcess.OligoSynthesisActivities.Single(x => x.Id == item.Id); } entity.DNASequence = !string.IsNullOrWhiteSpace(item.DNASequence) ? item.DNASequence.ToUpper() : "A"; if (item.SynthesisCycle != null && !string.IsNullOrWhiteSpace(item.SynthesisCycle.Id)) { entity.SynthesisCycle = unitOfWork.GetById <SynthesisCycle>(item.SynthesisCycle.Id); } else { SynthesisCycle defaultSynthesisCycle = await unitOfWork.GetAll <SynthesisCycle>() .Include(x => x.CycleSteps) .SingleAsync(x => x.UserId == null); entity.SynthesisCycle = defaultSynthesisCycle; } await AssignToChannel(entity, currentOligoSynthesisProcess.OligoSynthesisActivities); CalcOligoSynthesisActivityTotalTime(entity); unitOfWork.InsertOrUpdate(entity); unitOfWork.Commit(); currentOligoSynthesisProcess = await GetCurrentOligoSynthesisProcess(); currentOligoSynthesisProcess.TotalTime = currentOligoSynthesisProcess.OligoSynthesisActivities.Sum(x => x.TotalTime); unitOfWork.Commit(); }
private void CalcOligoSynthesisActivityTotalTime(OligoSynthesisActivity oligoSynthesisActivity) { string dnaSequence = oligoSynthesisActivity.DNASequence.Substring(1, oligoSynthesisActivity.DNASequence.Length - 1); int synthesisActivityTime = 0; foreach (char nucleotide in dnaSequence.ToCharArray()) { foreach (CycleStep cycleStep in oligoSynthesisActivity.SynthesisCycle.CycleSteps.OrderBy(x => x.Number)) { bool isCycleStepApplicable = IsCycleStepApplicable(nucleotide, cycleStep); if (isCycleStepApplicable) { synthesisActivityTime += cycleStep.StepTime; } } } oligoSynthesisActivity.TotalTime = synthesisActivityTime; }
public async Task StartSynthesis(OligoSynthesisActivity oligoSynthesisActivity, IEnumerable <HardwareFunction> bAndTetToColFunctions, HardwareFunction closeAllValvesFunction, int oligoSynthesisActivityTotalTime) { try { isRunning = true; if (oligoSynthesisActivity.DNASequence.Length < 2) { throw new Exception($"DNA sequence length should be >= 2: {oligoSynthesisActivity.DNASequence}"); } logAction( $"Starting oligo synthesis activity. CH '{oligoSynthesisActivity.ChannelNumber}': SEQ '{oligoSynthesisActivity.DNASequence}' : Time '{oligoSynthesisActivityTotalTime} s'"); string dnaSequence = oligoSynthesisActivity.DNASequence.Substring(1, oligoSynthesisActivity.DNASequence.Length - 1); await ExecuteHardwareFunction(oligoSynthesisActivity.ChannelApiFunction.ApiUrl); foreach (char nucleotide in dnaSequence.ToCharArray()) { foreach (CycleStep cycleStep in oligoSynthesisActivity.SynthesisCycle.CycleSteps.OrderBy(x => x.Number)) { bool isCycleStepApplicable = IsCycleStepApplicable(nucleotide, cycleStep); if (isCycleStepApplicable) { string cycleStepApiUrl = GetCycleStepApiUrl(cycleStep, nucleotide, bAndTetToColFunctions); logAction( $"CH '{oligoSynthesisActivity.ChannelNumber}': SEQ '{oligoSynthesisActivity.DNASequence}': NUC '{nucleotide}' : STEP '{cycleStep.Number}' : TIME: {cycleStep.StepTime} : FUNC '{cycleStepApiUrl}': NAME '{cycleStep.HardwareFunction.Name}'"); await ExecuteHardwareFunction(cycleStepApiUrl); await Task.Delay(cycleStep.StepTime * 1000); logAction($"Opened valves: {cycleStep.HardwareFunction.Description}"); await ExecuteHardwareFunction(closeAllValvesFunction.ApiUrl); oligoSynthesisActivityTotalTime -= cycleStep.StepTime; logAction($"Remaining time: {oligoSynthesisActivityTotalTime} s "); if (cycleStep.SafeStep && IsSuspended) { logAction("Synthesis suspended"); while (isSuspended) { await Task.Delay(100); } logAction("Synthesis resumed"); } if (cycleStep.SafeStep && IsForceStopRequested) { ResetStates(); logAction("Synthesis stopped"); return; } } } } logAction( $"Synthesis completed. CH '{oligoSynthesisActivity.ChannelNumber}': SEQ '{oligoSynthesisActivity.DNASequence}'"); } catch (Exception ex) { logAction( $"Synthesis failed: CH '{oligoSynthesisActivity.ChannelNumber}': SEQ '{oligoSynthesisActivity.DNASequence}'. Message: {ex.Message}"); await ExecuteHardwareFunction(closeAllValvesFunction.ApiUrl); } finally { ResetStates(); } }