<commit_before>﻿Imports BioNovoGene.Analytical.MassSpectrometry.Assembly.DataReader
Imports BioNovoGene.Analytical.MassSpectrometry.Assembly.mzData.mzWebCache
Imports BioNovoGene.Analytical.MassSpectrometry.Assembly.ThermoRawFileReader
Imports BioNovoGene.Analytical.MassSpectrometry.Assembly.ThermoRawFileReader.DataObjects
Imports Microsoft.VisualBasic.Language
Imports Microsoft.VisualBasic.Language.Default

Public Class XRawStream

    ReadOnly raw As MSFileReader

    Dim MS1 As ScanMS1 = Nothing
    Dim MS2 As New List(Of ScanMS2)
    Dim MSscans As New List(Of ScanMS1)

    Friend scanIdFunc As Func(Of SingleScanInfo, Integer, String)

    Sub New(raw As MSFileReader, Optional scanIdFunc As Func(Of SingleScanInfo, Integer, String) = Nothing)
        Me.raw = raw
        Me.raw.Options.MaxScan = raw.ScanMax
        Me.scanIdFunc = scanIdFunc Or defaultId()
    End Sub

    Private Shared Function defaultId() As [Default](Of Func(Of SingleScanInfo, Integer, String))
        Return New [Default](Of Func(Of SingleScanInfo, Integer, String))(
            Function(scaninfo As SingleScanInfo, i As Integer) As String
                Return $"{If(scaninfo.MSLevel = 1, "[MS1]", "[MSn]")}[Scan_{scaninfo.ScanNumber}] {scaninfo.FilterText}"
            End Function)
    End Function

    Public Function StreamTo(Optional skipEmptyScan As Boolean = True) As mzPack
        Dim scan_times As New List(Of Double)
        Dim TIC As New List(Of Double)
        Dim BPC As New List(Of Double)

        For Each scan As RawLabelData In raw.GetLabelData(skipEmptyScan)
            Call WalkScan(scan)

            If scan.MsLevel = 1 Then
                Dim scaninfo As SingleScanInfo = raw.GetScanInfo(scan.ScanNumber)

                scan_times += scan.ScanTime * 60
                TIC += scaninfo.TotalIonCurrent
                BPC += scaninfo.BasePeakIntensity
            End If
        Next

        MS1.products = MS2.PopAll
        MSscans += MS1

        Return New mzPack With {
            .MS = MSscans.PopAll,
            .Chromatogram = New Chromatogram With {
                .BPC = BPC.PopAll,
                .TIC = TIC.PopAll,
                .scan_time = scan_times.PopAll
            }
        }
    End Function

    Private Sub WalkScan(scan As RawLabelData)
        Dim scanInfo As SingleScanInfo = raw.GetScanInfo(scan.ScanNumber)
        Dim mz As Double() = scan.MSData.Select(Function(a) a.Mass).ToArray
        Dim into As Double() = scan.MSData.Select(Function(a) a.Intensity).ToArray
        Dim scanId As String = scanIdFunc(scanInfo, MSscans.Count)

        If scanInfo.MSLevel = 1 Then
            If Not MS1 Is Nothing Then
                MS1.products = MS2.PopAll
                MSscans += MS1
            End If

            MS1 = New ScanMS1 With {
                .BPC = scanInfo.BasePeakIntensity,
                .into = into,
                .mz = mz,
                .rt = scanInfo.RetentionTime,
                .scan_id = scanId,
                .TIC = scanInfo.TotalIonCurrent
            }
        Else
            MS2 += New ScanMS2 With {
                .activationMethod = scanInfo.ActivationType,
                .centroided = scanInfo.IsCentroided,
                .charge = scanInfo.ChargeState,
                .collisionEnergy = 0,
                .intensity = scanInfo.TotalIonCurrent,
                .parentMz = scanInfo.ParentIonMZ,
                .scan_id = scanId,
                .rt = scanInfo.RetentionTime,
                .polarity = scanInfo.IonMode,
                .mz = mz,
                .into = into
            }
        End If<commit_msg>bugs fixed of time unit<commit_after>﻿Imports BioNovoGene.Analytical.MassSpectrometry.Assembly.DataReader
Imports BioNovoGene.Analytical.MassSpectrometry.Assembly.mzData.mzWebCache
Imports BioNovoGene.Analytical.MassSpectrometry.Assembly.ThermoRawFileReader
Imports BioNovoGene.Analytical.MassSpectrometry.Assembly.ThermoRawFileReader.DataObjects
Imports Microsoft.VisualBasic.Language
Imports Microsoft.VisualBasic.Language.Default

Public Class XRawStream

    ReadOnly raw As MSFileReader

    Dim MS1 As ScanMS1 = Nothing
    Dim MS2 As New List(Of ScanMS2)
    Dim MSscans As New List(Of ScanMS1)

    Friend scanIdFunc As Func(Of SingleScanInfo, Integer, String)

    Sub New(raw As MSFileReader, Optional scanIdFunc As Func(Of SingleScanInfo, Integer, String) = Nothing)
        Me.raw = raw
        Me.raw.Options.MaxScan = raw.ScanMax
        Me.scanIdFunc = scanIdFunc Or defaultId()
    End Sub

    Private Shared Function defaultId() As [Default](Of Func(Of SingleScanInfo, Integer, String))
        Return New [Default](Of Func(Of SingleScanInfo, Integer, String))(
            Function(scaninfo As SingleScanInfo, i As Integer) As String
                Return $"{If(scaninfo.MSLevel = 1, "[MS1]", "[MSn]")}[Scan_{scaninfo.ScanNumber}] {scaninfo.FilterText}"
            End Function)
    End Function

    Public Function StreamTo(Optional skipEmptyScan As Boolean = True) As mzPack
        Dim scan_times As New List(Of Double)
        Dim TIC As New List(Of Double)
        Dim BPC As New List(Of Double)

        For Each scan As RawLabelData In raw.GetLabelData(skipEmptyScan)
            Call WalkScan(scan)

            If scan.MsLevel = 1 Then
                Dim scaninfo As SingleScanInfo = raw.GetScanInfo(scan.ScanNumber)

                scan_times += scan.ScanTime * 60
                TIC += scaninfo.TotalIonCurrent
                BPC += scaninfo.BasePeakIntensity
            End If
        Next

        MS1.products = MS2.PopAll
        MSscans += MS1

        Return New mzPack With {
            .MS = MSscans.PopAll,
            .Chromatogram = New Chromatogram With {
                .BPC = BPC.PopAll,
                .TIC = TIC.PopAll,
                .scan_time = scan_times.PopAll
            }
        }
    End Function

    Private Sub WalkScan(scan As RawLabelData)
        Dim scanInfo As SingleScanInfo = raw.GetScanInfo(scan.ScanNumber)
        Dim mz As Double() = scan.MSData.Select(Function(a) a.Mass).ToArray
        Dim into As Double() = scan.MSData.Select(Function(a) a.Intensity).ToArray
        Dim scanId As String = scanIdFunc(scanInfo, MSscans.Count)

        If scanInfo.MSLevel = 1 Then
            If Not MS1 Is Nothing Then
                MS1.products = MS2.PopAll
                MSscans += MS1
            End If

            MS1 = New ScanMS1 With {
                .BPC = scanInfo.BasePeakIntensity,
                .into = into,
                .mz = mz,
                .rt = scanInfo.RetentionTime * 60,
                .scan_id = scanId,
                .TIC = scanInfo.TotalIonCurrent
            }
        Else
            MS2 += New ScanMS2 With {
                .activationMethod = scanInfo.ActivationType,
                .centroided = scanInfo.IsCentroided,
                .charge = scanInfo.ChargeState,
                .collisionEnergy = 0,
                .intensity = scanInfo.TotalIonCurrent,
                .parentMz = scanInfo.ParentIonMZ,
                .scan_id = scanId,
                .rt = scanInfo.RetentionTime * 60,
                .polarity = scanInfo.IonMode,
                .mz = mz,
                .into = into
            }
        End If
{'fraction_non_alphanumeric': 0.07425742574257425, 'fraction_numerical': 0.006875687568756876, 'mean_word_length': 1.9385858585858586, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6636248', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2054, 'n_words': 537}