Exemple #1
0
        private Observation CreateObsCVterms(SdtmRow sdtmRow, SdtmRowDescriptor sdtmDescriptor, out Observation ob)
        {
            //const string AESOCvar = "AESOC";
            //const string AEHG

            throw new NotImplementedException();
        }
Exemple #2
0
 private List <SdtmRow> GetFakeAEoccurN(List <string> subjectIds, SdtmRow yAE)
 {
     return(subjectIds.Select(subj => new SdtmRow()
     {
         USubjId = subj,
         //StudyId = subj.Study.Name,
         //Topic = obsreq.O3,
         //DBTopicId = termId,//obsreq.TermIds[0],
         //Id = Guid.NewGuid(),
         ActivityId = yAE.ActivityId,
         DatasetId = yAE.DatasetId,
         DatafileId = yAE.DatafileId,
         ProjectId = yAE.ProjectId,
         ProjectAccession = yAE.ProjectAccession,
         Class = yAE.Class,
         Group = yAE.Group,
         TopicControlledTerm = yAE.TopicControlledTerm,
         QualifierQualifiers = yAE.QualifierQualifiers,
         Qualifiers = new Dictionary <string, string>()
         {
             { "AEOCCUR", "N" }
         }
     }).ToList());
 }
Exemple #3
0
        public static DataRow WriteSDTMrow(SdtmRow sdtmrow, DataTable datatable, SdtmRowDescriptor descriptor)
        {
            var row = datatable.NewRow();

            foreach (DataColumn column in datatable.Columns)
            {
                var colName = column.ColumnName;
                //var value = tableRow[column].ToString();

                if (colName == descriptor.StudyIdentifierVariable.Name)
                {
                    //"STUDYID":
                    row[column] = sdtmrow.StudyId ?? "";
                }
                else if (colName == descriptor.DomainVariable.Name)
                {
                    //DOMAIN
                    row[column] = sdtmrow.DomainCode ?? "";
                }
                else if (colName == descriptor.SubjIdVariable?.Name)
                {
                    //SUBJID
                    row[column] = sdtmrow.SubjectId ?? "";
                }
                else if (colName == descriptor.UniqueSubjIdVariable.Name)
                {
                    //USUBJID
                    row[column] = sdtmrow.USubjId ?? "";
                }
                else if (colName == descriptor.SampleIdVariable?.Name)
                {
                    //--REFID
                    row[column] = sdtmrow.SampleId ?? "";
                }
                else if (colName == descriptor.TopicVariable?.Name)
                {
                    //--TESTCD // --TERM // --TRT
                    row[column] = sdtmrow.Topic ?? "";
                }
                else if (colName == descriptor.TopicSynonymVariable?.Name)
                {
                    //--TEST // --MODIFY
                    row[column] = sdtmrow.TopicSynonym ?? "";
                }
                else if (colName == descriptor.TopicCVtermVariable?.Name)
                {
                    //--LOINC --DECODE
                    row[column] = sdtmrow.TopicControlledTerm ?? "";
                }
                else if (colName == descriptor.GroupVariable?.Name)
                {
                    //--CAT
                    row[column] = sdtmrow.Group ?? "";
                }
                else if (colName == descriptor.SubgroupVariable?.Name)
                {
                    //--SCAT
                    row[column] = sdtmrow.Subgroup ?? "";
                }
                else if (descriptor.QualifierVariables.Select(v => v.Name).Contains(colName))
                {
                    //RECORD QUALIFIERS
                    var val = sdtmrow.Qualifiers.FirstOrDefault(c => c.Key == colName).Value;
                    row[column] = val ?? "";
                }
                else if (descriptor.ResultVariables.Select(v => v.Name).Contains(colName))
                {
                    //RESULT QUALIFIERS
                    var val = sdtmrow.ResultQualifiers.FirstOrDefault(c => c.Key == colName).Value;
                    row[column] = val ?? "";
                }
                else if (descriptor.SynonymVariables.Select(v => v.Name).Contains(colName))
                {
                    //SYNOYMS
                    var val = sdtmrow.QualifierSynonyms.FirstOrDefault(c => c.Key == colName).Value;
                    row[column] = val ?? "";
                }
                else if (descriptor.VariableQualifierVariables.Select(v => v.Name).Contains(colName))
                {
                    //VARIABLE QUALIFIERS
                    var val = sdtmrow.QualifierQualifiers.FirstOrDefault(c => c.Key == colName).Value;
                    row[column] = val ?? "";
                }
                else if (descriptor.TimeVariables.Select(v => v.Name).Contains(colName))
                {
                    //VISIT
                    var val = sdtmrow.TimingQualifiers.FirstOrDefault(c => c.Key == colName).Value;
                    row[column] = val ?? "";
                }
                else if (descriptor.DurationVariable?.Name == colName)
                {
                    //--DUR
                    row[column] = sdtmrow.Duration;
                }

                else if (sdtmrow.Leftovers.ContainsKey(colName))
                {
                    var val = sdtmrow.Leftovers.FirstOrDefault(c => c.Key == colName).Value;
                    row[column] = val ?? "";
                }

                else
                {
                    row[column] = "";
                }
                //TODO: else if(column is not mandatory add it and set null)
                //else throw new Exception("Unknown column. Cannot find value for it.");column in descriptor
            }
            return(row);
        }
Exemple #4
0
        public static SdtmRow readSDTMrow(DataRow tableRow, DataTable dataTable, SdtmRowDescriptor descriptor)
        {
            var sdtmrow = new SdtmRow();

            sdtmrow.Class = descriptor.Class;
            foreach (DataColumn column in dataTable.Columns)
            {
                var colName = column.ColumnName;
                var value   = tableRow[column].ToString();

                if (colName == descriptor.StudyIdentifierVariable.Name)
                {
                    //"STUDYID":
                    sdtmrow.StudyId = value;
                }
                else if (colName == descriptor.DomainVariable.Name)
                {
                    //DOMAIN
                    sdtmrow.DomainCode = value;
                }
                else if (colName == descriptor.SubjIdVariable?.Name)
                {
                    //SUBJID
                    sdtmrow.SubjectId = value;
                }
                else if (colName == descriptor.UniqueSubjIdVariable.Name)
                {
                    //USUBJID
                    sdtmrow.USubjId = value;
                }
                else if (colName == descriptor.SampleIdVariable?.Name)
                {
                    //--REFID
                    sdtmrow.SampleId = value;
                }
                else if (colName == descriptor.TopicVariable?.Name)
                {
                    //--TESTCD // --TERM // --TRT
                    sdtmrow.Topic = value;
                }
                else if (colName == descriptor.TopicSynonymVariable?.Name)
                {
                    //--TEST // --MODIFY
                    sdtmrow.TopicSynonym = value;
                }
                else if (colName == descriptor.TopicCVtermVariable?.Name)
                {
                    //--LOINC --DECODE
                    sdtmrow.TopicControlledTerm = value;
                }
                else if (colName == descriptor.GroupVariable?.Name)
                {
                    //--CAT
                    sdtmrow.Group = value;
                }
                else if (colName == descriptor.SubgroupVariable?.Name)
                {
                    //--SCAT
                    sdtmrow.Subgroup = value;
                }
                else if (descriptor.QualifierVariables.Select(v => v.Name).Contains(colName))
                {
                    //RECORD QUALIFIERS
                    sdtmrow.Qualifiers.Add(colName, value);
                }
                else if (descriptor.ResultVariables.Select(v => v.Name).Contains(colName))
                {
                    //RESULT QUALIFIERS
                    sdtmrow.ResultQualifiers.Add(colName, value);
                }
                else if (descriptor.SynonymVariables.Select(v => v.Name).Contains(colName))
                {
                    //SYNOYMS
                    sdtmrow.QualifierSynonyms.Add(colName, value);
                }
                else if (descriptor.VariableQualifierVariables.Select(v => v.Name).Contains(colName))
                {
                    //VARIABLE QUALIFIERS
                    sdtmrow.QualifierQualifiers.Add(colName, value);
                }
                else if (descriptor.VisitNameVariable?.Name == colName)
                {
                    //VISIT
                    sdtmrow.VisitName = value;
                    sdtmrow.TimingQualifiers.Add(colName, value);
                }
                else if (descriptor.VisitNumVariable?.Name == colName)
                {
                    //VISITNUM
                    int num;
                    if (!int.TryParse(value, out num))
                    {
                        continue;
                    }
                    sdtmrow.VisitNum = int.Parse(value);
                    sdtmrow.TimingQualifiers.Add(colName, value);
                }
                else if (descriptor.VisitPlannedStudyDay?.Name == colName)
                {
                    //VISITNUM
                    int num;
                    if (!int.TryParse(value, out num))
                    {
                        continue;
                    }
                    sdtmrow.VisitPlannedStudyDay = int.Parse(value);
                    sdtmrow.TimingQualifiers.Add(colName, value);
                }
                else if (descriptor.DateTimeVariable?.Name == colName)
                {
                    //DTC
                    DateTime DT;
                    if (!DateTime.TryParse(value, out DT))
                    {
                        continue;
                    }
                    if (sdtmrow.CollectionDateTime == null)
                    {
                        sdtmrow.CollectionDateTime = new AbsoluteTimePoint();
                    }
                    sdtmrow.CollectionDateTime.DateTime = DT;
                    sdtmrow.TimingQualifiers.Add(colName, value);
                }
                else if (descriptor.StudyDayVariable?.Name == colName)
                {
                    //--DY
                    int num;
                    if (!int.TryParse(value, out num))
                    {
                        continue;
                    }
                    if (sdtmrow.CollectionStudyDay == null)
                    {
                        sdtmrow.CollectionStudyDay = new RelativeTimePoint();
                    }

                    sdtmrow.CollectionStudyDay.Number = num;
                    sdtmrow.TimingQualifiers.Add(colName, value);
                }
                else if (descriptor.TimePointNameVariable?.Name == colName)
                {
                    //--TPT
                    if (sdtmrow.CollectionStudyTimePoint == null)
                    {
                        sdtmrow.CollectionStudyTimePoint = new RelativeTimePoint();
                    }
                    sdtmrow.CollectionStudyTimePoint.Name = value;
                    sdtmrow.TimingQualifiers.Add(colName, value);
                }
                else if (descriptor.TimePointNameVariable?.Name == colName)
                {
                    //--TPTNUM
                    int num;
                    if (!int.TryParse(value, out num))
                    {
                        continue;
                    }
                    if (sdtmrow.CollectionStudyTimePoint == null)
                    {
                        sdtmrow.CollectionStudyTimePoint = new RelativeTimePoint();
                    }
                    sdtmrow.CollectionStudyTimePoint.Number = num;
                    sdtmrow.TimingQualifiers.Add(colName, value);
                }
                else if (descriptor.DurationVariable?.Name == colName)
                {
                    //--DUR
                    sdtmrow.Duration = value;
                }
                else if (descriptor.StartDateTimeVariable?.Name == colName)
                {
                    //--STDTC
                    DateTime dt;
                    if (!DateTime.TryParse(value, out dt))
                    {
                        continue;
                    }

                    if (sdtmrow.DateTimeInterval == null)
                    {
                        sdtmrow.DateTimeInterval = new TimeInterval();
                    }
                    sdtmrow.DateTimeInterval.Start = new AbsoluteTimePoint();
                    ((AbsoluteTimePoint)sdtmrow.DateTimeInterval.Start).DateTime = dt;
                    sdtmrow.TimingQualifiers.Add(colName, value);
                }
                else if (descriptor.EndDateTimeVariable?.Name == colName)
                {
                    //--ENDTC
                    DateTime dt;
                    if (!DateTime.TryParse(value, out dt))
                    {
                        continue;
                    }

                    if (sdtmrow.DateTimeInterval == null)
                    {
                        sdtmrow.DateTimeInterval = new TimeInterval();
                    }
                    sdtmrow.DateTimeInterval.End = new AbsoluteTimePoint();
                    ((AbsoluteTimePoint)sdtmrow.DateTimeInterval.End).DateTime = dt;
                    sdtmrow.TimingQualifiers.Add(colName, value);
                }
                else if (descriptor.StartStudyDayVariable?.Name == colName)
                {
                    //--STDY
                    int val;
                    if (!int.TryParse(value, out val))
                    {
                        continue;
                    }

                    if (sdtmrow.StudyDayInterval == null)
                    {
                        sdtmrow.StudyDayInterval = new TimeInterval();
                    }
                    sdtmrow.StudyDayInterval.Start = new RelativeTimePoint();
                    ((RelativeTimePoint)sdtmrow.StudyDayInterval.Start).Number = val;
                    sdtmrow.TimingQualifiers.Add(colName, value);
                }
                else if (descriptor.EndStudyDayVariable?.Name == colName)
                {
                    //--ENDY
                    int val;
                    if (!int.TryParse(value, out val))
                    {
                        continue;
                    }

                    if (sdtmrow.StudyDayInterval == null)
                    {
                        sdtmrow.StudyDayInterval = new TimeInterval();
                    }
                    sdtmrow.StudyDayInterval.End = new RelativeTimePoint();
                    ((RelativeTimePoint)sdtmrow.StudyDayInterval.End).Number = val;
                    sdtmrow.TimingQualifiers.Add(colName, value);
                }
                else
                {
                    sdtmrow.Leftovers.Add(colName, value);
                }
            }
            return(sdtmrow);
        }
Exemple #5
0
        private string ComputeValue(SdtmRow obs, VariableDefinition var)
        {
            //NO VALIDATIONS AND NO CHECKING WATSOEVA!
            var expressionList = var.ComputedVarExpression.Split(',');

            if (expressionList[0] == "CASE")
            {
                var sourceVarAcc = expressionList[1];
                var varDef       = _variableDefinitionRepository.FindSingle(v => v.Accession == sourceVarAcc && v.ProjectId == var.ProjectId);
                var oriVal       = obs.Qualifiers[varDef.Name];

                var mappings = new Dictionary <string, string>();
                var i        = 2;
                //IT's assumed that the expression has already been validated
                while (i < expressionList.Length - 1)
                {
                    mappings.Add(expressionList[i + 1], expressionList[i + 3]);
                    i += 4;
                }

                string mappedVal = null;
                if (mappings.TryGetValue(oriVal, out mappedVal))
                {
                    return(mappedVal);
                }
            }

            //VERY TEMP ...need to define variable type (rpw vs qggregate
            else if (expressionList.Contains("-") || expressionList.Contains("+") || expressionList.Contains("\\") || expressionList.Contains("*"))
            {
                var left           = expressionList[0];
                var right          = expressionList[2];
                var leftOperandVar = _variableDefinitionRepository.FindSingle(
                    v => v.Accession == left && v.ProjectId == var.ProjectId);
                var rightOperandVar = _variableDefinitionRepository.FindSingle(
                    v => v.Accession == right && v.ProjectId == var.ProjectId);
                var op = expressionList[1];

                //FOR DATES
                if (leftOperandVar.Name.EndsWith("ENDTC") && rightOperandVar.Name.EndsWith("STDTC") && op == "-")
                {
                    TimeSpan?res = ((AbsoluteTimePoint)obs.DateTimeInterval?.End)?.DateTime -
                                   ((AbsoluteTimePoint)obs.DateTimeInterval?.Start)?.DateTime;
                    return(res != null?Math.Round(((TimeSpan)res).TotalDays).ToString() : "");
                }

                var leftOperandValStr  = obs.Qualifiers[leftOperandVar?.Name];
                var rightOperandValStr = obs.Qualifiers[rightOperandVar?.Name];

                int leftOperandVal, rightOperandVal;

                if (int.TryParse(leftOperandValStr, out leftOperandVal) && int.TryParse(rightOperandValStr, out rightOperandVal))
                {
                    return(Evaluate(leftOperandVal, rightOperandVal, op).ToString());
                }
            }
            else if (expressionList[0] == "SUM")
            {
            }
            else if (expressionList[0] == "MAX")
            {
            }

            //if case
            //then parse the expression
            //get the source variable
            //for each when then create a dictionary where the key is the when and the value is the then
            //retrieve the value of the source variable from the qualifiers of subjobservation, compare it to dictionary key, if found add to qualifiers new var with value from dictionary value
            return("");
        }
        private async Task <DataTable> ExportSubjectClinicalTable(DataExportObject exportData, ExportFileDefinition fileDefinition) // FEATURE BY FEATURE  MAIN QUICK
        {
            return(await Task.Factory.StartNew(() =>
            {
                #region Create Table Columns
                var datatable = new DataTable();
                datatable.TableName = fileDefinition.Name;

                foreach (var field in fileDefinition.Fields)
                {
                    datatable.Columns.Add(field.ColumnHeader.ToLower());
                }

                #endregion

                //var subjGroupedObservations = exportData.Observations.GroupBy(ob => new { subjId = ob.USubjId });

                var fieldsByO3Id = fileDefinition.Fields.FindAll(f => f.QueryObjectType == nameof(SdtmRow)).GroupBy(f => f.QueryObject.QueryObjectName).ToList();
                var subjPropertiesFields = fileDefinition.Fields.FindAll(f => f.QueryObjectType != nameof(SdtmRow)).ToList();

                foreach (var subject in exportData.Subjects)
                {
                    var uniqSubjectId = subject.UniqueSubjectId;
                    var subjectObservations = exportData.Observations.FindAll(o => o.USubjId == uniqSubjectId).ToList();
                    var subjectCharacteristics = exportData.SubjChars.FindAll(sc => sc.SubjectId == subject.Id).ToList();

                    var firstRow = true;
                    while (subjectObservations.Any() || firstRow)
                    {
                        var row = datatable.NewRow();
                        firstRow = false;

                        #region Design Elements
                        // row["studyid"] = subject.Study.Name;
                        #endregion

                        #region Subject Properties

                        foreach (var subjPropField in subjPropertiesFields)
                        {
                            var charVal = _queryService.GetSubjectOrSampleProperty(subject, subjPropField.QueryObject);
                            if (charVal != null)
                            {
                                row[subjPropField.ColumnHeader.ToLower()] = charVal;
                            }
                        }

                        #endregion

                        #region WRITE CLINICAL OBSERVATIONS

                        foreach (var fieldgrp in fieldsByO3Id)//HEADACHE //BMI (EVENTS AND FINDINGS TOGETHER)//NOTE .. TIMEING ARE NOT synchronized YET
                        {
                            SdtmRow obs = null;
                            foreach (var field in fieldgrp) //AEOCCUR / AESEV
                            {
                                //ONTOLOGY TERM REQUEST
                                var query = (ObservationQuery)field.QueryObject;
                                if (query.IsOntologyEntry)
                                {
                                    obs = subjectObservations.FirstOrDefault(
                                        o => o.QualifierQualifiers.ContainsKey(query.TermCategory) &&
                                        ((ObservationQuery)field.QueryObject).TermId.ToString() == o.QualifierQualifiers[query.TermCategory]);
                                }

                                //GROUP OF OBSERVATIONS
                                else if (field.QueryObject.GetType() == typeof(GroupedObservationsQuery))
                                {
                                    //ASSUMPTION: GROUPS AREONLY COMPOSED OF ONTOLOGY ENTRY
                                    //ASSUMPTION:
                                    string v;
                                    foreach (var obsQuery in ((GroupedObservationsQuery)field.QueryObject).GroupedObservations)
                                    {
                                        obs = subjectObservations.FirstOrDefault(
                                            o => o.QualifierQualifiers.TryGetValue(obsQuery.TermCategory, out v) &&
                                            obsQuery.TermId.ToString() == o.QualifierQualifiers[obsQuery.TermCategory]);
                                        if (obs != null)
                                        {
                                            break;
                                        }
                                    }
                                }

                                //SINGLE OBSERVATION OBJECT TERM REQUEST
                                else
                                {
                                    obs = subjectObservations.FirstOrDefault(
                                        o => ((ObservationQuery)field.QueryObject).TermId == o.DBTopicId);
                                }

                                string val = "";


                                //WRITE OBSERVATION INSTANCE TO ROW
                                obs?.Qualifiers.TryGetValue(((ObservationQuery)field.QueryObject).PropertyName, out val);
                                if (val == null)
                                {
                                    obs?.ResultQualifiers.TryGetValue(((ObservationQuery)field.QueryObject).PropertyName, out val);
                                }

                                //Write
                                //if (val == null)
                                //if (((ObservationQuery)field.QueryObject).PropertyName.ToLower().Equals("visit"))
                                //val = obs.VisitName;
                                //if (val==null && ((ObservationQuery)field.QueryObject).PropertyName.EndsWith("DY") && obs.CollectionStudyDay.Number!=null)
                                //{
                                //    val = obs.CollectionStudyDay.Number.HasValue ? obs.CollectionStudyDay.Number.ToString() : "";
                                //    //row[field.ColumnHeader.ToLower()] = val;
                                //}

                                row[field.ColumnHeader.ToLower()] = val ?? "";
                            }
                            var visitField = fileDefinition.Fields.Find(f => f.QueryObjectType == nameof(Visit));
                            if (visitField != null && obs != null)
                            {
                                row[visitField.ColumnHeader.ToLower()] = obs?.VisitName ?? "";
                            }

                            var timepointField = fileDefinition.Fields.FirstOrDefault(f => f.QueryObject.QueryFor != null &&
                                                                                      f.QueryObject.QueryFor.Equals(nameof(SdtmRow.CollectionStudyTimePoint)));
                            if (timepointField != null && obs != null)
                            {
                                row[timepointField.ColumnHeader.ToLower()] = obs?.CollectionStudyTimePoint?.Name ?? "";
                            }

                            subjectObservations.Remove(obs);
                        }

                        #endregion

                        #region Write Timing Variables

                        #endregion


                        datatable.Rows.Add(row);
                    }
                }

                return datatable;
            }));
        }