f0cd304218bd294a3630f6d12f42325d3c6472d5 chmalee Tue Sep 22 11:24:43 2020 -0700 Fix genome in a bottle structural variants build script to not fail when a field is missing from input diff --git src/hg/utils/otto/dbVar/processNstd175.py src/hg/utils/otto/dbVar/processNstd175.py index 1a95b7a..ba46e3e 100755 --- src/hg/utils/otto/dbVar/processNstd175.py +++ src/hg/utils/otto/dbVar/processNstd175.py @@ -39,30 +39,31 @@ return "128,128,128" elif cnvtype == "deletion": return "255,0,0" elif cnvtype == "delins": return "255,0,0" elif cnvtype == "insertion": return "0,0,255" else: return "0,0,0" def getMouseover(bed): """Return the mouseOver string for this bed record.""" ret = "Position: %s:%s-%s" % (bed["chrom"], int(bed["chromStart"])+1, bed["chromEnd"]) ret += ", Size: %d" % (int(bed["chromEnd"]) - int(bed["chromStart"])) ret += ", Variant Type: %s" % (bed["Variant Type"]) + if "Phenotype" in bed: ret += ", Phenotype: %s" % bed["Phenotype"] return ret def dumpBedLines(): """Write out the bed lines, with the same number of fields in each line.""" fields = bed9Fields + extraFieldList + ["_mouseOver"] print("#%s" % ("\t".join(fields))) for b in bedLines: bed = bedLines[b] finalBed = [] for field in fields: try: if type(bed[field]) is list: finalBed.append(", ".join(bed[field])) else: @@ -120,30 +121,35 @@ global bedLines for line in inf: if line.startswith('#') or line.startswith('track') or line.startswith('browser'): continue trimmed = line.strip() fields = trimmed.split(maxsplit=8) extraFields = fields[-1].split(';') itemName = extraFields[1].split('=')[1] extraHash = {} for f in extraFields: k,v = f.strip().split('=') fixedName = fixupFieldName(k) extraHash[fixedName] = v bedId = itemName extraHash["Variant Type"] = fields[2] + try: bedLines[bedId] = makeBedLine(fields[0], int(fields[3]) - 1, int(fields[4]), bedId, extraHash) + except KeyError as e: + sys.stderr.write("KeyError\n") + sys.stderr.write("Offending line: %s\n" % line) + sys.exit(1) dumpBedLines() def main(): args = setupCommandLine() if args.liftFile: parseLiftFile(args.liftFile) if args.infile == "stdin": processNstd175(sys.stdin) else: with open(args.infile) as inf: processNstd175(inf) if __name__=="__main__": main()