Class: Bio::FlatFile::AutoDetect
- Includes:
- TSort
- Defined in:
- lib/bio/io/flatfile.rb
Overview
AutoDetect automatically determines database class of given data.
Defined Under Namespace
Classes: RuleDebug, RuleProc, RuleRegexp, RuleRegexp2, RuleSpecial, RuleTemplate, RulesArray
Constant Summary collapse
- TopRule =
Special element that is always top priority.
RuleSpecial.new('top')
- BottomRule =
Special element that is always bottom priority.
RuleSpecial.new('bottom')
Class Method Summary collapse
-
.[](*arg) ⇒ Object
make a new autodetect object.
-
.default ⇒ Object
returns the default autodetect object.
-
.default=(ad) ⇒ Object
sets the default autodetect object.
-
.make_default ⇒ Object
make a default of default autodetect object.
Instance Method Summary collapse
-
#add(elem) ⇒ Object
Adds a new element.
-
#autodetect(text, meta = {}) ⇒ Object
Autodetect from the text.
-
#autodetect_flatfile(ff, lines = 31) ⇒ Object
autodetect from the FlatFile object.
-
#each_rule(&x) ⇒ Object
Iterates over each element.
-
#elements ⇒ Object
Returns current elements as an array whose order fulfills all elements’ priorities.
-
#initialize ⇒ AutoDetect
constructor
Creates a new Autodetect object.
-
#inspect ⇒ Object
visualizes the object (mainly for debug).
-
#rehash ⇒ Object
rebuilds the object and clears internal cache.
-
#tsort_each_child(elem) ⇒ Object
(required by TSort.) For a given element, yields each child (= lower priority elements) of the element.
-
#tsort_each_node(&x) ⇒ Object
(required by TSort.) For all elements, yields each element.
Constructor Details
#initialize ⇒ AutoDetect
Creates a new Autodetect object
984 985 986 987 988 989 990 991 |
# File 'lib/bio/io/flatfile.rb', line 984 def initialize # stores autodetection rules. @rules = Hash.new # stores elements (cache) @elements = nil self.add(TopRule) self.add(BottomRule) end |
Class Method Details
.[](*arg) ⇒ Object
make a new autodetect object
1119 1120 1121 1122 1123 |
# File 'lib/bio/io/flatfile.rb', line 1119 def self.[](*arg) a = self.new arg.each { |e| a.add(e) } a end |
.default ⇒ Object
returns the default autodetect object
1106 1107 1108 1109 1110 1111 |
# File 'lib/bio/io/flatfile.rb', line 1106 def self.default unless @default then @default = self.make_default end @default end |
.default=(ad) ⇒ Object
sets the default autodetect object.
1114 1115 1116 |
# File 'lib/bio/io/flatfile.rb', line 1114 def self.default=(ad) @default = ad end |
.make_default ⇒ Object
make a default of default autodetect object
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 |
# File 'lib/bio/io/flatfile.rb', line 1126 def self.make_default a = self[ genbank = RuleRegexp[ 'Bio::GenBank', /^LOCUS .+ bp .*[a-z]*[DR]?NA/ ], genpept = RuleRegexp[ 'Bio::GenPept', /^LOCUS .+ aa .+/ ], medline = RuleRegexp[ 'Bio::MEDLINE', /^PMID\- [0-9]+$/ ], embl = RuleRegexp[ 'Bio::EMBL', /^ID .+\; .*(DNA|RNA|XXX)\;/ ], sptr = RuleRegexp2[ 'Bio::SPTR', /^ID .+\; *PRT\;/, /^ID [-A-Za-z0-9_\.]+ .+\; *[0-9]+ *AA\./ ], prosite = RuleRegexp[ 'Bio::PROSITE', /^ID [-A-Za-z0-9_\.]+\; (PATTERN|RULE|MATRIX)\.$/ ], transfac = RuleRegexp[ 'Bio::TRANSFAC', /^AC [-A-Za-z0-9_\.]+$/ ], aaindex = RuleProc.new('Bio::AAindex1', 'Bio::AAindex2') do |text| if /^H [-A-Z0-9_\.]+$/ =~ text then if text =~ /^M [rc]/ then Bio::AAindex2 elsif text =~ /^I A\/L/ then Bio::AAindex1 else false #fail to determine end else nil end end, litdb = RuleRegexp[ 'Bio::LITDB', /^CODE [0-9]+$/ ], brite = RuleRegexp[ 'Bio::KEGG::BRITE', /^Entry [A-Z0-9]+/ ], orthology = RuleRegexp[ 'Bio::KEGG::ORTHOLOGY', /^ENTRY .+ KO\s*/ ], drug = RuleRegexp[ 'Bio::KEGG::DRUG', /^ENTRY .+ Drug\s*/ ], glycan = RuleRegexp[ 'Bio::KEGG::GLYCAN', /^ENTRY .+ Glycan\s*/ ], enzyme = RuleRegexp2[ 'Bio::KEGG::ENZYME', /^ENTRY EC [0-9\.]+$/, /^ENTRY .+ Enzyme\s*/ ], compound = RuleRegexp2[ 'Bio::KEGG::COMPOUND', /^ENTRY C[A-Za-z0-9\._]+$/, /^ENTRY .+ Compound\s*/ ], reaction = RuleRegexp2[ 'Bio::KEGG::REACTION', /^ENTRY R[A-Za-z0-9\._]+$/, /^ENTRY .+ Reaction\s*/ ], genes = RuleRegexp[ 'Bio::KEGG::GENES', /^ENTRY .+ (CDS|gene|.*RNA|Contig) / ], genome = RuleRegexp[ 'Bio::KEGG::GENOME', /^ENTRY [a-z]+$/ ], fantom = RuleProc.new('Bio::FANTOM::MaXML::Cluster', 'Bio::FANTOM::MaXML::Sequence') do |text| if /\<\!DOCTYPE\s+maxml\-(sequences|clusters)\s+SYSTEM/ =~ text case $1 when 'clusters' Bio::FANTOM::MaXML::Cluster when 'sequences' Bio::FANTOM::MaXML::Sequence else nil #unknown end else nil end end, pdb = RuleRegexp[ 'Bio::PDB', /^HEADER .{40}\d\d\-[A-Z]{3}\-\d\d [0-9A-Z]{4}/ ], het = RuleRegexp[ 'Bio::PDB::ChemicalComponent', /^RESIDUE +.+ +\d+\s*$/ ], clustal = RuleRegexp2[ 'Bio::ClustalW::Report', /^CLUSTAL .*\(.*\).*sequence +alignment/, /^CLUSTAL FORMAT for T-COFFEE/ ], gcg_msf = RuleRegexp[ 'Bio::GCG::Msf', /^!!(N|A)A_MULTIPLE_ALIGNMENT .+/ ], gcg_seq = RuleRegexp[ 'Bio::GCG::Seq', /^!!(N|A)A_SEQUENCE .+/ ], blastxml = RuleRegexp[ 'Bio::Blast::Report', /\<\!DOCTYPE BlastOutput PUBLIC / ], wublast = RuleRegexp[ 'Bio::Blast::WU::Report', /^BLAST.? +[\-\.\w]+\-WashU +\[[\-\.\w ]+\]/ ], wutblast = RuleRegexp[ 'Bio::Blast::WU::Report_TBlast', /^TBLAST.? +[\-\.\w]+\-WashU +\[[\-\.\w ]+\]/ ], blast = RuleRegexp[ 'Bio::Blast::Default::Report', /^BLAST.? +[\-\.\w]+ +\[[\-\.\w ]+\]/ ], tblast = RuleRegexp[ 'Bio::Blast::Default::Report_TBlast', /^TBLAST.? +[\-\.\w]+ +\[[\-\.\w ]+\]/ ], blat = RuleRegexp[ 'Bio::Blat::Report', /^psLayout version \d+/ ], spidey = RuleRegexp[ 'Bio::Spidey::Report', /^\-\-SPIDEY version .+\-\-$/ ], hmmer = RuleRegexp[ 'Bio::HMMER::Report', /^HMMER +\d+\./ ], sim4 = RuleRegexp[ 'Bio::Sim4::Report', /^seq1 \= .*\, \d+ bp(\r|\r?\n)seq2 \= .*\, \d+ bp(\r|\r?\n)/ ], fastaformat = RuleProc.new('Bio::FastaFormat', 'Bio::NBRF', 'Bio::FastaNumericFormat') do |text| if /^>.+$/ =~ text case text when /^>([PF]1|[DR][LC]|N[13]|XX)\;.+/ Bio::NBRF when /^>.+$\s+(^\#.*$\s*)*^\s*\d*\s*[-a-zA-Z_\.\[\]\(\)\*\+\$]+/ Bio::FastaFormat when /^>.+$\s+^\s*\d+(\s+\d+)*\s*$/ Bio::FastaNumericFormat else false end else nil end end ] # dependencies # NCBI genbank.is_prior_to genpept # EMBL/UniProt embl.is_prior_to sptr sptr.is_prior_to prosite prosite.is_prior_to transfac # KEGG #aaindex.is_prior_to litdb #litdb.is_prior_to brite brite.is_prior_to orthology orthology.is_prior_to drug drug.is_prior_to glycan glycan.is_prior_to enzyme enzyme.is_prior_to compound compound.is_prior_to reaction reaction.is_prior_to genes genes.is_prior_to genome # PDB pdb.is_prior_to het # BLAST wublast.is_prior_to wutblast wutblast.is_prior_to blast blast.is_prior_to tblast # FastaFormat BottomRule.is_prior_to(fastaformat) # for debug #debug_first = RuleDebug.new('debug_first') #a.add(debug_first) #debug_first.is_prior_to(TopRule) ## for debug #debug_last = RuleDebug.new('debug_last') #a.add(debug_last) #BottomRule.is_prior_to(debug_last) #fastaformat.is_prior_to(debug_last) a.rehash return a end |
Instance Method Details
#add(elem) ⇒ Object
Adds a new element. Returns elem.
995 996 997 998 999 1000 |
# File 'lib/bio/io/flatfile.rb', line 995 def add(elem) raise 'element name conflicts' if @rules[elem.name] @elements = nil @rules[elem.name] = elem elem end |
#autodetect(text, meta = {}) ⇒ Object
Autodetect from the text. Returns a database class if succeeded. Returns nil if failed.
1063 1064 1065 1066 1067 1068 1069 1070 1071 |
# File 'lib/bio/io/flatfile.rb', line 1063 def autodetect(text, = {}) r = nil elements.each do |e| #$stderr.puts e.name r = e.guess(text, ) break if r end r end |
#autodetect_flatfile(ff, lines = 31) ⇒ Object
autodetect from the FlatFile object. Returns a database class if succeeded. Returns nil if failed.
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 |
# File 'lib/bio/io/flatfile.rb', line 1076 def autodetect_flatfile(ff, lines = 31) = {} stream = ff.instance_eval { @stream } begin path = stream.path rescue NameError end if path then [:path] = path # call autodetect onece with meta and without any read action if r = self.autodetect(stream.prefetch_buffer, ) return r end end # reading stream 1.upto(lines) do |x| break unless line = stream.prefetch_gets if line.strip.size > 0 then if r = self.autodetect(stream.prefetch_buffer, ) return r end end end return nil end |
#each_rule(&x) ⇒ Object
Iterates over each element.
1056 1057 1058 |
# File 'lib/bio/io/flatfile.rb', line 1056 def each_rule(&x) #:yields: elem elements.each(&x) end |
#elements ⇒ Object
Returns current elements as an array whose order fulfills all elements’ priorities.
1033 1034 1035 1036 1037 1038 1039 1040 |
# File 'lib/bio/io/flatfile.rb', line 1033 def elements unless @elements ary = tsort ary.reverse! @elements = ary end @elements end |
#inspect ⇒ Object
visualizes the object (mainly for debug)
1049 1050 1051 1052 1053 |
# File 'lib/bio/io/flatfile.rb', line 1049 def inspect "<#{self.class.to_s} " + self.elements.collect { |e| e.name.inspect }.join(' ') + ">" end |
#rehash ⇒ Object
rebuilds the object and clears internal cache.
1043 1044 1045 1046 |
# File 'lib/bio/io/flatfile.rb', line 1043 def rehash @rules.rehash @elements = nil end |
#tsort_each_child(elem) ⇒ Object
(required by TSort.) For a given element, yields each child (= lower priority elements) of the element.
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 |
# File 'lib/bio/io/flatfile.rb', line 1011 def tsort_each_child(elem) if elem == TopRule then @rules.each_value do |e| yield e unless e == TopRule or e.lower_priority_elements.index(TopRule) end elsif elem == BottomRule then @rules.each_value do |e| yield e if e.higher_priority_elements.index(BottomRule) end else elem.lower_priority_elements.each do |e| yield e if e != BottomRule end unless elem.higher_priority_elements.index(BottomRule) yield BottomRule end end end |
#tsort_each_node(&x) ⇒ Object
(required by TSort.) For all elements, yields each element.
1004 1005 1006 |
# File 'lib/bio/io/flatfile.rb', line 1004 def tsort_each_node(&x) @rules.each_value(&x) end |