/
files.go
944 lines (889 loc) · 31.2 KB
/
files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/*
* files.go, part of gochem.
*
*
* Copyright 2012 Raul Mera <rmera{at}chemDOThelsinkiDOTfi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* Gochem is developed at the laboratory for instruction in Swedish, Department of Chemistry,
* University of Helsinki, Finland.
*
*
*/
/***Dedicated to the long life of the Ven. Khenpo Phuntzok Tenzin Rinpoche***/
package chem
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
v3 "github.com/rmera/gochem/v3"
)
//PDB_read family
//A map for assigning mass to elements.
//Note that just common "bio-elements" are present
var symbolMass = map[string]float64{
"H": 1.0,
"C": 12.01,
"O": 16.00,
"N": 14.01,
"P": 30.97,
"S": 32.06,
"Se": 78.96,
"K": 39.1,
"Ca": 40.08,
"Mg": 24.30,
"Cl": 35.45,
"Na": 22.99,
"Cu": 63.55,
"Zn": 65.38,
"Co": 58.93,
"Fe": 55.84,
"Mn": 54.94,
"Si": 28.08,
"Be": 9.012,
"F": 18.998,
}
//A map between 3-letters name for aminoacidic residues to the corresponding 1-letter names.
var three2OneLetter = map[string]byte{
"SER": 'S',
"THR": 'T',
"ASN": 'N',
"GLN": 'Q',
"SEC": 'U', //Selenocysteine!
"CYS": 'C',
"GLY": 'G',
"PRO": 'P',
"ALA": 'A',
"VAL": 'V',
"ILE": 'I',
"LEU": 'L',
"MET": 'M',
"PHE": 'F',
"TYR": 'Y',
"TRP": 'W',
"ARG": 'R',
"HIS": 'H',
"LYS": 'K',
"ASP": 'D',
"GLU": 'E',
}
//This tries to guess a chemical element symbol from a PDB atom name. Mostly based on AMBER names.
//It only deals with some common bio-elements.
func symbolFromName(name string) (string, error) {
symbol := ""
if len(name) == 1 {
symbol = name //should work
} else if len(name) == 4 || name[0] == 'H' { //I thiiink only Hs can have 4-char names in amber.
symbol = "H"
//it name has more than one character but less than four.
} else if name[0] == 'C' {
if name[0:2] == "CU" {
symbol = "Cu"
} else if name == "CO" {
symbol = "Co"
} else if name == "CL" {
symbol = "Cl"
} else {
symbol = "C"
}
} else if name[0] == 'B' {
if name == "BE" {
symbol = "Be"
}
} else if name[0] == 'N' {
if name == "NA" {
symbol = "Na"
} else {
symbol = "N"
}
} else if name[0] == 'O' {
symbol = "O"
} else if name[0] == 'P' {
symbol = "P"
} else if name[0] == 'S' {
if name == "SE" {
symbol = "Se"
} else {
symbol = "S"
}
} else if name[0:2] == "ZN" {
symbol = "Zn"
}
if symbol == "" {
return symbol, CError{"Couldn't guess symbol from PDB name", []string{"symbolFromName"}}
}
return symbol, nil
}
//Parses a valid ATOM or HETATM line of a PDB file, returns an Atom
// object with the info except for the coordinates and b-factors, which are returned
// separately as an array of 3 float64 and a float64, respectively
func read_full_pdb_line(line string, read_additional bool, contlines int) (*Atom, []float64, float64, error) {
err := make([]error, 7, 7) //accumulate errors to check at the end of the readed line.
coords := make([]float64, 3, 3)
atom := new(Atom)
atom.Het = strings.HasPrefix(line, "HETATM") //this is called twice in the worst case. should fix
atom.ID, err[0] = strconv.Atoi(strings.TrimSpace(line[6:12]))
atom.Name = strings.TrimSpace(line[12:16])
atom.Char16 = line[16]
//PDB says that pos. 17 is for other thing but I see that is
//used for residue name in many cases*/
atom.Molname = line[17:20]
atom.Molname1 = three2OneLetter[atom.Molname]
atom.Chain = string(line[21])
atom.MolID, err[1] = strconv.Atoi(strings.TrimSpace(line[22:30]))
//Here we shouldn't need TrimSpace, but I keep it just in case someone
// doesn's use all the fields when writting a PDB*/
coords[0], err[2] = strconv.ParseFloat(strings.TrimSpace(line[30:38]), 64)
coords[1], err[3] = strconv.ParseFloat(strings.TrimSpace(line[38:46]), 64)
coords[2], err[4] = strconv.ParseFloat(strings.TrimSpace(line[46:54]), 64)
atom.Occupancy, err[5] = strconv.ParseFloat(strings.TrimSpace(line[54:60]), 64)
//If I try not to declare this and just use :=, I get an "expected identifier" error
var bfactor float64
bfactor, err[6] = strconv.ParseFloat(strings.TrimSpace(line[60:66]), 64)
//we try to read the additional only if indicated and if it is there
// In this part we don't catch errors. If something is missing we
// just ommit it
if read_additional && len(line) >= 80 {
atom.Symbol = strings.TrimSpace(line[76:78])
atom.Symbol = strings.Title(strings.ToLower(atom.Symbol))
atom.Charge = float64(line[78]) //strconv.ParseFloat(strings.TrimSpace(line[78:78]),64)
if strings.Contains(line[79:79], "-") {
atom.Charge = -1.0 * atom.Charge
}
}
//This part tries to guess the symbol from the atom name, if it has not been read
//No error checking here, just fills symbol with the empty string the function returns
if len(atom.Symbol) == 0 {
atom.Symbol, _ = symbolFromName(atom.Name)
}
for i := range err {
if err[i] != nil {
//Here I should add the line number to the returned error.
return nil, nil, 0, CError{err[i].Error(), []string{"strconv.Atoi/ParseFloat", "read_full_pdb_line"}}
}
}
if atom.Symbol != "" {
atom.Mass = symbolMass[atom.Symbol] //Not error checking
}
return atom, coords, bfactor, nil
}
/*Parses a PDB line if only the coordinates and bfactors are to be read*/
func read_onlycoords_pdb_line(line string, contlines int) ([]float64, float64, error) {
coords := make([]float64, 3, 3)
err := make([]error, 4, 4)
var bfactor float64 //I dont get why I must declare this instead of using :=
//I get an "expected identifier" error if I do so.
coords[0], err[0] = strconv.ParseFloat(strings.TrimSpace(line[30:38]), 64)
coords[1], err[1] = strconv.ParseFloat(strings.TrimSpace(line[38:46]), 64)
coords[2], err[2] = strconv.ParseFloat(strings.TrimSpace(line[46:54]), 64)
bfactor, err[3] = strconv.ParseFloat(strings.TrimSpace(line[60:66]), 64)
//this will take care of any error
for i := range err {
if err[i] != nil {
//Here I should add the line number to the returned error.
return nil, 0, CError{err[i].Error(), []string{"strconv.ParseFloat", "read_onlycoords_pdb_line"}}
}
}
return coords, bfactor, nil
}
//PDBRRead reads a pdb file from an io.Reader. Returns a bunch of without coordinates,
// and the coordinates in a separate array of arrays. If there is one frame in the PDB
// the coordinates array will be of lenght 1. It also returns an error which is not
// really well set up right now.
func PDBRead(pdb io.Reader, read_additional bool) (*Molecule, error) {
bufiopdb := bufio.NewReader(pdb)
mol, err := pdbBufIORead(bufiopdb, read_additional)
return mol, errDecorate(err, "PDBReaderREad")
}
//PDBFileRead reads the atomic entries for a PDB file, returns a bunch of without coordinates,
// and the coordinates in a separate array of arrays. If there is one frame in the PDB
// the coordinates array will be of lenght 1. It also returns an error which is not
// really well set up right now.
func PDBFileRead(pdbname string, read_additional bool) (*Molecule, error) {
pdbfile, err := os.Open(pdbname)
if err != nil {
//fmt.Println("Unable to open file!!")
return nil, err
}
defer pdbfile.Close()
pdb := bufio.NewReader(pdbfile)
mol, err := pdbBufIORead(pdb, read_additional)
return mol, err
}
//pdbBufIORead reads the atomic entries for a PDB bufio.IO, returns a bunch of without coordinates,
// and the coordinates in a separate array of arrays. If there is one frame in the PDB
// the coordinates array will be of lenght 1. It also returns an error which is not
// really well set up right now.
func pdbBufIORead(pdb *bufio.Reader, read_additional bool) (*Molecule, error) {
molecule := make([]*Atom, 0)
modelnumber := 0 //This is the number of frames read
coords := make([][]float64, 1, 1)
coords[0] = make([]float64, 0)
bfactors := make([][]float64, 1, 1)
bfactors[0] = make([]float64, 0)
first_model := true //are we reading the first model? if not we only save coordinates
contlines := 1 //count the lines read to better report errors
for {
line, err := pdb.ReadString('\n')
if err != nil {
//fmt.Println("PDB reading complete") /***change this to stderr************/
break
// contlines++ //count all the lines even if empty. This is unreachable but I'm not sure at this point if it's better this way! goChem does read PDBs correctly as far as I can see.
}
if len(line) < 4 {
continue
}
//here we start actually reading
/*There might be a bug for not copying the string (symbol, name, etc) but just assigning the slice
* which is a reference. Check!*/
var c = make([]float64, 3, 3)
var bfactemp float64 //temporary bfactor
var atomtmp *Atom
// var foo string // not really needed
if strings.HasPrefix(line, "ATOM") || strings.HasPrefix(line, "HETATM") {
if !first_model {
c, bfactemp, err = read_onlycoords_pdb_line(line, contlines)
if err != nil {
return nil, errDecorate(err, "pdbBufIORead")
}
} else {
atomtmp = new(Atom)
atomtmp, c, bfactemp, err = read_full_pdb_line(line, read_additional, contlines)
if err != nil {
return nil, errDecorate(err, "pdbBufIORead")
}
//atom data other than coords is the same in all models so just read for the first.
molecule = append(molecule, atomtmp)
}
//coords are appended for all the models
//we add the coords to the latest frame of coordinaates
coords[len(coords)-1] = append(coords[len(coords)-1], c[0], c[1], c[2])
bfactors[len(bfactors)-1] = append(bfactors[len(bfactors)-1], bfactemp)
} else if strings.HasPrefix(line, "MODEL") {
modelnumber++ //,_=strconv.Atoi(strings.TrimSpace(line[6:]))
if modelnumber > 1 { //will be one for the first model, 2 for the second.
first_model = false
coords = append(coords, make([]float64, 0)) //new bunch of coords for a new frame
bfactors = append(bfactors, make([]float64, 0))
}
}
}
//This could be done faster if done in the same loop where the coords are read
//Instead of having another loop just for them.
top := NewTopology(0, 1, molecule)
var err error
frames := len(coords)
mcoords := make([]*v3.Matrix, frames, frames) //Final thing to return
for i := 0; i < frames; i++ {
mcoords[i], err = v3.NewMatrix(coords[i])
if err != nil {
return nil, errDecorate(err, "pdbBufIORead")
}
}
//if something happened during the process
if err != nil {
return nil, errDecorate(err, "pdbBufIORead")
}
returned, err := NewMolecule(mcoords, top, bfactors)
return returned, errDecorate(err, "pdbBufIORead")
}
//End PDB_read family
//correctBfactors check that coords and bfactors have the same number of elements.
func correctBfactors(coords []*v3.Matrix, bfactors [][]float64) bool {
if len(coords) != len(bfactors) || bfactors == nil {
return false
}
for key, coord := range coords {
cr, _ := coord.Dims()
br := len(bfactors[key])
if cr != br {
return false
}
}
return true
}
//writePDBLine writes a line in PDB format from the data passed as a parameters. It takes the chain of the previous atom
//and returns the written line, the chain of the just-written atom, and error or nil.
func writePDBLine(atom *Atom, coord *v3.Matrix, bfact float64, chainprev string) (string, string, error) {
var ter string
var out string
if atom.Chain != chainprev {
ter = fmt.Sprint(out, "TER\n")
chainprev = atom.Chain
}
first := "ATOM"
if atom.Het {
first = "HETATM"
}
formatstring := "%-6s%5d %-3s %-4s%1s%4d %8.3f%8.3f%8.3f%6.2f%6.2f %2s \n"
//4 chars for the atom name are used when hydrogens are included.
//This has not been tested
if len(atom.Name) == 4 {
formatstring = strings.Replace(formatstring, "%-3s ", "%-4s", 1)
} else if len(atom.Name) > 4 {
return "", chainprev, CError{"Cant print PDB line", []string{"writePDBLine"}}
}
//"%-6s%5d %-3s %3s %1c%4d %8.3f%8.3f%8.3f%6.2f%6.2f %2s \n"
out = fmt.Sprintf(formatstring, first, atom.ID, atom.Name, atom.Molname, atom.Chain,
atom.MolID, coord.At(0, 0), coord.At(0, 1), coord.At(0, 2), atom.Occupancy, bfact, atom.Symbol)
out = strings.Join([]string{ter, out}, "")
return out, chainprev, nil
}
//PDBFileWrite writes a PDB for the molecule mol and the coordinates Coords.
func PDBFileWrite(pdbname string, coords *v3.Matrix, mol Atomer, Bfactors []float64) error {
out, err := os.Create(pdbname)
if err != nil {
return CError{err.Error(), []string{"os.Create", "PDBFileWrite"}}
}
defer out.Close()
fmt.Fprintf(out, "REMARK WRITTEN WITH GOCHEM :-) \n")
err = PDBWrite(out, coords, mol, Bfactors)
if err != nil {
return errDecorate(err, "PDBFileWrite")
}
return nil
}
//PDBWrite writes a PDB formatted sequence of bytes to an io.Writer for a given reference, coordinate set and bfactor set, which must match each other
//returns error or nil.
func PDBWrite(out io.Writer, coords *v3.Matrix, mol Atomer, bfact []float64) error {
err := pdbWrite(out, coords, mol, bfact)
if err != nil {
errDecorate(err, "PDBWrite")
}
_, err = out.Write([]byte{'\n'}) //This function is just a wrapper to add the newline to what pdbWrite does.
if err != nil {
return CError{"Failed to write in io.Writer", []string{"io.Write.Write", "PDBWrite"}}
}
return nil
}
func pdbWrite(out io.Writer, coords *v3.Matrix, mol Atomer, bfact []float64) error {
if bfact == nil {
bfact = make([]float64, mol.Len())
}
cr, _ := coords.Dims()
br := len(bfact)
if cr != mol.Len() || cr != br {
return CError{"Ref and Coords and/or Bfactors dont have the same number of atoms", []string{"pdbWrite"}}
}
chainprev := mol.Atom(0).Chain //this is to know when the chain changes.
var outline string
var err error
iowriteError := func(err error) error {
return CError{"Failed to write in io.Writer" + err.Error(), []string{"io.Write.Write", "pdbWrite"}}
}
for i := 0; i < mol.Len(); i++ {
// r,c:=coords.Dims()
// fmt.Println("IIIIIIIIIIIi", i,coords,r,c, "lllllll")
writecoord := coords.VecView(i)
outline, chainprev, err = writePDBLine(mol.Atom(i), writecoord, bfact[i], chainprev)
if err != nil {
return errDecorate(err, "pdbWrite "+fmt.Sprintf("Could not print PDB line: %d", i))
}
_, err := out.Write([]byte(outline))
if err != nil {
return iowriteError(err)
}
}
_, err = out.Write([]byte("TER\n")) // New Addition, should help to recognize the end of the chain.
_, err = out.Write([]byte("END")) //no newline, this is in case the write is part of a PDB and one needs to write "ENDMDEL".
if err != nil {
return iowriteError(err)
}
return nil
}
//PDBStringWrite writes a string in PDB format for a given reference, coordinate set and bfactor set, which must match each other
//returns the written string and error or nil.
func PDBStringWrite(coords *v3.Matrix, mol Atomer, bfact []float64) (string, error) {
if bfact == nil {
bfact = make([]float64, mol.Len())
}
cr, _ := coords.Dims()
br := len(bfact)
if cr != mol.Len() || cr != br {
return "", CError{"Ref and Coords and/or Bfactors dont have the same number of atoms", []string{"PDBStringWrite"}}
}
chainprev := mol.Atom(0).Chain //this is to know when the chain changes.
var outline string
var outstring string
var err error
for i := 0; i < mol.Len(); i++ {
// r,c:=coords.Dims()
// fmt.Println("IIIIIIIIIIIi", i,coords,r,c, "lllllll")
writecoord := coords.VecView(i)
outline, chainprev, err = writePDBLine(mol.Atom(i), writecoord, bfact[i], chainprev)
if err != nil {
return "", errDecorate(err, "PDBStringWrite "+fmt.Sprintf("Could not print PDB line: %d", i))
}
outstring = strings.Join([]string{outstring, outline}, "")
}
outstring = strings.Join([]string{outstring, "END\n"}, "")
return outstring, nil
}
//MultiPDBWrite writes a multiPDB for the molecule mol and the various coordinate sets in CandB, to the io.Writer given.
//CandB is a list of lists of *matrix.DenseMatrix. If it has 2 elements or more, the second will be used as
//Bfactors. If it has one element, all b-factors will be zero.
//Returns an error if fails, or nil if succeeds.
func MultiPDBWrite(out io.Writer, Coords []*v3.Matrix, mol Atomer, Bfactors [][]float64) error {
if !correctBfactors(Coords, Bfactors) {
Bfactors = make([][]float64, len(Coords), len(Coords))
}
iowriterError := func(err error) error {
return CError{"Failed to write in io.Writer" + err.Error(), []string{"io.Writer.Write", "MultiPDBWrite"}}
}
_, err := out.Write([]byte("REMARK WRITTEN WITH GOCHEM :-)")) //The model number starts with one
if err != nil {
return iowriterError(err)
}
//OK now the real business.
for j := range Coords {
_, err := out.Write([]byte(fmt.Sprintf("MODEL %d\n", j+1))) //The model number starts with one
if err != nil {
return iowriterError(err)
}
err = pdbWrite(out, Coords[j], mol, Bfactors[j])
if err != nil {
return errDecorate(err, "MultiPDBWrite")
}
_, err = out.Write([]byte("MDL\n"))
if err != nil {
return iowriterError(err)
}
}
_, err = out.Write([]byte("END\n"))
if err != nil {
return iowriterError(err)
}
return nil
}
/***End of PDB part***/
//XYZFileRead Reads an xyz or multixyz file (as produced by Turbomole). Returns a Molecule and error or nil.
func XYZFileRead(xyzname string) (*Molecule, error) {
xyzfile, err := os.Open(xyzname)
if err != nil {
//fmt.Println("Unable to open file!!")
return nil, CError{err.Error(), []string{"os.Open", "XYZFileRead"}}
}
defer xyzfile.Close()
mol, err := XYZRead(xyzfile)
if err != nil {
err = errDecorate(err, "XYZFileRead "+fmt.Sprintf(strings.Join([]string{"error in file ", xyzname}, "")))
}
return mol, err
}
//Reads an xyz or multixyz formatted bufio.Reader (as produced by Turbomole). Returns a Molecule and error or nil.
func XYZRead(xyzp io.Reader) (*Molecule, error) {
snaps := 1
xyz := bufio.NewReader(xyzp)
var err error
var top *Topology
var molecule []*Atom
Coords := make([]*v3.Matrix, 1, 1)
for {
//When we read the first snapshot we collect also the topology data, later
//only coords are collected.
if snaps == 1 {
Coords[0], molecule, err = xyzReadSnap(xyz, nil, true)
if err != nil {
return nil, errDecorate(err, "XYZRead")
}
top = NewTopology(0, 1, molecule)
if err != nil {
return nil, errDecorate(err, "XYZRead")
}
snaps++
continue
}
tmpcoords, _, err := xyzReadSnap(xyz, nil, false)
if err != nil {
//An error here simply means that there are no more snapshots
errm := err.Error()
if strings.Contains(errm, "Empty") || strings.Contains(errm, "header") {
err = nil
break
}
return nil, errDecorate(err, "XYZRead")
}
Coords = append(Coords, tmpcoords)
}
bfactors := make([][]float64, len(Coords), len(Coords))
for key, _ := range bfactors {
bfactors[key] = make([]float64, top.Len())
}
returned, err := NewMolecule(Coords, top, bfactors)
return returned, errDecorate(err, "XYZRead")
}
type XYZTraj struct {
natoms int
xyz *bufio.Reader //The DCD file
frames int
xyzfile *os.File
readable bool
firstframe *v3.Matrix
}
func (X *XYZTraj) Readable() bool {
return X.readable
}
func (X *XYZTraj) Len() int {
return X.natoms
}
func (X *XYZTraj) Next(coords *v3.Matrix) error {
if X.frames == 0 {
coords.Copy(X.firstframe) //slow, but I don't want to mess with the pointer I got.
X.frames++
X.firstframe = nil
return nil
}
_, _, err := xyzReadSnap(X.xyz, coords, false)
if err != nil {
//An error here simply means that there are no more snapshots
errm := err.Error()
if strings.Contains(errm, "Empty") || strings.Contains(errm, "header") {
X.xyzfile.Close()
X.readable = false
return newlastFrameError("", X.frames)
}
}
X.frames++
return err
}
//Reads a multi-xyz file. Returns the first snapshot as a molecule, and the other ones as a XYZTraj
func XYZFileAsTraj(xyzname string) (*Molecule, *XYZTraj, error) {
xyzfile, err := os.Open(xyzname)
if err != nil {
//fmt.Println("Unable to open file!!")
return nil, nil, CError{err.Error(), []string{"os.Open", "XYZFileRead"}}
}
xyz := bufio.NewReader(xyzfile)
//the molecule first
coords, atoms, err := xyzReadSnap(xyz, nil, true)
top := NewTopology(0, 1, atoms)
bfactors := make([][]float64, 1, 1)
bfactors[0] = make([]float64, top.Len())
returned, err := NewMolecule([]*v3.Matrix{coords}, top, bfactors)
//now the traj
traj := new(XYZTraj)
traj.xyzfile = xyzfile
traj.xyz = xyz
traj.natoms = returned.Len()
traj.readable = true
traj.firstframe = coords
return returned, traj, nil
}
//xyzReadSnap reads an xyz file snapshot from a bufio.Reader, returns a slice of Atom objects, which will be nil if ReadTopol is false,
// a slice of matrix.DenseMatrix and an error or nil.
func xyzReadSnap(xyz *bufio.Reader, toplace *v3.Matrix, ReadTopol bool) (*v3.Matrix, []*Atom, error) {
line, err := xyz.ReadString('\n')
if err != nil {
return nil, nil, CError{fmt.Sprintf("Empty XYZ File: %s", err.Error()), []string{"bufio.Reader.ReadString", "xyzReadSnap"}}
}
natoms, err := strconv.Atoi(strings.TrimSpace(line))
if err != nil {
return nil, nil, CError{fmt.Sprintf("Wrong header for an XYZ file %s", err.Error()), []string{"strconv.Atoi", "xyzReadSnap"}}
}
var molecule []*Atom
if ReadTopol {
molecule = make([]*Atom, natoms, natoms)
}
var coords []float64
if toplace == nil {
coords = make([]float64, natoms*3, natoms*3)
} else {
coords = toplace.RawSlice()
}
_, err = xyz.ReadString('\n') //We dont care about this line
if err != nil {
return nil, nil, CError{fmt.Sprintf("Ill formatted XYZ file: %s", err.Error()), []string{"bufio.Reader.ReadString", "xyzReadSnap"}}
}
errs := make([]error, 3, 3)
for i := 0; i < natoms; i++ {
line, errs[0] = xyz.ReadString('\n')
if errs[0] != nil { //inefficient, (errs[1] can be checked once before), but clearer.
if strings.Contains(errs[0].Error(), "EOF") && i == natoms-1 { //This allows that an XYZ ends without a newline
errs[0] = nil
} else {
break
}
}
fields := strings.Fields(line)
if len(fields) < 4 {
errs[0] = fmt.Errorf("Line number %d ill formed", i)
break
}
if ReadTopol {
molecule[i] = new(Atom)
molecule[i].Symbol = strings.Title(fields[0])
molecule[i].Mass = symbolMass[molecule[i].Symbol]
molecule[i].Molname = "UNK"
molecule[i].Name = molecule[i].Symbol
}
coords[i*3], errs[0] = strconv.ParseFloat(fields[1], 64)
coords[i*3+1], errs[1] = strconv.ParseFloat(fields[2], 64)
coords[i*3+2], errs[2] = strconv.ParseFloat(fields[3], 64)
}
//This could be done faster if done in the same loop where the coords are read
//Instead of having another loop just for them.
for _, i := range errs {
if i != nil {
// fmt.Println("line", line, k)
return nil, nil, CError{i.Error(), []string{"strconv.ParseFloat", "xyzReadSnap"}}
}
}
//this should be fine even if I had a toplace matrix. Both toplace and mcoord should just point to the same data.
mcoords, err := v3.NewMatrix(coords)
return mcoords, molecule, errDecorate(err, "xyzReadSnap")
}
//XYZWrite writes the mol Ref and the Coord coordinates in an XYZ file with name xyzname which will
//be created fot that. If the file exist it will be overwritten.
func XYZFileWrite(xyzname string, Coords *v3.Matrix, mol Atomer) error {
out, err := os.Create(xyzname)
if err != nil {
return CError{err.Error(), []string{"os.Create", "XYZFileWrite"}}
}
defer out.Close()
err = XYZWrite(out, Coords, mol)
if err != nil {
return errDecorate(err, "XYZFileWrite")
}
return nil
}
//XYZStringWrite writes the mol Ref and the Coord coordinates in an XYZ-formatted string.
func XYZStringWrite(Coords *v3.Matrix, mol Atomer) (string, error) {
var out string
if mol.Len() != Coords.NVecs() {
return "", CError{"Ref and Coords dont have the same number of atoms", []string{"XYZStringWrite"}}
}
c := make([]float64, 3, 3)
out = fmt.Sprintf("%-4d\n\n", mol.Len())
//towrite := Coords.Arrays() //An array of array with the data in the matrix
for i := 0; i < mol.Len(); i++ {
//c := towrite[i] //coordinates for the corresponding atoms
c = Coords.Row(c, i)
temp := fmt.Sprintf("%-2s %12.6f%12.6f%12.6f \n", mol.Atom(i).Symbol, c[0], c[1], c[2])
out = strings.Join([]string{out, temp}, "")
}
return out, nil
}
//XYZWrite writes the mol Ref and the Coord coordinates to a io.Writer.
func XYZWrite(out io.Writer, Coords *v3.Matrix, mol Atomer) error {
iowriterError := func(err error) error {
return CError{"Failed to write in io.Writer" + err.Error(), []string{"io.Writer.Write", "XYZWrite"}}
}
if mol.Len() != Coords.NVecs() {
return CError{"Ref and Coords dont have the same number of atoms", []string{"XYZWrite"}}
}
c := make([]float64, 3, 3)
_, err := out.Write([]byte(fmt.Sprintf("%-4d\n\n", mol.Len())))
if err != nil {
return iowriterError(err)
}
//towrite := Coords.Arrays() //An array of array with the data in the matrix
for i := 0; i < mol.Len(); i++ {
//c := towrite[i] //coordinates for the corresponding atoms
c = Coords.Row(c, i)
temp := fmt.Sprintf("%-2s %12.6f%12.6f%12.6f \n", mol.Atom(i).Symbol, c[0], c[1], c[2])
_, err := out.Write([]byte(temp))
if err != nil {
return iowriterError(err)
}
}
return nil
}
func GroFileRead(groname string) (*Molecule, error) {
grofile, err := os.Open(groname)
if err != nil {
//fmt.Println("Unable to open file!!")
return nil, CError{err.Error(), []string{"os.Open", "GroFileRead"}}
}
defer grofile.Close()
snaps := 1
gro := bufio.NewReader(grofile)
var top *Topology
var molecule []*Atom
Coords := make([]*v3.Matrix, 1, 1)
for {
//When we read the first snapshot we collect also the topology data, later
//only coords are collected.
if snaps == 1 {
Coords[0], molecule, err = groReadSnap(gro, true)
if err != nil {
return nil, errDecorate(err, "GroFileRead")
}
top = NewTopology(0, 1, molecule)
if err != nil {
return nil, errDecorate(err, "GroFileRead")
}
snaps++
continue
}
//fmt.Println("how manytimes?") /////////////////////
tmpcoords, _, err := groReadSnap(gro, false)
if err != nil {
//An error here may just mean that there are no more snapshots
errm := err.Error()
if strings.Contains(errm, "Empty") || strings.Contains(errm, "EOF") {
err = nil
break
}
return nil, errDecorate(err, "GroRead")
}
Coords = append(Coords, tmpcoords)
}
returned, err := NewMolecule(Coords, top, nil)
fmt.Println("2 return!", top.Atom(1), returned.Coords[0].VecView(2)) ///////////////////////
return returned, errDecorate(err, "GroRead")
}
func groReadSnap(gro *bufio.Reader, ReadTopol bool) (*v3.Matrix, []*Atom, error) {
nm2A := 10.0
chains := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
line, err := gro.ReadString('\n') //we don't care about this line,but it has to be there
if err != nil {
return nil, nil, CError{fmt.Sprintf("Empty gro File: %s", err.Error()), []string{"bufio.Reader.ReadString", "groReadSnap"}}
}
line, err = gro.ReadString('\n')
if err != nil {
return nil, nil, CError{fmt.Sprintf("Malformed gro File: %s", err.Error()), []string{"bufio.Reader.ReadString", "groReadSnap"}}
}
natoms, err := strconv.Atoi(strings.TrimSpace(line))
if err != nil {
return nil, nil, CError{fmt.Sprintf("Wrong header for a gro file %s", err.Error()), []string{"strconv.Atoi", "groReadSnap"}}
}
var molecule []*Atom
if ReadTopol {
molecule = make([]*Atom, 0, natoms)
}
coords := make([]float64, 0, natoms*3)
prevres := 0
chainindex := 0
for i := 0; i < natoms; i++ {
line, err = gro.ReadString('\n')
if err != nil {
return nil, nil, CError{fmt.Sprintf("Failure to read gro File: %s", err.Error()), []string{"bufio.Reader.ReadString", "groReadSnap"}}
}
fields := strings.Fields(line)
if len(fields) < 4 {
break //meaning this line contains the unit cell vectors, and it is the last line of the snapshot
}
if ReadTopol {
atom, c, err := read_gro_line(line)
if err != nil {
return nil, nil, CError{fmt.Sprintf("Failure to read gro File: %s", err.Error()), []string{"bufio.Reader.ReadString", "groReadSnap"}}
}
if atom.MolID < prevres {
chainindex++
}
prevres = atom.MolID
if chainindex >= len(chains) {
chainindex = 0 //more chains inthe molecule than letters in the alphabet!
}
atom.Chain = string(chains[chainindex])
molecule = append(molecule, atom)
coords = append(coords, c...)
// fmt.Println(atom, c) //////////////////
continue
}
c := make([]float64, 3, 3)
for i := 0; i < 3; i++ {
c[i], err = strconv.ParseFloat(strings.TrimSpace(line[20+(i*8):28+(i*8)]), 64)
if err != nil {
return nil, nil, err
}
c[i] = c[i] * nm2A //gro uses nm, goChem uses A.
}
coords = append(coords, c...)
}
mcoords, err := v3.NewMatrix(coords)
// fmt.Println(molecule) //, mcoords) ////////////////////////
return mcoords, molecule, nil
}
//Parses a valid ATOM or HETATM line of a PDB file, returns an Atom
// object with the info except for the coordinates and b-factors, which are returned
// separately as an array of 3 float64 and a float64, respectively
func read_gro_line(line string) (*Atom, []float64, error) {
coords := make([]float64, 3, 3)
atom := new(Atom)
nm2A := 10.0
var err error
atom.MolID, err = strconv.Atoi(strings.TrimSpace(line[0:5]))
if err != nil {
return nil, nil, err
}
atom.Molname = strings.TrimSpace(line[5:10])
atom.Molname1 = three2OneLetter[atom.Molname]
atom.Name = strings.TrimSpace(line[10:15])
atom.ID, err = strconv.Atoi(strings.TrimSpace(line[15:20]))
if err != nil {
return nil, nil, err
}
for i := 0; i < 3; i++ {
coords[i], err = strconv.ParseFloat(strings.TrimSpace(line[20+(i*8):28+(i*8)]), 64)
if err != nil {
return nil, nil, err
}
coords[i] = coords[i] * nm2A //gro uses nm, goChem uses A.
}
atom.Symbol, _ = symbolFromName(atom.Name)
return atom, coords, nil
}
func GroFileWrite(outname string, Coords []*v3.Matrix, mol Atomer) error {
out, err := os.Create(outname)
if err != nil {
return CError{"Failed to write open file" + err.Error(), []string{"os.Create", "GroFileWrite"}}
}
defer out.Close()
for _, v := range Coords {
err := GroSnapWrite(v, mol, out)
if err != nil {
return errDecorate(err, "GoFileWrite")
}
}
return nil
}
func GroSnapWrite(coords *v3.Matrix, mol Atomer, out io.Writer) error {
A2nm := 0.1
iowriterError := func(err error) error {
return CError{"Failed to write in io.Writer" + err.Error(), []string{"io.Writer.Write", "GroSnapWrite"}}
}
if mol.Len() != coords.NVecs() {
return CError{"Ref and Coords dont have the same number of atoms", []string{"GroSnapWrite"}}
}
c := make([]float64, 3, 3)
_, err := out.Write([]byte(fmt.Sprintf("Written with goChem :-)\n%-4d\n", mol.Len())))
if err != nil {
return iowriterError(err)
}
//towrite := Coords.Arrays() //An array of array with the data in the matrix
for i := 0; i < mol.Len(); i++ {
//c := towrite[i] //coordinates for the corresponding atoms
c = coords.Row(c, i)
at := mol.Atom(i)
//velocities are set to 0
temp := fmt.Sprintf("%5d%-5s%5s%5d%8.3f%8.3f%8.3f%8.4f%8.4f%8.4f\n", at.MolID, at.Molname, at.Name, at.ID, c[0]*A2nm, c[1]*A2nm, c[2]*A2nm, 0.0, 0.0, 0.0)
_, err := out.Write([]byte(temp))
if err != nil {
return iowriterError(err)
}
}
//the box vectors at the end of the snappshot
_, err = out.Write([]byte("0.0 0.0 0.0\n"))
if err != nil {
return iowriterError(err)
}
return nil
}