annotate tools/data_source/microbial_import_code.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 def load_microbial_data( GALAXY_DATA_INDEX_DIR, sep='\t' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 # FIXME: this function is duplicated in the DynamicOptions class. It is used here only to
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 # set data.name in exec_after_process().
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 microbe_info= {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 orgs = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 filename = "%s/microbial_data.loc" % GALAXY_DATA_INDEX_DIR
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 for i, line in enumerate( open( filename ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 line = line.rstrip( '\r\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 if line and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 fields = line.split( sep )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 #read each line, if not enough fields, go to next line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 info_type = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 if info_type.upper() == "ORG":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 #ORG 12521 Clostridium perfringens SM101 bacteria Firmicutes CP000312,CP000313,CP000314,CP000315 http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=genomeprj&cmd=Retrieve&dopt=Overview&list_uids=12521
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 org_num = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 name = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 kingdom = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 group = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 chromosomes = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 info_url = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 link_site = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 if org_num not in orgs:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 orgs[ org_num ] = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 orgs[ org_num ][ 'chrs' ] = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 orgs[ org_num ][ 'name' ] = name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 orgs[ org_num ][ 'kingdom' ] = kingdom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 orgs[ org_num ][ 'group' ] = group
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 orgs[ org_num ][ 'chromosomes' ] = chromosomes
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 orgs[ org_num ][ 'info_url' ] = info_url
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 orgs[ org_num ][ 'link_site' ] = link_site
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 elif info_type.upper() == "CHR":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 #CHR 12521 CP000315 Clostridium perfringens phage phiSM101, complete genome 38092 110684521 CP000315.1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 org_num = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 chr_acc = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 name = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 length = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 gi = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 gb = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 info_url = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 chr = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 chr[ 'name' ] = name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 chr[ 'length' ] = length
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 chr[ 'gi' ] = gi
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 chr[ 'gb' ] = gb
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 chr[ 'info_url' ] = info_url
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 if org_num not in orgs:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 orgs[ org_num ] = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 orgs[ org_num ][ 'chrs' ] = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 orgs[ org_num ][ 'chrs' ][ chr_acc ] = chr
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 elif info_type.upper() == "DATA":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 #DATA 12521_12521_CDS 12521 CP000315 CDS bed /home/djb396/alignments/playground/bacteria/12521/CP000315.CDS.bed
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 uid = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 org_num = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 chr_acc = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 feature = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 filetype = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 path = fields.pop(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 data = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 data[ 'filetype' ] = filetype
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 data[ 'path' ] = path
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 data[ 'feature' ] = feature
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 if org_num not in orgs:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 orgs[ org_num ] = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 orgs[ org_num ][ 'chrs' ] = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 if 'data' not in orgs[ org_num ][ 'chrs' ][ chr_acc ]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 orgs[ org_num ][ 'chrs' ][ chr_acc ][ 'data' ] = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 orgs[ org_num ][ 'chrs' ][ chr_acc ][ 'data' ][ uid ] = data
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 else: continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 except: continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 for org_num in orgs:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 org = orgs[ org_num ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 if org[ 'kingdom' ] not in microbe_info:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 microbe_info[ org[ 'kingdom' ] ] = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 if org_num not in microbe_info[ org[ 'kingdom' ] ]:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 microbe_info[ org[ 'kingdom' ] ][org_num] = org
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 return microbe_info
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 #post processing, set build for data and add additional data to history
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 from galaxy import datatypes, config, jobs, tools
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 from shutil import copyfile
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 def exec_after_process(app, inp_data, out_data, param_dict, tool, stdout, stderr):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 base_dataset = out_data.items()[0][1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 history = base_dataset.history
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 if history == None:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 print "unknown history!"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 return
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 kingdom = param_dict.get( 'kingdom', None )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 #group = param_dict.get( 'group', None )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 org = param_dict.get( 'org', None )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 #if not (kingdom or group or org):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 if not (kingdom or org):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 print "Parameters are not available."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 #workflow passes galaxy.tools.parameters.basic.UnvalidatedValue instead of values
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 if isinstance( kingdom, tools.parameters.basic.UnvalidatedValue ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 kingdom = kingdom.value
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 if isinstance( org, tools.parameters.basic.UnvalidatedValue ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 org = org.value
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 GALAXY_DATA_INDEX_DIR = app.config.tool_data_path
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 microbe_info = load_microbial_data( GALAXY_DATA_INDEX_DIR, sep='\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 new_stdout = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 split_stdout = stdout.split("\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 basic_name = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 for line in split_stdout:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 fields = line.split("\t")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 if fields[0] == "#File1":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 description = fields[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 chr = fields[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 dbkey = fields[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 file_type = fields[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 name, data = out_data.items()[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 data.set_size()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 basic_name = data.name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 data.name = data.name + " (" + microbe_info[kingdom][org]['chrs'][chr]['data'][description]['feature'] +" for " + microbe_info[kingdom][org]['name'] + ":" + chr + ")"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 data.dbkey = dbkey
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 data.info = data.name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 data = app.datatypes_registry.change_datatype( data, file_type )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 data.init_meta()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 data.set_peek()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 app.model.context.add( data )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 app.model.context.flush()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 elif fields[0] == "#NewFile":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 description = fields[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 chr = fields[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 dbkey = fields[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 filepath = fields[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 file_type = fields[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 newdata = app.model.HistoryDatasetAssociation( create_dataset = True, sa_session = app.model.context ) #This import should become a library
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 newdata.set_size()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 newdata.extension = file_type
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 newdata.name = basic_name + " (" + microbe_info[kingdom][org]['chrs'][chr]['data'][description]['feature'] +" for "+microbe_info[kingdom][org]['name']+":"+chr + ")"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 app.model.context.add( newdata )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 app.model.context.flush()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 app.security_agent.copy_dataset_permissions( base_dataset.dataset, newdata.dataset )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 history.add_dataset( newdata )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 app.model.context.add( history )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 app.model.context.flush()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 copyfile(filepath,newdata.file_name)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 newdata.info = newdata.name
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 newdata.state = jobs.JOB_OK
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 newdata.info = "The requested file is missing from the system."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 newdata.state = jobs.JOB_ERROR
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 newdata.dbkey = dbkey
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 newdata.init_meta()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 newdata.set_peek()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 app.model.context.flush()