github.com/Bio-core/jtree@v0.0.0-20190705165106-1d7a7e7d6272/dummydata/dummydata.go (about) 1 package dummydata 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "log" 8 "math/rand" 9 "net/http" 10 "path/filepath" 11 "strconv" 12 "time" 13 14 models "github.com/Bio-core/jtree/models" 15 repos "github.com/Bio-core/jtree/repos" 16 yaml "gopkg.in/yaml.v2" 17 ) 18 19 var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") 20 var r *rand.Rand 21 var id1 int 22 var id2 int 23 var id3 int 24 var id4 int 25 var id5 int 26 var genes *GeneArray 27 var random []RandomPerson 28 29 const shortForm = "2006-01-02" 30 31 //GeneArray is an object with an array of gene types 32 type GeneArray struct { 33 Genes []string 34 } 35 36 //RandomPerson is a scruct to help make real names 37 type RandomPerson struct { 38 Name string `json:"name,omitempty"` 39 Surname string `json:"surname,omitempty"` 40 Gender string `json:"gender,omitempty"` 41 Region string `json:"region,omitempty"` 42 } 43 44 //MakeData makes dummy data and puts it into the db 45 func MakeData(number int) { 46 r = rand.New(rand.NewSource(99)) 47 random = getManyRandomPeople(number) 48 genes = &GeneArray{} 49 genes = genes.GetGenes() 50 num1 := createPatients(number) 51 num2 := createSamples(num1) 52 num3 := createExperiments(num2) 53 num4 := createResults(num3) 54 createResultDetails(num4) 55 } 56 57 func makeRandomString() string { 58 num := rand.Intn(50) 59 value := randSeq(num) 60 return value 61 } 62 func makeRandomName(id int, last bool) string { 63 if id < 0 { 64 id = rand.Intn(99) + 1 65 } 66 if last { 67 return random[id-1].Surname 68 } 69 return random[id-1].Name 70 } 71 72 func makeRandomDate() string { 73 year := strconv.Itoa(rand.Intn(118) + 1900) 74 monthint := rand.Intn(11) + 1 75 month := strconv.Itoa(monthint) 76 if monthint < 10 { 77 month = "0" + month 78 } 79 dayint := rand.Intn(27) + 1 80 day := strconv.Itoa(dayint) 81 if dayint < 10 { 82 day = "0" + day 83 } 84 date := year + "-" + month + "-" + day 85 return date 86 } 87 88 func makeRandomFloat() float32 { 89 num := rand.Float32() 90 num += float32(rand.Intn(600)) 91 return num 92 } 93 94 func makeRandomBool() bool { 95 num := rand.Intn(1) 96 if num == 1 { 97 return true 98 } 99 return false 100 } 101 102 func makeRandomGene() string { 103 num := genrand(0, 568, 0, 568, 5) 104 return genes.Genes[num] 105 } 106 107 func getManyRandomPeople(num int) []RandomPerson { 108 url := fmt.Sprintf("https://uinames.com/api/?region=england&amount=%v", num) 109 110 // Build the request 111 req, err := http.NewRequest("GET", url, nil) 112 if err != nil { 113 log.Fatal("NewRequest: ", err) 114 return nil 115 } 116 117 client := &http.Client{} 118 resp, err := client.Do(req) 119 if err != nil { 120 log.Fatal("Do: ", err) 121 return nil 122 } 123 defer resp.Body.Close() 124 var persons []RandomPerson 125 126 if err := json.NewDecoder(resp.Body).Decode(&persons); err != nil { 127 log.Println(err) 128 } 129 return persons 130 131 } 132 133 func genrand(bmin, bmax, rmin, rmax, n int) int { 134 const randMax = 32767 135 // Generalized random number generator; 136 // sum of n random variables (usually 3). 137 // Bell curve spans bmin<=x<bmax; then, 138 // values outside rmin<=x<rmax are rejected. 139 var sum, i, u int 140 sum = 0 141 for { 142 for i = 0; i < n; i++ { 143 sum += bmin + (rand.Intn(randMax) % (bmax - bmin)) 144 } 145 if sum < 0 { 146 sum -= n - 1 147 } 148 u = sum / n 149 150 if rmin <= u && u < rmax { 151 break 152 } 153 } 154 return u 155 } 156 157 func createPatients(number int) int { 158 for i := 0; i < number; i++ { 159 id1++ 160 tempPatient := MakePatient(id1) 161 repos.InsertPatient(&tempPatient) 162 } 163 return id1 164 } 165 func createResults(number int) int { 166 id1 = 0 167 for i := 0; i < number; i++ { 168 id1++ 169 c := rand.Intn(2) + 1 170 for j := 0; j < c; j++ { 171 id4++ 172 tempResult := MakeResult(id1, id4) 173 repos.InsertResult(&tempResult) 174 } 175 } 176 return id4 177 } 178 func createResultDetails(number int) int { 179 id1 = 0 180 for i := 0; i < number; i++ { 181 id1++ 182 c := rand.Intn(2) + 1 183 for j := 0; j < c; j++ { 184 id5++ 185 tempResultDetail := MakeResultDetail(id1, id5) 186 repos.InsertResultDetail(&tempResultDetail) 187 } 188 } 189 return id5 190 } 191 192 func createSamples(number int) int { 193 id1 = 0 194 for i := 0; i < number; i++ { 195 id1++ 196 c := rand.Intn(5) + 1 197 for j := 0; j < c; j++ { 198 id2++ 199 tempSample := MakeSample(id1, id2) 200 repos.InsertSample(&tempSample) 201 } 202 } 203 return id2 204 } 205 func createExperiments(number int) int { 206 id1 = 0 207 for i := 0; i < number; i++ { 208 id1++ 209 c := rand.Intn(5) + 1 210 for j := 0; j < c; j++ { 211 id3++ 212 tempExperiment := MakeExperiment(id1, id3) 213 repos.InsertExperiment(&tempExperiment) 214 } 215 } 216 return id3 217 } 218 219 func randSeq(n int) string { 220 b := make([]rune, n) 221 for i := range b { 222 b[i] = letters[rand.Intn(len(letters))] 223 } 224 return string(b) 225 } 226 227 //MakePatient makes a patient provided a pateint ID 228 func MakePatient(patientID int) models.Patient { 229 patient := models.Patient{} 230 ClinicalHistory := makeRandomString() 231 patient.ClinicalHistory = &ClinicalHistory 232 DateReceived, _ := time.Parse(shortForm, makeRandomDate()) 233 patient.DateReceived = &DateReceived 234 DateReported, _ := time.Parse(shortForm, makeRandomDate()) 235 patient.DateReported = &DateReported 236 Dob, _ := time.Parse(shortForm, makeRandomDate()) 237 patient.Dob = &Dob 238 FirstName := makeRandomName(patientID, false) 239 patient.FirstName = &FirstName 240 Gender := makeRandomString() 241 patient.Gender = &Gender 242 Initials := makeRandomString() 243 patient.Initials = &Initials 244 LastName := makeRandomName(patientID, true) 245 patient.LastName = &LastName 246 Mrn := makeRandomString() 247 patient.Mrn = &Mrn 248 OnHcn := makeRandomString() 249 patient.OnHcn = &OnHcn 250 PatientID := strconv.Itoa(patientID) 251 if patientID > 0 { 252 patient.PatientID = &PatientID 253 } 254 PatientType := makeRandomString() 255 patient.PatientType = &PatientType 256 ReferringPhysican := makeRandomString() 257 patient.ReferringPhysican = &ReferringPhysican 258 SeNum := makeRandomString() 259 patient.SeNum = &SeNum 260 SurgicalDate, _ := time.Parse(shortForm, makeRandomDate()) 261 patient.SurgicalDate = &SurgicalDate 262 263 return patient 264 } 265 266 //MakeSample makes a sample provided a sample ID 267 func MakeSample(patientID int, sampleID int) models.Sample { 268 sample := models.Sample{} 269 SampleID := strconv.Itoa(sampleID) 270 if sampleID > 0 { 271 sample.SampleID = &SampleID 272 } 273 Facility := makeRandomString() 274 sample.Facility = &Facility 275 TestRequested := makeRandomString() 276 sample.TestRequested = &TestRequested 277 SeNum := makeRandomString() 278 sample.SeNum = &SeNum 279 DateCollected, _ := time.Parse(shortForm, makeRandomDate()) 280 sample.DateCollected = &DateCollected 281 DateReceived, _ := time.Parse(shortForm, makeRandomDate()) 282 sample.DateReceived = &DateReceived 283 SampleType := makeRandomString() 284 sample.SampleType = &SampleType 285 MaterialReceived := makeRandomString() 286 sample.MaterialReceived = &MaterialReceived 287 MaterialReceivedNum := makeRandomString() 288 sample.MaterialReceivedNum = &MaterialReceivedNum 289 MaterialReceivedOther := makeRandomString() 290 sample.MaterialReceivedOther = &MaterialReceivedOther 291 VolumeOfBloodMarrow := makeRandomFloat() 292 sample.VolumeOfBloodMarrow = &VolumeOfBloodMarrow 293 SurgicalNum := makeRandomString() 294 sample.SurgicalNum = &SurgicalNum 295 TumorSite := makeRandomString() 296 sample.TumorSite = &TumorSite 297 HistoricalDiagnosis := makeRandomString() 298 sample.HistoricalDiagnosis = &HistoricalDiagnosis 299 TumorPercntOfTotal := makeRandomFloat() 300 sample.TumorPercntOfTotal = &TumorPercntOfTotal 301 TumorPercntOfCircled := makeRandomFloat() 302 sample.TumorPercntOfCircled = &TumorPercntOfCircled 303 ReviewedBy := makeRandomString() 304 sample.ReviewedBy = &ReviewedBy 305 HESlideLocation := makeRandomString() 306 sample.HESlideLocation = &HESlideLocation 307 NonUhnID := makeRandomString() 308 sample.NonUhnID = &NonUhnID 309 NameOfRequestor := makeRandomString() 310 sample.NameOfRequestor = &NameOfRequestor 311 DnaConcentration := makeRandomFloat() 312 sample.DnaConcentration = &DnaConcentration 313 DnaVolume := makeRandomFloat() 314 sample.DnaVolume = &DnaVolume 315 DnaLocation := makeRandomString() 316 sample.DnaLocation = &DnaLocation 317 RnaConcentration := makeRandomFloat() 318 sample.RnaConcentration = &RnaConcentration 319 RnaVolume := makeRandomFloat() 320 sample.RnaVolume = &RnaVolume 321 RnaLocation := makeRandomString() 322 sample.RnaLocation = &RnaLocation 323 WbcLocation := makeRandomString() 324 sample.WbcLocation = &WbcLocation 325 PlasmaLocation := makeRandomString() 326 sample.PlasmaLocation = &PlasmaLocation 327 CfPlasmaLocation := makeRandomString() 328 sample.CfPlasmaLocation = &CfPlasmaLocation 329 PbBmLocation := makeRandomString() 330 sample.PbBmLocation = &PbBmLocation 331 RnaLysateLocation := makeRandomString() 332 sample.RnaLysateLocation = &RnaLysateLocation 333 SampleSize := makeRandomString() 334 sample.SampleSize = &SampleSize 335 StudyID := makeRandomString() 336 sample.StudyID = &StudyID 337 SampleName := makeRandomString() 338 sample.SampleName = &SampleName 339 DateSubmitted, _ := time.Parse(shortForm, makeRandomDate()) 340 sample.DateSubmitted = &DateSubmitted 341 ContainerType := makeRandomString() 342 sample.ContainerType = &ContainerType 343 ContainerID := makeRandomString() 344 sample.ContainerID = &ContainerID 345 ContainerWell := makeRandomString() 346 sample.ContainerWell = &ContainerWell 347 CopathNum := makeRandomString() 348 sample.CopathNum = &CopathNum 349 OtherIdentifier := makeRandomString() 350 sample.OtherIdentifier = &OtherIdentifier 351 HasSampleFiles := makeRandomBool() 352 sample.HasSampleFiles = &HasSampleFiles 353 DnaSampleBarcode := makeRandomString() 354 sample.DnaSampleBarcode = &DnaSampleBarcode 355 DnaExtractionDate, _ := time.Parse(shortForm, makeRandomDate()) 356 sample.DnaExtractionDate = &DnaExtractionDate 357 DnaQuality := makeRandomString() 358 sample.DnaQuality = &DnaQuality 359 FfpeQcDate, _ := time.Parse(shortForm, makeRandomDate()) 360 sample.FfpeQcDate = &FfpeQcDate 361 DeltaCtValue := makeRandomFloat() 362 sample.DeltaCtValue = &DeltaCtValue 363 Comments := makeRandomString() 364 sample.Comments = &Comments 365 RnasePDate, _ := time.Parse(shortForm, makeRandomDate()) 366 sample.RnasePDate = &RnasePDate 367 DnaQualityByRnaseP := makeRandomFloat() 368 sample.DnaQualityByRnaseP = &DnaQualityByRnaseP 369 RnaQuality := makeRandomFloat() 370 sample.RnaQuality = &RnaQuality 371 RnaExtractionDate, _ := time.Parse(shortForm, makeRandomDate()) 372 sample.RnaExtractionDate = &RnaExtractionDate 373 PatientID := strconv.Itoa(patientID) 374 sample.PatientID = &PatientID 375 376 return sample 377 } 378 379 //MakeExperiment makes a experiemnt provided a experiment ID 380 func MakeExperiment(sampleID int, experimentID int) models.Experiment { 381 experiment := models.Experiment{} 382 ChipCartridgeBarcode := makeRandomString() 383 experiment.ChipCartridgeBarcode = &ChipCartridgeBarcode 384 CompleteDate, _ := time.Parse(shortForm, makeRandomDate()) 385 experiment.CompleteDate = &CompleteDate 386 ExperimentID := strconv.Itoa(experimentID) 387 if experimentID > 0 { 388 experiment.ExperimentID = &ExperimentID 389 } 390 HasProjectFiles := makeRandomBool() 391 experiment.HasProjectFiles = &HasProjectFiles 392 OpenedDate, _ := time.Parse(shortForm, makeRandomDate()) 393 experiment.OpenedDate = &OpenedDate 394 PanelAssayScreened := makeRandomString() 395 experiment.PanelAssayScreened = &PanelAssayScreened 396 Pcr := makeRandomString() 397 experiment.Pcr = &Pcr 398 Priority := makeRandomString() 399 experiment.Priority = &Priority 400 ProcedureOrderDatetime, _ := time.Parse(shortForm, makeRandomDate()) 401 experiment.ProcedureOrderDatetime = &ProcedureOrderDatetime 402 ProjectID := makeRandomString() 403 experiment.ProjectID = &ProjectID 404 ProjectName := makeRandomString() 405 experiment.ProjectName = &ProjectName 406 SampleID := strconv.Itoa(sampleID) 407 experiment.SampleID = &SampleID 408 StudyID := makeRandomString() 409 experiment.StudyID = &StudyID 410 TestDate, _ := time.Parse(shortForm, makeRandomDate()) 411 experiment.TestDate = &TestDate 412 413 return experiment 414 } 415 416 //MakeResult makes a result provided a result ID 417 func MakeResult(experimentID int, resultID int) models.Result { 418 result := models.Result{} 419 FailedRegions := makeRandomString() 420 result.FailedRegions = &FailedRegions 421 MeanDepthOfCoveage := makeRandomFloat() 422 result.MeanDepthOfCoveage = &MeanDepthOfCoveage 423 MlpaPcr := makeRandomString() 424 result.MlpaPcr = &MlpaPcr 425 Mutation := makeRandomString() 426 result.Mutation = &Mutation 427 OverallHotspotsThreshold := makeRandomFloat() 428 result.OverallHotspotsThreshold = &OverallHotspotsThreshold 429 OverallQualityThreshold := makeRandomFloat() 430 result.OverallQualityThreshold = &OverallQualityThreshold 431 ResultsID := strconv.Itoa(resultID) 432 if resultID > 0 { 433 result.ResultsID = &ResultsID 434 } 435 ExperimentID := strconv.Itoa(experimentID) 436 result.ExperimentID = &ExperimentID 437 UID := makeRandomString() 438 result.UID = &UID 439 VerificationPcr := makeRandomString() 440 result.VerificationPcr = &VerificationPcr 441 442 return result 443 } 444 445 //MakeResultDetail makes a result detail provided a resiltdetail ID 446 func MakeResultDetail(resultID int, resultdetailID int) models.Resultdetails { 447 resultdetail := models.Resultdetails{} 448 CNomenclature := makeRandomString() 449 resultdetail.CNomenclature = &CNomenclature 450 Coverage := int64(rand.Intn(1000)) 451 resultdetail.Coverage = &Coverage 452 Exon := int64(rand.Intn(1000)) 453 resultdetail.Exon = &Exon 454 Gene := makeRandomGene() 455 resultdetail.Gene = &Gene 456 Pcr := makeRandomString() 457 resultdetail.Pcr = &Pcr 458 PNomenclature := makeRandomString() 459 resultdetail.PNomenclature = &PNomenclature 460 QualityScore := makeRandomFloat() 461 resultdetail.QualityScore = &QualityScore 462 Result := makeRandomString() 463 resultdetail.Result = &Result 464 ResultsDetailsID := strconv.Itoa(resultdetailID) 465 if resultdetailID > 0 { 466 resultdetail.ResultsDetailsID = &ResultsDetailsID 467 } 468 ResultsID := strconv.Itoa(resultID) 469 resultdetail.ResultsID = &ResultsID 470 RiskScore := makeRandomFloat() 471 resultdetail.RiskScore = &RiskScore 472 UID := makeRandomString() 473 resultdetail.UID = &UID 474 VAF := makeRandomFloat() 475 resultdetail.VAF = &VAF 476 477 return resultdetail 478 } 479 480 //GetGenes fills the gene array struct 481 func (g *GeneArray) GetGenes() *GeneArray { 482 path, _ := filepath.Abs("./models/genes.yaml") 483 yamlFile, err := ioutil.ReadFile(path) 484 if err != nil { 485 log.Printf("yamlFile.Get err #%v ", err) 486 } 487 err = yaml.Unmarshal(yamlFile, g) 488 if err != nil { 489 log.Fatalf("Unmarshal: %v", err) 490 } 491 return g 492 }