PageRenderTime 22ms CodeModel.GetById 10ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/tools/data_source/microbial_import.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 86 lines | 67 code | 10 blank | 9 comment | 10 complexity | 38c6c7e221b6325283a97117a1867690 MD5 | raw file
 1#!/usr/bin/env python
 2
 3"""
 4Script that imports locally stored data as a new dataset for the user
 5Usage: import id outputfile
 6"""
 7import sys, os
 8from shutil import copyfile
 9
10assert sys.version_info[:2] >= ( 2, 4 )
11
12BUFFER = 1048576
13
14uids = sys.argv[1].split(",")
15out_file1 = sys.argv[2]
16
17#remove NONE from uids
18have_none = True
19while have_none:
20    try:
21        uids.remove('None')
22    except:
23        have_none = False
24
25
26#create dictionary keyed by uid of tuples of (displayName,filePath,build) for all files
27available_files = {}
28try:
29    filename = sys.argv[-1]
30    for i, line in enumerate( file( filename ) ):
31        if not line or line[0:1] == "#" : continue
32        fields = line.split('\t')
33        try:
34            info_type = fields.pop(0)
35            
36            if info_type.upper()=="DATA":
37                uid = fields.pop(0)
38                org_num = fields.pop(0)
39                chr_acc = fields.pop(0)
40                feature = fields.pop(0)
41                filetype = fields.pop(0)
42                path = fields.pop(0).replace("\r","").replace("\n","")
43                
44                file_type = filetype
45                build = org_num
46                description = uid
47            else:
48                continue
49        except:
50            continue
51
52        available_files[uid]=(description,path,build,file_type,chr_acc)
53except:
54    print >>sys.stderr, "It appears that the configuration file for this tool is missing."
55
56#create list of tuples of (displayName,FileName,build) for desired files
57desired_files = []
58for uid in uids:
59    try:
60        desired_files.append(available_files[uid])
61    except:
62        continue
63
64#copy first file to contents of given output file
65file1_copied = False
66while not file1_copied:
67    try:
68        first_file = desired_files.pop(0)
69    except:
70        print >>sys.stderr, "There were no valid files requested."
71        sys.exit()
72    file1_desc, file1_path, file1_build, file1_type,file1_chr_acc = first_file
73    try:
74        copyfile(file1_path,out_file1)
75        print "#File1\t"+file1_desc+"\t"+file1_chr_acc+"\t"+file1_build+"\t"+file1_type
76        file1_copied = True
77    except:
78        print >>sys.stderr, "The file specified is missing."
79        continue
80        #print >>sys.stderr, "The file specified is missing."
81    
82
83#Tell post-process filter where remaining files reside
84for extra_output in desired_files:
85    file_desc, file_path, file_build, file_type,file_chr_acc = extra_output
86    print "#NewFile\t"+file_desc+"\t"+file_chr_acc+"\t"+file_build+"\t"+file_path+"\t"+file_type