OSDN Git Service

Start implementing 'additional exponent' for JANPA output
[molby/Molby.git] / Scripts / loadsave.rb
1 # coding: utf-8
2 #
3 #  loadsave.rb
4 #
5 #  Created by Toshi Nagata.
6 #  Copyright 2008 Toshi Nagata. All rights reserved.
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation version 2 of the License.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 require "scanf.rb"
18
19 class Molecule
20
21   def loadcrd(filename)
22     if natoms == 0
23       raise MolbyError, "cannot load crd; the molecule is empty"
24     end
25         fp = open(filename, "rb")
26     count = 0
27         frame = 0
28         coords = (0...natoms).collect { Vector3D[0, 0, 0] }
29     periodic = (self.box && self.box[4].any? { |n| n != 0 })
30         show_progress_panel("Loading AMBER crd file...")
31 #    puts "sframe = #{sframe}, pos = #{fp.pos}"
32     while line = fp.gets
33       line.chomp!
34           values = line.scan(/......../)
35           if fp.lineno == 1
36             #  The first line should be skipped. However, if this line seems to contain
37                 #  coordinates, then try reading them
38                 if values.size != 10 && (values.size > 10 || values.size != natoms * 3)
39                   next  #  Wrong number of coordinates
40                 end
41                 if values.each { |v| Float(v) rescue break } == nil
42                   #  The line contains non-number 
43                   next
44             end
45                 puts "Loadcrd: The title line seems to be missing"
46           end
47       if count + values.size > natoms * 3
48         raise MolbyError, sprintf("crd format error - too many values at line %d in file %s; number of atoms = %d, current frame = %d", fp.lineno, fp.path, natoms, frame)
49       end
50           values.each { |v|
51             coords[count / 3][count % 3] = Float(v)
52             count += 1
53           }
54       if count == natoms * 3
55             #  End of frame
56                 if frame == 0 && atoms.all? { |ap| ap.r.x == 0.0 && ap.r.y == 0.0 && ap.r.z == 0.0 }
57                         #  Do not create a new frame
58                         atoms.each_with_index { |ap, i| ap.r = coords[i] }
59                 else
60                         create_frame([coords])
61                 end
62                 if periodic
63                   #  Should have box information
64                   line = fp.gets
65                   if line == nil || (values = line.chomp.scan(/......../)).length != 3
66                     #  Periodic but no cell information
67                         puts "Loadcrd: the molecule has a periodic cell but the crd file does not contain cell info"
68                         periodic = false
69                         redo
70               end
71                   self.cell = [Float(values[0]), Float(values[1]), Float(values[2]), 90, 90, 90]
72                 end
73         count = 0
74         frame += 1
75                 if frame % 5 == 0
76                   set_progress_message("Loading AMBER crd file...\n(#{frame} frames completed)")
77                 end
78       end
79     end
80         fp.close
81         if (count > 0)
82           raise MolbyError, sprintf("crd format error - file ended in the middle of frame %d", frame)
83         end
84         hide_progress_panel
85         if frame > 0
86           self.frame = self.nframes - 1
87           return true
88         else
89           return false
90         end
91   end
92     
93   def savecrd(filename)
94     if natoms == 0
95       raise MolbyError, "cannot save crd; the molecule is empty"
96     end
97     fp = open(filename, "wb")
98         show_progress_panel("Saving AMBER crd file...")
99         fp.printf("TITLE: %d atoms\n", natoms)
100         cframe = self.frame
101         nframes = self.nframes
102         j = 0
103         self.update_enabled = false
104         begin
105                 (0...nframes).each { |i|
106                   select_frame(i)
107                   j = 0
108                   while (j < natoms * 3)
109                         w = atoms[j / 3].r[j % 3]
110                         fp.printf(" %7.3f", w)
111                         fp.print("\n") if (j % 10 == 9 || j == natoms * 3 - 1)
112                         j += 1
113                   end
114                   if i % 5 == 0
115                         set_progress_message("Saving AMBER crd file...\n(#{i} frames completed)")
116                   end
117                 }
118         ensure
119                 self.update_enabled = true
120         end
121         select_frame(cframe)
122         fp.close
123         hide_progress_panel
124         true
125   end
126
127   alias :loadmdcrd :loadcrd
128   alias :savemdcrd :savecrd
129
130   def sub_load_gamess_log_basis_set(lines, lineno)
131     ln = 0
132         while (line = lines[ln])
133                 ln += 1
134                 break if line =~ /SHELL\s+TYPE\s+PRIMITIVE/
135         end
136         ln += 1
137         i = -1
138         nprims = 0
139         sym = -10  #  undefined
140         ncomps = 0
141         clear_basis_set
142         while (line = lines[ln])
143                 ln += 1
144                 break if line =~ /TOTAL NUMBER OF BASIS SET/
145                 if line =~ /^\s*$/
146                   #  End of one shell
147                   add_gaussian_orbital_shell(i, sym, nprims)
148                   nprims = 0
149                   sym = -10
150                   next
151                 end
152                 a = line.split
153                 if a.length == 1
154                   i += 1
155                   ln += 1  #  Skip the blank line
156                   next
157                 elsif a.length == 5 || a.length == 6
158                   if sym == -10
159                         case a[1]
160                         when "S"
161                           sym = 0; n = 1
162                         when "P"
163                           sym = 1; n = 3
164                         when "L"
165                           sym = -1; n = 4
166                         when "D"
167                           sym = 2; n = 6
168                         when "F"
169                           sym = 3; n = 10
170                         when "G"
171                           sym = 4; n = 15
172                         else
173                           raise MolbyError, "Unknown gaussian shell type at line #{lineno + ln}"
174                         end
175                         ncomps += n
176                   end
177                   if (a.length == 5 && sym == -1) || (a.length == 6 && sym != -1)
178                         raise MolbyError, "Wrong format in gaussian shell information at line #{lineno + ln}"
179                   end
180                   exp = Float(a[3])
181                   c = Float(a[4])
182                   csp = Float(a[5] || 0.0)
183                   add_gaussian_primitive_coefficients(exp, c, csp)
184                   nprims += 1
185                 else
186                   raise MolbyError, "Error in reading basis set information at line #{lineno + ln}"
187                 end
188         end
189         return ncomps
190   end
191
192   def sub_load_gamess_log_mo_coefficients(lines, lineno, ncomps)
193     ln = 0
194         idx = 0
195         alpha = true
196         while (line = lines[ln]) != nil
197                 ln += 1
198                 if line =~ /BETA SET/
199                         alpha = false
200                         next
201                 end
202                 if line =~ /------------/ || line =~ /EIGENVECTORS/ || line =~ /\*\*\*\* (ALPHA|BETA) SET/
203                         next
204                 end
205                 next unless line =~ /^\s*\d/
206                 mo_labels = line.split       #  MO numbers (1-based)
207                 mo_energies = lines[ln].split
208                 mo_symmetries = lines[ln + 1].split
209         #       puts "mo #{mo_labels.inspect}"
210                 ln += 2
211                 mo = mo_labels.map { [] }    #  array of *independent* empty arrays
212                 while (line = lines[ln]) != nil
213                         ln += 1
214                         break unless line =~ /^\s*\d/
215                         5.times { |i|
216                           s = line[15 + 11 * i, 11].chomp
217                           break if s =~ /^\s*$/
218                           mo[i].push(Float(s)) rescue print "line = #{line}, s = #{s}"
219                         # line[15..-1].split.each_with_index { |s, i|
220                         #       mo[i].push(Float(s))
221                         }
222                 end
223                 mo.each_with_index { |m, i|
224                         idx = Integer(mo_labels[i])
225                         set_mo_coefficients(idx + (alpha ? 0 : ncomps), Float(mo_energies[i]), m)
226                 #       if mo_labels[i] % 8 == 1
227                 #               puts "set_mo_coefficients #{idx}, #{mo_energies[i]}, [#{m[0]}, ..., #{m[-1]}]"
228                 #       end
229                 }
230 #               if line =~ /^\s*$/
231 #                       next
232 #               else
233 #                       break
234 #               end
235         end
236   end
237     
238   def sub_load_gamess_log(fp)
239
240     if natoms == 0
241                 new_unit = true
242         else
243                 new_unit = false
244     end
245         self.update_enabled = false
246         mes = "Loading GAMESS log file"
247         show_progress_panel(mes)
248         energy = nil
249         ne_alpha = ne_beta = 0   #  number of electrons
250         rflag = nil  #  0, UHF; 1, RHF; 2, ROHF
251         mo_count = 0
252         search_mode = 0   #  0, no search; 1, optimize; 2, irc
253         ncomps = 0   #  Number of AO terms per one MO (sum of the number of components over all shells)
254         alpha_beta = nil   #  Flag to read alpha/beta MO appropriately
255         nsearch = 0  #  Search number for optimization
256         begin
257         #       if nframes > 0
258         #               create_frame
259         #               frame = nframes - 1
260         #       end
261                 n = 0
262                 while 1
263                         line = fp.gets
264                         if line == nil
265                                 break
266                         end
267                         line.chomp!
268                         if line =~ /ATOM\s+ATOMIC\s+COORDINATES/ || line =~ /COORDINATES OF ALL ATOMS ARE/ || line =~ /COORDINATES \(IN ANGSTROM\) FOR \$DATA GROUP ARE/
269                                 set_progress_message(mes + "\nReading atomic coordinates...")
270                                 if line =~ /ATOMIC/
271                                   first_line = true
272                                 #  if !new_unit
273                                 #    next   #  Skip initial atomic coordinates unless loading into an empty molecule
274                                 #  end
275                                   line = fp.gets  #  Skip one line
276                                 else
277                                   first_line = false
278                                   nsearch += 1
279                                   if line =~ /COORDINATES OF ALL ATOMS ARE/
280                                     line = fp.gets  #  Skip one line
281                               end
282                                 end
283                                 n = 0
284                                 coords = []
285                                 names = []
286                                 while (line = fp.gets) != nil
287                                         break if line =~ /^\s*$/ || line =~ /END OF ONE/ || line =~ /\$END/
288                                         next if line =~ /-----/
289                                         name, charge, x, y, z = line.split
290                                         v = Vector3D[x, y, z]
291                                         coords.push(v * (first_line ? 0.529177 : 1.0))  #  Bohr to angstrom
292                                         names.push([name, charge])
293                                 end
294                                 if new_unit
295                                         #  Build a new molecule
296                                         names.each_index { |i|
297                                                 ap = add_atom(names[i][0])
298                                                 ap.atomic_number = names[i][1].to_i
299                                                 ap.atom_type = ap.element
300                                                 ap.r = coords[i]
301                                         }
302                                         #  Find bonds
303                                         guess_bonds
304                                 #       atoms.each { |ap|
305                                 #               j = ap.index
306                                 #               (j + 1 ... natoms).each { |k|
307                                 #                       if calc_bond(j, k) < 1.7
308                                 #                               create_bond(j, k)
309                                 #                       end
310                                 #               }
311                                 #       }
312                                         new_unit = false
313                                 #       create_frame
314                                 else
315                                     dont_create = false
316                                         if (search_mode == 1 && nsearch == 1) || first_line
317                                                 #  The input coordinate and the first frame for geometry search
318                                                 #  can have the same coordinate as the last frame; if this is the case, then
319                                                 #  do not create the new frame
320                                                 select_frame(nframes - 1)
321                                                 dont_create = true
322                                                 each_atom { |ap|
323                                                   if (ap.r - coords[ap.index]).length2 > 1e-8
324                                                     dont_create = false
325                                                         break
326                                                   end
327                                                 }
328                                         end
329                                         if !dont_create
330                                                 create_frame([coords])  #  Should not be (coords)
331                                         end
332                                 end
333                                 set_property("energy", energy) if energy
334                         elsif line =~ /BEGINNING GEOMETRY SEARCH POINT/
335                                 energy = nil   #  New search has begun, so clear the energy
336                                 search_mode = 1
337                         elsif line =~ /CONSTRAINED OPTIMIZATION POINT/
338                                 energy = nil   #  New search has begun, so clear the energy
339                                 search_mode = 2
340                         elsif line =~ /FINAL .* ENERGY IS *([-.0-9]+) AFTER/
341                                 if search_mode != 2
342                                         energy = $1.to_f
343                                         set_property("energy", energy)
344                                 end
345                         elsif line =~ /TOTAL ENERGY += +([-.0-9]+)/
346                                 energy = $1.to_f
347                         elsif false && line =~ /EQUILIBRIUM GEOMETRY LOCATED/i
348                                 set_progress_message(mes + "\nReading optimized coordinates...")
349                                 fp.gets; fp.gets; fp.gets
350                                 n = 0
351                                 while (line = fp.gets) != nil
352                                         break if line =~ /^\s*$/
353                                         line.chomp
354                                         atom, an, x, y, z = line.split
355                                         ap = atoms[n]
356                                         ap.r = Vector3D[x, y, z]
357                                         n += 1
358                                         break if n >= natoms
359                                 end
360                         #       if ne_alpha > 0 && ne_beta > 0
361                         #               #  Allocate basis set record again, to update the atomic coordinates
362                         #               allocate_basis_set_record(rflag, ne_alpha, ne_beta)
363                         #       end
364                         elsif line =~ /ATOMIC BASIS SET/
365                                 lines = []
366                                 lineno = fp.lineno
367                                 while (line = fp.gets)
368                                         break if line =~ /TOTAL NUMBER OF BASIS SET/
369                                         line.chomp!
370                                         lines.push(line)
371                                 end
372                                 ncomps = sub_load_gamess_log_basis_set(lines, lineno)
373                         elsif line =~ /NUMBER OF OCCUPIED ORBITALS/
374                                 line =~ /=\s*(\d+)/
375                                 n = Integer($1)
376                                 if line =~ /ALPHA/
377                                         ne_alpha = n
378                                 else
379                                         ne_beta = n
380                                 end
381                         elsif line =~ /SCFTYP=(\w+)/
382                                 scftyp = $1
383                                 if ne_alpha > 0 || ne_beta > 0
384                                         rflag = 0
385                                         case scftyp
386                                         when "RHF"
387                                                 rflag = 1
388                                         when "ROHF"
389                                                 rflag = 2
390                                         end
391                                 end
392                         elsif line =~ /(ALPHA|BETA)\s*SET/
393                                 alpha_beta = $1
394                         elsif line =~ /^\s*(EIGENVECTORS|MOLECULAR ORBITALS)\s*$/
395                                 if mo_count == 0
396                                         clear_mo_coefficients
397                                         set_mo_info(:type=>["UHF", "RHF", "ROHF"][rflag], :alpha=>ne_alpha, :beta=>ne_beta)
398                                 end
399                                 mo_count += 1
400                                 line = fp.gets; line = fp.gets
401                                 lineno = fp.lineno
402                                 lines = []
403                                 set_progress_message(mes + "\nReading MO coefficients...")
404                                 while (line = fp.gets)
405                                         break if line =~ /\.\.\.\.\.\./ || line =~ /----------------/
406                                         line.chomp!
407                                         lines.push(line)
408                                 end
409                                 sub_load_gamess_log_mo_coefficients(lines, lineno, ncomps)
410                                 set_progress_message(mes)
411                         elsif line =~ /N A T U R A L   B O N D   O R B I T A L   A N A L Y S I S/
412                                 nbo_lines = []
413                                 while (line = fp.gets) != nil
414                                   break if line =~ /done with NBO analysis/
415                                   nbo_lines.push(line)
416                                 end
417                                 import_nbo_log(nbo_lines)
418                         end
419                 end
420         end
421         if nframes > 0
422           select_frame(nframes - 1)
423         end
424         if energy && energy != 0.0
425           set_property("energy", energy)
426         end
427         hide_progress_panel
428         self.update_enabled = true
429         (n > 0 ? true : false)
430   end
431   
432   def sub_load_gaussian_log(fp)
433
434     if natoms == 0
435                 new_unit = true
436         else
437                 new_unit = false
438     end
439         if nframes > 0
440                 create_frame
441                 frame = nframes - 1
442         end
443         n = 0
444         nf = 0
445         energy = nil
446         use_input_orientation = false
447         show_progress_panel("Loading Gaussian out file...")
448         while 1
449                 line = fp.gets
450                 if line == nil
451                         break
452                 end
453                 line.chomp!
454                 if line =~ /(Input|Standard) orientation/
455                         match = $1
456                         if match == "Input"
457                                 use_input_orientation = true if nf == 0
458                                 next if !use_input_orientation
459                         else
460                                 next if use_input_orientation
461                         end
462                         4.times { line = fp.gets }    #  Skip four lines
463                         n = 0
464                         coords = []
465                         anums = []
466                         while (line = fp.gets) != nil
467                                 break if line =~ /-----/
468                                 num, charge, type, x, y, z = line.split
469                                 coords.push(Vector3D[x, y, z])
470                                 anums.push(charge)
471                                 n += 1
472                         end
473                         if new_unit
474                                 #  Build a new molecule
475                                 anums.each_index { |i|
476                                         ap = add_atom("X")
477                                         ap.atomic_number = anums[i]
478                                         ap.atom_type = ap.element
479                                         ap.name = sprintf("%s%d", ap.element, i)
480                                         ap.r = coords[i]
481                                 }
482                                 #  Find bonds
483                         #       atoms.each { |ap|
484                         #               j = ap.index
485                         #               (j + 1 ... natoms).each { |k|
486                         #                       if calc_bond(j, k) < 1.7
487                         #                               create_bond(j, k)
488                         #                       end
489                         #               }
490                         #       }
491                                 guess_bonds
492                                 new_unit = false
493                         #       create_frame
494                         else
495                                 create_frame([coords])  #  Should not be (coords)
496                         end
497                         if energy
498                                 # TODO: to ensure whether the energy output line comes before
499                                 # or after the atomic coordinates.
500                                 set_property("energy", energy)
501                         end
502                         nf += 1
503                 elsif line =~ /SCF Done: *E\(\w+\) *= *([-.0-9]+)/
504                         energy = $1.to_f
505                 end
506         end
507         if energy
508                 set_property("energy", energy)
509         end
510         hide_progress_panel
511         (n > 0 ? true : false)
512   end
513
514   def sub_load_psi4_log(fp)
515     if natoms == 0
516       new_unit = true
517     else
518       new_unit = false
519     end
520     n = 0
521     nf = 0
522     energy = nil
523   
524     show_progress_panel("Loading Psi4 output file...")
525
526     getline = lambda { @lineno += 1; return fp.gets }
527
528     #  Import coordinates and energies
529     vecs = []
530     ats = []
531     first_frame = nframes
532     trans = nil
533     hf_type = nil
534     nalpha = nil
535     nbeta = nil
536     while line = getline.call
537       if line =~ /==> Geometry <==/
538         #  Skip until line containing "------"
539         while line = getline.call
540           break if line =~ /------/
541         end
542         vecs.clear
543         index = 0
544         #  Read atom positions
545         while line = getline.call
546           line.chomp!
547           break if line =~ /^\s*$/
548           tokens = line.split(' ')
549           if natoms > 0 && first_frame == nframes
550             if index >= natoms || tokens[0] != atoms[index].element
551               hide_progress_panel
552               raise MolbyError, "The atom list does not match the current structure at line #{@lineno}"
553             end
554           end
555           vecs.push(Vector3D[Float(tokens[1]), Float(tokens[2]), Float(tokens[3])])
556           if natoms == 0
557             ats.push(tokens[0])
558           end
559           index += 1
560         end
561         if natoms == 0
562           #  Create molecule from the initial geometry
563           ats.each_with_index { |aname, i|
564             #  Create atoms
565             ap = add_atom(aname)
566             ap.element = aname
567             ap.atom_type = ap.element
568             ap.name = sprintf("%s%d", aname, i)
569             ap.r = vecs[i]
570           }
571           guess_bonds
572         else
573           if vecs.length != natoms
574             break  #  Log file is incomplete
575           end
576           #  Does this geometry differ from the last one?
577           vecs.length.times { |i|
578             if (atoms[i].r - vecs[i]).length2 > 1.0e-14
579               #  Create a new frame and break
580               create_frame
581               vecs.length.times { |j|
582                 atoms[j].r = vecs[j]
583               }
584               break
585             end
586           }
587         end
588         #  end geometry
589       elsif line =~ /Final Energy: +([-.0-9]+)/
590         #  Energy for this geometry
591         energy = Float($1)
592         set_property("energy", energy)
593         if line =~ /RHF/
594           hf_type = "RHF"
595         elsif line =~ /UHF/
596           hf_type = "UHF"
597         elsif line =~ /ROHF/
598           hf_type = "ROHF"
599         end
600       elsif line =~ /^ *Nalpha *= *(\d+)/
601         nalpha = Integer($1)
602       elsif line =~ /^ *Nbeta *= *(\d+)/
603         nbeta = Integer($1)
604       end
605     end
606     hide_progress_panel
607     clear_basis_set
608     clear_mo_coefficients
609     set_mo_info(:type => hf_type, :alpha => nalpha, :beta => nbeta)
610     return true
611   end
612
613   #  mol.set_mo_info should be set before calling this function
614   #  Optional label is for importing JANPA output: "NAO" or "CPLO"
615   #  If label is not nil, then returns a hash containing the following key/value pairs:
616   #    :atoms => an array of [element_symbol, seq_num, atomic_num, x, y, z] (angstrom)
617   #    :gto => an array of an array of [sym, [ex0, c0, ex1, c1, ...]]
618   #    :moinfo => an array of [sym, energy, spin (0 or 1), occ]
619   #    :mo => an array of [c0, c1, ...]
620   def sub_load_molden(fp, label = nil)
621     getline = lambda { @lineno += 1; return fp.gets }
622     bohr = 0.529177210903
623     errmsg = nil
624     ncomps = 0  #  Number of components (AOs)
625     occ_alpha = 0  #  Number of occupied alpha orbitals
626     occ_beta = 0   #  Number of occupied beta orbitals
627     if label
628       hash = Hash.new
629     end
630     #  The GTOs (orbital type, contractions and exponents) are stored in gtos[]
631     #  and set just before first [MO] is processed.
632     #  This is because we do not know whether the orbital type is cartesian or spherical
633     #  until we see "[5D]" or "[9G]".
634     gtos = []
635     spherical = false
636     #  Number of components for each orbital type
637     ncomp_hash = { 0=>1, 1=>3, -1=>4, 2=>6, -2=>5, 3=>10, -3=>7, 4=>15, -4=>9 }
638     catch :ignore do
639       while line = getline.call
640         if line =~ /^\[Atoms\]/
641           i = 0
642           while line = getline.call
643             if line =~ /^[A-Z]/
644               #  element, index, atomic_number, x, y, z (in AU)
645               a = line.split(' ')
646               if label
647                 (hash[:atoms] ||= []).push([a[0], Integer(a[1]), Integer(a[2]), Float(a[3]) * bohr, Float(a[4]) * bohr, Float(a[5]) * bohr])
648               else
649                 if atoms[i].atomic_number != Integer(a[2]) ||
650                   (atoms[i].x - Float(a[3]) * bohr).abs > 1e-4 ||
651                   (atoms[i].y - Float(a[4]) * bohr).abs > 1e-4 ||
652                   (atoms[i].z - Float(a[5]) * bohr).abs > 1e-4
653                   errmsg = "The atom list does not match the current molecule."
654                   throw :ignore
655                 end
656               end
657               i += 1
658             else
659               break
660             end
661           end
662           redo  #  The next line will be the beginning of the next block
663         elsif line =~ /^\[GTO\]/
664           shell = 0
665           atom_index = 0
666           while line = getline.call
667             #  index, 0?
668             a = line.split(' ')
669             break if a.length != 2
670             atom_gtos = []  #  [[sym1, [e11, c11, e12, c12, ...], add_exp1], [sym2, [e21, c22, ...], add_exp2], ...]
671             #  loop for shells
672             while line = getline.call
673               #  type, no_of_primitives, 1.00?
674               a = line.split(' ')
675               break if a.length != 3   #  Terminated by a blank line
676               a[0] =~ /^([a-z]+)([0-9]+)?$/
677               symcode = $1
678               add_exp = ($2 == nil ? 0 : $2.to_i)
679               case symcode
680               when "s"
681                 sym = 0
682               when "p"
683                 sym = 1
684               when "d"
685                 sym = 2
686               when "f"
687                 sym = 3
688               when "g"
689                 sym = 4
690               else
691                 raise MolbyError, "Unknown gaussian shell type '#{a[0]}' at line #{@lineno} in MOLDEN file"
692               end
693               nprimitives = Integer(a[1])
694               gtoline = [sym, [], add_exp]
695               atom_gtos.push(gtoline)
696               nprimitives.times { |i|
697                 line = getline.call   #  exponent, contraction
698                 b = line.split(' ')
699                 gtoline[1].push(Float(b[0]), Float(b[1]))
700               }
701               #  end of one shell
702               shell += 1
703             end
704             #  end of one atom
705             atom_index += 1
706             gtos.push(atom_gtos)
707           end
708           if label
709             hash[:gto] = gtos
710           end
711           redo  #  The next line will be the beginning of the next block
712         elsif line =~ /^\[5D\]/ || line =~ /^\[9G\]/
713           spherical = true
714           #  Change the orbital type if necessary
715           gtos.each_with_index { | atom_gtos, atom_index|
716             atom_gtos.each { |gtoline|
717               if gtoline[0] >= 2
718                 gtoline[0] = -gtoline[0]  #  D5/F7/G9
719               end
720             }
721           }
722         elsif line =~ /^\[MO\]/
723           #  Add shell info and primitive coefficients to molecule
724           gtos.each_with_index { | atom_gtos, atom_index|
725             atom_gtos.each { |gtoline|
726               sym = gtoline[0]
727               coeffs = gtoline[1]
728               nprimitives = coeffs.length / 2
729               add_exp = gtoline[2]
730               ncomps += ncomp_hash[sym]
731               if !label
732                 add_gaussian_orbital_shell(atom_index, sym, nprimitives, add_exp)
733                 nprimitives.times { |prim|
734                   add_gaussian_primitive_coefficients(coeffs[prim * 2], coeffs[prim * 2 + 1], 0.0)
735                 }
736               end
737             }
738           }
739           m = []
740           idx_alpha = 1   #  set_mo_coefficients() accepts 1-based index of MO
741           idx_beta = 1
742           if label
743             hash[:mo] = []
744             hash[:moinfo] = []
745           end
746           while true
747             #  Loop for each MO
748             m.clear
749             ene = nil
750             spin = nil
751             sym = nil   #  Not used in Molby
752             occ = nil
753             i = 0
754             while line = getline.call
755               if line =~ /^ *Sym= *(\w+)/
756                 sym = $1
757               elsif line =~ /^ *Ene= *([-+.0-9e]+)/
758                 ene = Float($1)
759               elsif line =~ /^ *Spin= *(\w+)/
760                 spin = $1
761               elsif line =~ /^ *Occup= *([-+.0-9e]+)/
762                 occ = Float($1)
763                 if occ > 0.0
764                   if spin == "Alpha"
765                     occ_alpha += 1
766                   else
767                     occ_beta += 1
768                   end
769                 end
770                 if label
771                   hash[:moinfo].push([sym, ene, (spin == "Alpha" ? 0 : 1), occ])
772                 end
773               elsif line =~ /^ *([0-9]+) +([-+.0-9e]+)/
774                 m[i] = Float($2)
775                 i += 1
776                 if i >= ncomps
777                   if spin == "Alpha"
778                     idx = idx_alpha
779                     idx_alpha += 1
780                   else
781                     idx = idx_beta
782                     idx_beta += 1
783                   end
784                   set_mo_coefficients(idx, ene, m)
785                   if label
786                     hash[:mo].push(m.dup)
787                   end
788                   break
789                 end
790               else
791                 break
792               end
793             end
794             break if i < ncomps  #  no MO info was found
795           end
796           #  TODO: reorder D, F, G coefficients for Molby order
797           next
798         end #  end if
799       end   #  end while
800     end     #  end catch
801     if errmsg
802       message_box("The MOLDEN file was found but not imported. " + errmsg, "Psi4 import info", :ok)
803       return (label ? nil : false)
804     end
805     return (label ? hash : true)
806   end
807
808   #  Import the JANPA log and related molden files
809   #  Files: inppath.{NAO.molden,CLPO.molden,janpa.log}
810   #  If inppath.spherical.molden is available, then clear existing mo info
811   #  and load from it (i.e. use the basis set converted by molden2molden)
812   def sub_load_janpa_log(inppath)
813     begin
814       fp = File.open(inppath + ".janpa.log", "rt") rescue fp = nil
815       if fp == nil
816         hide_progress_panel  #  Close if it is open
817         message_box("Cannot open JANPA log file #{inppath + '.janpa.log'}: " + $!.to_s)
818         return false
819       end
820       print("Importing #{inppath}.janpa.log.\n")
821       lineno = 0
822       getline = lambda { lineno += 1; return fp.gets }
823       h = Hash.new
824       mfiles = Hash.new
825       h["software"] = "JANPA"
826       nao_num = nil  #  Set later
827       nao_infos = [] #  index=atom_index, value=Hash with key "s", "px", "py" etc.
828       #  nao_infos[index][key]: array of [nao_num, occupancy], in the reverse order of appearance
829       while line = getline.call
830         if line =~ /molden2molden: a conversion tool for MOLDEN/
831           while line = getline.call
832             break if line =~ /^All done!/
833             if line =~ /\.spherical\.molden/
834               #  The MOs are converted to spherical basis set
835               #  Clear the existing MO and load *.spherical.molden
836               sname = inppath + ".spherical.molden"
837               fps = File.open(sname, "rt") rescue fps = nil
838               if fps != nil
839                 print("Importing #{sname}.\n")
840                 @lineno = 0
841                 type = get_mo_info(:type)
842                 alpha = get_mo_info(:alpha)
843                 beta = get_mo_info(:beta)
844                 clear_basis_set
845                 set_mo_info(:type=>type, :alpha=>alpha, :beta=>beta)
846                 #  mol.@hf_type should be set before calling sub_load_molden
847                 @hf_type = type
848                 sub_load_molden(fps)
849                 fps.close
850               end
851             end
852           end
853         elsif line =~ /^NAO \#/
854           h["NAO"] = []
855           while line = getline.call
856             break if line !~ /^\s*[1-9]/
857             num = Integer(line[0, 5])
858             name = line[5, 21]
859             occ = Float(line[26, 11])
860             #  like A1*: R1*s(0)
861             #  atom_number, occupied?, group_number, orb_sym, angular_number
862             name =~ /\s*[A-Z]+([0-9]+)(\*?):\s* R([0-9]+)\*([a-z]+)\(([-0-9]+)\)/
863             anum = Integer($1)
864             occupied = $2
865             group_num = Integer($3)
866             orb_sym = $4
867             ang_num = Integer($5)
868             orb_desc = orb_sym
869             if orb_desc == "p"
870               orb_desc += ["z", "x", "y"][ang_num + 1]
871             elsif orb_desc == "d"
872             #  TODO: handle d, f, g orbitals
873             end
874             h["NAO"].push([num, anum, occupied, group_num, orb_desc, occ])
875             nao_num = h["NAO"].length
876             ((nao_infos[anum - 1] ||= Hash.new)[orb_desc] ||= []).unshift([nao_num, occ])
877           end
878           #  Create labels
879           h["NAO_L"] = []
880           nao_infos.each_with_index { |value, atom_index|
881             aname = self.atoms[atom_index].name
882             value.each { |orb_desc, ar|
883               ar.each_with_index { |v, group_index|
884                 if v[1] > 1.9
885                   label = "core"
886                 elsif v[1] > 0.01
887                   label = "val"
888                 else
889                   label = "ryd"
890                 end
891                 principle = group_index + 1
892                 orb_sym = orb_desc[0]
893                 if orb_sym == "p"
894                   principle += 1
895                 elsif orb_sym == "d"
896                   principle += 2
897                 elsif orb_sym == "f"
898                   principle += 3
899                 elsif orb_sym == "g"
900                   principle += 4
901                 end
902                 h["NAO_L"][v[0] - 1] = "#{aname} (#{principle}#{orb_desc}) (#{label})"
903               }
904             }
905           }
906         elsif line =~ /^\s*(C?)LPO\s+D e s c r i p t i o n\s+Occupancy\s+Composition/
907           if $1 == "C"
908             key = "CLPO"
909           else
910             key = "LPO"
911           end
912           h[key] = []
913           while line = getline.call
914             break if line =~ /^\s*$/
915             num = Integer(line[0, 5])
916             label1 = line[5, 6].strip
917             desc = line[11, 30].strip
918             occ = line[41, 11].strip
919             comp = line[52, 1000].strip
920             desc =~ /\s*([-A-Za-z0-9]+)(,\s*(.*$))?/
921             desc1 = $1
922             desc2 = ($3 || "")
923             if desc2 =~ /^(.*)*\(NB\)\s*$/ && label1 == ""
924               label1 = "(NB)"
925               desc2 = $1.strip
926             end
927             atoms = desc1.scan(/[A-Za-z]+(\d+)/)   # "C1-H3" -> [["1"], ["3"]]
928             atoms = atoms.map { |a| Integer(a[0]) }  # [1, 3]
929             hybrids_a = comp.scan(/h(\d+)@[A-Za-z]+(\d+)/)  #  "h8@C1...h13@H3" -> "[["8", "1"], ["13", "3"]]
930             hybrids = []
931             hybrids_a.each { |a|
932               i = atoms.find_index(Integer(a[1]))
933               if i != nil
934                 hybrids[i] = Integer(a[0])
935               end
936             } # [8, 13]
937             #  like ["(BD)", [1, 3], "Io = 0.2237", occ, [8, 13]]
938             #  1, 3 are the atom indices (1-based)
939             #  8, 13 are the number of hybrid orbitals (1-based)
940             h[key][num - 1] = [label1, atoms, desc2, Float(occ), hybrids]
941           end
942           h[key + "_L"] = []
943           if key == "CLPO"
944             #  Also register labels of "LHO"
945             h["LHO_L"] = [""] * nao_num
946           end
947           nao_num.times { |i|
948             val = h[key][i]
949             if val == nil
950               label = ""  #  The labels for Rydberg orbitals may be replaced later
951             else
952               aname1 = self.atoms[val[1][0] - 1].name rescue aname1 = ""
953               aname2 = self.atoms[val[1][1] - 1].name rescue aname2 = ""
954               if aname2 == ""
955                 label = "#{aname1} #{val[0]}"
956               else
957                 label = "#{aname1}(#{aname2}) #{val[0]}"
958               end
959             end
960             h[key + "_L"][i] = label
961             if key == "CLPO" && val != nil && val[0] != "(NB)"
962               hybrids = val[4]
963               kind = (val[0] == "(BD)" ? "(val)" : "(lp)")
964               if aname2 == ""
965                 label = "#{aname1} #{kind}"
966               else
967                 label = "#{aname1}(#{aname2}) #{kind}"
968               end
969               h["LHO_L"][hybrids[0] - 1] = label
970               if hybrids[1] != nil
971                 #  aname2 should be non-empty
972                 label = "#{aname2}(#{aname1}) #{kind}"
973                 h["LHO_L"][hybrids[1] - 1] = label
974               end
975             end
976           }
977         elsif line =~ /^ -NAO_Molden_File: (\S*)/
978           mfiles["NAO"] = $1
979         elsif line =~ /^ -LHO_Molden_File: (\S*)/
980           mfiles["LHO"] = $1
981         elsif line =~ /^ -CLPO_Molden_File: (\S*)/
982           mfiles["CLPO"] = $1
983         elsif line =~ /^ -PNAO_Molden_File: (\S*)/
984           mfiles["PNAO"] = $1
985         elsif line =~ /^ -AHO_Molden_File: (\S*)/
986           mfiles["AHO"] = $1
987         elsif line =~ /^ -LPO_Molden_File: (\S*)/
988           mfiles["LPO"] = $1
989         end
990       end
991       fp.close
992       #  Read molden files
993       mfiles.each { |key, value|
994         fp = Kernel.open(value, "rt") rescue fp = nil
995         if fp
996           print("Importing #{value}.\n")
997           res = sub_load_molden(fp, key)
998           if res
999             #  Some kind of orbital based on AO
1000             h["AO/#{key}"] = LAMatrix.new(res[:mo])
1001           end
1002           fp.close
1003           if key == "CLPO" || key == "LPO" || key == "LHO"
1004             #  Set the label of Rydberg orbitals if possible
1005             if h[key + "_L"] != nil
1006               a = h["AO/#{key}"]
1007               nao_num.times { |i|
1008                 label = h[key + "_L"][i]
1009                 if label == ""
1010                   max_idx = nil
1011                   max_val = -1.0
1012                   nao_infos.each_with_index { |inf, atom_index|
1013                     atomic_contrib = 0.0
1014                     inf.each { |k, v| # k is "s", "px" etc, v is array of [nao_num, occupancy]
1015                       #  Sum for all naos belonging to this atom
1016                       v.each { |num_occ|
1017                         atomic_contrib += a[i, num_occ[0] - 1] ** 2
1018                       }
1019                     }
1020                     if atomic_contrib > max_val
1021                       max_val = atomic_contrib
1022                       max_idx = atom_index
1023                     end
1024                   }
1025                   label = self.atoms[max_idx].name + " (ry)"
1026                   h[key + "_L"][i] = label
1027                 end
1028               }
1029             end
1030           end
1031         end
1032       }
1033       @nbo = h
1034       if @nbo["AO/NAO"] && @nbo["AO/LHO"] && @nbo["AO/PNAO"]
1035         #  Generate PLHO from PNAO, NAO, LHO
1036         #  This protocol was suggested by the JANPA author in a private commnunication.
1037         begin
1038           nao2lho = @nbo["AO/NAO"].inverse * @nbo["AO/LHO"]
1039           nao2pnao = @nbo["AO/NAO"].inverse * @nbo["AO/PNAO"]
1040           sign = LAMatrix.diagonal((0...nao2pnao.column_size).map { |i| (nao2pnao[i, i] < 0 ? -1 : 1)})
1041           @nbo["AO/PLHO"] = @nbo["AO/PNAO"] * sign * nao2lho
1042         rescue
1043           @nbo["AO/PLHO"] = nil
1044         end
1045       end
1046       return true
1047     rescue => e
1048       $stderr.write(e.message + "\n")
1049       $stderr.write(e.backtrace.inspect + "\n")
1050     end
1051   end
1052
1053   def loadout(filename)
1054   retval = false
1055   fp = open(filename, "rb")
1056   @lineno = 0
1057   begin
1058     while s = fp.gets
1059       @lineno += 1
1060       if s =~ /Gaussian/
1061         retval = sub_load_gaussian_log(fp)
1062         break
1063       elsif s =~ /GAMESS/
1064         retval = sub_load_gamess_log(fp)
1065         break
1066       elsif s =~ /Psi4/
1067         retval = sub_load_psi4_log(fp)
1068         if retval
1069           #  If .molden file exists, then try to read it
1070           namepath = filename.gsub(/\.\w*$/, "")
1071           mname = "#{namepath}.molden"
1072           if File.exists?(mname)
1073             fp2 = open(mname, "rb")
1074             if fp2
1075               flag = sub_load_molden(fp2)
1076               fp2.close
1077               status = (flag ? 0 : -1)
1078             end
1079           end
1080           if File.exists?("#{namepath}.janpa.log")
1081             flag = sub_load_janpa_log(namepath)
1082             status = (flag ? 0 : -1)
1083           end
1084         end
1085         break
1086       end
1087     end
1088   rescue
1089     hide_progress_panel
1090     raise
1091   end
1092         fp.close
1093         return retval
1094   end
1095   
1096   alias :loadlog :loadout
1097
1098   def loadxyz(filename)
1099         fp = open(filename, "rb")
1100         n = 0
1101         coords = []
1102         names = []
1103         cell = nil
1104         while 1
1105           line = fp.gets
1106           if line == nil
1107                 fp.close
1108                 break
1109           end
1110           line.chomp
1111           toks = line.split
1112           if coords.length == 0
1113             #  Allow "number of atoms" line or "number of atoms and crystallographic cell parameter"
1114                 #  (Chem3D xyz format)
1115                 next if toks.length == 1
1116                 if toks.length == 7
1117                   cell = toks[1..6].map { |s| Float(s.sub(/\(\d+\)/, "")) }  #  Remove (xx) and convert to float
1118                   next
1119                 end
1120           end
1121           name, x, y, z = line.split
1122           next if z == nil
1123           x = Float(x.sub(/\(\d+\)/, ""))
1124           y = Float(y.sub(/\(\d+\)/, ""))
1125           z = Float(z.sub(/\(\d+\)/, ""))
1126           r = Vector3D[x, y, z]
1127           coords.push(r)
1128           names.push(name)
1129           n += 1
1130         end
1131         celltr = nil
1132         if cell
1133           self.cell = cell
1134           celltr = self.cell_transform
1135         end
1136         names.each_index { |i|
1137           ap = add_atom(names[i])
1138           names[i] =~ /^([A-Za-z]{1,2})/
1139           element = $1.capitalize
1140           ap.element = element
1141           ap.atom_type = element
1142           if celltr
1143             ap.r = celltr * coords[i]
1144           else
1145             ap.r = coords[i]
1146           end
1147         }
1148         guess_bonds
1149         #  Find bonds
1150 #       atoms.each { |ap|
1151 #         j = ap.index
1152 #         (j + 1 ... natoms).each { |k|
1153 #               if calc_bond(j, k) < 1.7
1154 #               #  create_bond(j, k)
1155 #               end
1156 #         }
1157 #       }
1158 #       self.undo_enabled = save_undo_enabled
1159         (n > 0 ? true : false)
1160   end
1161   
1162   def savexyz(filename)
1163     open(filename, "wb") { |fp|
1164           fp.printf "%d\n", self.natoms
1165           each_atom { |ap|
1166             fp.printf "%s %.5f %.5f %.5f\n", ap.element, ap.x, ap.y, ap.z
1167           }
1168         }
1169         return true
1170   end
1171   
1172   def loadzmat(filename)
1173     self.remove(All)
1174         open(filename, "rb") { |fp|
1175           while (line = fp.gets)
1176             line.chomp!
1177                 a = line.split
1178                 an = Molecule.guess_atomic_number_from_name(a[0])
1179                 elm = Parameter.builtin.elements[an].name
1180                 base1 = a[1].to_i - 1
1181                 base2 = a[3].to_i - 1
1182                 base3 = a[5].to_i - 1
1183                 base1 = nil if base1 < 0
1184                 base2 = nil if base2 < 0
1185                 base3 = nil if base3 < 0
1186                 add_atom(a[0], elm, elm, a[2].to_f, base1, a[4].to_f, base2, a[6].to_f, base3)
1187           end
1188         }
1189         return true
1190   end
1191   
1192   def savezmat(filename)
1193   end
1194   
1195   def loadcom(filename)
1196 #       save_undo_enabled = self.undo_enabled?
1197 #       self.undo_enabled = false
1198         self.remove(All)
1199         fp = open(filename, "rb")
1200         section = 0
1201         while (line = fp.gets)
1202           line.chomp!
1203           if section == 0
1204             section = 1 if line =~ /^\#/
1205                 next
1206           elsif section == 1 || section == 2
1207             section += 1 if line =~ /^\s*$/
1208                 next
1209           else
1210             #  The first line is skipped (charge and multiplicity)
1211                 while (line = fp.gets)
1212                   line.chomp!
1213                   break if line =~ /^\s*$/
1214                   a = line.split(/\s*[ \t,\/]\s*/)
1215                   r = Vector3D[Float(a[1]), Float(a[2]), Float(a[3])]
1216                   ap = add_atom(a[0])
1217                   a[0] =~ /^([A-Za-z]{1,2})/
1218                   element = $1.capitalize
1219               ap.element = element
1220               ap.atom_type = element
1221               ap.r = r
1222                 end
1223                 break
1224           end
1225         end
1226         fp.close
1227         guess_bonds
1228 #       self.undo_enabled = save_undo_enabled
1229         return true
1230   end
1231   
1232   def loadinp(filename)
1233 #       save_undo_enabled = self.undo_enabled?
1234 #       self.undo_enabled = false
1235         self.remove(All)
1236         fp = open(filename, "rb")
1237         section = 0
1238         has_basis = false
1239         while (line = fp.gets)
1240           if line =~ /\A \$BASIS/
1241             has_basis = true
1242                 next
1243           end
1244           next if line !~ /\A \$DATA/
1245           line = fp.gets   #  Title line
1246           line = fp.gets   #  Symmetry line
1247           while (line = fp.gets)
1248 #           puts line
1249             line.chomp!
1250                 break if line =~ /\$END/
1251                 a = line.split
1252                 ap = add_atom(a[0])
1253                 ap.atomic_number = Integer(a[1])
1254                 ap.atom_type = ap.element
1255                 r = Vector3D[Float(a[2]), Float(a[3]), Float(a[4])]
1256                 ap.r = r;
1257                 if !has_basis
1258                   #  Skip until one blank line is detected
1259                   while (line = fp.gets) && line =~ /\S/
1260                   end
1261                 end
1262       end
1263           break
1264         end
1265         fp.close
1266         guess_bonds
1267 #       self.undo_enabled = save_undo_enabled
1268         return true
1269   end
1270   
1271   def saveinp(filename)
1272     if natoms == 0
1273       raise MolbyError, "cannot save GAMESS input; the molecule is empty"
1274     end
1275     fp = open(filename, "wb")
1276         now = Time.now.to_s
1277         fp.print <<end_of_header
1278 !  GAMESS input
1279 !  Generated by Molby at #{now}
1280  $CONTRL COORD=UNIQUE EXETYP=RUN ICHARG=0
1281          ICUT=20 INTTYP=HONDO ITOL=30
1282          MAXIT=200 MOLPLT=.T. MPLEVL=0
1283          MULT=1 QMTTOL=1e-08 RUNTYP=OPTIMIZE
1284          SCFTYP=RHF UNITS=ANGS                  $END
1285  $SCF    CONV=1.0E-06 DIRSCF=.T.                $END
1286  $STATPT NSTEP=400 OPTTOL=1.0E-06               $END
1287  $SYSTEM MEMDDI=0 MWORDS=16 TIMLIM=50000        $END
1288  $BASIS  GBASIS=N31 NDFUNC=1 NGAUSS=6           $END
1289  $GUESS  GUESS=HUCKEL                           $END
1290 !
1291  $DATA
1292  #{name}
1293  C1
1294 end_of_header
1295         each_atom { |ap|
1296                 next if ap.atomic_number == 0
1297                 fp.printf " %-6s %4d %10.6f %10.6f %10.6f\n", ap.name, ap.atomic_number, ap.r.x, ap.r.y, ap.r.z
1298         }
1299         fp.print " $END\n"
1300         fp.close
1301         return true
1302   end
1303
1304   def savecom(filename)
1305     if natoms == 0
1306       raise MolbyError, "cannot save Gaussian input; the molecule is empty"
1307     end
1308     fp = open(filename, "wb")
1309         base = File.basename(filename, ".*")
1310         fp.print <<end_of_header
1311 %Chk=#{base}.chk
1312 \# PM3 Opt
1313
1314  #{name}; created by Molby at #{Time.now.to_s}
1315
1316  0 1
1317 end_of_header
1318         each_atom { |ap|
1319             next if ap.atomic_number == 0
1320                 fp.printf "%-6s %10.6f %10.6f %10.6f\n", ap.element, ap.r.x, ap.r.y, ap.r.z
1321         }
1322         fp.print "\n"
1323         fp.close
1324         return true
1325   end
1326
1327   alias :loadgjf :loadcom
1328   alias :savegjf :savecom
1329   
1330   def loadcif(filename)
1331     mol = self
1332     def getciftoken(fp)
1333           while @tokens.length == 0
1334             line = fp.gets
1335             return nil if !line
1336                 if line[0] == ?;
1337                   s = line
1338                   while line = fp.gets
1339                     break if line[0] == ?;
1340                     s += line
1341                   end
1342                   return s if !line
1343                   s += ";"
1344                   @tokens.push(s)
1345                   line = line[1...line.length]
1346                 end
1347             line.strip!
1348             line.scan(/'[^\']*'|"[^\"]*"|[^#\s]+|#.*/) do |s|
1349               next if s[0] == ?#
1350                   if s =~ /^data_|loop_|global_|save_|stop_/i
1351                     s = "#" + s    #  Label for reserved words
1352                   end
1353                   @tokens.push(s)
1354             end
1355           end
1356           return @tokens.shift
1357         end
1358         def float_strip_rms(str)
1359           str =~ /^(-?)(\d*)(\.(\d*))?(\((\d+)\))?$/
1360           sgn, i, frac, rms = $1, $2, $4, $6
1361           i = i.to_f
1362           if frac
1363                 base = 0.1 ** frac.length
1364                 i = i + frac.to_f * base
1365           else
1366             base = 1.0
1367           end
1368           if rms
1369             rms = rms.to_f * base
1370           else
1371             rms = 0.0
1372           end
1373           if sgn == "-"
1374             i = -i
1375           end
1376           return i, rms
1377         end
1378         def parse_symmetry_operation(str)
1379           if str == "."
1380             sym = nil
1381           elsif (str =~ /(\d+)_(\d)(\d)(\d)/) || (str =~ /(\d+) +(\d)(\d)(\d)/)
1382             sym = [Integer($1) - 1, Integer($2) - 5, Integer($3) - 5, Integer($4) - 5]
1383           elsif (str =~ /^(\d+)$/)
1384             sym = [Integer($1) - 1, 0, 0, 0]
1385           end
1386           if sym && (sym[0] == 0 && sym[1] == 0 && sym[2] == 0 && sym[3] == 0)
1387             sym = nil
1388           end
1389           sym
1390         end
1391         def find_atom_by_name(mol, name)
1392           name = name.delete(" ()")
1393           ap = mol.atoms[name] rescue ap = nil
1394           return ap
1395         end
1396         selfname = self.name
1397         fp = open(filename, "rb")
1398         data_identifier = nil
1399         @tokens = []
1400         count_up = 1
1401         while true
1402           warn_message = ""
1403           verbose = nil
1404           bond_defined = false
1405           special_positions = []
1406           mol.remove(All)
1407           cell = []
1408           cell_trans = cell_trans_inv = Transform.identity
1409           token = getciftoken(fp)
1410           pardigits_re = /\(\d+\)/
1411           calculated_atoms = []
1412           while token != nil
1413             if token =~ /^\#data_/i
1414                   if data_identifier == nil || mol.natoms == 0
1415                     #  First block or no atoms yet
1416             #  Continue processing of this molecule
1417                     data_identifier = token
1418                         token = getciftoken(fp)
1419                         next
1420                   else
1421                     #  Description of another molecule begins here
1422                         data_identifier = token
1423                         break
1424                   end
1425             elsif token =~ /^_cell/
1426                   val = getciftoken(fp)
1427                   if token == "_cell_length_a"
1428                     cell[0], cell[6] = float_strip_rms(val)
1429                   elsif token == "_cell_length_b"
1430                     cell[1], cell[7] = float_strip_rms(val)
1431                   elsif token == "_cell_length_c"
1432                     cell[2], cell[8] = float_strip_rms(val)
1433                   elsif token == "_cell_angle_alpha"
1434                     cell[3], cell[9] = float_strip_rms(val)
1435                   elsif token == "_cell_angle_beta"
1436                     cell[4], cell[10] = float_strip_rms(val)
1437                   elsif token == "_cell_angle_gamma"
1438                     cell[5], cell[11] = float_strip_rms(val)
1439                   end
1440                   if cell.length == 12 && cell.all?
1441                     mol.cell = cell
1442             puts "Unit cell is set to #{mol.cell.inspect}." if verbose
1443                     cell = []
1444                     cell_trans = mol.cell_transform
1445                     cell_trans_inv = cell_trans.inverse
1446                   end
1447                   token = getciftoken(fp)
1448                   next
1449         elsif token.casecmp("#loop_") == 0
1450               labels = []
1451                   while (token = getciftoken(fp)) && token[0] == ?_
1452                     labels.push(token)
1453                   end
1454                   if labels[0] =~ /symmetry_equiv_pos|space_group_symop|atom_site_label|atom_site_aniso_label|geom_bond/
1455                     hlabel = Hash.new(-10000000)
1456                     labels.each_with_index { |lb, i|
1457                           hlabel[lb] = i
1458                     }
1459                     data = []
1460                     n = labels.length
1461                     a = []
1462                     while true
1463                           break if token == nil || token[0] == ?_ || token[0] == ?#
1464                           a.push(token)
1465                           if a.length == n
1466                             data.push(a)
1467                             a = []
1468                           end
1469                           token = getciftoken(fp)
1470                     end
1471                     if labels[0] =~ /^_symmetry_equiv_pos/ || labels[0] =~ /^_space_group_symop/
1472                       data.each { |d|
1473                             symstr = d[hlabel["_symmetry_equiv_pos_as_xyz"]] || d[hlabel["_space_group_symop_operation_xyz"]]
1474                             symstr.delete("\"\'")
1475                             exps = symstr.split(/,/)
1476                             sym = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1477                             exps.each_with_index { |s, i|
1478                               terms = s.scan(/([-+]?)(([.0-9]+)(\/([0-9]+))?([xXyYzZ])?|([xXyYzZ]))/)
1479                                   terms.each { |a|
1480                                     #  a[0]: sign, a[2]: numerator, a[4]: denometer
1481                                     if a[4] != nil
1482                                       #  The number part is a[2]/a[4]
1483                                       num = Float(a[2])/Float(a[4])
1484                                     elsif a[2] != nil
1485                                       #  The number part is either integer or a floating point
1486                                       num = Float(a[2])
1487                                     else
1488                                       num = 1.0
1489                                     end
1490                                     num = -num if a[0][0] == ?-
1491                                     xyz = (a[5] || a[6])
1492                                     if xyz == "x" || xyz == "X"
1493                                       sym[i] = num
1494                                     elsif xyz == "y" || xyz == "Y"
1495                                       sym[i + 3] = num
1496                                     elsif xyz == "z" || xyz == "Z"
1497                                       sym[i + 6] = num
1498                                     else
1499                                       sym[9 + i] = num
1500                                     end
1501                                   }
1502                             }
1503                             puts "symmetry operation #{sym.inspect}" if verbose
1504                             mol.add_symmetry(Transform.new(sym))
1505                           }
1506                           puts "#{mol.nsymmetries} symmetry operations are added" if verbose
1507                     elsif labels[0] =~ /^_atom_site_label/
1508                           #  Create atoms
1509                           data.each { |d|
1510                             name = d[hlabel["_atom_site_label"]]
1511                             elem = d[hlabel["_atom_site_type_symbol"]]
1512                             fx = d[hlabel["_atom_site_fract_x"]]
1513                             fy = d[hlabel["_atom_site_fract_y"]]
1514                             fz = d[hlabel["_atom_site_fract_z"]]
1515                             uiso = d[hlabel["_atom_site_U_iso_or_equiv"]]
1516                             biso = d[hlabel["_atom_site_B_iso_or_equiv"]]
1517                             occ = d[hlabel["_atom_site_occupancy"]]
1518                             calc = d[hlabel["_atom_site_calc_flag"]]
1519                             name = name.delete(" ()")
1520                             if elem == nil || elem == ""
1521                               if name =~ /[A-Za-z]{1,2}/
1522                                     elem = $&.capitalize
1523                                   else
1524                                     elem = "Du"
1525                                   end
1526                             end
1527                             ap = mol.add_atom(name, elem, elem)
1528                             ap.fract_x, ap.sigma_x = float_strip_rms(fx)
1529                             ap.fract_y, ap.sigma_y = float_strip_rms(fy)
1530                             ap.fract_z, ap.sigma_z = float_strip_rms(fz)
1531                             if biso
1532                               ap.temp_factor, sig = float_strip_rms(biso)
1533                             elsif uiso
1534                               ap.temp_factor, sig = float_strip_rms(uiso)
1535                                   ap.temp_factor *= 78.9568352087149          #  8*pi*pi
1536                             end
1537                             ap.occupancy, sig = float_strip_rms(occ)
1538                             if calc == "c" || calc == "calc"
1539                               calculated_atoms.push(ap.index)
1540                         end
1541                             #  Guess special positions
1542                             (1...mol.nsymmetries).each { |isym|
1543                               sr = ap.fract_r
1544                               sr = (mol.transform_for_symop(isym) * sr) - sr;
1545                                   nx = (sr.x + 0.5).floor
1546                                   ny = (sr.y + 0.5).floor
1547                                   nz = (sr.z + 0.5).floor
1548                                   if (Vector3D[sr.x - nx, sr.y - ny, sr.z - nz].length2 < 1e-6)
1549                                     #  [isym, -nx, -ny, -nz] transforms this atom to itself
1550                                     #  The following line is equivalent to:
1551                                     #    if special_positions[ap.index] == nil; special_positions[ap.index] = []; end;
1552                                     #    special_positions[ap.index].push(...)
1553                                     (special_positions[ap.index] ||= []).push([isym, -nx, -ny, -nz])
1554                                   end
1555                             }
1556                             if verbose && special_positions[ap.index]
1557                               puts "#{name} is on the special position: #{special_positions[ap.index].inspect}"
1558                             end
1559                           }
1560                           puts "#{mol.natoms} atoms are created." if verbose
1561                     elsif labels[0] =~ /^_atom_site_aniso_label/
1562                       #  Set anisotropic parameters
1563                           c = 0
1564                           data.each { |d|
1565                             name = d[hlabel["_atom_site_aniso_label"]]
1566                             ap = find_atom_by_name(mol, name)
1567                             next if !ap
1568                             u11 = d[hlabel["_atom_site_aniso_U_11"]]
1569                             if u11
1570                               usig = []
1571                               u11, usig[0] = float_strip_rms(u11)
1572                               u22, usig[1] = float_strip_rms(d[hlabel["_atom_site_aniso_U_22"]])
1573                               u33, usig[2] = float_strip_rms(d[hlabel["_atom_site_aniso_U_33"]])
1574                               u12, usig[3] = float_strip_rms(d[hlabel["_atom_site_aniso_U_12"]])
1575                               u13, usig[4] = float_strip_rms(d[hlabel["_atom_site_aniso_U_13"]])
1576                               u23, usig[5] = float_strip_rms(d[hlabel["_atom_site_aniso_U_23"]])
1577                               ap.aniso = [u11, u22, u33, u12, u13, u23, 8] + usig
1578                                   c += 1
1579                             end
1580                           }
1581                           puts "#{c} anisotropic parameters are set." if verbose
1582                     elsif labels[0] =~ /^_geom_bond/
1583                       #  Create bonds
1584                           exbonds = []
1585                           data.each { |d|
1586                             n1 = d[hlabel["_geom_bond_atom_site_label_1"]]
1587                             n2 = d[hlabel["_geom_bond_atom_site_label_2"]]
1588                             sym1 = d[hlabel["_geom_bond_site_symmetry_1"]] || "."
1589                             sym2 = d[hlabel["_geom_bond_site_symmetry_2"]] || "."
1590                             n1 = find_atom_by_name(mol, n1)
1591                             n2 = find_atom_by_name(mol, n2)
1592                             next if n1 == nil || n2 == nil
1593                         n1 = n1.index
1594                             n2 = n2.index
1595                             sym1 = parse_symmetry_operation(sym1)
1596                             sym2 = parse_symmetry_operation(sym2)
1597                             if sym1 || sym2
1598                               exbonds.push([n1, n2, sym1, sym2])
1599                             else
1600                               mol.create_bond(n1, n2)
1601                             end
1602                             tr1 = (sym1 ? mol.transform_for_symop(sym1) : Transform.identity)
1603                             tr2 = (sym2 ? mol.transform_for_symop(sym2) : Transform.identity)
1604                             if special_positions[n1]
1605                                   #  Add extra bonds for equivalent positions of n1
1606                                   special_positions[n1].each { |symop|
1607                                     sym2x = mol.symop_for_transform(tr1 * mol.transform_for_symop(symop) * tr1.inverse * tr2)
1608                                     exbonds.push([n1, n2, sym1, sym2x])
1609                                   }
1610                             end
1611                             if special_positions[n2]
1612                                   #  Add extra bonds n2-n1.symop, where symop transforms n2 to self
1613                                   tr = (sym1 ? mol.transform_for_symop(sym1) : Transform.identity)
1614                                   special_positions[n2].each { |symop|
1615                                     sym1x = mol.symop_for_transform(tr2 * mol.transform_for_symop(symop) * tr2.inverse * tr1)
1616                                     exbonds.push([n2, n1, sym2, sym1x])
1617                                   }
1618                             end                         
1619                       }
1620               if mol.nbonds > 0
1621                 bond_defined = true
1622               end
1623                           puts "#{mol.nbonds} bonds are created." if verbose
1624                           if calculated_atoms.length > 0
1625                             #  Guess bonds for calculated hydrogen atoms
1626                             n1 = 0
1627                             calculated_atoms.each { |ai|
1628                               if mol.atoms[ai].connects.length == 0
1629                                     as = mol.find_close_atoms(ai)
1630                                     as.each { |aj|
1631                                       mol.create_bond(ai, aj)
1632                                           n1 += 1
1633                                     }
1634                                   end
1635                             }
1636                             puts "#{n1} bonds are guessed." if verbose
1637                           end
1638                           if exbonds.length > 0
1639                             h = Dialog.run("CIF Import: Symmetry Expansion") {
1640                               layout(1,
1641                                     item(:text, :title=>"There are bonds including symmetry related atoms.\nWhat do you want to do?"),
1642                                     item(:radio, :title=>"Expand only atoms that are included in those extra bonds.", :tag=>"atoms_only"),
1643                                     item(:radio, :title=>"Expand fragments having atoms included in the extra bonds.", :tag=>"fragment", :value=>1),
1644                                     item(:radio, :title=>"Ignore these extra bonds.", :tag=>"ignore")
1645                                   )
1646                                   radio_group("atoms_only", "fragment", "ignore")
1647                             }
1648                             if h[:status] == 0 && h["ignore"] == 0
1649                               atoms_only = (h["atoms_only"] != 0)
1650                                   if !atoms_only
1651                                     fragments = []
1652                                     mol.each_fragment { |f| fragments.push(f) }
1653                                   end
1654                                   debug = nil
1655                                   exbonds.each { |ex|
1656                                     if debug; puts "extra bond #{ex[0]}(#{ex[2].inspect}) - #{ex[1]}(#{ex[3].inspect})"; end
1657                                     ex0 = ex.dup
1658                                     (2..3).each { |i|
1659                                       symop = ex[i]
1660                                           if symop == nil
1661                                             ex[i + 2] = ex[i - 2]
1662                                           else
1663                                             if debug; puts "  symop = #{symop.inspect}"; end
1664                                             #  Expand the atom or the fragment including the atom
1665                                             if atoms_only
1666                                                   ig = IntGroup[ex[i - 2]]
1667                                                   idx = 0
1668                                             else
1669                                                   ig = fragments.find { |f| f.include?(ex[i - 2]) }
1670                                                   ig.each_with_index { |n, ii| if n == ex[i - 2]; idx = ii; break; end }
1671                                             end
1672                                             symop[4] = ex[i - 2]  #  Base atom
1673                                             if debug; puts "  expanding #{ig} by #{symop.inspect}"; end
1674                                             a = mol.expand_by_symmetry(ig, symop[0], symop[1], symop[2], symop[3])
1675                                             ex[i + 2] = a[idx]   #  Index of the expanded atom
1676                                           end
1677                                     }
1678                                     if ex[4] && ex[5] && ex[4] != ex[5]
1679                                       if debug; puts "  creating bond #{ex[4]} - #{ex[5]}"; end
1680                                       mol.create_bond(ex[4], ex[5])
1681                                     end
1682                                   }
1683                             end
1684                           end
1685                           puts "#{mol.nbonds} bonds are created." if verbose
1686                     end
1687                     next
1688                   else
1689                   #  puts "Loop beginning with #{labels[0]} is skipped"
1690                   end
1691             else
1692               #  Skip this token
1693                   token = getciftoken(fp)
1694             end
1695             #  Skip tokens until next tag or reserved word is detected
1696             while token != nil && token[0] != ?_ && token[0] != ?#
1697                   token = getciftoken(fp)
1698             end
1699             next
1700           end
1701       if !bond_defined
1702                 mol.guess_bonds
1703           end
1704           if token != nil && token == data_identifier
1705             #  Process next molecule: open a new molecule and start adding atom on that
1706                 mol = Molecule.new
1707                 count_up += 1
1708                 (@aux_mols ||= []).push(mol)
1709                 next
1710           end
1711           break
1712         end
1713         fp.close
1714 #       self.undo_enabled = save_undo_enabled
1715         return true
1716   end
1717   
1718   def dump(group = nil)
1719     def quote(str)
1720           if str == ""
1721             str = "\"\""
1722           else
1723             str = str.gsub(/%/, "%%")
1724                 str.gsub!(/ /, "%20")
1725                 str.gsub!(/\t/, "%09")
1726           end
1727           str
1728         end
1729         group = atom_group(group ? group : 0...natoms)
1730         s = ""
1731         group.each { |i|
1732           ap = atoms[i]
1733           s += sprintf("%4d %-7s %-4s %-4s %-2s %7.3f %7.3f %7.3f %6.3f [%s]\n",
1734                 ap.index, sprintf("%3s.%d", ap.res_name, ap.res_seq),
1735                 quote(ap.name), quote(ap.atom_type), quote(ap.element),
1736                 ap.r.x, ap.r.y, ap.r.z, ap.charge,
1737                 ap.connects.join(","))
1738         }
1739         print s
1740   end
1741
1742   def from_dump(arg)
1743     if natoms > 0
1744           raise "Molecule must be empty"
1745         end
1746     format = "index residue name atom_type element rx ry rz charge connects"
1747         keys = []
1748         resAtoms = Hash.new
1749         newBonds = []
1750         #  arg can be either a String or an array of String.
1751         #  Iterates for each line in the string or each member of the array.
1752         if arg.is_a?(String)
1753           arg = arg.split("\n")
1754         end
1755     arg.each { |line|
1756           if line =~ /^\#/
1757             format = line[1..-1]
1758                 keys = []
1759           end
1760           if keys.length == 0
1761             keys = format.split(" ").collect { |key| key.to_sym }
1762           end
1763           values = line.chomp.split(" ")
1764           next if values == nil || values.length == 0
1765           ap = create_atom(sprintf("X%03d", natoms))
1766           r = Vector3D[0, 0, 0]
1767           keys.each_index { |i|
1768             break if (value = values[i]) == nil
1769                 if value == "\"\""
1770                   value = ""
1771                 else
1772                   value.gsub(/%09/, "\t")
1773                   value.gsub(/%20/, " ")
1774                   value.gsub(/%%/, "%")
1775                 end
1776                 key = keys[i]
1777                 if key == :residue
1778                   if resAtoms[value] == nil
1779                     resAtoms[value] = []
1780                   end
1781                   resAtoms[value].push(ap.index)
1782                 elsif key == :rx
1783                   r.x = value.to_f
1784                 elsif key == :ry
1785                   r.y = value.to_f
1786                 elsif key == :rz
1787                   r.z = value.to_f
1788                 elsif key == :connects
1789                   value.scan(/\d+/).each { |i|
1790                     i = i.to_i
1791                     if ap.index < i
1792                           newBonds.push(ap.index)
1793                           newBonds.push(i)
1794                         end
1795                   }
1796                 elsif key == :index
1797                   next
1798                 else
1799                   ap.set_attr(key, value)
1800                 end
1801           }
1802           ap.r = r
1803         }
1804         resAtoms.each_key { |key|
1805           assign_residue(atom_group(resAtoms[key]), key)
1806         }
1807         create_bond(*newBonds)
1808         self
1809   end
1810
1811   #  Plug-in for loading mbsf
1812   def loadmbsf_plugin(s, lineno)
1813     ""
1814   end
1815   
1816   #  Plug-in for saving mbsf
1817   def savembsf_plugin
1818     ""
1819   end
1820   
1821 end