Skip to content

Instantly share code, notes, and snippets.

@hurrialice
Last active May 24, 2022 17:46
Show Gist options
  • Save hurrialice/ef2838cb82628801717eed40836e3ed5 to your computer and use it in GitHub Desktop.
Save hurrialice/ef2838cb82628801717eed40836e3ed5 to your computer and use it in GitHub Desktop.
# parse args
import argparse
import subprocess
def escape(s):
s = s.replace("<", "&lt;")
s = s.replace(">", "&gt;")
# this has to be last:
s = s.replace("&", "&amp;")
return s
def translate_to_s3(f):
cmd = "bs -c RADY link file -i " + str(f) +" | xargs curl -w %\{redirect_url\}"
return escape(subprocess.check_output(cmd, shell=True).decode())
def parse_args():
parser = argparse.ArgumentParser(description = "write a minimal IGV xml that allows streaming BAM/BAI")
# reference
parser.add_argument("-ref", required = True, default='hg38', choices=['hg19', 'hg38'] )
# input files
parser.add_argument("-bams", required = True, help = "comma-deliminated bam BSSH file ids")
# output xml
parser.add_argument("-o", required = True, help = "output file path")
args = parser.parse_args()
# check args: if indices and names have same length with paths
return args
args = parse_args()
filelist = [{'bai': translate_to_s3(int(bam)+1), 'bam':translate_to_s3(bam)} for bam in args.bams.split(',')]
with open(args.o, 'w') as xml:
# headers
xml.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n")
xml.write("<Session genome=\""+args.ref+"\" hasGeneTrack=\"true\" hasSequenceTrack=\"true\" locus=\"All\" nextAutoscaleGroup=\"4\" path=\"\" version=\"8\">\n")
xml.write("\t<Resources>\n")
# file handlers
for f in filelist:
xml.write("\t\t<Resource index=\"{bai}\" path=\"{bam}\"/>\n".format(bai = f['bai'], bam = f['bam']) )
# end
xml.write("\t</Resources>\n")
xml.write("</Session>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment