counting number of different molecules from an xyz file

248 views Asked by At

I am very new to python. I have an xyz file that has the coordinates of all atoms in my system. my system consists of a few different molecules made from these atoms. I need to count the number of different molecules in this system, for eaxmple, the total number of molecule A, the total number of Molecule B, etc. I have included a snapshot of the file. The orange, yellow, and blue are the different molecules- similar to the orange section ( the PBIIII molecule) I also have a few that are PBIII. How can count the different molecules for each type from an xyz file?enter image description here

1

There are 1 answers

0
Eftal Gezer On
import csv
elements = []
with open("compound.xyz", 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    for i, row in enumerate(csvreader):
        if i > 1:
            elements.append(row[0].split()[0])
elementnames = set(elements)
counts = []
for elementname in elementnames:
    counts.append(elements.count(elementname))

zipped = list(zip(elementnames, counts))
print(zipped)