annotate infernal.py @ 3:2c2c5e5e495b draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/infernal commit 9eeedfaf35c069d75014c5fb2e42046106bf813c-dirty
author bgruening
date Fri, 04 Mar 2016 07:24:53 -0500
parents 652f9d550531
children c9e29ac5d099
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
652f9d550531 Uploaded
bgruening
parents:
diff changeset
1 # -*- coding: utf-8 -*-
652f9d550531 Uploaded
bgruening
parents:
diff changeset
2
652f9d550531 Uploaded
bgruening
parents:
diff changeset
3 from galaxy.datatypes.data import Text
652f9d550531 Uploaded
bgruening
parents:
diff changeset
4 from galaxy.datatypes.sniff import get_headers, get_test_fname
652f9d550531 Uploaded
bgruening
parents:
diff changeset
5 from galaxy.datatypes.data import get_file_peek
652f9d550531 Uploaded
bgruening
parents:
diff changeset
6 import subprocess
652f9d550531 Uploaded
bgruening
parents:
diff changeset
7 import os
652f9d550531 Uploaded
bgruening
parents:
diff changeset
8
652f9d550531 Uploaded
bgruening
parents:
diff changeset
9 from galaxy.datatypes.metadata import MetadataElement
652f9d550531 Uploaded
bgruening
parents:
diff changeset
10 from galaxy.datatypes import metadata
652f9d550531 Uploaded
bgruening
parents:
diff changeset
11
652f9d550531 Uploaded
bgruening
parents:
diff changeset
12 def count_special_lines( word, filename, invert = False ):
652f9d550531 Uploaded
bgruening
parents:
diff changeset
13 """
652f9d550531 Uploaded
bgruening
parents:
diff changeset
14 searching for special 'words' using the grep tool
652f9d550531 Uploaded
bgruening
parents:
diff changeset
15 grep is used to speed up the searching and counting
652f9d550531 Uploaded
bgruening
parents:
diff changeset
16 The number of hits is returned.
652f9d550531 Uploaded
bgruening
parents:
diff changeset
17 """
652f9d550531 Uploaded
bgruening
parents:
diff changeset
18 try:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
19 cmd = ["grep", "-c"]
652f9d550531 Uploaded
bgruening
parents:
diff changeset
20 if invert:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
21 cmd.append('-v')
652f9d550531 Uploaded
bgruening
parents:
diff changeset
22 cmd.extend([word, filename])
652f9d550531 Uploaded
bgruening
parents:
diff changeset
23 out = subprocess.Popen(cmd, stdout=subprocess.PIPE)
652f9d550531 Uploaded
bgruening
parents:
diff changeset
24 return int(out.communicate()[0].split()[0])
652f9d550531 Uploaded
bgruening
parents:
diff changeset
25 except:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
26 pass
652f9d550531 Uploaded
bgruening
parents:
diff changeset
27 return 0
652f9d550531 Uploaded
bgruening
parents:
diff changeset
28
652f9d550531 Uploaded
bgruening
parents:
diff changeset
29 def count_lines( filename, non_empty = False):
652f9d550531 Uploaded
bgruening
parents:
diff changeset
30 """
652f9d550531 Uploaded
bgruening
parents:
diff changeset
31 counting the number of lines from the 'filename' file
652f9d550531 Uploaded
bgruening
parents:
diff changeset
32 """
652f9d550531 Uploaded
bgruening
parents:
diff changeset
33 try:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
34 if non_empty:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
35 out = subprocess.Popen(['grep', '-cve', '^\s*$', filename], stdout=subprocess.PIPE)
652f9d550531 Uploaded
bgruening
parents:
diff changeset
36 else:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
37 out = subprocess.Popen(['wc', '-l', filename], stdout=subprocess.PIPE)
652f9d550531 Uploaded
bgruening
parents:
diff changeset
38 return int(out.communicate()[0].split()[0])
652f9d550531 Uploaded
bgruening
parents:
diff changeset
39 except:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
40 pass
652f9d550531 Uploaded
bgruening
parents:
diff changeset
41 return 0
652f9d550531 Uploaded
bgruening
parents:
diff changeset
42
652f9d550531 Uploaded
bgruening
parents:
diff changeset
43
652f9d550531 Uploaded
bgruening
parents:
diff changeset
44 class Infernal_CM_1_1( Text ):
652f9d550531 Uploaded
bgruening
parents:
diff changeset
45 file_ext = "cm"
652f9d550531 Uploaded
bgruening
parents:
diff changeset
46
652f9d550531 Uploaded
bgruening
parents:
diff changeset
47 MetadataElement( name="number_of_models", default=0, desc="Number of covariance models", readonly=True, visible=True, optional=True, no_value=0 )
652f9d550531 Uploaded
bgruening
parents:
diff changeset
48
652f9d550531 Uploaded
bgruening
parents:
diff changeset
49 def set_peek( self, dataset, is_multi_byte=False ):
652f9d550531 Uploaded
bgruening
parents:
diff changeset
50 if not dataset.dataset.purged:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
51 dataset.peek = get_file_peek( dataset.file_name, is_multi_byte=is_multi_byte )
652f9d550531 Uploaded
bgruening
parents:
diff changeset
52 if (dataset.metadata.number_of_models == 1):
652f9d550531 Uploaded
bgruening
parents:
diff changeset
53 dataset.blurb = "1 model"
652f9d550531 Uploaded
bgruening
parents:
diff changeset
54 else:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
55 dataset.blurb = "%s models" % dataset.metadata.number_of_models
3
2c2c5e5e495b planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/infernal commit 9eeedfaf35c069d75014c5fb2e42046106bf813c-dirty
bgruening
parents: 0
diff changeset
56 dataset.peek = get_file_peek( dataset.file_name, is_multi_byte=is_multi_byte )
0
652f9d550531 Uploaded
bgruening
parents:
diff changeset
57 else:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
58 dataset.peek = 'file does not exist'
652f9d550531 Uploaded
bgruening
parents:
diff changeset
59 dataset.blurb = 'file purged from disc'
652f9d550531 Uploaded
bgruening
parents:
diff changeset
60
652f9d550531 Uploaded
bgruening
parents:
diff changeset
61 def sniff( self, filename ):
652f9d550531 Uploaded
bgruening
parents:
diff changeset
62 if count_special_lines("^INFERNAL1/a", filename) > 0:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
63 return True
652f9d550531 Uploaded
bgruening
parents:
diff changeset
64 else:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
65 return False
652f9d550531 Uploaded
bgruening
parents:
diff changeset
66
652f9d550531 Uploaded
bgruening
parents:
diff changeset
67 def set_meta( self, dataset, **kwd ):
652f9d550531 Uploaded
bgruening
parents:
diff changeset
68 """
652f9d550531 Uploaded
bgruening
parents:
diff changeset
69 Set the number of models in dataset.
652f9d550531 Uploaded
bgruening
parents:
diff changeset
70 """
652f9d550531 Uploaded
bgruening
parents:
diff changeset
71 dataset.metadata.number_of_models = count_special_lines("^INFERNAL1/a", dataset.file_name)
652f9d550531 Uploaded
bgruening
parents:
diff changeset
72
652f9d550531 Uploaded
bgruening
parents:
diff changeset
73 def split( cls, input_datasets, subdir_generator_function, split_params):
652f9d550531 Uploaded
bgruening
parents:
diff changeset
74 """
652f9d550531 Uploaded
bgruening
parents:
diff changeset
75 Split the input files by model records.
652f9d550531 Uploaded
bgruening
parents:
diff changeset
76 """
652f9d550531 Uploaded
bgruening
parents:
diff changeset
77 if split_params is None:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
78 return None
652f9d550531 Uploaded
bgruening
parents:
diff changeset
79
652f9d550531 Uploaded
bgruening
parents:
diff changeset
80 if len(input_datasets) > 1:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
81 raise Exception("CM-file splitting does not support multiple files")
652f9d550531 Uploaded
bgruening
parents:
diff changeset
82 input_files = [ds.file_name for ds in input_datasets]
652f9d550531 Uploaded
bgruening
parents:
diff changeset
83
652f9d550531 Uploaded
bgruening
parents:
diff changeset
84 chunk_size = None
652f9d550531 Uploaded
bgruening
parents:
diff changeset
85 if split_params['split_mode'] == 'number_of_parts':
652f9d550531 Uploaded
bgruening
parents:
diff changeset
86 raise Exception('Split mode "%s" is currently not implemented for CM-files.' % split_params['split_mode'])
652f9d550531 Uploaded
bgruening
parents:
diff changeset
87 elif split_params['split_mode'] == 'to_size':
652f9d550531 Uploaded
bgruening
parents:
diff changeset
88 chunk_size = int(split_params['split_size'])
652f9d550531 Uploaded
bgruening
parents:
diff changeset
89 else:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
90 raise Exception('Unsupported split mode %s' % split_params['split_mode'])
652f9d550531 Uploaded
bgruening
parents:
diff changeset
91
652f9d550531 Uploaded
bgruening
parents:
diff changeset
92 def _read_cm_records( filename ):
652f9d550531 Uploaded
bgruening
parents:
diff changeset
93 lines = []
652f9d550531 Uploaded
bgruening
parents:
diff changeset
94 with open(filename) as handle:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
95 for line in handle:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
96 if line.startswith("INFERNAL1/a") and lines:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
97 yield lines
652f9d550531 Uploaded
bgruening
parents:
diff changeset
98 lines = [line]
652f9d550531 Uploaded
bgruening
parents:
diff changeset
99 else:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
100 lines.append( line )
652f9d550531 Uploaded
bgruening
parents:
diff changeset
101 yield lines
652f9d550531 Uploaded
bgruening
parents:
diff changeset
102
652f9d550531 Uploaded
bgruening
parents:
diff changeset
103 def _write_part_cm_file( accumulated_lines ):
652f9d550531 Uploaded
bgruening
parents:
diff changeset
104 part_dir = subdir_generator_function()
652f9d550531 Uploaded
bgruening
parents:
diff changeset
105 part_path = os.path.join( part_dir, os.path.basename( input_files[0] ) )
652f9d550531 Uploaded
bgruening
parents:
diff changeset
106 part_file = open( part_path, 'w' )
652f9d550531 Uploaded
bgruening
parents:
diff changeset
107 part_file.writelines( accumulated_lines )
652f9d550531 Uploaded
bgruening
parents:
diff changeset
108 part_file.close()
652f9d550531 Uploaded
bgruening
parents:
diff changeset
109
652f9d550531 Uploaded
bgruening
parents:
diff changeset
110 try:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
111 cm_records = _read_cm_records( input_files[0] )
652f9d550531 Uploaded
bgruening
parents:
diff changeset
112 cm_lines_accumulated = []
652f9d550531 Uploaded
bgruening
parents:
diff changeset
113 for counter, cm_record in enumerate( cm_records, start = 1):
652f9d550531 Uploaded
bgruening
parents:
diff changeset
114 cm_lines_accumulated.extend( cm_record )
652f9d550531 Uploaded
bgruening
parents:
diff changeset
115 if counter % chunk_size == 0:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
116 _write_part_cm_file( cm_lines_accumulated )
652f9d550531 Uploaded
bgruening
parents:
diff changeset
117 cm_lines_accumulated = []
652f9d550531 Uploaded
bgruening
parents:
diff changeset
118 if cm_lines_accumulated:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
119 _write_part_cm_file( cm_lines_accumulated )
652f9d550531 Uploaded
bgruening
parents:
diff changeset
120 except Exception, e:
652f9d550531 Uploaded
bgruening
parents:
diff changeset
121 log.error('Unable to split files: %s' % str(e))
652f9d550531 Uploaded
bgruening
parents:
diff changeset
122 raise
652f9d550531 Uploaded
bgruening
parents:
diff changeset
123 split = classmethod(split)
652f9d550531 Uploaded
bgruening
parents:
diff changeset
124
652f9d550531 Uploaded
bgruening
parents:
diff changeset
125 if __name__ == '__main__':
652f9d550531 Uploaded
bgruening
parents:
diff changeset
126 Infernal_CM_1_1()
652f9d550531 Uploaded
bgruening
parents:
diff changeset
127 Stockholm_1_0()
652f9d550531 Uploaded
bgruening
parents:
diff changeset
128