コード例 #1
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 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);
        }
コード例 #2
0
        public override bool ProcessMessage(Sequencer sequencerMsg, object queueMessage)
        {
            System.Console.WriteLine("***\n ****** bucket Name: " + sequencerMsg.bucket);
            System.Console.WriteLine("***\n ****** project name: " + sequencerMsg.project_name);
            bool success = false;

            try
            {
                System.Console.WriteLine("***\n  Searching for Org " + sequencerMsg.bucket);
                GNOrganization organization = db.GNOrganizations.Where(a => a.Repository.Equals(sequencerMsg.bucket)).FirstOrDefault();
                System.Console.WriteLine("***\n ****** organization: " + organization.Name);

                if (organization == null)
                {
                    System.Console.WriteLine("***\n  NO ORG FOUND!!!!");
                    return(success);
                }

                System.Console.WriteLine("***\n  organization: " + organization.Id);
                if (sequencerMsg.bucket.Equals("ERROR") && sequencerMsg.project_name.Equals("UNDEFINED"))
                {
                    this.NotifyError(sequencerMsg);
                    return(true);
                }


                //check if a project is already undergoing for the same Org and same name (repeated message)
                int seqJobsRunning = db.GNSequencerJobs.Where(a => a.GNOrganizationId.Equals(organization.Id) && a.Project.Equals(sequencerMsg.project_name.Trim())).Count();
                //if none exists, create
                if (seqJobsRunning == 0)
                {
                    string datetimeString = DateTime.Now.ToString("MM-dd 0:HH:mm:ss");

                    /**
                     * 1. Create a Team
                     * 2. Create a Project
                     */
                    GNContact contact = db.GNContacts.Find(organization.GNContactId);

                    GNTeam newTeam = new GNTeam
                    {
                        Id             = Guid.NewGuid(),
                        CreateDateTime = DateTime.Now,
                        CreatedBy      = organization.GNContactId,
                        Name           = "Batch " + datetimeString,
                        GNContactId    = contact.Id,
                        Organization   = organization,
                        OrganizationId = organization.Id,
                        TeamLead       = contact
                    };

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

                    Guid newProjectId = Guid.NewGuid();

                    System.Console.WriteLine("*********************************\n  Contact for project: " + contact.FullName);
                    System.Console.WriteLine("*********************************\n  Contact for project: " + newProjectId);

                    GNProject newProject = new GNProject
                    {
                        Id             = newProjectId,
                        CreateDateTime = DateTime.Now,
                        CreatedBy      = organization.GNContactId,
                        ProjectLead    = contact,
                        ProjectLeadId  = contact.Id.ToString(),
                        Name           = sequencerMsg.project_name, //name assigned to the project
                        TeamId         = newTeam.Id.ToString(),
                        StartDate      = DateTime.Now,
                        EndDate        = DateTime.Now.AddDays(30),
                        Description    = "Created automatically from the Sample Batch Process"
                    };

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

                    System.Console.WriteLine("*********************************\n  Contact for project: " + newProjectId);
                    GNSequencerJob sequencerJob = new GNSequencerJob
                    {
                        Id               = Guid.NewGuid(),
                        CreateDateTime   = DateTime.Now,
                        Project          = sequencerMsg.project_name,
                        Status           = "STARTED",
                        GNOrganization   = organization,
                        GNOrganizationId = organization.Id,
                        GNProject        = newProject
                    };

                    db.GNSequencerJobs.Add(sequencerJob);
                    System.Console.WriteLine("***\n  New sequencerJob Created: " + sequencerJob.Id);

                    try
                    {
                        db.SaveChanges();
                        //NOTIFY USER
                        bool notifySuccess =
                            new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                                "SEQUENCER_JOB_STARTED",
                                "*****@*****.**",
                                "SequencerJob:" + sequencerJob.Id.ToString(),
                                new Dictionary <string, string>
                        {
                            { "JobId", sequencerJob.Id.ToString() },
                            { "ProjectName", sequencerJob.Project },
                            { "CreateDateTime", DateTime.Now.ToString() }
                        });
                        success = true;
                    }
                    catch (Exception eRDS)
                    {
                        System.Console.WriteLine("***\n  EXCEPCION!!! " + eRDS.Message + eRDS.StackTrace + eRDS.InnerException);

                        success = false;
                    }
                }
            }
            catch (Exception e1)
            {
                Exception e2 = new Exception("Unable to process Sequencer Job Message.", e1);
                System.Console.WriteLine("***\n  EXCEPCION!!! " + e1.Message + e1.StackTrace);
                LogUtil.Warn(logger, e2.Message, e2);
                success = false;
            }
            return(success);
        }
コード例 #3
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);
        }