pierrepo / grodecoder

GroDecoder extracts and identifies the molecular components of a structure file (PDB or GRO) issued from a molecular dynamics simulation.
https://grodecoder.streamlit.app/
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Adapt the format of some file, so it can be readable by MDAnalysis #81

Open KarinDuong opened 1 month ago

KarinDuong commented 1 month ago

Files from https://acs.figshare.com/articles/dataset/Small_Molecule_Solvation_Free_Energy_Enhanced_Conformational_Sampling_Using_Expanded_Ensemble_Molecular_Dynamics_Simulation/2615170 have some issues to be read by MDAnalysis. So I have to change the spacing and the float number

KarinDuong commented 1 month ago

With this code:

def fix_gro_file(file_path):
    with open(file_path, 'r') as file:
        lines = file.readlines()

    with open(f'{file_path}', 'w') as file:
        for line in lines:
            print(f"before if : {len(line)}")
            if len(line) > 30 :
                print("if")
                try:
                    print("try")
                    line_content = line.split()

                    if len(line_content) > 3:
                        print(line_content)

                        fixed_line = f" {line_content[0]:>7}  {line_content[1]:>5} {line_content[2]:>4} {line_content[3][:5]:>7}{line_content[4][:5]:>7}{line_content[5][:5]:>7}\n"
                        print(fixed_line)
                        file.write(fixed_line)
                    else:
                        file.write(line)
                except ValueError:
                    print(f"Warning: Could not parse line: {line}")
                    continue
            else:
                print("else")
                file.write(line)

It works for ibuprofen_ethanol.gro but not for methanol and water ...