Skip to content

Commit

Permalink
Allow usage of template input to amend geometry information (#11)
Browse files Browse the repository at this point in the history
- transfer lattice, periodicity, bonds and other annotations
- the template structure is read before the input structure from STDIN
- error if template and input structure mismatch in the number of atoms
  • Loading branch information
awvwgk authored Jan 27, 2021
1 parent 2945349 commit 9d043c1
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ get_directory_property(is-subproject PARENT_DIRECTORY)
project(
"mctc-lib"
LANGUAGES "Fortran"
VERSION "0.2.0"
VERSION "0.2.1"
DESCRIPTION "Modular computation tool chain"
)

Expand Down
66 changes: 62 additions & 4 deletions app/main.f90
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,36 @@ program main
implicit none
character(len=*), parameter :: prog_name = "mctc-convert"

character(len=:), allocatable :: input, output
integer, allocatable :: input_format, output_format
character(len=:), allocatable :: input, output, template
integer, allocatable :: input_format, output_format, template_format
type(structure_type) :: mol
type(structure_type), allocatable :: mol_template
type(error_type), allocatable :: error
logical :: normalize

call get_arguments(input, input_format, output, output_format, normalize, error)
call get_arguments(input, input_format, output, output_format, normalize, &
& template, template_format, error)
if (allocated(error)) then
write(error_unit, '(a)') error%message
error stop
end if

if (allocated(template)) then
allocate(mol_template)
if (template == "-") then
if (.not.allocated(template_format)) then
template_format = merge(output_format, filetype%xyz, allocated(output_format))
end if
call read_structure(mol_template, input_unit, template_format, error)
else
call read_structure(mol_template, template, error, template_format)
end if
if (allocated(error)) then
write(error_unit, '(a)') error%message
error stop
end if
end if

if (input == "-") then
if (.not.allocated(input_format)) input_format = filetype%xyz
call read_structure(mol, input_unit, input_format, error)
Expand All @@ -60,6 +78,22 @@ program main
error stop
end if

if (allocated(mol_template)) then
if (mol%nat /= mol_template%nat) then
write(error_unit, '(*(a, 1x))') &
"Number of atoms missmatch in", template, "and", input
error stop
end if

! move_alloc can also move non-allocated objects
call move_alloc(mol_template%lattice, mol%lattice)
call move_alloc(mol_template%periodic, mol%periodic)
call move_alloc(mol_template%bond, mol%bond)
call move_alloc(mol_template%comment, mol%comment)
call move_alloc(mol_template%pdb, mol%pdb)
call move_alloc(mol_template%sdf, mol%sdf)
end if

if (normalize) then
mol%sym = to_symbol(mol%num)
end if
Expand Down Expand Up @@ -95,6 +129,9 @@ subroutine help(unit)
"-i, --input <format>", "Hint for the format of the input file", &
"-o, --output <format>", "Hint for the format of the output file", &
"--normalize", "Normalize all element symbols to capitalized format", &
"--template <file>", "File to use as template to fill in meta data", &
"", "(useful to add back SDF or PDB annotions)", &
"--template-format <format>", "", "", "Hint for the format of the template file", &
"--version", "Print program version and exit", &
"--help", "Show this help message"

Expand All @@ -115,7 +152,7 @@ end subroutine version


subroutine get_arguments(input, input_format, output, output_format, normalize, &
& error)
& template, template_format, error)

!> Input file name
character(len=:), allocatable :: input
Expand All @@ -129,6 +166,12 @@ subroutine get_arguments(input, input_format, output, output_format, normalize,
!> Output file format
integer, allocatable, intent(out) :: output_format

!> Template file name
character(len=:), allocatable :: template

!> Template file format
integer, allocatable, intent(out) :: template_format

!> Normalize element symbols
logical, intent(out) :: normalize

Expand Down Expand Up @@ -180,6 +223,21 @@ subroutine get_arguments(input, input_format, output, output_format, normalize,
output_format = get_filetype("."//arg)
case("--normalize")
normalize = .true.
case("--template")
iarg = iarg + 1
call get_argument(iarg, template)
if (.not.allocated(template)) then
call fatal_error(error, "Missing argument for template file")
exit
end if
case("--template-format")
iarg = iarg + 1
call get_argument(iarg, arg)
if (.not.allocated(arg)) then
call fatal_error(error, "Missing argument for template format")
exit
end if
template_format = get_filetype("."//arg)
end select
end do

Expand Down
2 changes: 1 addition & 1 deletion fpm.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "mctc-lib"
version = "0.2.0"
version = "0.2.1"
license = "Apache-2.0"
maintainer = ["@awvwgk"]
author = ["Sebastian Ehlert"]
Expand Down
13 changes: 13 additions & 0 deletions man/mctc-convert.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ mctc-convert - Convert between supported input formats of the tool chain library

Read structure from input file and writes it to output file.
The format is determined by the file extension or the format hint.
The input structure can be read from standard input by providing - as argument.
Similarly, the output structure can be written to standard output with - as argument.
Standard input and standard output should be combined with a format hint option.

Supported formats:

Expand All @@ -36,6 +39,16 @@ Hint for the format of the output file
*--normalize*::
Normalize all element symbols to capitalized format

*--template* _file_::
File to use as template to fill in meta data (useful to add back SDF or PDB annotions).
Transfers lattice, periodicity, comments and format specific annotations from the template
to the input structure.
If the standard input, -, is provided the template structure will
be read _before_ the input structure.

*--template-format* _format_::
Hint for the format of the template file (only used if template file name is provided)

*--version*::
Print program version and exit

Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
project(
'mctc-lib',
'fortran',
version: '0.2.0',
version: '0.2.1',
license: 'Apache-2.0',
meson_version: '>=0.53',
default_options: [
Expand Down
2 changes: 1 addition & 1 deletion src/mctc/io/read/pdb.f90
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ subroutine read_pdb(self, unit, error)
end if

call new(self, sym(:iatom), xyz(:, :iatom))
!self%pdb = pdb(:iatom)
self%pdb = pdb(:iatom)
self%charge = sum(pdb(:iatom)%charge)

if (.not.all(self%num > 0)) then
Expand Down
4 changes: 2 additions & 2 deletions src/mctc/version.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ module mctc_version


!> String representation of the mctc-lib version
character(len=*), parameter :: mctc_version_string = "0.2.0"
character(len=*), parameter :: mctc_version_string = "0.2.1"

!> Numeric representation of the mctc-lib version
integer, parameter :: mctc_version_compact(3) = [0, 2, 0]
integer, parameter :: mctc_version_compact(3) = [0, 2, 1]


contains
Expand Down

0 comments on commit 9d043c1

Please sign in to comment.