PageRenderTime 38ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/scripts/microbes/create_nib_seq_loc_file.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 88 lines | 77 code | 9 blank | 2 comment | 6 complexity | d3f936bf520e913e62906f1505d019ac MD5 | raw file
  1. #!/usr/bin/env python
  2. #Dan Blankenberg
  3. import sys, os
  4. assert sys.version_info[:2] >= ( 2, 4 )
  5. def __main__():
  6. base_dir = os.path.join( os.getcwd(), "bacteria" )
  7. try:
  8. base_dir = sys.argv[1]
  9. except:
  10. print "using default base_dir:", base_dir
  11. loc_out = os.path.join( base_dir, "seq.loc" )
  12. try:
  13. loc_out = os.path.join( base_dir, sys.argv[2] )
  14. except:
  15. print "using default seq.loc:", loc_out
  16. organisms = {}
  17. loc_out = open( loc_out, 'wb' )
  18. for result in os.walk( base_dir ):
  19. this_base_dir, sub_dirs, files = result
  20. for file in files:
  21. if file[-5:] == ".info":
  22. dict = {}
  23. info_file = open( os.path.join( this_base_dir, file ), 'r' )
  24. info = info_file.readlines()
  25. info_file.close()
  26. for line in info:
  27. fields = line.replace( "\n", "" ).split( "=" )
  28. dict[fields[0]]="=".join(fields[1:])
  29. if 'genome project id' in dict.keys():
  30. name = dict['genome project id']
  31. if 'build' in dict.keys():
  32. name = dict['build']
  33. if name not in organisms.keys():
  34. organisms[name] = {'chrs':{}, 'base_dir':this_base_dir}
  35. for key in dict.keys():
  36. organisms[name][key] = dict[key]
  37. else:
  38. if dict['organism'] not in organisms.keys():
  39. organisms[dict['organism']] = {'chrs':{},'base_dir':this_base_dir}
  40. organisms[dict['organism']]['chrs'][dict['chromosome']] = dict
  41. for org in organisms:
  42. org = organisms[org]
  43. try:
  44. build = org['genome project id']
  45. except: continue
  46. if 'build' in org:
  47. build = org['build']
  48. seq_path = os.path.join( org['base_dir'], "seq" )
  49. #create seq dir, if exists go to next org
  50. ##TODO: add better checking, i.e. for updating
  51. try:
  52. os.mkdir( seq_path )
  53. except:
  54. print "Skipping", build
  55. #continue
  56. loc_out.write( "seq %s %s\n" % ( build, seq_path ) )
  57. #print org info
  58. for chr in org['chrs']:
  59. chr = org['chrs'][chr]
  60. fasta_file = os.path.join( org['base_dir'], "%s.fna" % chr['chromosome'] )
  61. nib_out_file = os.path.join( seq_path, "%s.nib "% chr['chromosome'] )
  62. #create nibs using faToNib binary
  63. #TODO: when bx supports writing nib, use it here instead
  64. command = "faToNib %s %s" % ( fasta_file, nib_out_file )
  65. os.system( command )
  66. loc_out.close()
  67. if __name__ == "__main__": __main__()