<commit_before>﻿Imports System.IO
Imports System.Reflection
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
Imports Microsoft.VisualBasic.Language

Namespace File

    ''' <summary>
    ''' Chemical descriptor
    ''' </summary>
    Public Class ChemicalDescriptor : Implements IEnumerable(Of Double)

        ''' <summary>
        ''' Computed Octanol/Water Partition Coefficient
        ''' </summary>
        ''' <returns></returns>
        Public Property XLogP3 As Double
        Public Property XLogP3_AA As Double

        ''' <summary>
        ''' Hydrogen Bond Donor Count
        ''' </summary>
        ''' <returns></returns>
        Public Property HydrogenDonors As Integer
        ''' <summary>
        ''' Hydrogen Bond Acceptor count
        ''' </summary>
        ''' <returns></returns>
        Public Property HydrogenAcceptor As Integer
        ''' <summary>
        ''' Rotatable Bond Count
        ''' </summary>
        ''' <returns></returns>
        Public Property RotatableBonds As Integer
        Public Property ExactMass As Double
        Public Property TopologicalPolarSurfaceArea As Double
        Public Property HeavyAtoms As Integer
        Public Property FormalCharge As Integer
        Public Property Complexity As Integer

        Shared ReadOnly schema As PropertyInfo() = DataFramework _
            .Schema(Of ChemicalDescriptor)(PropertyAccess.Readable, True, True) _
            .Values _
            .OrderBy(Function(p) p.Name) _
            .ToArray

        Sub New(data As Dictionary(Of String, String()))
            Dim read = getOne(data)

            On Error Resume Next

            ExactMass = Double.Parse(read("PUBCHEM_EXACT_MASS"))
            XLogP3 = Double.Parse(read("PUBCHEM_XLOGP3"))
            XLogP3_AA = Double.Parse(read("PUBCHEM_XLOGP3_AA"))
            FormalCharge = Integer.Parse(read("PUBCHEM_TOTAL_CHARGE"))
            TopologicalPolarSurfaceArea = Double.Parse(read("PUBCHEM_CACTVS_TPSA"))
            HydrogenAcceptor = Integer.Parse(read("PUBCHEM_CACTVS_HBOND_ACCEPTOR"))
            HydrogenDonors = Integer.Parse(read("PUBCHEM_CACTVS_HBOND_DONOR"))
            RotatableBonds = Integer.Parse(read("PUBCHEM_CACTVS_ROTATABLE_BOND"))
            HeavyAtoms = Integer.Parse(read("PUBCHEM_HEAVY_ATOM_COUNT"))
            Complexity = Integer.Parse(read("PUBCHEM_CACTVS_COMPLEXITY"))
        End Sub

        Sub New()
        End Sub

        Public Shared Function GetBytesBuffer(descript As ChemicalDescriptor) As Byte()
            Dim bytes As New List(Of Byte)

            For Each data As PropertyInfo In schema
                Dim value = data.GetValue(descript)

                If TypeOf value Is Double Then
                    bytes += BitConverter.GetBytes(DirectCast(value, Double))
                ElseIf TypeOf value Is Integer Then
                    bytes += BitConverter.GetBytes(DirectCast(value, Integer))
                Else
                    Throw New NotImplementedException
                End If
            Next

            Return bytes
        End Function

        Public Shared Function FromBytes(stream As Byte()) As ChemicalDescriptor
            Dim descript As New ChemicalDescriptor
            Dim i As Integer = 0<commit_msg>try to fix the number format errors<commit_after>﻿Imports System.IO
Imports System.Reflection
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
Imports Microsoft.VisualBasic.Language

Namespace File

    ''' <summary>
    ''' Chemical descriptor
    ''' </summary>
    Public Class ChemicalDescriptor : Implements IEnumerable(Of Double)

        ''' <summary>
        ''' Computed Octanol/Water Partition Coefficient
        ''' </summary>
        ''' <returns></returns>
        Public Property XLogP3 As Double
        Public Property XLogP3_AA As Double

        ''' <summary>
        ''' Hydrogen Bond Donor Count
        ''' </summary>
        ''' <returns></returns>
        Public Property HydrogenDonors As Integer
        ''' <summary>
        ''' Hydrogen Bond Acceptor count
        ''' </summary>
        ''' <returns></returns>
        Public Property HydrogenAcceptor As Integer
        ''' <summary>
        ''' Rotatable Bond Count
        ''' </summary>
        ''' <returns></returns>
        Public Property RotatableBonds As Integer
        Public Property ExactMass As Double
        Public Property TopologicalPolarSurfaceArea As Double
        Public Property HeavyAtoms As Integer
        Public Property FormalCharge As Integer
        Public Property Complexity As Integer

        Shared ReadOnly schema As PropertyInfo() = DataFramework _
            .Schema(Of ChemicalDescriptor)(PropertyAccess.Readable, True, True) _
            .Values _
            .OrderBy(Function(p) p.Name) _
            .ToArray

        Sub New(data As Dictionary(Of String, String()))
            Dim read = getOne(data)

            On Error Resume Next

            ExactMass = TryParseDouble(read("PUBCHEM_EXACT_MASS"))
            XLogP3 = TryParseDouble(read("PUBCHEM_XLOGP3"))
            XLogP3_AA = TryParseDouble(read("PUBCHEM_XLOGP3_AA"))
            FormalCharge = TryParseInteger(read("PUBCHEM_TOTAL_CHARGE"))
            TopologicalPolarSurfaceArea = TryParseDouble(read("PUBCHEM_CACTVS_TPSA"))
            HydrogenAcceptor = TryParseInteger(read("PUBCHEM_CACTVS_HBOND_ACCEPTOR"))
            HydrogenDonors = TryParseInteger(read("PUBCHEM_CACTVS_HBOND_DONOR"))
            RotatableBonds = TryParseInteger(read("PUBCHEM_CACTVS_ROTATABLE_BOND"))
            HeavyAtoms = TryParseInteger(read("PUBCHEM_HEAVY_ATOM_COUNT"))
            Complexity = TryParseInteger(read("PUBCHEM_CACTVS_COMPLEXITY"))
        End Sub

        Sub New()
        End Sub

        Private Function TryParseDouble(s As String) As Double
            If PrimitiveParser.IsNumeric(s) Then
                Return Double.Parse(s)
            Else
                Return -10000000
            End If
        End Function

        Private Function TryParseInteger(s As String) As Integer
            If PrimitiveParser.IsInteger(s) Then
                Return Integer.Parse(s)
            Else
                Return -10000000
            End If
        End Function

        Public Shared Function GetBytesBuffer(descript As ChemicalDescriptor) As Byte()
            Dim bytes As New List(Of Byte)

            For Each data As PropertyInfo In schema
                Dim value = data.GetValue(descript)

                If TypeOf value Is Double Then
                    bytes += BitConverter.GetBytes(DirectCast(value, Double))
                ElseIf TypeOf value Is Integer Then
                    bytes += BitConverter.GetBytes(DirectCast(value, Integer))
                Else
                    Throw New NotImplementedException
                End If
            Next

            Return bytes
        End Function

        Public Shared Function FromBytes(stream As Byte()) As ChemicalDescriptor
            Dim descript As New ChemicalDescriptor
            Dim i As Integer = 0
{'fraction_non_alphanumeric': 0.07464169149992905, 'fraction_numerical': 0.004257130693912303, 'mean_word_length': 2.181941309255079, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2257678', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1763, 'n_words': 485}