PageRenderTime 47ms CodeModel.GetById 37ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/scripts/microbes/get_builds_lengths.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 56 lines | 49 code | 4 blank | 3 comment | 7 complexity | 2f519fac856df81f583bd517319e3758 MD5 | raw file
 1#!/usr/bin/env python
 2#Dan Blankenberg
 3
 4import sys, os
 5
 6assert sys.version_info[:2] >= ( 2, 4 )
 7
 8def __main__():
 9    base_dir = os.path.join( os.getcwd(), "bacteria" )
10    try:
11        base_dir = sys.argv[1]
12    except:
13        pass
14        #print "using default base_dir:", base_dir
15
16    organisms = {}
17    for result in os.walk(base_dir):
18        this_base_dir,sub_dirs,files = result
19        for file in files:
20            if file[-5:] == ".info":
21                dict = {}
22                info_file = open(os.path.join(this_base_dir,file),'r')
23                info = info_file.readlines()
24                info_file.close()
25                for line in info:
26                    fields = line.replace("\n","").split("=")
27                    dict[fields[0]]="=".join(fields[1:])
28                if 'genome project id' in dict.keys():
29                    name = dict['genome project id']
30                    if 'build' in dict.keys():
31                        name = dict['build']
32                    if name not in organisms.keys():
33                        organisms[name] = {'chrs':{},'base_dir':this_base_dir}
34                    for key in dict.keys():
35                        organisms[name][key]=dict[key]
36                else:
37                    if dict['organism'] not in organisms.keys():
38                        organisms[dict['organism']] = {'chrs':{},'base_dir':this_base_dir}
39                    organisms[dict['organism']]['chrs'][dict['chromosome']]=dict
40    for org in organisms:
41        org = organisms[org]
42        #if no gpi, then must be a ncbi chr which corresponds to a UCSC org, w/o matching UCSC designation
43        try:
44            build = org['genome project id']
45        except: continue
46        
47        if 'build' in org:
48            build = org['build']
49            
50        chrs=[]
51        for chrom in org['chrs']:
52            chrom = org['chrs'][chrom]
53            chrs.append( "%s=%s" % ( chrom['chromosome'], chrom['length'] ) )
54        print "%s\t%s\t%s" % ( build, org['name'], ",".join( chrs ) )
55     
56if __name__ == "__main__": __main__()