PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/scripts/microbes/create_bacteria_table.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 79 lines | 72 code | 4 blank | 3 comment | 7 complexity | b263c164cd15bffd375bdffbed7f014b 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
41    orgs = organisms.keys()
42    for org in orgs:
43        if 'name' not in organisms[org]:del organisms[org]
44
45    orgs = organisms.keys()
46    #need to sort by name
47    swap_test = False
48    for i in range(0, len(orgs) - 1):
49        for j in range(0, len(orgs) - i - 1):
50            if organisms[orgs[j]]['name'] >  organisms[orgs[j + 1]]['name']:
51                orgs[j], orgs[j + 1] = orgs[j + 1], orgs[j]
52            swap_test = True
53        if swap_test == False:
54            break
55
56
57
58
59    print "||'''Organism'''||'''Kingdom'''||'''Group'''||'''Links to UCSC Archaea Browser'''||"
60
61
62    for org in orgs:
63        org = organisms[org]
64        at_ucsc = False
65        #if no gpi, then must be a ncbi chr which corresponds to a UCSC org, w/o matching UCSC designation
66        try:
67            build = org['genome project id']
68        except: continue
69        if 'build' in org:
70            build = org['build']
71            at_ucsc = True
72            
73        out_str = "||"+org['name']+"||"+org['kingdom']+"||"+org['group']+"||"
74        if at_ucsc:
75            out_str = out_str + "Yes"
76        out_str = out_str + "||"
77        print out_str
78
79if __name__ == "__main__": __main__()