Skip to content

xyz2mol ? #121

@Nick-Mul

Description

@Nick-Mul

I think this project is great but something I think could be really useful would be converting xyz file to a MolGraph, I had a unsuccessful attempt at it and I was wondering if you could suggest a way to create a MolGraph I was thinking via a dictionary

Many thanks,
Nick

# Function to read an XYZ file and extract atomic symbols and coordinates
function read_xyz(filename::String)
    open(filename, "r") do file
        n_atoms = parse(Int, readline(file)) # First line: number of atoms
        readline(file)  # Second line: comment, skip it
        
        atoms = []
        coords = []
        
        # Read the remaining lines: atomic symbols and coordinates
        for line in eachline(file)
            parts = split(line)
            push!(atoms, parts[1])  # Atomic symbol
            x, y, z = parse.(Float64, parts[2:4])  # Coordinates
            push!(coords, [x, y, z])
        end
        
        return atoms, hcat(coords...)
    end
end

# Function to calculate pairwise bond distances
function bond_distance_matrix(coords::Matrix{Float64})
    n = size(coords, 2)
    distance_matrix = zeros(Float64, n, n)
    
    # Calculate pairwise distances
    for i in 1:n
        for j in i+1:n
            distance = norm(coords[:, i] - coords[:, j])
            distance_matrix[i, j] = distance
            distance_matrix[j, i] = distance  # Symmetric matrix
        end
    end
    
    return distance_matrix
end

# Assign bonds based on distance and covalent radii
function assign_bonds(atoms, dist_matrix::Matrix{Float64})
    bonds = []
    n = length(atoms)
    for i in 1:n, j in i+1:n
        ri = get(covalent_radii, atoms[i], 0.0)
        rj = get(covalent_radii, atoms[j], 0.0)
        if ri == 0.0 || rj == 0.0
            continue  # Unknown element, skip
        end
        threshold = (ri + rj) * vdw_covalent_factor
        if dist_matrix[i, j]  threshold
            push!(bonds, (i, j))
        end
    end
    return bonds
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions