<commit_before>﻿''' <summary>
''' The genetic algorithm core for the simulator.
''' </summary>
Module GA

End Module<commit_msg>add alignment method<commit_after>﻿Imports System.Runtime.CompilerServices
Imports Microsoft.VisualBasic.Math.LinearAlgebra
Imports Microsoft.VisualBasic.Math

''' <summary>
''' The genetic algorithm core for the simulator.
''' </summary>
Module GA

    ''' <summary>
    ''' xy分别为预测或者标准品的结果数据，无顺序之分
    ''' </summary>
    ''' <param name="x"></param>
    ''' <param name="y"></param>
    ''' <returns></returns>
    ''' 
    <MethodImpl(MethodImplOptions.AggressiveInlining)>
    Public Function ms2Alignment(x As ms2(), y As ms2(), method As AlignMethod) As (forward#, reverse#)
        Return (GA.Align(x, y, method), GA.Align(y, x, method))
    End Function

    ''' <summary>
    ''' 以<paramref name="ref"/>为基准，从<paramref name="query"/>之中选择出对应的<see cref="ms2.mz"/>信号响应信息，完成对齐操作
    ''' </summary>
    ''' <param name="query"></param>
    ''' <param name="ref"></param>
    ''' <returns></returns>
    Public Function Align(query As ms2(), ref As ms2(), method As AlignMethod) As Double
        Dim q As Vector = query.AlignMatrix(ref, method).VectorShadows.intensity
        Dim s As Vector = ref.VectorShadows.intensity

        Return SSM(q / q.Max, s / s.Max)
    End Function

    <Extension>
    Public Function AlignMatrix(query As ms2(), ref As ms2(), method As AlignMethod) As ms2()
        Return ref _
            .Select(Function(mz)
                        Return query _
                            .Where(Function(q) method.Assert(q.mz, mz.mz)) _
                            .FirstOrDefault
                    End Function) _
            .Where(Function(mz) mz.intensity > 0) _
            .ToArray
    End Function

    ''' <summary>
    ''' 分子量差值
    ''' </summary>
    ''' <param name="measured#"></param>
    ''' <param name="actualValue#"></param>
    ''' <returns></returns>
    Public Function ppm(measured#, actualValue#) As Double
        ' （测量值-实际分子量）/实际分子量
        ' |(实验数据 - 数据库结果)| / 实验数据 * 1000000
        Dim ppmd# = Math.Abs(measured - actualValue) / actualValue
        ppmd = ppmd * 1000000
        Return ppmd
    End Function
End Module

Public MustInherit Class AlignMethod

    Public MustOverride Function Assert(mz1#, mz2#) As Boolean
End Class

Public Class PPMmethod : Inherits AlignMethod

    Public Property ppm As Double

    Public Overrides Function Assert(mz1 As Double, mz2 As Double) As Boolean
        Return GA.ppm(mz1, mz2) <= ppm
    End Function
End Class

Public Class DAmethod : Inherits AlignMethod

    Public Property da As Double

    Public Overrides Function Assert(mz1 As Double, mz2 As Double) As Boolean
        Return Math.Abs(mz1 - mz2) <= da
    End Function
End Class

Public Structure ms2

    Public Property mz As Double
    Public Property quantity As Double
    Public Property intensity As Double

    Public Overrides Function ToString() As String
        Return $"{mz} ({intensity * 100%}%)"
    End Function
End Structure
{'fraction_non_alphanumeric': 0.11968348170128586, 'fraction_numerical': 0.01252884932410155, 'mean_word_length': 2.77363184079602, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '4893652', 'n_tokens_mistral': 980, 'n_tokens_neox': 945, 'n_words': 273}