OSDN Git Service

Molden import: handling of spherical functions is improved
[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 lines like "[5D]".
634     gtos = []
635     spherical_d = false
636     spherical_f = false
637     spherical_g = false
638     #  Number of components for each orbital type
639     ncomp_hash = { 0=>1, 1=>3, -1=>4, 2=>6, -2=>5, 3=>10, -3=>7, 4=>15, -4=>9 }
640     catch :ignore do
641       while line = getline.call
642         if line =~ /^\[Atoms\]/
643           i = 0
644           while line = getline.call
645             if line =~ /^[A-Z]/
646               #  element, index, atomic_number, x, y, z (in AU)
647               a = line.split(' ')
648               if label
649                 (hash[:atoms] ||= []).push([a[0], Integer(a[1]), Integer(a[2]), Float(a[3]) * bohr, Float(a[4]) * bohr, Float(a[5]) * bohr])
650               else
651                 if atoms[i].atomic_number != Integer(a[2]) ||
652                   (atoms[i].x - Float(a[3]) * bohr).abs > 1e-4 ||
653                   (atoms[i].y - Float(a[4]) * bohr).abs > 1e-4 ||
654                   (atoms[i].z - Float(a[5]) * bohr).abs > 1e-4
655                   errmsg = "The atom list does not match the current molecule."
656                   throw :ignore
657                 end
658               end
659               i += 1
660             else
661               break
662             end
663           end
664           redo  #  The next line will be the beginning of the next block
665         elsif line =~ /^\[GTO\]/
666           shell = 0
667           atom_index = 0
668           while line = getline.call
669             #  index, 0?
670             a = line.split(' ')
671             break if a.length != 2
672             atom_gtos = []  #  [[sym1, [e11, c11, e12, c12, ...], add_exp1], [sym2, [e21, c22, ...], add_exp2], ...]
673             #  loop for shells
674             while line = getline.call
675               #  type, no_of_primitives, 1.00?
676               a = line.split(' ')
677               break if a.length != 3   #  Terminated by a blank line
678               a[0] =~ /^([a-z]+)([0-9]+)?$/
679               symcode = $1
680               add_exp = ($2 == nil ? 0 : $2.to_i)
681               case symcode
682               when "s"
683                 sym = 0
684               when "p"
685                 sym = 1
686               when "d"
687                 sym = 2
688               when "f"
689                 sym = 3
690               when "g"
691                 sym = 4
692               else
693                 raise MolbyError, "Unknown gaussian shell type '#{a[0]}' at line #{@lineno} in MOLDEN file"
694               end
695               nprimitives = Integer(a[1])
696               gtoline = [sym, [], add_exp]
697               atom_gtos.push(gtoline)
698               nprimitives.times { |i|
699                 line = getline.call   #  exponent, contraction
700                 b = line.split(' ')
701                 gtoline[1].push(Float(b[0]), Float(b[1]))
702               }
703               #  end of one shell
704               shell += 1
705             end
706             #  end of one atom
707             atom_index += 1
708             gtos.push(atom_gtos)
709           end
710           if label
711             hash[:gto] = gtos
712           end
713           redo  #  The next line will be the beginning of the next block
714         elsif line =~ /^\[5D\]/ || line =~ /^\[5D7F\]/
715           spherical_d = spherical_f = true
716         elsif line =~ /^\[5D10F\]/
717           spherical_d = true
718           spherical_f = false
719         elsif line =~ /^\[7F\]/
720           spherical_f = true
721         elsif line =~ /^\[9G\]/
722           spherical_g = true
723         elsif line =~ /^\[MO\]/
724           #  Add shell info and primitive coefficients to molecule
725           gtos.each_with_index { | atom_gtos, atom_index|
726             atom_gtos.each { |gtoline|
727               sym = gtoline[0]
728               #  Change orbital type if we use spherical functions
729               sym = -2 if sym == 2 && spherical_d
730               sym = -3 if sym == 3 && spherical_f
731               sym = -4 if sym == 4 && spherical_g
732               gtoline[0] = sym
733               coeffs = gtoline[1]
734               nprimitives = coeffs.length / 2
735               add_exp = gtoline[2]
736               ncomps += ncomp_hash[sym]
737               if !label
738                 add_gaussian_orbital_shell(atom_index, sym, nprimitives, add_exp)
739                 nprimitives.times { |prim|
740                   add_gaussian_primitive_coefficients(coeffs[prim * 2], coeffs[prim * 2 + 1], 0.0)
741                 }
742               end
743             }
744           }
745           m = []
746           idx_alpha = 1   #  set_mo_coefficients() accepts 1-based index of MO
747           idx_beta = 1
748           if label
749             hash[:mo] = []
750             hash[:moinfo] = []
751           end
752           while true
753             #  Loop for each MO
754             m.clear
755             ene = nil
756             spin = nil
757             sym = nil   #  Not used in Molby
758             occ = nil
759             i = 0
760             while line = getline.call
761               if line =~ /^ *Sym= *(\w+)/
762                 sym = $1
763               elsif line =~ /^ *Ene= *([-+.0-9e]+)/
764                 ene = Float($1)
765               elsif line =~ /^ *Spin= *(\w+)/
766                 spin = $1
767               elsif line =~ /^ *Occup= *([-+.0-9e]+)/
768                 occ = Float($1)
769                 if occ > 0.0
770                   if spin == "Alpha"
771                     occ_alpha += 1
772                   else
773                     occ_beta += 1
774                   end
775                 end
776                 if label
777                   hash[:moinfo].push([sym, ene, (spin == "Alpha" ? 0 : 1), occ])
778                 end
779               elsif line =~ /^ *([0-9]+) +([-+.0-9e]+)/
780                 m[i] = Float($2)
781                 i += 1
782                 if i >= ncomps
783                   if spin == "Alpha"
784                     idx = idx_alpha
785                     idx_alpha += 1
786                   else
787                     idx = idx_beta
788                     idx_beta += 1
789                   end
790                   set_mo_coefficients(idx, ene, m)
791                   if label
792                     hash[:mo].push(m.dup)
793                   end
794                   break
795                 end
796               else
797                 break
798               end
799             end
800             break if i < ncomps  #  no MO info was found
801           end
802           #  TODO: reorder D, F, G coefficients for Molby order
803           next
804         end #  end if
805       end   #  end while
806     end     #  end catch
807     if errmsg
808       message_box("The MOLDEN file was found but not imported. " + errmsg, "Psi4 import info", :ok)
809       return (label ? nil : false)
810     end
811     return (label ? hash : true)
812   end
813
814   #  Import the JANPA log and related molden files
815   #  Files: inppath.{NAO.molden,CLPO.molden,janpa.log}
816   #  If inppath.spherical.molden is available, then clear existing mo info
817   #  and load from it (i.e. use the basis set converted by molden2molden)
818   def sub_load_janpa_log(inppath)
819     begin
820       fp = File.open(inppath + ".janpa.log", "rt") rescue fp = nil
821       if fp == nil
822         hide_progress_panel  #  Close if it is open
823         message_box("Cannot open JANPA log file #{inppath + '.janpa.log'}: " + $!.to_s)
824         return false
825       end
826       print("Importing #{inppath}.janpa.log.\n")
827       lineno = 0
828       getline = lambda { lineno += 1; return fp.gets }
829       h = Hash.new
830       mfiles = Hash.new
831       h["software"] = "JANPA"
832       nao_num = nil  #  Set later
833       nao_infos = [] #  index=atom_index, value=Hash with key "s", "px", "py" etc.
834       #  nao_infos[index][key]: array of [nao_num, occupancy], in the reverse order of appearance
835       while line = getline.call
836         if line =~ /molden2molden: a conversion tool for MOLDEN/
837           while line = getline.call
838             break if line =~ /^All done!/
839             if line =~ /\.spherical\.molden/
840               #  The MOs are converted to spherical basis set
841               #  Clear the existing MO and load *.spherical.molden
842               sname = inppath + ".spherical.molden"
843               fps = File.open(sname, "rt") rescue fps = nil
844               if fps != nil
845                 print("Importing #{sname}.\n")
846                 @lineno = 0
847                 type = get_mo_info(:type)
848                 alpha = get_mo_info(:alpha)
849                 beta = get_mo_info(:beta)
850                 clear_basis_set
851                 set_mo_info(:type=>type, :alpha=>alpha, :beta=>beta)
852                 #  mol.@hf_type should be set before calling sub_load_molden
853                 @hf_type = type
854                 sub_load_molden(fps)
855                 fps.close
856               end
857             end
858           end
859         elsif line =~ /^NAO \#/
860           h["NAO"] = []
861           while line = getline.call
862             break if line !~ /^\s*[1-9]/
863             num = Integer(line[0, 5])
864             name = line[5, 21]
865             occ = Float(line[26, 11])
866             #  like A1*: R1*s(0)
867             #  atom_number, occupied?, group_number, orb_sym, angular_number
868             name =~ /\s*[A-Z]+([0-9]+)(\*?):\s* R([0-9]+)\*([a-z]+)\(([-0-9]+)\)/
869             anum = Integer($1)
870             occupied = $2
871             group_num = Integer($3)
872             orb_sym = $4
873             ang_num = Integer($5)
874             orb_desc = orb_sym
875             if orb_desc == "p"
876               orb_desc += ["z", "x", "y"][ang_num + 1]
877             elsif orb_desc == "d"
878             #  TODO: handle d, f, g orbitals
879             end
880             h["NAO"].push([num, anum, occupied, group_num, orb_desc, occ])
881             nao_num = h["NAO"].length
882             ((nao_infos[anum - 1] ||= Hash.new)[orb_desc] ||= []).unshift([nao_num, occ])
883           end
884           #  Create labels
885           h["NAO_L"] = []
886           nao_infos.each_with_index { |value, atom_index|
887             aname = self.atoms[atom_index].name
888             value.each { |orb_desc, ar|
889               ar.each_with_index { |v, group_index|
890                 if v[1] > 1.9
891                   label = "core"
892                 elsif v[1] > 0.01
893                   label = "val"
894                 else
895                   label = "ryd"
896                 end
897                 principle = group_index + 1
898                 orb_sym = orb_desc[0]
899                 if orb_sym == "p"
900                   principle += 1
901                 elsif orb_sym == "d"
902                   principle += 2
903                 elsif orb_sym == "f"
904                   principle += 3
905                 elsif orb_sym == "g"
906                   principle += 4
907                 end
908                 h["NAO_L"][v[0] - 1] = "#{aname} (#{principle}#{orb_desc}) (#{label})"
909               }
910             }
911           }
912         elsif line =~ /^\s*(C?)LPO\s+D e s c r i p t i o n\s+Occupancy\s+Composition/
913           if $1 == "C"
914             key = "CLPO"
915           else
916             key = "LPO"
917           end
918           h[key] = []
919           while line = getline.call
920             break if line =~ /^\s*$/
921             num = Integer(line[0, 5])
922             label1 = line[5, 6].strip
923             desc = line[11, 30].strip
924             occ = line[41, 11].strip
925             comp = line[52, 1000].strip
926             desc =~ /\s*([-A-Za-z0-9]+)(,\s*(.*$))?/
927             desc1 = $1
928             desc2 = ($3 || "")
929             if desc2 =~ /^(.*)*\(NB\)\s*$/ && label1 == ""
930               label1 = "(NB)"
931               desc2 = $1.strip
932             end
933             atoms = desc1.scan(/[A-Za-z]+(\d+)/)   # "C1-H3" -> [["1"], ["3"]]
934             atoms = atoms.map { |a| Integer(a[0]) }  # [1, 3]
935             hybrids_a = comp.scan(/h(\d+)@[A-Za-z]+(\d+)/)  #  "h8@C1...h13@H3" -> "[["8", "1"], ["13", "3"]]
936             hybrids = []
937             hybrids_a.each { |a|
938               i = atoms.find_index(Integer(a[1]))
939               if i != nil
940                 hybrids[i] = Integer(a[0])
941               end
942             } # [8, 13]
943             #  like ["(BD)", [1, 3], "Io = 0.2237", occ, [8, 13]]
944             #  1, 3 are the atom indices (1-based)
945             #  8, 13 are the number of hybrid orbitals (1-based)
946             h[key][num - 1] = [label1, atoms, desc2, Float(occ), hybrids]
947           end
948           h[key + "_L"] = []
949           if key == "CLPO"
950             #  Also register labels of "LHO"
951             h["LHO_L"] = [""] * nao_num
952           end
953           nao_num.times { |i|
954             val = h[key][i]
955             if val == nil
956               label = ""  #  The labels for Rydberg orbitals may be replaced later
957             else
958               aname1 = self.atoms[val[1][0] - 1].name rescue aname1 = ""
959               aname2 = self.atoms[val[1][1] - 1].name rescue aname2 = ""
960               if aname2 == ""
961                 label = "#{aname1} #{val[0]}"
962               else
963                 label = "#{aname1}(#{aname2}) #{val[0]}"
964               end
965             end
966             h[key + "_L"][i] = label
967             if key == "CLPO" && val != nil && val[0] != "(NB)"
968               hybrids = val[4]
969               kind = (val[0] == "(BD)" ? "(val)" : "(lp)")
970               if aname2 == ""
971                 label = "#{aname1} #{kind}"
972               else
973                 label = "#{aname1}(#{aname2}) #{kind}"
974               end
975               h["LHO_L"][hybrids[0] - 1] = label
976               if hybrids[1] != nil
977                 #  aname2 should be non-empty
978                 label = "#{aname2}(#{aname1}) #{kind}"
979                 h["LHO_L"][hybrids[1] - 1] = label
980               end
981             end
982           }
983         elsif line =~ /^ -NAO_Molden_File: (\S*)/
984           mfiles["NAO"] = $1
985         elsif line =~ /^ -LHO_Molden_File: (\S*)/
986           mfiles["LHO"] = $1
987         elsif line =~ /^ -CLPO_Molden_File: (\S*)/
988           mfiles["CLPO"] = $1
989         elsif line =~ /^ -PNAO_Molden_File: (\S*)/
990           mfiles["PNAO"] = $1
991         elsif line =~ /^ -AHO_Molden_File: (\S*)/
992           mfiles["AHO"] = $1
993         elsif line =~ /^ -LPO_Molden_File: (\S*)/
994           mfiles["LPO"] = $1
995         end
996       end
997       fp.close
998       #  Read molden files
999       mfiles.each { |key, value|
1000         fp = Kernel.open(value, "rt") rescue fp = nil
1001         if fp
1002           print("Importing #{value}.\n")
1003           res = sub_load_molden(fp, key)
1004           if res
1005             #  Some kind of orbital based on AO
1006             h["AO/#{key}"] = LAMatrix.new(res[:mo])
1007           end
1008           fp.close
1009           if key == "CLPO" || key == "LPO" || key == "LHO"
1010             #  Set the label of Rydberg orbitals if possible
1011             if h[key + "_L"] != nil
1012               a = h["AO/#{key}"]
1013               nao_num.times { |i|
1014                 label = h[key + "_L"][i]
1015                 if label == ""
1016                   max_idx = nil
1017                   max_val = -1.0
1018                   nao_infos.each_with_index { |inf, atom_index|
1019                     atomic_contrib = 0.0
1020                     inf.each { |k, v| # k is "s", "px" etc, v is array of [nao_num, occupancy]
1021                       #  Sum for all naos belonging to this atom
1022                       v.each { |num_occ|
1023                         atomic_contrib += a[i, num_occ[0] - 1] ** 2
1024                       }
1025                     }
1026                     if atomic_contrib > max_val
1027                       max_val = atomic_contrib
1028                       max_idx = atom_index
1029                     end
1030                   }
1031                   label = self.atoms[max_idx].name + " (ry)"
1032                   h[key + "_L"][i] = label
1033                 end
1034               }
1035             end
1036           end
1037         end
1038       }
1039       @nbo = h
1040       if @nbo["AO/NAO"] && @nbo["AO/LHO"] && @nbo["AO/PNAO"]
1041         #  Generate PLHO from PNAO, NAO, LHO
1042         #  This protocol was suggested by the JANPA author in a private commnunication.
1043         begin
1044           nao2lho = @nbo["AO/NAO"].inverse * @nbo["AO/LHO"]
1045           nao2pnao = @nbo["AO/NAO"].inverse * @nbo["AO/PNAO"]
1046           sign = LAMatrix.diagonal((0...nao2pnao.column_size).map { |i| (nao2pnao[i, i] < 0 ? -1 : 1)})
1047           @nbo["AO/PLHO"] = @nbo["AO/PNAO"] * sign * nao2lho
1048         rescue
1049           @nbo["AO/PLHO"] = nil
1050         end
1051       end
1052       return true
1053     rescue => e
1054       $stderr.write(e.message + "\n")
1055       $stderr.write(e.backtrace.inspect + "\n")
1056     end
1057   end
1058
1059   def loadout(filename)
1060   retval = false
1061   fp = open(filename, "rb")
1062   @lineno = 0
1063   begin
1064     while s = fp.gets
1065       @lineno += 1
1066       if s =~ /Gaussian/
1067         retval = sub_load_gaussian_log(fp)
1068         break
1069       elsif s =~ /GAMESS/
1070         retval = sub_load_gamess_log(fp)
1071         break
1072       elsif s =~ /Psi4/
1073         retval = sub_load_psi4_log(fp)
1074         if retval
1075           #  If .molden file exists, then try to read it
1076           namepath = filename.gsub(/\.\w*$/, "")
1077           mname = "#{namepath}.molden"
1078           if File.exists?(mname)
1079             fp2 = open(mname, "rb")
1080             if fp2
1081               flag = sub_load_molden(fp2)
1082               fp2.close
1083               status = (flag ? 0 : -1)
1084             end
1085           end
1086           if File.exists?("#{namepath}.janpa.log")
1087             flag = sub_load_janpa_log(namepath)
1088             status = (flag ? 0 : -1)
1089           end
1090         end
1091         break
1092       end
1093     end
1094   rescue
1095     hide_progress_panel
1096     raise
1097   end
1098         fp.close
1099         return retval
1100   end
1101   
1102   alias :loadlog :loadout
1103
1104   def loadxyz(filename)
1105         fp = open(filename, "rb")
1106         n = 0
1107         coords = []
1108         names = []
1109         cell = nil
1110         while 1
1111           line = fp.gets
1112           if line == nil
1113                 fp.close
1114                 break
1115           end
1116           line.chomp
1117           toks = line.split
1118           if coords.length == 0
1119             #  Allow "number of atoms" line or "number of atoms and crystallographic cell parameter"
1120                 #  (Chem3D xyz format)
1121                 next if toks.length == 1
1122                 if toks.length == 7
1123                   cell = toks[1..6].map { |s| Float(s.sub(/\(\d+\)/, "")) }  #  Remove (xx) and convert to float
1124                   next
1125                 end
1126           end
1127           name, x, y, z = line.split
1128           next if z == nil
1129           x = Float(x.sub(/\(\d+\)/, ""))
1130           y = Float(y.sub(/\(\d+\)/, ""))
1131           z = Float(z.sub(/\(\d+\)/, ""))
1132           r = Vector3D[x, y, z]
1133           coords.push(r)
1134           names.push(name)
1135           n += 1
1136         end
1137         celltr = nil
1138         if cell
1139           self.cell = cell
1140           celltr = self.cell_transform
1141         end
1142         names.each_index { |i|
1143           ap = add_atom(names[i])
1144           names[i] =~ /^([A-Za-z]{1,2})/
1145           element = $1.capitalize
1146           ap.element = element
1147           ap.atom_type = element
1148           if celltr
1149             ap.r = celltr * coords[i]
1150           else
1151             ap.r = coords[i]
1152           end
1153         }
1154         guess_bonds
1155         #  Find bonds
1156 #       atoms.each { |ap|
1157 #         j = ap.index
1158 #         (j + 1 ... natoms).each { |k|
1159 #               if calc_bond(j, k) < 1.7
1160 #               #  create_bond(j, k)
1161 #               end
1162 #         }
1163 #       }
1164 #       self.undo_enabled = save_undo_enabled
1165         (n > 0 ? true : false)
1166   end
1167   
1168   def savexyz(filename)
1169     open(filename, "wb") { |fp|
1170           fp.printf "%d\n", self.natoms
1171           each_atom { |ap|
1172             fp.printf "%s %.5f %.5f %.5f\n", ap.element, ap.x, ap.y, ap.z
1173           }
1174         }
1175         return true
1176   end
1177   
1178   def loadzmat(filename)
1179     self.remove(All)
1180         open(filename, "rb") { |fp|
1181           while (line = fp.gets)
1182             line.chomp!
1183                 a = line.split
1184                 an = Molecule.guess_atomic_number_from_name(a[0])
1185                 elm = Parameter.builtin.elements[an].name
1186                 base1 = a[1].to_i - 1
1187                 base2 = a[3].to_i - 1
1188                 base3 = a[5].to_i - 1
1189                 base1 = nil if base1 < 0
1190                 base2 = nil if base2 < 0
1191                 base3 = nil if base3 < 0
1192                 add_atom(a[0], elm, elm, a[2].to_f, base1, a[4].to_f, base2, a[6].to_f, base3)
1193           end
1194         }
1195         return true
1196   end
1197   
1198   def savezmat(filename)
1199   end
1200   
1201   def loadcom(filename)
1202 #       save_undo_enabled = self.undo_enabled?
1203 #       self.undo_enabled = false
1204         self.remove(All)
1205         fp = open(filename, "rb")
1206         section = 0
1207         while (line = fp.gets)
1208           line.chomp!
1209           if section == 0
1210             section = 1 if line =~ /^\#/
1211                 next
1212           elsif section == 1 || section == 2
1213             section += 1 if line =~ /^\s*$/
1214                 next
1215           else
1216             #  The first line is skipped (charge and multiplicity)
1217                 while (line = fp.gets)
1218                   line.chomp!
1219                   break if line =~ /^\s*$/
1220                   a = line.split(/\s*[ \t,\/]\s*/)
1221                   r = Vector3D[Float(a[1]), Float(a[2]), Float(a[3])]
1222                   ap = add_atom(a[0])
1223                   a[0] =~ /^([A-Za-z]{1,2})/
1224                   element = $1.capitalize
1225               ap.element = element
1226               ap.atom_type = element
1227               ap.r = r
1228                 end
1229                 break
1230           end
1231         end
1232         fp.close
1233         guess_bonds
1234 #       self.undo_enabled = save_undo_enabled
1235         return true
1236   end
1237   
1238   def loadinp(filename)
1239 #       save_undo_enabled = self.undo_enabled?
1240 #       self.undo_enabled = false
1241         self.remove(All)
1242         fp = open(filename, "rb")
1243         section = 0
1244         has_basis = false
1245         while (line = fp.gets)
1246           if line =~ /\A \$BASIS/
1247             has_basis = true
1248                 next
1249           end
1250           next if line !~ /\A \$DATA/
1251           line = fp.gets   #  Title line
1252           line = fp.gets   #  Symmetry line
1253           while (line = fp.gets)
1254 #           puts line
1255             line.chomp!
1256                 break if line =~ /\$END/
1257                 a = line.split
1258                 ap = add_atom(a[0])
1259                 ap.atomic_number = Integer(a[1])
1260                 ap.atom_type = ap.element
1261                 r = Vector3D[Float(a[2]), Float(a[3]), Float(a[4])]
1262                 ap.r = r;
1263                 if !has_basis
1264                   #  Skip until one blank line is detected
1265                   while (line = fp.gets) && line =~ /\S/
1266                   end
1267                 end
1268       end
1269           break
1270         end
1271         fp.close
1272         guess_bonds
1273 #       self.undo_enabled = save_undo_enabled
1274         return true
1275   end
1276   
1277   def saveinp(filename)
1278     if natoms == 0
1279       raise MolbyError, "cannot save GAMESS input; the molecule is empty"
1280     end
1281     fp = open(filename, "wb")
1282         now = Time.now.to_s
1283         fp.print <<end_of_header
1284 !  GAMESS input
1285 !  Generated by Molby at #{now}
1286  $CONTRL COORD=UNIQUE EXETYP=RUN ICHARG=0
1287          ICUT=20 INTTYP=HONDO ITOL=30
1288          MAXIT=200 MOLPLT=.T. MPLEVL=0
1289          MULT=1 QMTTOL=1e-08 RUNTYP=OPTIMIZE
1290          SCFTYP=RHF UNITS=ANGS                  $END
1291  $SCF    CONV=1.0E-06 DIRSCF=.T.                $END
1292  $STATPT NSTEP=400 OPTTOL=1.0E-06               $END
1293  $SYSTEM MEMDDI=0 MWORDS=16 TIMLIM=50000        $END
1294  $BASIS  GBASIS=N31 NDFUNC=1 NGAUSS=6           $END
1295  $GUESS  GUESS=HUCKEL                           $END
1296 !
1297  $DATA
1298  #{name}
1299  C1
1300 end_of_header
1301         each_atom { |ap|
1302                 next if ap.atomic_number == 0
1303                 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
1304         }
1305         fp.print " $END\n"
1306         fp.close
1307         return true
1308   end
1309
1310   def savecom(filename)
1311     if natoms == 0
1312       raise MolbyError, "cannot save Gaussian input; the molecule is empty"
1313     end
1314     fp = open(filename, "wb")
1315         base = File.basename(filename, ".*")
1316         fp.print <<end_of_header
1317 %Chk=#{base}.chk
1318 \# PM3 Opt
1319
1320  #{name}; created by Molby at #{Time.now.to_s}
1321
1322  0 1
1323 end_of_header
1324         each_atom { |ap|
1325             next if ap.atomic_number == 0
1326                 fp.printf "%-6s %10.6f %10.6f %10.6f\n", ap.element, ap.r.x, ap.r.y, ap.r.z
1327         }
1328         fp.print "\n"
1329         fp.close
1330         return true
1331   end
1332
1333   alias :loadgjf :loadcom
1334   alias :savegjf :savecom
1335   
1336   def loadcif(filename)
1337     mol = self
1338     def getciftoken(fp)
1339           while @tokens.length == 0
1340             line = fp.gets
1341             return nil if !line
1342                 if line[0] == ?;
1343                   s = line
1344                   while line = fp.gets
1345                     break if line[0] == ?;
1346                     s += line
1347                   end
1348                   return s if !line
1349                   s += ";"
1350                   @tokens.push(s)
1351                   line = line[1...line.length]
1352                 end
1353             line.strip!
1354             line.scan(/'[^\']*'|"[^\"]*"|[^#\s]+|#.*/) do |s|
1355               next if s[0] == ?#
1356                   if s =~ /^data_|loop_|global_|save_|stop_/i
1357                     s = "#" + s    #  Label for reserved words
1358                   end
1359                   @tokens.push(s)
1360             end
1361           end
1362           return @tokens.shift
1363         end
1364         def float_strip_rms(str)
1365           str =~ /^(-?)(\d*)(\.(\d*))?(\((\d+)\))?$/
1366           sgn, i, frac, rms = $1, $2, $4, $6
1367           i = i.to_f
1368           if frac
1369                 base = 0.1 ** frac.length
1370                 i = i + frac.to_f * base
1371           else
1372             base = 1.0
1373           end
1374           if rms
1375             rms = rms.to_f * base
1376           else
1377             rms = 0.0
1378           end
1379           if sgn == "-"
1380             i = -i
1381           end
1382           return i, rms
1383         end
1384         def parse_symmetry_operation(str)
1385           if str == "."
1386             sym = nil
1387           elsif (str =~ /(\d+)_(\d)(\d)(\d)/) || (str =~ /(\d+) +(\d)(\d)(\d)/)
1388             sym = [Integer($1) - 1, Integer($2) - 5, Integer($3) - 5, Integer($4) - 5]
1389           elsif (str =~ /^(\d+)$/)
1390             sym = [Integer($1) - 1, 0, 0, 0]
1391           end
1392           if sym && (sym[0] == 0 && sym[1] == 0 && sym[2] == 0 && sym[3] == 0)
1393             sym = nil
1394           end
1395           sym
1396         end
1397         def find_atom_by_name(mol, name)
1398           name = name.delete(" ()")
1399           ap = mol.atoms[name] rescue ap = nil
1400           return ap
1401         end
1402         selfname = self.name
1403         fp = open(filename, "rb")
1404         data_identifier = nil
1405         @tokens = []
1406         count_up = 1
1407         while true
1408           warn_message = ""
1409           verbose = nil
1410           bond_defined = false
1411           special_positions = []
1412           mol.remove(All)
1413           cell = []
1414           cell_trans = cell_trans_inv = Transform.identity
1415           token = getciftoken(fp)
1416           pardigits_re = /\(\d+\)/
1417           calculated_atoms = []
1418           while token != nil
1419             if token =~ /^\#data_/i
1420                   if data_identifier == nil || mol.natoms == 0
1421                     #  First block or no atoms yet
1422             #  Continue processing of this molecule
1423                     data_identifier = token
1424                         token = getciftoken(fp)
1425                         next
1426                   else
1427                     #  Description of another molecule begins here
1428                         data_identifier = token
1429                         break
1430                   end
1431             elsif token =~ /^_cell/
1432                   val = getciftoken(fp)
1433                   if token == "_cell_length_a"
1434                     cell[0], cell[6] = float_strip_rms(val)
1435                   elsif token == "_cell_length_b"
1436                     cell[1], cell[7] = float_strip_rms(val)
1437                   elsif token == "_cell_length_c"
1438                     cell[2], cell[8] = float_strip_rms(val)
1439                   elsif token == "_cell_angle_alpha"
1440                     cell[3], cell[9] = float_strip_rms(val)
1441                   elsif token == "_cell_angle_beta"
1442                     cell[4], cell[10] = float_strip_rms(val)
1443                   elsif token == "_cell_angle_gamma"
1444                     cell[5], cell[11] = float_strip_rms(val)
1445                   end
1446                   if cell.length == 12 && cell.all?
1447                     mol.cell = cell
1448             puts "Unit cell is set to #{mol.cell.inspect}." if verbose
1449                     cell = []
1450                     cell_trans = mol.cell_transform
1451                     cell_trans_inv = cell_trans.inverse
1452                   end
1453                   token = getciftoken(fp)
1454                   next
1455         elsif token.casecmp("#loop_") == 0
1456               labels = []
1457                   while (token = getciftoken(fp)) && token[0] == ?_
1458                     labels.push(token)
1459                   end
1460                   if labels[0] =~ /symmetry_equiv_pos|space_group_symop|atom_site_label|atom_site_aniso_label|geom_bond/
1461                     hlabel = Hash.new(-10000000)
1462                     labels.each_with_index { |lb, i|
1463                           hlabel[lb] = i
1464                     }
1465                     data = []
1466                     n = labels.length
1467                     a = []
1468                     while true
1469                           break if token == nil || token[0] == ?_ || token[0] == ?#
1470                           a.push(token)
1471                           if a.length == n
1472                             data.push(a)
1473                             a = []
1474                           end
1475                           token = getciftoken(fp)
1476                     end
1477                     if labels[0] =~ /^_symmetry_equiv_pos/ || labels[0] =~ /^_space_group_symop/
1478                       data.each { |d|
1479                             symstr = d[hlabel["_symmetry_equiv_pos_as_xyz"]] || d[hlabel["_space_group_symop_operation_xyz"]]
1480                             symstr.delete("\"\'")
1481                             exps = symstr.split(/,/)
1482                             sym = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1483                             exps.each_with_index { |s, i|
1484                               terms = s.scan(/([-+]?)(([.0-9]+)(\/([0-9]+))?([xXyYzZ])?|([xXyYzZ]))/)
1485                                   terms.each { |a|
1486                                     #  a[0]: sign, a[2]: numerator, a[4]: denometer
1487                                     if a[4] != nil
1488                                       #  The number part is a[2]/a[4]
1489                                       num = Float(a[2])/Float(a[4])
1490                                     elsif a[2] != nil
1491                                       #  The number part is either integer or a floating point
1492                                       num = Float(a[2])
1493                                     else
1494                                       num = 1.0
1495                                     end
1496                                     num = -num if a[0][0] == ?-
1497                                     xyz = (a[5] || a[6])
1498                                     if xyz == "x" || xyz == "X"
1499                                       sym[i] = num
1500                                     elsif xyz == "y" || xyz == "Y"
1501                                       sym[i + 3] = num
1502                                     elsif xyz == "z" || xyz == "Z"
1503                                       sym[i + 6] = num
1504                                     else
1505                                       sym[9 + i] = num
1506                                     end
1507                                   }
1508                             }
1509                             puts "symmetry operation #{sym.inspect}" if verbose
1510                             mol.add_symmetry(Transform.new(sym))
1511                           }
1512                           puts "#{mol.nsymmetries} symmetry operations are added" if verbose
1513                     elsif labels[0] =~ /^_atom_site_label/
1514                           #  Create atoms
1515                           data.each { |d|
1516                             name = d[hlabel["_atom_site_label"]]
1517                             elem = d[hlabel["_atom_site_type_symbol"]]
1518                             fx = d[hlabel["_atom_site_fract_x"]]
1519                             fy = d[hlabel["_atom_site_fract_y"]]
1520                             fz = d[hlabel["_atom_site_fract_z"]]
1521                             uiso = d[hlabel["_atom_site_U_iso_or_equiv"]]
1522                             biso = d[hlabel["_atom_site_B_iso_or_equiv"]]
1523                             occ = d[hlabel["_atom_site_occupancy"]]
1524                             calc = d[hlabel["_atom_site_calc_flag"]]
1525                             name = name.delete(" ()")
1526                             if elem == nil || elem == ""
1527                               if name =~ /[A-Za-z]{1,2}/
1528                                     elem = $&.capitalize
1529                                   else
1530                                     elem = "Du"
1531                                   end
1532                             end
1533                             ap = mol.add_atom(name, elem, elem)
1534                             ap.fract_x, ap.sigma_x = float_strip_rms(fx)
1535                             ap.fract_y, ap.sigma_y = float_strip_rms(fy)
1536                             ap.fract_z, ap.sigma_z = float_strip_rms(fz)
1537                             if biso
1538                               ap.temp_factor, sig = float_strip_rms(biso)
1539                             elsif uiso
1540                               ap.temp_factor, sig = float_strip_rms(uiso)
1541                                   ap.temp_factor *= 78.9568352087149          #  8*pi*pi
1542                             end
1543                             ap.occupancy, sig = float_strip_rms(occ)
1544                             if calc == "c" || calc == "calc"
1545                               calculated_atoms.push(ap.index)
1546                         end
1547                             #  Guess special positions
1548                             (1...mol.nsymmetries).each { |isym|
1549                               sr = ap.fract_r
1550                               sr = (mol.transform_for_symop(isym) * sr) - sr;
1551                                   nx = (sr.x + 0.5).floor
1552                                   ny = (sr.y + 0.5).floor
1553                                   nz = (sr.z + 0.5).floor
1554                                   if (Vector3D[sr.x - nx, sr.y - ny, sr.z - nz].length2 < 1e-6)
1555                                     #  [isym, -nx, -ny, -nz] transforms this atom to itself
1556                                     #  The following line is equivalent to:
1557                                     #    if special_positions[ap.index] == nil; special_positions[ap.index] = []; end;
1558                                     #    special_positions[ap.index].push(...)
1559                                     (special_positions[ap.index] ||= []).push([isym, -nx, -ny, -nz])
1560                                   end
1561                             }
1562                             if verbose && special_positions[ap.index]
1563                               puts "#{name} is on the special position: #{special_positions[ap.index].inspect}"
1564                             end
1565                           }
1566                           puts "#{mol.natoms} atoms are created." if verbose
1567                     elsif labels[0] =~ /^_atom_site_aniso_label/
1568                       #  Set anisotropic parameters
1569                           c = 0
1570                           data.each { |d|
1571                             name = d[hlabel["_atom_site_aniso_label"]]
1572                             ap = find_atom_by_name(mol, name)
1573                             next if !ap
1574                             u11 = d[hlabel["_atom_site_aniso_U_11"]]
1575                             if u11
1576                               usig = []
1577                               u11, usig[0] = float_strip_rms(u11)
1578                               u22, usig[1] = float_strip_rms(d[hlabel["_atom_site_aniso_U_22"]])
1579                               u33, usig[2] = float_strip_rms(d[hlabel["_atom_site_aniso_U_33"]])
1580                               u12, usig[3] = float_strip_rms(d[hlabel["_atom_site_aniso_U_12"]])
1581                               u13, usig[4] = float_strip_rms(d[hlabel["_atom_site_aniso_U_13"]])
1582                               u23, usig[5] = float_strip_rms(d[hlabel["_atom_site_aniso_U_23"]])
1583                               ap.aniso = [u11, u22, u33, u12, u13, u23, 8] + usig
1584                                   c += 1
1585                             end
1586                           }
1587                           puts "#{c} anisotropic parameters are set." if verbose
1588                     elsif labels[0] =~ /^_geom_bond/
1589                       #  Create bonds
1590                           exbonds = []
1591                           data.each { |d|
1592                             n1 = d[hlabel["_geom_bond_atom_site_label_1"]]
1593                             n2 = d[hlabel["_geom_bond_atom_site_label_2"]]
1594                             sym1 = d[hlabel["_geom_bond_site_symmetry_1"]] || "."
1595                             sym2 = d[hlabel["_geom_bond_site_symmetry_2"]] || "."
1596                             n1 = find_atom_by_name(mol, n1)
1597                             n2 = find_atom_by_name(mol, n2)
1598                             next if n1 == nil || n2 == nil
1599                         n1 = n1.index
1600                             n2 = n2.index
1601                             sym1 = parse_symmetry_operation(sym1)
1602                             sym2 = parse_symmetry_operation(sym2)
1603                             if sym1 || sym2
1604                               exbonds.push([n1, n2, sym1, sym2])
1605                             else
1606                               mol.create_bond(n1, n2)
1607                             end
1608                             tr1 = (sym1 ? mol.transform_for_symop(sym1) : Transform.identity)
1609                             tr2 = (sym2 ? mol.transform_for_symop(sym2) : Transform.identity)
1610                             if special_positions[n1]
1611                                   #  Add extra bonds for equivalent positions of n1
1612                                   special_positions[n1].each { |symop|
1613                                     sym2x = mol.symop_for_transform(tr1 * mol.transform_for_symop(symop) * tr1.inverse * tr2)
1614                                     exbonds.push([n1, n2, sym1, sym2x])
1615                                   }
1616                             end
1617                             if special_positions[n2]
1618                                   #  Add extra bonds n2-n1.symop, where symop transforms n2 to self
1619                                   tr = (sym1 ? mol.transform_for_symop(sym1) : Transform.identity)
1620                                   special_positions[n2].each { |symop|
1621                                     sym1x = mol.symop_for_transform(tr2 * mol.transform_for_symop(symop) * tr2.inverse * tr1)
1622                                     exbonds.push([n2, n1, sym2, sym1x])
1623                                   }
1624                             end                         
1625                       }
1626               if mol.nbonds > 0
1627                 bond_defined = true
1628               end
1629                           puts "#{mol.nbonds} bonds are created." if verbose
1630                           if calculated_atoms.length > 0
1631                             #  Guess bonds for calculated hydrogen atoms
1632                             n1 = 0
1633                             calculated_atoms.each { |ai|
1634                               if mol.atoms[ai].connects.length == 0
1635                                     as = mol.find_close_atoms(ai)
1636                                     as.each { |aj|
1637                                       mol.create_bond(ai, aj)
1638                                           n1 += 1
1639                                     }
1640                                   end
1641                             }
1642                             puts "#{n1} bonds are guessed." if verbose
1643                           end
1644                           if exbonds.length > 0
1645                             h = Dialog.run("CIF Import: Symmetry Expansion") {
1646                               layout(1,
1647                                     item(:text, :title=>"There are bonds including symmetry related atoms.\nWhat do you want to do?"),
1648                                     item(:radio, :title=>"Expand only atoms that are included in those extra bonds.", :tag=>"atoms_only"),
1649                                     item(:radio, :title=>"Expand fragments having atoms included in the extra bonds.", :tag=>"fragment", :value=>1),
1650                                     item(:radio, :title=>"Ignore these extra bonds.", :tag=>"ignore")
1651                                   )
1652                                   radio_group("atoms_only", "fragment", "ignore")
1653                             }
1654                             if h[:status] == 0 && h["ignore"] == 0
1655                               atoms_only = (h["atoms_only"] != 0)
1656                                   if !atoms_only
1657                                     fragments = []
1658                                     mol.each_fragment { |f| fragments.push(f) }
1659                                   end
1660                                   debug = nil
1661                                   exbonds.each { |ex|
1662                                     if debug; puts "extra bond #{ex[0]}(#{ex[2].inspect}) - #{ex[1]}(#{ex[3].inspect})"; end
1663                                     ex0 = ex.dup
1664                                     (2..3).each { |i|
1665                                       symop = ex[i]
1666                                           if symop == nil
1667                                             ex[i + 2] = ex[i - 2]
1668                                           else
1669                                             if debug; puts "  symop = #{symop.inspect}"; end
1670                                             #  Expand the atom or the fragment including the atom
1671                                             if atoms_only
1672                                                   ig = IntGroup[ex[i - 2]]
1673                                                   idx = 0
1674                                             else
1675                                                   ig = fragments.find { |f| f.include?(ex[i - 2]) }
1676                                                   ig.each_with_index { |n, ii| if n == ex[i - 2]; idx = ii; break; end }
1677                                             end
1678                                             symop[4] = ex[i - 2]  #  Base atom
1679                                             if debug; puts "  expanding #{ig} by #{symop.inspect}"; end
1680                                             a = mol.expand_by_symmetry(ig, symop[0], symop[1], symop[2], symop[3])
1681                                             ex[i + 2] = a[idx]   #  Index of the expanded atom
1682                                           end
1683                                     }
1684                                     if ex[4] && ex[5] && ex[4] != ex[5]
1685                                       if debug; puts "  creating bond #{ex[4]} - #{ex[5]}"; end
1686                                       mol.create_bond(ex[4], ex[5])
1687                                     end
1688                                   }
1689                             end
1690                           end
1691                           puts "#{mol.nbonds} bonds are created." if verbose
1692                     end
1693                     next
1694                   else
1695                   #  puts "Loop beginning with #{labels[0]} is skipped"
1696                   end
1697             else
1698               #  Skip this token
1699                   token = getciftoken(fp)
1700             end
1701             #  Skip tokens until next tag or reserved word is detected
1702             while token != nil && token[0] != ?_ && token[0] != ?#
1703                   token = getciftoken(fp)
1704             end
1705             next
1706           end
1707       if !bond_defined
1708                 mol.guess_bonds
1709           end
1710           if token != nil && token == data_identifier
1711             #  Process next molecule: open a new molecule and start adding atom on that
1712                 mol = Molecule.new
1713                 count_up += 1
1714                 (@aux_mols ||= []).push(mol)
1715                 next
1716           end
1717           break
1718         end
1719         fp.close
1720 #       self.undo_enabled = save_undo_enabled
1721         return true
1722   end
1723   
1724   def dump(group = nil)
1725     def quote(str)
1726           if str == ""
1727             str = "\"\""
1728           else
1729             str = str.gsub(/%/, "%%")
1730                 str.gsub!(/ /, "%20")
1731                 str.gsub!(/\t/, "%09")
1732           end
1733           str
1734         end
1735         group = atom_group(group ? group : 0...natoms)
1736         s = ""
1737         group.each { |i|
1738           ap = atoms[i]
1739           s += sprintf("%4d %-7s %-4s %-4s %-2s %7.3f %7.3f %7.3f %6.3f [%s]\n",
1740                 ap.index, sprintf("%3s.%d", ap.res_name, ap.res_seq),
1741                 quote(ap.name), quote(ap.atom_type), quote(ap.element),
1742                 ap.r.x, ap.r.y, ap.r.z, ap.charge,
1743                 ap.connects.join(","))
1744         }
1745         print s
1746   end
1747
1748   def from_dump(arg)
1749     if natoms > 0
1750           raise "Molecule must be empty"
1751         end
1752     format = "index residue name atom_type element rx ry rz charge connects"
1753         keys = []
1754         resAtoms = Hash.new
1755         newBonds = []
1756         #  arg can be either a String or an array of String.
1757         #  Iterates for each line in the string or each member of the array.
1758         if arg.is_a?(String)
1759           arg = arg.split("\n")
1760         end
1761     arg.each { |line|
1762           if line =~ /^\#/
1763             format = line[1..-1]
1764                 keys = []
1765           end
1766           if keys.length == 0
1767             keys = format.split(" ").collect { |key| key.to_sym }
1768           end
1769           values = line.chomp.split(" ")
1770           next if values == nil || values.length == 0
1771           ap = create_atom(sprintf("X%03d", natoms))
1772           r = Vector3D[0, 0, 0]
1773           keys.each_index { |i|
1774             break if (value = values[i]) == nil
1775                 if value == "\"\""
1776                   value = ""
1777                 else
1778                   value.gsub(/%09/, "\t")
1779                   value.gsub(/%20/, " ")
1780                   value.gsub(/%%/, "%")
1781                 end
1782                 key = keys[i]
1783                 if key == :residue
1784                   if resAtoms[value] == nil
1785                     resAtoms[value] = []
1786                   end
1787                   resAtoms[value].push(ap.index)
1788                 elsif key == :rx
1789                   r.x = value.to_f
1790                 elsif key == :ry
1791                   r.y = value.to_f
1792                 elsif key == :rz
1793                   r.z = value.to_f
1794                 elsif key == :connects
1795                   value.scan(/\d+/).each { |i|
1796                     i = i.to_i
1797                     if ap.index < i
1798                           newBonds.push(ap.index)
1799                           newBonds.push(i)
1800                         end
1801                   }
1802                 elsif key == :index
1803                   next
1804                 else
1805                   ap.set_attr(key, value)
1806                 end
1807           }
1808           ap.r = r
1809         }
1810         resAtoms.each_key { |key|
1811           assign_residue(atom_group(resAtoms[key]), key)
1812         }
1813         create_bond(*newBonds)
1814         self
1815   end
1816
1817   #  Plug-in for loading mbsf
1818   def loadmbsf_plugin(s, lineno)
1819     ""
1820   end
1821   
1822   #  Plug-in for saving mbsf
1823   def savembsf_plugin
1824     ""
1825   end
1826   
1827 end