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