/// <summary> /// This method is called for every Lambda invocation. This method takes in an S3 event object and can be used /// to respond to S3 notifications. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task BPMToBIMLambdaFunctionHandler(S3Event evnt, ILambdaContext context) { JObject preference = new Preference().preference; var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return; } try { var bpm = new BeadPoolManifest(new BinaryReader(readStreamFromS3ToMemory(bucketName: s3Event.Bucket.Name, keyName: s3Event.Object.Key, bucketRegion: preference["S3"]["BPM"]["Region"].ToString()).Result)); var ms = new MemoryStream(); var sw = new StreamWriter(ms); for (int idx = 0; idx < bpm.num_loci; ++idx) { List <string> bimEntry = new List <string> { bpm.chroms[idx], bpm.names[idx], "0", bpm.map_infos[idx].ToString() }; List <string> alleles = bpm.ref_strands[idx] == 1 ? bpm.snps[idx].Replace("[", "").Replace("]", "").Split('/').ToList() : bpm.snps[idx].Replace("[", "").Replace("]", "").Split('/').Select(x => Utils.getComplement(x[0]).ToString()).ToList(); bimEntry.AddRange(alleles); sw.WriteLine(String.Join(' ', bimEntry)); sw.Flush(); } writeStreamFromStreamToS3(bucketName: s3Event.Bucket.Name, keyName: s3Event.Object.Key.Replace("bpm", "bim"), bucketRegion: preference["S3"]["BPM"]["Region"].ToString(), stream: ms).Wait(); } catch (Exception e) { context.Logger.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }
/* * Method for amending binary(BPM) manifest file. * * Attributes: * bpm (BeadPoolManifest): original manifest file * amend_file: amend info file (csv format, no header) * amend (list({'from': SNP name in original manifest, * 'to': SNP name in new manifest, * 'ref_strands': ref_strands in new manifest, * 'snps': snps in new manifest})) * * names (list(strings): Names of loci from manifest * snps (list(strings): SNP values of loci from manifest * * chroms (list(strings): Chromosome values for loci * map_infos (list(int)): Map infor values for loci * addresses(list(int)): AddressA IDs of loci from manifest * normalization_lookups(list(int)) : Normalization lookups from manifest.This indexes into * list of normalization transforms read from GTC file * ref_strands (list(int)): Reference strand annotation for loci(see RefStrand class) * source_strands(list(int)) : Source strand annotations for loci(see SourceStrand class) * num_loci(int) : Number of loci in manifest * manifest_name(string): Name of manifest * control_config(string): Control description from manifest */ public static BeadPoolManifest Amend(BeadPoolManifest bpm, List <BeadPoolManifestAmendEntry> amendEntries) { for (int idx = 0; idx < bpm.num_loci; ++idx) { foreach (BeadPoolManifestAmendEntry amendEntry in amendEntries) { if (amendEntry.originalSNPNameOnBPM == bpm.names[idx]) { // Console.WriteLine("Index: " + idx + ", locus_name: " + bpm.locus_entries[idx].name + ", name: " + x["to"] + ", Strand: " + x["ref_strands"] + ", snps: " + x["snps"]); bpm.names[idx] = amendEntry.newSNPNameOnBPM; // bpm.ref_strands[idx] = Convert.ToInt32(x["ref_strands"]); bpm.locus_entries[idx].ref_strand = amendEntry.newStrandOnBPM; // bpm.snps[idx] = x["snps"]; bpm.locus_entries[idx].snp = amendEntry.newAlleleOnBPM; bpm.locus_entries[idx].name = amendEntry.newSNPNameOnBPM; } } } return(bpm); }
public static Plink.Plink ConvertGTCToPlink(List <GenotypeCalls> gtcs, BeadPoolManifest bpm = null) { Dictionary <char, byte> sex_map = new Dictionary <char, byte> { { 'F', 0x02 }, { 'M', 0x01 }, { 'U', 0x00 } }; List <string> FIDs = gtcs.AsParallel().Select(gtc => gtc.get_sample_name()).ToList(); List <string> parent = gtcs.AsParallel().Select(gtc => "0").ToList(); List <byte> Sexs = gtcs.AsParallel().Select(gtc => sex_map[gtc.get_gender()]).ToList(); List <byte> Phenotypes = gtcs.AsParallel().Select(gtc => (byte)1).ToList(); int num_snps = gtcs[0].get_genotypes().Length; List <byte[]> genotypesGTC = gtcs.AsParallel().Select(gtc => gtc.get_genotypes()).ToList(); List <byte[]> genotypes = new List <byte[]>(); for (int snpIdx = 0; snpIdx < num_snps; ++snpIdx) { genotypes.Add(new byte[gtcs.Count]); for (int gtcIdx = 0; gtcIdx < gtcs.Count; ++gtcIdx) { genotypes[snpIdx][gtcIdx] = genotypesGTC[gtcIdx][snpIdx]; } } Console.WriteLine("GTC data loaded; Converting to Plink"); return(new Plink.Plink(FIDs: FIDs, IIDs: FIDs, Fathers: parent, Mothers: parent, Sexs: Sexs, Phenotypes: Phenotypes, Chromosomes: bpm == null ? null : bpm.chroms, snpNames: bpm == null ? null : bpm.names, cMs: bpm == null ? null : Enumerable.Range(0, bpm.num_loci).Select(x => 0).ToList(), Positions: bpm == null ? null : bpm.map_infos, alleles: bpm == null ? null : bpm.snps.AsParallel().Select(allele => allele.Replace("[", "").Replace("]", "").Split('/').ToList()).ToList(), genotypes: genotypes, strands: bpm == null ? null : bpm.ref_strands.Select(x => Convert.ToByte(x)).ToList())); }
public static MemoryStreamNotDispose beadPoolManifestWriter(BeadPoolManifest bpm) { MemoryStreamNotDispose fs = new MemoryStreamNotDispose(); using (BinaryWriter bw = new BinaryWriter(fs)) { for (int locus_idx = 0; locus_idx < bpm.num_loci; ++locus_idx) { bpm.normalization_ids[locus_idx] -= 100 * bpm.assay_types[locus_idx]; } bw.Write(System.Text.Encoding.ASCII.GetBytes("BPM")); bw.Write((byte)1); bw.Write(bpm.version); bw.Write(bpm.manifest_name); bw.Write(bpm.control_config); bw.Write(bpm.num_loci); bw.Write(bpm.after_num_loci); for (int idx = 0; idx < bpm.num_loci; ++idx) { bw.Write(bpm.names[idx]); } for (int idx = 0; idx < bpm.num_loci; ++idx) { bw.Write((byte)bpm.normalization_ids[idx]); } for (int idx = 0; idx < bpm.num_loci; ++idx) { LocusEntryWriter.Writer(bw, bpm.locus_entries[idx]); } } return(fs); }