Пример #1
0
        public bool Notify(GNAnalysisRequest analysisRequest)
        {
            LogUtil.LogMethod(logger, MethodBase.GetCurrentMethod());

            try
            {
                GNCloudFile GNCloudVCF = (from a in analysisRequest.AnalysisResult.ResultFiles
                                          where a.GNCloudFileCategoryId == 2
                                          select a).FirstOrDefault <GNCloudFile>();

                GNNewSampleBatch          batch   = analysisRequest.GNAnalysisRequestGNSamples.FirstOrDefault().GNSample.GNNewSampleBatchSample.GNNewSampleBatch;
                AnalysisCompletionMessage message = new AnalysisCompletionMessage
                {
                    analysisId   = analysisRequest.Id.ToString(),
                    batchId      = batch.BatchId,
                    project      = batch.Project,
                    repository   = batch.Repository,
                    bamViewerURL = "https://secure.genomenext.net/GNPortal-1.3/AnalysisRequests/BamViewer?analysisId=" + analysisRequest.Id.ToString(),
                    vcfFilename  = GNCloudVCF.FileURL.Replace("https://", "s3://").Replace(".s3.amazonaws.com", "")
                };

                System.Console.WriteLine("***\n ******  Sending message " + analysisRequest.CreateDateTime);
                this.SendMessage(message);
            }
            catch (Exception e1)
            {
                System.Console.WriteLine("***\n ******  Exception Sending message " + e1.InnerException + e1.Message + e1.StackTrace);
                Exception e2 = new Exception("Unable to send notification to queue.", e1);
                LogUtil.Warn(logger, e2.Message, e2);
                return(false);
            }

            return(true);
        }
Пример #2
0
        public bool NotifyErrorAtEndOfBatch(GNNewSampleBatch Batch, string errorMessage)
        {
            //NOTIFY USER
            bool notifySuccess =
                new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                    "SAMPLE_STATUS_UPDATE_ERROR",
                    Batch.GNSequencerJob.GNOrganization.OrgMainContact.Email,
                    "Sample Status Update",
                    new Dictionary <string, string>
            {
                { "BatchId", Batch.Id.ToString() },
                { "CreatorName", Batch.GNSequencerJob.GNOrganization.OrgMainContact.FullName },
                { "ErrorMessage", errorMessage },
                { "CreateDateTime", DateTime.Now.ToString() }
            });

            return(notifySuccess);
        }
Пример #3
0
        public bool NotifyEndOfBatch(GNNewSampleBatch Batch)
        {
            bool notifySuccess = true;

            try
            {
                String notifyCreateAnalysis = "";
                if (Batch.CreateAnalysisPerSample)
                {
                    notifyCreateAnalysis = "Each sample had one analysis created automatically.";
                }

                String notifyAutostartAnalysis = "";
                if (Batch.AutoStartAnalysis)
                {
                    notifyAutostartAnalysis = "Each analysis create was autostarted as soon as the samples were completed. New notifications will be sent to your inbox -with links to each analysis- as these move along.";
                }

                //NOTIFY USER
                notifySuccess =
                    new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                        "SAMPLE_STATUS_UPDATE_COMPLETE",
                        Batch.GNSequencerJob.GNOrganization.OrgMainContact.Email,
                        "Sample Status Update",
                        new Dictionary <string, string>
                {
                    { "ProjectName", Batch.GNSequencerJob.Project },
                    { "TotalSamples", Batch.TotalSamples.ToString() },
                    { "CreateAnalysisPerSample", notifyCreateAnalysis },
                    { "AutoStartAnalysis", notifyAutostartAnalysis },
                    { "ErrorMessage", "" },
                    { "CreateDateTime", DateTime.Now.ToString() }
                });
            }
            catch (Exception e2)
            {
                System.Console.WriteLine("***Message " + e2.Message + e2.StackTrace + " **********************************");
            }

            return(notifySuccess);
        }
        public override bool ProcessMessage(NewSampleBatchStatus newSampleMessageStatus, object queueMessage)
        {
            System.Console.WriteLine("***\n newSampleMessageStatus Batch Name: " + newSampleMessageStatus.batchId);

            if (newSampleMessageStatus.batchId.Equals("NO_SAMPLE_FOUND"))
            {
                //processSpecialCases();
            }

            if (newSampleMessageStatus.status.Equals("QC-COMPLETED"))
            {
                // ProcessQCMessage(newSampleMessageStatus);
                return(true);
            }

            bool success = false;

            try
            {
                GNOrganization   Organization = db.GNOrganizations.Where(a => a.Repository.Equals(newSampleMessageStatus.repositoryId)).FirstOrDefault();
                GNNewSampleBatch Batch        = db.GNNewSampleBatches.Where(a => a.BatchId.Equals(newSampleMessageStatus.batchId)).FirstOrDefault();

                bool isError = false;
                if (newSampleMessageStatus.isError.Equals("true"))
                {
                    isError = true;
                }


                GNNewSampleBatchStatus batchStatus = new GNNewSampleBatchStatus
                {
                    Id                 = Guid.NewGuid(),
                    CreateDateTime     = DateTime.Now,
                    GNNewSampleBatch   = Batch,
                    GNNewSampleBatchId = Batch.Id,
                    IsError            = isError,
                    PercentComplete    = newSampleMessageStatus.percentComplete,
                    Status             = newSampleMessageStatus.status,
                    RepositoryId       = newSampleMessageStatus.repositoryId,
                    FilesBucket        = newSampleMessageStatus.filesBucket,
                    Message            = newSampleMessageStatus.message
                };
                db.GNNewSampleBatchStatus.Add(batchStatus);
                db.SaveChanges();


                /*
                 * var tx = db.Database.BeginTransaction();
                 *
                 * string insertSQL =
                 *  "INSERT INTO [gn].[GNSampleStatus]" +
                 *  "([Id],[GNSampleId],[SampleName],[Repository],[Message],[PercentComplete],[IsError],[CreatedBy],[CreateDateTime])" +
                 *  "VALUES " +
                 *  "(@Id, @GNSampleId, @SampleName, @Repository, @Message, @PercentComplete, @IsError, @CreatedBy, @CreateDateTime)";
                 *
                 * db.Database.ExecuteSqlCommand(
                 *  insertSQL,
                 *  new SqlParameter("@Id", Guid.NewGuid()),
                 *  new SqlParameter("@GNSampleId", sample.Id),
                 *  new SqlParameter("@SampleName", newSampleMessageStatus.name),
                 *  new SqlParameter("@Repository", newSampleMessageStatus.repositoryId),
                 *  new SqlParameter("@Message", newSampleMessageStatus.message),
                 *  new SqlParameter("@PercentComplete", newSampleMessageStatus.percentComplete),
                 *  new SqlParameter("@IsError", isError),
                 *  new SqlParameter("@CreatedBy", Guid.Parse("0750f896-e7d6-48d4-a1b9-007059f62784")),
                 *  new SqlParameter("@CreateDateTime", DateTime.Now));
                 *
                 * LogUtil.Warn(logger, insertSQL);
                 * tx.Commit();
                 */



                if (newSampleMessageStatus.filesBucket != null && newSampleMessageStatus.filesBucket != "")
                {
                    System.Console.WriteLine("&&&&& \n Will copy files &&&&&&& ");

                    try
                    {
                        /*
                         * IQueryable<GNSample> listOfSamples = db.GNSamples.Where(a => a.GNNewSampleBatchSample.GNNewSampleBatchId.Equals(Batch.Id));
                         *
                         * GNCloudStorageService st = new GNCloudStorageService();
                         * string originBucket = newSampleMessageStatus.filesBucket;
                         * originBucket = "tfrege-test-20140711"; //testing purposes
                         * Dictionary<String, long> listOfFiles = st.ListingObjects(originBucket);
                         *
                         * string s3BucketName = st.FetchAWSS3Bucket().ARN;
                         *
                         * /*
                         * //string volume = newSampleMessageStatus.filesBucket.Substring(newSampleMessageStatus.filesBucket.IndexOf("/")+1, )
                         * string folderPath1 = newSampleMessageStatus.filesBucket.Replace("http://", "");
                         * folderPath1 = folderPath1.Replace("https://", "");
                         * int idxSlash = folderPath1.IndexOf("/");
                         * int strLen = folderPath1.Length;
                         *
                         * //string volume = folderPath1.Substring(0, idxSlash);
                         * //string folderPath = folderPath1.Substring(idxSlash + 1, strLen - idxSlash - 1);
                         * GNCloudFileCategory fileCategory = db.GNCloudFileCategories.Where(a => a.Id == 1).FirstOrDefault();
                         *
                         *
                         * string volume = s3BucketName;
                         *
                         * foreach (GNSample sample in listOfSamples)
                         * {
                         *  Guid fileId = Guid.NewGuid();
                         *
                         *  //Create new folder in S3
                         *  st.PutObjectOnBucket(s3BucketName + "fastq/", sample.Id.ToString(), sample.Id.ToString());
                         *  st.PutObjectOnBucket(s3BucketName + "fastq/", sample.Id.ToString() + "/" + fileId.ToString(), sample.Id.ToString() + "/" + fileId.ToString());
                         *
                         *  string folderPath = "fastq/" + sample.Id.ToString() + "/" + fileId.ToString();
                         *
                         *
                         *  var subList = listOfFiles.Where(a => a.Key.Contains(sample.Name));
                         *  foreach (var file in subList)
                         *  {
                         *      System.Console.WriteLine("&&&&& \n Will copy files &&&&&&& "+file.Key);
                         *
                         *      string fileURL = "https://dev-gn-s3-01.s3.amazonaws.com/" + folderPath + file.Key;
                         *
                         *      GNCloudFile newFile = new GNCloudFile
                         *      {
                         *          Id = fileId,
                         *          GNCloudFileCategoryId = fileCategory.Id, //FASTQ
                         *          CloudFileCategory = fileCategory,
                         *          FileName = folderPath + file.Key,
                         *          FileURL = fileURL,
                         *          FolderPath = folderPath,
                         *          Volume = volume,
                         *          Description = file.Key,
                         *          FileSize = file.Value,
                         *          AWSRegionSystemName = db.AWSRegions.FirstOrDefault().AWSRegionSystemName,
                         *          CreateDateTime = DateTime.Now,
                         *          CreatedBy = Guid.Parse("0750f896-e7d6-48d4-a1b9-007059f62784"),
                         *          SampleId = sample.Id.ToString()
                         *      };
                         *      db.GNCloudFiles.Add(newFile);
                         *
                         *   //   st.CopyS3Object(originBucket, fileURL, file.Key);
                         *  }
                         *  db.SaveChanges()
                         *
                         * }*/
                    }
                    catch (Exception filesUpdateExc)
                    {
                        System.Console.WriteLine("***\n Unable to Read the Files from S3: " + newSampleMessageStatus.batchId + "\n" + filesUpdateExc.Message + filesUpdateExc.InnerException + filesUpdateExc.StackTrace);

                        Exception e2 = new Exception("Unable to Read the Files from S3 for this batch New Sample: ", filesUpdateExc);
                        LogUtil.Warn(logger, filesUpdateExc.Message, filesUpdateExc);
                        success = false;
                    }

                    success = true;
                }
                ////////////////////////////END OF READING FILES FROM S3 AND UPDATING RDS //////////////



                if (isError)
                {
                    Batch.GNSequencerJob.Status = "ERROR";
                    db.SaveChanges();

                    //NOTIFY USER
                    bool notifySuccess =
                        new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                            "SAMPLE_STATUS_UPDATE_ERROR",
                            "*****@*****.**",
                            "Sample Status Update",
                            new Dictionary <string, string>
                    {
                        { "BatchId", newSampleMessageStatus.batchId },
                        { "CreatorName", Organization.OrgMainContact.FullName },
                        { "ErrorMessage", newSampleMessageStatus.message },
                        { "CreateDateTime", DateTime.Now.ToString() }
                    });
                }

                if (newSampleMessageStatus.percentComplete == 100 || newSampleMessageStatus.percentComplete.Equals("100"))
                {
                    Batch.GNSequencerJob.Status = "COMPLETED";
                    db.SaveChanges();


                    if (Batch.AutoStartAnalysis)
                    {
                        //loop through all the samples, look for their analyses, and start them.
                        List <GNNewSampleBatchSamples> ListOfSamples = Batch.GNNewSampleBatchSamples.ToList();

                        foreach (GNNewSampleBatchSamples batchSample in ListOfSamples)
                        {
                            GNSample sample = batchSample.GNSample;
                            var      result = this.AutostartAnalysis(sample);
                        }
                    }


                    try
                    {
                        //NOTIFY USER
                        bool notifySuccess =
                            new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                                "SAMPLE_STATUS_UPDATE_COMPLETE",
                                "*****@*****.**",
                                "Sample Status Update",
                                new Dictionary <string, string>
                        {
                            { "BatchId", Batch.Id.ToString() },
                            { "TotalSamples", Batch.TotalSamples.ToString() },
                            { "CreatorName", Organization.OrgMainContact.FullName },
                            { "ErrorMessage", newSampleMessageStatus.message },
                            { "CreateDateTime", DateTime.Now.ToString() }
                        });
                    }
                    catch (Exception e2)
                    {
                        System.Console.WriteLine("***Message " + e2.Message + e2.StackTrace + " **********************************");
                    }
                }
            }
            catch (Exception e1)
            {
                Exception e2 = new Exception("Unable to Update Status of New Sample.", e1);
                LogUtil.Warn(logger, e2.Message, e2);
                success = false;
            }
            return(success);
        }
        public override bool ProcessMessage(NewSampleBatch newSampleMessage, object queueMessage)
        {
            System.Console.WriteLine("***\n ****** Batch Id: " + newSampleMessage.batchId);
            System.Console.WriteLine("***\n ****** Sample type: " + newSampleMessage.type);
            System.Console.WriteLine("***\n ****** Sample repositoryId: " + newSampleMessage.repositoryId);

            SampleResponseService sampleResponseService = new SampleResponseService();

            GNOrganization Organization = db.GNOrganizations.Where(a => a.Repository.Equals(newSampleMessage.repositoryId)).FirstOrDefault();

            //Find sequencer job
            GNSequencerJob sequencerJob = db.GNSequencerJobs.Where(a => a.GNOrganizationId.Equals(Organization.Id) && a.Project.Equals(newSampleMessage.project)).FirstOrDefault();

            sequencerJob.Status = "PROCESSING SAMPLES";

            GNNewSampleBatch newSampleBatch = new GNNewSampleBatch {
                Id                      = Guid.NewGuid(),
                BatchId                 = newSampleMessage.batchId,
                GNSequencerJobId        = sequencerJob.Id,
                GNSequencerJob          = sequencerJob,
                Project                 = newSampleMessage.project,
                AutoStartAnalysis       = (newSampleMessage.autoStartAnalysis.Equals("true")),
                CreateAnalysisPerSample = (newSampleMessage.createAnalysisPerSample.Equals("true")),
                Qualifier               = newSampleMessage.qualifier,
                Type                    = newSampleMessage.type,
                RepositoryId            = newSampleMessage.repositoryId,
                Gender                  = newSampleMessage.gender,
                ReadType                = newSampleMessage.read,
                TotalSamples            = newSampleMessage.samples.Count(),
                TotalSamplesCompleted   = 0,
                CreateDateTime          = DateTime.Now
            };

            db.GNNewSampleBatches.Add(newSampleBatch);

            db.SaveChanges();

            bool success = false;

            try
            {
                GNSampleType type = db.GNSampleTypes.Where(a => a.Name.Equals(newSampleMessage.type.ToUpper())).FirstOrDefault();

                System.Console.WriteLine("***\n  type " + type.Name);

                System.Console.WriteLine("***\n  Organization " + Organization.Name);

                GNTeam         newTeam      = null;
                GNProject      newProject   = null;
                GNAnalysisType analysisType = null;
                String         AnalysisCode = newSampleMessage.qualifier;
                if (newSampleBatch.CreateAnalysisPerSample)
                {
                    /**
                     * 1. Create a Team
                     * 2. Create a Project
                     */
                    GNContact contact = db.GNContacts.Find(Organization.GNContactId);

                    newTeam = new GNTeam
                    {
                        Id             = Guid.NewGuid(),
                        CreateDateTime = DateTime.Now,
                        CreatedBy      = Guid.Parse("0750f896-e7d6-48d4-a1b9-007059f62784"),
                        Name           = "TeamBatch" + newSampleBatch.BatchId.Substring(0, 15),
                        GNContactId    = contact.Id,
                        Organization   = Organization,
                        OrganizationId = Organization.Id,
                        TeamLead       = contact
                    };
                    db.GNTeams.Add(newTeam);
                    db.SaveChanges();
                    System.Console.WriteLine("***\n  New newTeam Created: " + newTeam.Id);

                    newProject = new GNProject
                    {
                        Id             = Guid.NewGuid(),
                        CreateDateTime = DateTime.Now,
                        CreatedBy      = Guid.Parse("0750f896-e7d6-48d4-a1b9-007059f62784"),
                        ProjectLead    = contact,
                        ProjectLeadId  = contact.Id.ToString(),
                        Name           = "ProjectBatch-" + newSampleBatch.BatchId.Substring(0, 15),
                        TeamId         = newTeam.Id.ToString(),
                        StartDate      = DateTime.Now,
                        EndDate        = DateTime.Now.AddDays(30),
                        Description    = "Created automatically from the Sample Batch Process"
                    };

                    System.Console.WriteLine("***\n  New newProject Created: " + newProject.Id);

                    newTeam.Projects.Add(newProject);
                    newProject.Teams.Add(newTeam);
                    db.GNProjects.Add(newProject);

                    db.SaveChanges();

                    if (newSampleMessage.qualifier.Equals("TUMOR"))
                    {
                        AnalysisCode = "TUMORNORMAL";
                    }
                    analysisType = db.GNAnalysisTypes.Where(a => a.Name.Equals(newSampleMessage.type.ToUpper())).FirstOrDefault();
                }

                foreach (String sampleName in newSampleMessage.samples)
                {
                    String   newSampleName   = sampleName;
                    GNSample sampleExist     = db.GNSamples.Where(a => a.Name.Equals(sampleName) && a.GNOrganizationId.Equals(Organization.Id)).FirstOrDefault();
                    bool     createNewSample = false;
                    if (sampleExist != null)
                    {
                        if (sampleExist.IsReady)
                        {
                            //the sample is ready, let's create a new one with a suffix of (1)
                            newSampleName   = sampleName + " (1)";
                            createNewSample = true;
                        }
                        else
                        {
                            //the sample is in the middle of being created, so ignore the message
                            createNewSample = false;
                        }
                    }
                    else
                    {
                        createNewSample = true;
                    }

                    try
                    {
                        if (createNewSample)
                        {
                            GNSample newSample = new GNSample
                            {
                                Id = Guid.NewGuid(),
                                GNOrganizationId      = Organization.Id,
                                Name                  = newSampleName,
                                Gender                = newSampleMessage.gender,
                                GNSampleTypeId        = type.Id,
                                GNSampleQualifierCode = newSampleMessage.qualifier,
                                IsReady               = false,
                                IsPairEnded           = (newSampleMessage.read == "paired-end"),
                                CreateDateTime        = DateTime.Now,
                                CreatedBy             = Guid.Parse("0750f896-e7d6-48d4-a1b9-007059f62784")
                            };

                            db.GNSamples.Add(newSample);

                            db.SaveChanges();
                            System.Console.WriteLine("***\n  New Sample Created: " + newSample.Id);

                            GNNewSampleBatchSamples batchSample = new GNNewSampleBatchSamples {
                                Id                 = Guid.NewGuid(),
                                GNSample           = newSample,
                                CreateDateTime     = DateTime.Now,
                                GNNewSampleBatch   = newSampleBatch,
                                GNNewSampleBatchId = newSampleBatch.Id,
                                Name               = sampleName
                            };
                            db.GNNewSampleBatchSamples.Add(batchSample);

                            db.SaveChanges();
                            System.Console.WriteLine("***\n  New batchSample Created: " + batchSample.Id);

                            GNNewSampleBatchStatus batchStatus = new GNNewSampleBatchStatus {
                                Id                 = Guid.NewGuid(),
                                CreateDateTime     = DateTime.Now,
                                GNNewSampleBatch   = newSampleBatch,
                                GNNewSampleBatchId = newSampleBatch.Id,
                                IsError            = false,
                                PercentComplete    = 0,
                                Status             = "STARTING PROCESS",
                                RepositoryId       = newSampleMessage.repositoryId
                            };
                            db.GNNewSampleBatchStatus.Add(batchStatus);
                            db.SaveChanges();
                            System.Console.WriteLine("***\n  New batchStatus Created: " + batchStatus.Id);


                            if (newSampleBatch.CreateAnalysisPerSample)
                            {
                                GNAnalysisRequest newAnalysis = new GNAnalysisRequest {
                                    Id                        = Guid.NewGuid(),
                                    Project                   = newProject,
                                    GNProjectId               = newProject.Id,
                                    CreateDateTime            = DateTime.Now,
                                    CreatedBy                 = Guid.Parse("0750f896-e7d6-48d4-a1b9-007059f62784"),
                                    AnalysisType              = analysisType,
                                    RequestProgress           = 0, RequestDateTime = DateTime.Now,
                                    GNAnalysisRequestTypeCode = AnalysisCode,
                                    Description               = newSampleName,
                                    GNAnalysisAdaptorCode     = "NONE",
                                    AnalysisTypeId            = analysisType.Id.ToString(),
                                    AutoStart                 = newSampleBatch.AutoStartAnalysis,
                                    AWSRegionSystemName       = db.AWSRegions.FirstOrDefault().AWSRegionSystemName
                                };
                                db.GNAnalysisRequests.Add(newAnalysis);

                                db.SaveChanges();
                                System.Console.WriteLine("***\n  New newAnalysis Created: " + newAnalysis.Id);


                                GNAnalysisRequestGNSample newAnalysisSample = new GNAnalysisRequestGNSample {
                                    AffectedIndicator   = "N",
                                    TargetIndicator     = "N",
                                    GNAnalysisRequest   = newAnalysis,
                                    GNSample            = newSample,
                                    GNAnalysisRequestId = newAnalysis.Id,
                                    GNSampleId          = newSample.Id
                                };
                                db.GNAnalysisRequestGNSamples.Add(newAnalysisSample);
                            } //end of "if(newSampleBatch.CreateAnalysisPerSample)"

                            System.Console.WriteLine("***\n  Sample Name: " + sampleName);

                            //Notify BCL2FASTQ service
                            sampleResponseService.NotifyBcl2FastqSystem(newSample, newSampleBatch.RepositoryId);
                        }

                        db.SaveChanges();
                    }
                    catch (Exception e1)
                    {
                        System.Console.WriteLine("***\n  Exception: " + e1.Message + e1.InnerException);
                    }
                }

                System.Console.WriteLine("***\n  EVERYTHING WORKED!");

                //NOTIFY USER
                bool notifySuccess =
                    new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                        "SAMPLE_CREATION",
                        "*****@*****.**",
                        "Sample Creation",
                        new Dictionary <string, string>
                {
                    { "BatchId", newSampleMessage.batchId },
                    { "TotalSamples", newSampleBatch.TotalSamples.ToString() },
                    { "CreatorName", Organization.OrgMainContact.FullName },
                    { "CreateDateTime", DateTime.Now.ToString() }
                });
                success = true;
            }
            catch (Exception e1)
            {
                Exception e2 = new Exception("Unable to Create New Sample.", e1);
                LogUtil.Warn(logger, e2.Message, e2);
                success = false;
            }
            return(success);
        }
Пример #6
0
        /**
         * JSON Format
         */
        public void PrepareBamViewer(GNAnalysisRequest analysisRequest)
        {
            GNCloudFile GNCloudVCF = (from a in analysisRequest.AnalysisResult.ResultFiles
                                      where a.GNCloudFileCategoryId == 2
                                      select a).FirstOrDefault <GNCloudFile>();
            string vcfFilePath  = GNCloudVCF.FileURL.Substring(0, GNCloudVCF.Description.LastIndexOf('/'));
            string vcfFile      = GNCloudVCF.Description.Substring(GNCloudVCF.Description.LastIndexOf('/') + 1);
            string fileContents = "";
            //List<string>.Enumerator enumerator = samplesSelected.GetEnumerator();


            BamConfigFile bamRequest = new BamConfigFile();

            bamRequest.analysisId           = analysisRequest.Id.ToString();
            bamRequest.analysisDescription  = analysisRequest.Description;
            bamRequest.vcfFilename          = vcfFile;
            bamRequest.listOfSamples        = new List <AnalysisResultSample>();
            bamRequest.totalNumberOfSamples = analysisRequest.GNAnalysisRequestGNSamples.Count();

            try
            {
                int lineNum = 0;
                foreach (GNAnalysisRequestGNSample analysisSampleInput in analysisRequest.GNAnalysisRequestGNSamples)
                {
                    lineNum++;
                    GNSample sampleInput     = analysisSampleInput.GNSample;
                    string   sampleInputName = sampleInput.Name;
                    sampleInputName = sampleInputName.Replace(" ", "_").Replace("(", "_").Replace(")", "_").ToUpper();

                    string baiFile = analysisRequest.AnalysisResult.ResultFiles.Where(a => a.FileName.Contains(sampleInputName) && a.FileName.Contains("bai")).FirstOrDefault().FileName;
                    string bamFile = analysisRequest.AnalysisResult.ResultFiles.Where(a => a.FileName.Contains(sampleInputName) && a.FileName.Contains("bam") && !a.FileName.Contains("bai")).FirstOrDefault().FileName;
                    string item    = bamFile + " " + baiFile + " " + analysisRequest.Id.ToString() + " " + vcfFile + " " + sampleInput.Name + " " + lineNum + " " + analysisRequest.Description.Replace(" ", "_").Trim().ToUpper();

                    fileContents = fileContents + item + "\n";

                    //   await this.transactionService.CreateBillingTransactionsPerAnalysisSample(userContact, analysisRequest, analysisRequest.AnalysisType.Name, analysisRequest.Description, false, false, "REPORT_BAM_VIEWER", sampleInput);
                    bamRequest.listOfSamples.Add(new AnalysisResultSample
                    {
                        index       = lineNum,
                        sampleId    = sampleInput.Id.ToString(),
                        name        = sampleInputName,
                        baiFilename = baiFile,
                        bamFilename = bamFile
                    }
                                                 );
                }
            }
            catch (Exception e)
            {
                logger.Error("Error while launching BAM Viewer " + e.Message);
            }

            GNNewSampleBatch batch = analysisRequest.GNAnalysisRequestGNSamples.FirstOrDefault().GNSample.GNNewSampleBatchSample.GNNewSampleBatch;

            BamViewerRequest bamViewerRequest = new BamViewerRequest {
                bamViewerURL  = vcfFilePath,
                batchId       = batch.BatchId,
                organization  = batch.GNSequencerJob.GNOrganization.Name,
                project       = batch.GNSequencerJob.Project,
                repository    = batch.Repository,
                vcfFilepath   = vcfFilePath,
                bamConfigFile = bamRequest
            };

            try
            {
                System.Console.WriteLine("***\n ******  Sending message ");
                this.SendMessage(bamViewerRequest);
            }
            catch (Exception exception)
            {
                logger.Error(exception.Message);
                throw;
            }
        }
Пример #7
0
        public override bool ProcessMessage(NewSampleBatchStatus newSampleMessageStatus, object queueMessage)
        {
            System.Console.WriteLine("***\n newSampleMessageStatus Batch Name: " + newSampleMessageStatus.batchId);

            if (newSampleMessageStatus.batchId.Equals("NO_SAMPLE_FOUND"))
            {
                //processSpecialCases();
            }

            if (newSampleMessageStatus.status.Equals("QC-COMPLETED"))
            {
                // ProcessQCMessage(newSampleMessageStatus);
                return(true);
            }

            bool success = false;

            try
            {
                GNOrganization   Organization = db.GNOrganizations.Where(a => a.Repository.Equals(newSampleMessageStatus.repository)).FirstOrDefault();
                GNNewSampleBatch Batch        = db.GNNewSampleBatches.Where(a => a.BatchId.Trim().Equals(newSampleMessageStatus.batchId)).FirstOrDefault();

                if (Batch == null)
                {
                    throw new Exception("Batch not found!");
                }

                System.Console.WriteLine("\n --==========================---------> Batch " + Batch.Id);

                try
                {
                    Batch.TotalNumberOfFastqFiles = Int32.Parse(newSampleMessageStatus.numberOfFastqFiles);
                }
                catch (Exception numberNull)
                {
                    Batch.TotalNumberOfFastqFiles = 0;
                }

                System.Console.WriteLine("\n -----------> Organization " + Organization.Name);


                bool isError = false;
                if (newSampleMessageStatus.isError.Equals("true"))
                {
                    isError = true;
                }

                string status = newSampleMessageStatus.status;
                if (newSampleMessageStatus.percentComplete > 89)
                {
                    //higher percentages will be dealt by the Portal during the Quality Control step
                    return(true);
                }


                //check if the status has been already recorded
                int alreadyExists = db.GNNewSampleBatchStatus.Where(a => a.GNNewSampleBatch.BatchId.Equals(newSampleMessageStatus.batchId) && a.PercentComplete == newSampleMessageStatus.percentComplete && a.Status.Equals(status)).Count();

                if (alreadyExists == 0)
                {
                    GNNewSampleBatchStatus batchStatus = new GNNewSampleBatchStatus
                    {
                        Id                 = Guid.NewGuid(),
                        CreateDateTime     = DateTime.Now,
                        GNNewSampleBatch   = Batch,
                        GNNewSampleBatchId = Batch.Id,
                        IsError            = isError,
                        PercentComplete    = newSampleMessageStatus.percentComplete,
                        Status             = status,
                        Repository         = newSampleMessageStatus.repository,
                        Message            = newSampleMessageStatus.message
                    };
                    db.GNNewSampleBatchStatus.Add(batchStatus);
                    db.SaveChanges();

                    System.Console.WriteLine("\n -----------> Added status " + newSampleMessageStatus.status + " " + newSampleMessageStatus.percentComplete + "%");

                    if (isError)
                    {
                        Batch.GNSequencerJob.Status = "ERROR";
                        db.SaveChanges();

                        //NOTIFY USER
                        bool notifySuccess =
                            new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                                "SAMPLE_STATUS_UPDATE_ERROR",
                                Organization.OrgMainContact.Email,
                                "Sample Status Update",
                                new Dictionary <string, string>
                        {
                            { "BatchId", newSampleMessageStatus.batchId },
                            { "CreatorName", Organization.OrgMainContact.FullName },
                            { "ErrorMessage", newSampleMessageStatus.message },
                            { "CreateDateTime", DateTime.Now.ToString() }
                        });
                    }

                    /*
                     * if (newSampleMessageStatus.percentComplete == 100 || newSampleMessageStatus.percentComplete.Equals("100"))
                     * {
                     *  Batch.GNSequencerJob.Status = "COMPLETED";
                     *
                     *
                     *  //loop through all the samples, look for their analyses, and start them.
                     *  foreach (GNNewSampleBatchSamples sampleBatch in Batch.GNNewSampleBatchSamples)
                     *  {
                     *      sampleBatch.GNSample.IsReady = true;
                     *  }
                     *
                     *  db.SaveChanges();
                     *
                     *
                     *  if (Batch.AutoStartAnalysis)
                     *  {
                     *      //loop through all the samples, look for their analyses, and start them.
                     *      List<GNNewSampleBatchSamples> ListOfSamples = Batch.GNNewSampleBatchSamples.ToList();
                     *
                     *      foreach (GNNewSampleBatchSamples batchSample in ListOfSamples)
                     *      {
                     *          GNSample sample = batchSample.GNSample;
                     *          var result = this.AutostartAnalysis(sample);
                     *      }
                     *  }
                     *
                     *  try
                     *  {
                     *      String notifyCreateAnalysis = "";
                     *      if (Batch.CreateAnalysisPerSample)
                     *      {
                     *          notifyCreateAnalysis = "Each sample had one analysis created automatically.";
                     *      }
                     *
                     *      String notifyAutostartAnalysis = "";
                     *      if (Batch.AutoStartAnalysis)
                     *      {
                     *          notifyAutostartAnalysis = "Each analysis create was autostarted as soon as the samples were completed. New notifications will be sent to your inbox -with links to each analysis- as these move along.";
                     *      }
                     *
                     *      //NOTIFY USER
                     *      bool notifySuccess =
                     *              new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                     *                  "SAMPLE_STATUS_UPDATE_COMPLETE",
                     *                  "*****@*****.**", //Organization.OrgMainContact.Email,
                     *                  "Sample Status Update",
                     *                  new Dictionary<string, string>
                     *                      {
                     *                          {"ProjectName", Batch.GNSequencerJob.Project},
                     *                          {"TotalSamples", Batch.TotalSamples.ToString()},
                     *                          {"CreateAnalysisPerSample", notifyCreateAnalysis},
                     *                          {"AutoStartAnalysis", notifyAutostartAnalysis},
                     *                          {"ErrorMessage", newSampleMessageStatus.message},
                     *                          {"CreateDateTime",DateTime.Now.ToString()}
                     *                      });
                     *  }
                     *  catch (Exception e2)
                     *  {
                     *      System.Console.WriteLine("***Message " + e2.Message + e2.StackTrace + " **********************************");
                     *
                     *  }
                     *
                     * }
                     */
                }


                success = true;
            }
            catch (Exception e1)
            {
                Exception e2 = new Exception("Unable to Update Status of New Sample.", e1);
                System.Console.WriteLine("***\n  Exception: " + e1.Message + e1.StackTrace + e1.InnerException);
                LogUtil.Warn(logger, e2.Message, e2);
                success = false;
            }
            return(success);
        }
Пример #8
0
        /**
         * For this batch:
         *  - Add a status of 90%: Running quality control
         *  - Check that each sample:
         *      - Has at least one file
         *      - If the sample is pair-ended, it has an even number of files
         *      - If so, change to "is ready"
         *  If the batch has the "start analysis" in true, start analyses
         *  Log errors if apply
         *  Change status to 100% if no errors were found
         */
        public void QualityControl(GNNewSampleBatch Batch)
        {
            bool result = true;
            GNNewSampleBatchStatus batchStatus = new GNNewSampleBatchStatus
            {
                Id                 = Guid.NewGuid(),
                CreateDateTime     = DateTime.Now,
                GNNewSampleBatch   = Batch,
                GNNewSampleBatchId = Batch.Id,
                IsError            = false,
                PercentComplete    = 90,
                Status             = "RUNNING QUALITY CONTROL",
                Repository         = Batch.Repository,
                Message            = ""
            };

            db.GNNewSampleBatchStatus.Add(batchStatus);

            string errorMessage = "";

            foreach (GNNewSampleBatchSamples BatchSample in Batch.GNNewSampleBatchSamples)
            {
                GNSample sample = BatchSample.GNSample;
                if (sample.IsPairEnded && sample.CloudFiles.Count() > 0 && sample.CloudFiles.Count() % 2 == 0)
                {
                    sample.IsReady = true;
                }
                else if (!sample.IsPairEnded && sample.CloudFiles.Count() > 0 && sample.CloudFiles.Count() % 2 == 1)
                {
                    sample.IsReady = true;
                }
                else
                {
                    errorMessage += "Sample " + sample.Id.ToString() + "doesn't have the right number of files.\n";
                    result        = false;
                }
            }

            if (result && Batch.AutoStartAnalysis)
            {
                GNNewSampleBatchStatus batchStatus2 = new GNNewSampleBatchStatus
                {
                    Id                 = Guid.NewGuid(),
                    CreateDateTime     = DateTime.Now,
                    GNNewSampleBatch   = Batch,
                    GNNewSampleBatchId = Batch.Id,
                    IsError            = false,
                    PercentComplete    = 95,
                    Status             = "STARTING ANALYSIS",
                    Repository         = Batch.Repository,
                    Message            = ""
                };
                db.GNNewSampleBatchStatus.Add(batchStatus2);

                List <GNAnalysisRequest> analysisRequestsToStart = new List <GNAnalysisRequest>();
                foreach (GNNewSampleBatchSamples BatchSample in Batch.GNNewSampleBatchSamples)
                {
                    GNSample          sample   = BatchSample.GNSample;
                    GNAnalysisRequest analysis = sample.GNAnalysisRequestGNSamples.FirstOrDefault().GNAnalysisRequest;
                    if (!analysisRequestsToStart.Contains(analysis))
                    {
                        analysisRequestsToStart.Add(analysis);
                    }
                }

                foreach (GNAnalysisRequest analysis in analysisRequestsToStart)
                {
                    this.AutostartAnalysis(analysis);
                }

                GNNewSampleBatchStatus batchStatus3 = new GNNewSampleBatchStatus
                {
                    Id                 = Guid.NewGuid(),
                    CreateDateTime     = DateTime.Now,
                    GNNewSampleBatch   = Batch,
                    GNNewSampleBatchId = Batch.Id,
                    IsError            = false,
                    PercentComplete    = 100,
                    Status             = "COMPLETED",
                    Repository         = Batch.Repository,
                    Message            = ""
                };
                db.GNNewSampleBatchStatus.Add(batchStatus3);

                this.NotifyEndOfBatch(Batch);
            }

            if (!result)
            {
                GNNewSampleBatchStatus batchStatus4 = new GNNewSampleBatchStatus
                {
                    Id                 = Guid.NewGuid(),
                    CreateDateTime     = DateTime.Now,
                    GNNewSampleBatch   = Batch,
                    GNNewSampleBatchId = Batch.Id,
                    IsError            = true,
                    PercentComplete    = 92,
                    Status             = "ERRORS DETECTED",
                    Repository         = Batch.Repository,
                    Message            = ""
                };
                db.GNNewSampleBatchStatus.Add(batchStatus4);
                this.NotifyErrorAtEndOfBatch(Batch, errorMessage);
            }

            db.SaveChanges();
        }
Пример #9
0
        public override bool ProcessMessage(NewSampleBatch newSampleMessage, object queueMessage)
        {
            System.Console.WriteLine("***\n ****** Batch Id: " + newSampleMessage.batchId);
            System.Console.WriteLine("***\n ****** Sample type: " + newSampleMessage.type);
            System.Console.WriteLine("***\n ****** Sample repository: " + newSampleMessage.repository);

            SampleResponseService sampleResponseService = new SampleResponseService();

            List <GNSample> listOfSamples = new List <GNSample>();
            List <GNNewSampleBatchSamples> listOfBatchSamples = new List <GNNewSampleBatchSamples>();
            List <String> listOfAnalysisNames = new List <String>();

            GNOrganization Organization = db.GNOrganizations.Where(a => a.Repository.Equals(newSampleMessage.repository)).FirstOrDefault();

            System.Console.WriteLine("***\n  Organization " + Organization.Name);
            System.Console.WriteLine("***\n  Organization Id " + Organization.Id);
            System.Console.WriteLine("***\n  Project " + newSampleMessage.project);


            bool success = false;

            try
            {
                System.Console.WriteLine("***\n  Searching sequencerJob ");

                //Find sequencer job
                GNSequencerJob sequencerJob = db.GNSequencerJobs.Where(a => a.GNOrganizationId.Equals(Organization.Id) && a.Project.Equals(newSampleMessage.project)).FirstOrDefault();
                if (sequencerJob == null)
                {
                    throw new Exception("Unable to find Sequencer Job!");
                }

                sequencerJob.Status = "PROCESSING SAMPLES";

                System.Console.WriteLine("***\n  Starting.  sequencerJob project: " + sequencerJob.Project);

                GNProject newProject = sequencerJob.GNProject;
                GNTeam    newTeam    = newProject.Teams.FirstOrDefault();

                GNNewSampleBatch newSampleBatch = new GNNewSampleBatch
                {
                    Id                      = Guid.NewGuid(),
                    BatchId                 = newSampleMessage.batchId,
                    GNSequencerJobId        = sequencerJob.Id,
                    GNSequencerJob          = sequencerJob,
                    Project                 = newSampleMessage.project,
                    AutoStartAnalysis       = (newSampleMessage.autoStartAnalysis.ToLower().Equals("true")),
                    CreateAnalysisPerSample = (newSampleMessage.createAnalysisPerSample.ToLower().Equals("true")),
                    Qualifier               = newSampleMessage.qualifier,
                    Type                    = newSampleMessage.type,
                    Repository              = newSampleMessage.repository,
                    ReadType                = newSampleMessage.read,
                    TotalSamples            = newSampleMessage.samples.Count(),
                    TotalSamplesCompleted   = 0,
                    TotalNumberOfFastqFiles = 0,
                    CreateDateTime          = DateTime.Now
                };

                db.GNNewSampleBatches.Add(newSampleBatch);

                GNSampleType type = db.GNSampleTypes.Where(a => a.Name.Equals(newSampleMessage.type.ToUpper())).FirstOrDefault();
                System.Console.WriteLine("***\n  type " + type.Name);

                string datetimeString = DateTime.Now.ToString("MM-dd 0:HH:mm:ss");

                GNAnalysisType analysisType = null;
                String         AnalysisCode = newSampleMessage.qualifier;
                if (newSampleBatch.CreateAnalysisPerSample)
                {
                    /**
                     * 1. Create a Team
                     * 2. Create a Project
                     */
                    GNContact contact = db.GNContacts.Find(Organization.GNContactId);

                    if (newSampleMessage.qualifier.Equals("TUMOR"))
                    {
                        AnalysisCode = "TUMORNORMAL";
                    }
                    analysisType = db.GNAnalysisTypes.Where(a => a.Name.Equals(newSampleMessage.type.ToUpper())).FirstOrDefault();
                }

                int i = 0;
                GNAnalysisRequest newAnalysis = null;

                /**
                 * Loop samples
                 */

                GNNewSampleBatchStatus batchStatus = new GNNewSampleBatchStatus
                {
                    Id                 = Guid.NewGuid(),
                    CreateDateTime     = DateTime.Now,
                    GNNewSampleBatch   = newSampleBatch,
                    GNNewSampleBatchId = newSampleBatch.Id,
                    IsError            = false,
                    PercentComplete    = 0,
                    Status             = "STARTING PROCESS",
                    Repository         = newSampleMessage.repository
                };
                db.GNNewSampleBatchStatus.Add(batchStatus);
                System.Console.WriteLine("***\n  New batchStatus Created: " + batchStatus.Id);

                foreach (SampleBatch sampleBatch in newSampleMessage.samples)
                {
                    String newSampleName = sampleBatch.name;
                    //tfrege 2016.12.07 do not append family id to sample name anymore

                    /*
                     * if(sampleBatch.familyId != "")
                     * {
                     *  newSampleName = newSampleName + " (" + sampleBatch.familyId + ")";
                     *  System.Console.WriteLine("***\n ------------->  newSampleName: " + newSampleName);
                     * }
                     */

                    GNSample sampleExist     = db.GNSamples.Where(a => a.Name.Equals(sampleBatch.name) && a.GNOrganizationId.Equals(Organization.Id)).FirstOrDefault();
                    bool     createNewSample = false;


                    //Since BCL2FASTQ is not passing gender for now, figure it out based on the family relation
                    String sampleBatchgender = "U";
                    switch (sampleBatch.familyRelation)
                    {
                    case "F":
                    case "S":
                        sampleBatchgender = "M";
                        break;

                    case "M":
                    case "D":
                        sampleBatchgender = "F";
                        break;

                    default:
                        sampleBatchgender = "F";
                        break;
                    }

                    if (sampleExist != null)
                    {
                        if (sampleExist.IsReady)
                        {
                            //the sample is ready, let's create a new one with a suffix of (1)
                            newSampleName   = sampleBatch.name + " (1)";
                            createNewSample = true;
                        }
                        else
                        {
                            //the sample is in the middle of being created, so ignore the message
                            createNewSample = false;
                            //tfrege remove this control just for testing purposes, create new sample always
                            newSampleName   = sampleBatch.name + " (1)"; //remove this after validation is put back in
                            createNewSample = true;
                        }
                    }
                    else
                    {
                        createNewSample = true;
                    }

                    try
                    {
                        if (createNewSample)
                        {
                            GNReplicate replicate = db.GNReplicates.Where(a => a.Name.Equals("NO")).FirstOrDefault();

                            GNSample newSample = new GNSample
                            {
                                Id = Guid.NewGuid(),
                                GNOrganizationId      = Organization.Id,
                                Name                  = newSampleName,
                                Gender                = sampleBatchgender,
                                GNSampleTypeId        = type.Id,
                                GNSampleQualifierCode = newSampleMessage.qualifier,
                                IsReady               = false,
                                IsPairEnded           = (newSampleMessage.read == "paired-end"),
                                GNReplicateCode       = "0",
                                GNReplicate           = replicate,
                                CreateDateTime        = DateTime.Now,
                                CreatedBy             = CreatedBy
                            };

                            db.GNSamples.Add(newSample);
                            System.Console.WriteLine("***\n  New Sample Created: " + newSample.Id);


                            string analysisName = sampleBatch.name;
                            if (sampleBatch.familyId != null)
                            {
                                analysisName = sampleBatch.familyId;
                            }

                            GNNewSampleBatchSamples batchSample = new GNNewSampleBatchSamples {
                                Id                 = Guid.NewGuid(),
                                GNSample           = newSample,
                                CreateDateTime     = DateTime.Now,
                                GNNewSampleBatch   = newSampleBatch,
                                GNNewSampleBatchId = newSampleBatch.Id,
                                Name               = sampleBatch.name,
                                Affected           = sampleBatch.affected,
                                Proband            = sampleBatch.proband,
                                FamilyId           = sampleBatch.familyId,
                                RelationId         = sampleBatch.familyRelation,
                                Gender             = sampleBatchgender,
                                AnalysisName       = analysisName
                            };
                            db.GNNewSampleBatchSamples.Add(batchSample);
                            System.Console.WriteLine("***\n  New batchSample Created: " + batchSample.Id);
                            System.Console.WriteLine("***\n  ====>>>>> Adding to listOfAnalysisNames: " + analysisName);

                            if (!listOfAnalysisNames.Contains(analysisName))
                            {
                                listOfAnalysisNames.Add(analysisName);
                            }

                            db.SaveChanges();
                            System.Console.WriteLine("***\n  DB CHANGES SAVED.");

                            System.Console.WriteLine("***\n  Sample Name: " + sampleBatch.name);

                            //Build list to notify the BCL2FASTQ service later
                            listOfSamples.Add(newSample);
                            listOfBatchSamples.Add(batchSample);
                        }
                    }
                    catch (Exception e1)
                    {
                        System.Console.WriteLine("***\n  Exception: " + e1.Message + e1.StackTrace + e1.InnerException);
                    }
                    i++;
                }

                /**
                 * Update pedigrees, if applies
                 */
                int maxPedigreeId = db.GNSampleRelationships.Max(a => a.Id);
                System.Console.WriteLine("***\n  Updating pedigrees for families ");

                foreach (GNNewSampleBatchSamples pedigreeSample in listOfBatchSamples.Where(a => a.Gender != "U" && a.RelationId != ""))
                {
                    switch (pedigreeSample.RelationId)
                    {
                    case "F":
                    case "M":
                    {
                        GNSampleRelationshipType relationshipType = db.GNSampleRelationshipTypes.Where(a => a.Name.Equals("SON")).FirstOrDefault();

                        //find sons and daughters
                        List <GNNewSampleBatchSamples> sons = listOfBatchSamples.Where(a => a.FamilyId.Equals(pedigreeSample.FamilyId) && a.RelationId.Equals("S")).ToList();
                        foreach (GNNewSampleBatchSamples son in sons)
                        {
                            GNSampleRelationship sampleRelationship = new GNSampleRelationship
                            {
                                Id                         = maxPedigreeId++,
                                GNLeftSample               = pedigreeSample.GNSample,
                                GNLeftSampleId             = pedigreeSample.GNSample.Id,
                                GNRightSample              = son.GNSample,
                                GNRightSampleId            = son.GNSample.Id,
                                GNSampleRelationshipType   = relationshipType,
                                GNSampleRelationshipTypeId = relationshipType.Id,
                                CreateDateTime             = DateTime.Now,
                                CreatedBy                  = CreatedBy
                            };
                            db.GNSampleRelationships.Add(sampleRelationship);
                            System.Console.WriteLine("***\n  Added Son " + son.GNSample.Id + " to Parent Sample : " + pedigreeSample.GNSample.Id);
                        }

                        relationshipType = db.GNSampleRelationshipTypes.Where(a => a.Name.Equals("DAUGHTER")).FirstOrDefault();

                        //find sons and daughters
                        List <GNNewSampleBatchSamples> daughters = listOfBatchSamples.Where(a => a.FamilyId.Equals(pedigreeSample.FamilyId) && a.RelationId.Equals("D")).ToList();
                        foreach (GNNewSampleBatchSamples daughter in daughters)
                        {
                            GNSampleRelationship sampleRelationship = new GNSampleRelationship
                            {
                                Id                         = maxPedigreeId++,
                                GNLeftSample               = pedigreeSample.GNSample,
                                GNLeftSampleId             = pedigreeSample.GNSample.Id,
                                GNRightSample              = daughter.GNSample,
                                GNRightSampleId            = daughter.GNSample.Id,
                                GNSampleRelationshipType   = relationshipType,
                                GNSampleRelationshipTypeId = relationshipType.Id,
                                CreateDateTime             = DateTime.Now,
                                CreatedBy                  = CreatedBy
                            };
                            db.GNSampleRelationships.Add(sampleRelationship);
                            System.Console.WriteLine("***\n  Added Daughter " + daughter.GNSample.Id + " to Parent Sample : " + pedigreeSample.GNSample.Id);
                        }
                    } break;

                    case "S":
                    case "D":
                    {
                        GNSampleRelationshipType relationshipType = db.GNSampleRelationshipTypes.Where(a => a.Name.Equals("FATHER")).FirstOrDefault();

                        //find sons and daughters
                        List <GNNewSampleBatchSamples> fathers = listOfBatchSamples.Where(a => a.FamilyId.Equals(pedigreeSample.FamilyId) && a.RelationId.Equals("F")).ToList();
                        foreach (GNNewSampleBatchSamples dad in fathers)
                        {
                            GNSampleRelationship sampleRelationship = new GNSampleRelationship
                            {
                                Id                         = maxPedigreeId++,
                                GNLeftSample               = pedigreeSample.GNSample,
                                GNLeftSampleId             = pedigreeSample.GNSample.Id,
                                GNRightSample              = dad.GNSample,
                                GNRightSampleId            = dad.GNSample.Id,
                                GNSampleRelationshipType   = relationshipType,
                                GNSampleRelationshipTypeId = relationshipType.Id,
                                CreateDateTime             = DateTime.Now,
                                CreatedBy                  = CreatedBy
                            };
                            db.GNSampleRelationships.Add(sampleRelationship);
                            System.Console.WriteLine("***\n  Added Dad " + dad.GNSample.Id + " to Child Sample : " + pedigreeSample.GNSample.Id);
                        }

                        relationshipType = db.GNSampleRelationshipTypes.Where(a => a.Name.Equals("MOTHER")).FirstOrDefault();

                        //find sons and daughters
                        List <GNNewSampleBatchSamples> mothers = listOfBatchSamples.Where(a => a.FamilyId.Equals(pedigreeSample.FamilyId) && a.RelationId.Equals("M")).ToList();
                        foreach (GNNewSampleBatchSamples mom in mothers)
                        {
                            GNSampleRelationship sampleRelationship = new GNSampleRelationship
                            {
                                Id                         = maxPedigreeId++,
                                GNLeftSample               = pedigreeSample.GNSample,
                                GNLeftSampleId             = pedigreeSample.GNSample.Id,
                                GNRightSample              = mom.GNSample,
                                GNRightSampleId            = mom.GNSample.Id,
                                GNSampleRelationshipType   = relationshipType,
                                GNSampleRelationshipTypeId = relationshipType.Id,
                                CreateDateTime             = DateTime.Now,
                                CreatedBy                  = CreatedBy
                            };
                            db.GNSampleRelationships.Add(sampleRelationship);
                            System.Console.WriteLine("***\n  Added Mom " + mom.GNSample.Id + " to Child Sample : " + pedigreeSample.GNSample.Id);
                        }
                    } break;

                    case "U":
                    default:
                        //don't do a thing.
                        System.Console.WriteLine("***\n  Relation is undefined, do not add anything.");
                        break;
                    }
                }

                /******************************************************************************************************/
                //SAVE ALL CHANGES (new team, new project, new samples, new analyses, new batch records, new pedigrees)
                /******************************************************************************************************/

                try
                {
                    db.SaveChanges();
                }
                catch (Exception eRDS)
                {
                    System.Console.WriteLine("***EXCEPTION DB!!! " + eRDS.Message + " " + eRDS.StackTrace + " " + eRDS.InnerException);
                }


                try
                {
                    if (newSampleMessage.autoStartAnalysis.ToLower().Equals("true"))
                    {
                        System.Console.WriteLine("***\n  Elements: " + listOfAnalysisNames.Count());

                        foreach (String anName in listOfAnalysisNames)
                        {
                            List <GNNewSampleBatchSamples> samplesForAnalysis = new List <GNNewSampleBatchSamples>();

                            newAnalysis = new GNAnalysisRequest
                            {
                                Id                        = Guid.NewGuid(),
                                Project                   = newProject,
                                GNProjectId               = newProject.Id,
                                CreateDateTime            = DateTime.Now,
                                CreatedBy                 = CreatedBy,
                                AnalysisType              = analysisType,
                                RequestProgress           = 0,
                                RequestDateTime           = DateTime.Now,
                                GNAnalysisRequestTypeCode = AnalysisCode,
                                Description               = anName,
                                GNAnalysisAdaptorCode     = "NONE",
                                AnalysisTypeId            = analysisType.Id.ToString(),
                                AutoStart                 = (newSampleMessage.autoStartAnalysis.ToLower().Equals("true")),
                                AWSRegionSystemName       = db.AWSRegions.FirstOrDefault().AWSRegionSystemName
                            };
                            db.GNAnalysisRequests.Add(newAnalysis);
                            System.Console.WriteLine("***\n  ========================\n New newAnalysis Created: " + newAnalysis.Id);

                            samplesForAnalysis = db.GNNewSampleBatchSamples.Where(a => a.GNNewSampleBatchId.Equals(newSampleBatch.Id) && a.AnalysisName.Equals(anName)).ToList();
                            foreach (GNNewSampleBatchSamples sample in samplesForAnalysis)
                            {
                                GNAnalysisRequestGNSample newAnalysisSample = new GNAnalysisRequestGNSample
                                {
                                    AffectedIndicator   = sample.Affected,
                                    TargetIndicator     = sample.Proband,
                                    GNAnalysisRequest   = newAnalysis,
                                    GNSample            = sample.GNSample,
                                    GNAnalysisRequestId = newAnalysis.Id,
                                    GNSampleId          = sample.GNSample.Id
                                };
                                db.GNAnalysisRequestGNSamples.Add(newAnalysisSample);
                                System.Console.WriteLine("***\n  Sample Added to New newAnalysis: " + sample.GNSample.Name);
                            }
                        }
                    } //end of "if(newSampleBatch.CreateAnalysisPerSample)"
                }
                catch (Exception eAnalysis)
                {
                    System.Console.WriteLine("***\n  Exception: " + eAnalysis.Message + eAnalysis.StackTrace + eAnalysis.InnerException);
                }


                db.SaveChanges();

                //Notify BCL2FASTQ service
                sampleResponseService.NotifyBcl2FastqSystem(listOfBatchSamples);

                System.Console.WriteLine("***\n  EVERYTHING WORKED!");

                //NOTIFY USER
                bool notifySuccess =
                    new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                        "SAMPLE_CREATION",
                        Organization.OrgMainContact.Email,
                        "Sample Creation",
                        new Dictionary <string, string>
                {
                    { "BatchId", newSampleMessage.batchId },
                    { "TotalSamples", newSampleBatch.TotalSamples.ToString() },
                    { "CreatorName", Organization.OrgMainContact.FullName },
                    { "JobId", sequencerJob.Id.ToString() },
                    { "ProjectName", sequencerJob.Project },
                    { "CreateDateTime", DateTime.Now.ToString() }
                });
                success = true;
            }
            catch (Exception e1)
            {
                Exception e2 = new Exception("Unable to Create New Sample.", e1);
                LogUtil.Warn(logger, e2.Message, e2);
                success = false;
            }
            return(success);
        }