Skip to content

Commit

Permalink
add pdb exporter
Browse files Browse the repository at this point in the history
Remove NETStandard2.0
  • Loading branch information
JensKrumsieck committed Feb 19, 2024
1 parent 808002e commit 355d3b0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ChemSharp.Molecules/ChemSharp.Molecules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<PackageId>ChemSharp.Molecules</PackageId>
<Description>Chemsharp: Molecule IO (cif, xyz, ...) and Elemental Analysis</Description>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;net6.0;net7.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
72 changes: 72 additions & 0 deletions ChemSharp.Molecules/Export/PDBExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using ChemSharp.Export;
using ChemSharp.Extensions;

namespace ChemSharp.Molecules.Export;

public class PDBExporter : AbstractMoleculeExporter
{
public static void Export(Molecule mol, string filename)
{
var exporter = new PDBExporter();
using var stream = File.Create(filename);
exporter.Export(mol, stream);
}

public override void Export(IExportable exportable, Stream stream)
{
base.Export(exportable, stream);
if (Molecule == null) return;
var atomsCount = Molecule.Atoms.Count;
using var sw = new StreamWriter(stream);
sw.WriteLine("HEADER");
sw.WriteLine($"TITLE{StringUtil.Spaces(5)}{Molecule.Title}");

var line = new char[80];
for (var i = 0; i < atomsCount; i++)
{
var atom = Molecule.Atoms[i];
Array.Fill(line, ' ');
AppendToCharArrayAt(in line, "ATOM", 0, PdbAlign.Left);
AppendToCharArrayAt(in line, i.ToString(), 11);
AppendToCharArrayAt(in line, atom.Title, 13, PdbAlign.Left);
AppendToCharArrayAt(in line, atom.Residue, 20);
AppendToCharArrayAt(in line, atom.ChainId != default ? atom.ChainId : ' ', 22);
AppendToCharArrayAt(in line, atom.ResidueId.ToString(), 26);
AppendToCharArrayAt(in line, atom.Location.X.ToInvariantString(), 38);
AppendToCharArrayAt(in line, atom.Location.Y.ToInvariantString(), 46);
AppendToCharArrayAt(in line, atom.Location.Z.ToInvariantString(), 54);
AppendToCharArrayAt(in line, atom.Symbol, 78);
AppendToCharArrayAt(in line, atom.Charge.ToString(), 80);
sw.WriteLine(line);
}
}

private static void AppendToCharArrayAt(in char[] arr, char chr, int index) => arr[index] = chr;

//if align is right, give end column index
private static void AppendToCharArrayAt(in char[] arr, ReadOnlySpan<char> str, int index,
PdbAlign align = PdbAlign.Right)
{
switch (align)
{
case PdbAlign.Left:
{
for (var i = 0; i < str.Length; i++) arr[index + i] = str[i];
break;
}
case PdbAlign.Right:
{
for (var i = 0; i < str.Length; i++) arr[index - str.Length + i] = str[i];
break;
}
default:
throw new ArgumentOutOfRangeException(nameof(align), align, null);
}
}

private enum PdbAlign
{
Left,
Right
}
}
2 changes: 1 addition & 1 deletion ChemSharp.Spectroscopy/ChemSharp.Spectroscopy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<PackageId>ChemSharp.Spectroscopy</PackageId>
<Description>Chemsharp: Spectroscopry IO: NMR, EPR, UVVIS, CSV</Description>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;net6.0;net7.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion ChemSharp.UnitConversion/ChemSharp.UnitConversion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<PackageId>ChemSharp.UnitConversion</PackageId>
<Description>Chemsharp: UnitConversion</Description>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;net6.0;net7.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion ChemSharp/ChemSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<PackageId>ChemSharp</PackageId>
<Description>Library for processing of chemistry related files (aiming at spectroscopy and structural files)</Description>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;net6.0;net7.0</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down

0 comments on commit 355d3b0

Please sign in to comment.