/cron/add_manual_builds.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 49 lines · 35 code · 3 blank · 11 comment · 8 complexity · e66820acdae84f728ad95da8408e2055 MD5 · raw file

  1. #!/usr/bin/env python
  2. """
  3. Adds Manually created builds and chrom info to Galaxy's info tables
  4. Usage:
  5. python add_manual_builds.py input_file builds.txt chrom_length_dir
  6. """
  7. import sys,os
  8. def add_manual_builds(input_file, build_file, chr_dir):
  9. #determine existing builds, so as to not overwrite
  10. existing_builds = []
  11. for line in open(build_file):
  12. try:
  13. if line.startswith("#"): continue
  14. existing_builds.append(line.replace("\n","").replace("\r","").split("\t")[0])
  15. except:
  16. continue
  17. build_file_out = open(build_file,'a')
  18. for line in open(input_file):
  19. try:
  20. fields = line.replace("\n","").replace("\r","").split("\t")
  21. build = fields.pop(0)
  22. if build in existing_builds: continue # if build exists, leave alone
  23. name = fields.pop(0)
  24. try: # get chrom lens if included in file, otherwise still add build
  25. chrs = fields.pop(0).split(",")
  26. except:
  27. chrs = []
  28. print>>build_file_out, build+"\t"+name+" ("+build+")"
  29. if chrs: # create len file if provided chrom lens
  30. chr_len_out=open( os.path.join(chr_dir,build+".len"),'w')
  31. for chr in chrs:
  32. print>>chr_len_out, chr.replace("=","\t")
  33. chr_len_out.close()
  34. except:
  35. continue
  36. build_file_out.close()
  37. if __name__ == "__main__":
  38. if len(sys.argv) < 4:
  39. print "USAGE: python add_manual_builds.py input_file builds.txt chrom_length_dir"
  40. sys.exit(1)
  41. input_file = sys.argv[1]
  42. build_file = sys.argv[2]
  43. chr_dir = sys.argv[3]
  44. add_manual_builds(input_file,build_file,chr_dir)