PageRenderTime 38ms CodeModel.GetById 27ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/cron/add_manual_builds.py

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