Skip to content

Commit

Permalink
Raw file connection not closing (#709)
Browse files Browse the repository at this point in the history
* ini

* //

* Tests Passing

* Cleaning

* Deleted redundant files

* Directory path fix

* Tests Passing

* tests passed

* clean up

* stream.Close() removed

* better coverage

* ready to go

* Lock added to ensure only one thread can access msDataScans

* Update ThermoRawFileReader.cs

* fixes from comments in PR

---------

Co-authored-by: Nic Bollis <nbollis@comcast.net>
Co-authored-by: trishorts <mshort@chem.wisc.edu>
Co-authored-by: Alexander-Sol <41119316+Alexander-Sol@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 22, 2023
1 parent e4c48c6 commit 732c003
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 106 deletions.
3 changes: 0 additions & 3 deletions mzLib/MassSpectrometry/MsDataFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@
// You should have received a copy of the GNU Lesser General Public
// License along with MassSpectrometry. If not, see <http://www.gnu.org/licenses/>.

using Chemistry;
using MzLibUtil;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MassSpectrometry
{
Expand Down
27 changes: 18 additions & 9 deletions mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,14 +480,17 @@ private bool Peak2satisfiesRatio(double peak1theorIntensity, double peak2theorIn

public static byte[] Get64Bitarray(IEnumerable<double> array)
{
var mem = new MemoryStream();
foreach (var okk in array)
using (var mem = new MemoryStream())
{
byte[] ok = BitConverter.GetBytes(okk);
mem.Write(ok, 0, ok.Length);
foreach (var okk in array)
{
byte[] ok = BitConverter.GetBytes(okk);
mem.Write(ok, 0, ok.Length);
}
mem.Position = 0;
var memory = mem.ToArray();
return memory;
}
mem.Position = 0;
return mem.ToArray();
}

public byte[] Get64BitYarray()
Expand All @@ -505,7 +508,9 @@ public override string ToString()
return string.Format("{0} (Peaks {1})", Range, Size);
}

public void XCorrPrePreprocessing(double scanRangeMinMz, double scanRangeMaxMz, double precursorMz, double precursorDiscardRange = 1.5, double discreteMassBin = 1.0005079, double minimumAllowedIntensityRatioToBasePeak = 0.05)
public void XCorrPrePreprocessing(double scanRangeMinMz, double scanRangeMaxMz,
double precursorMz, double precursorDiscardRange = 1.5,
double discreteMassBin = 1.0005079, double minimumAllowedIntensityRatioToBasePeak = 0.05)
{
//The discrete bin value 1.0005079 was from J. Proteome Res., 2018, 17 (11), pp 3644–3656

Expand Down Expand Up @@ -544,9 +549,13 @@ public void XCorrPrePreprocessing(double scanRangeMinMz, double scanRangeMaxMz,

//we've already filtered for when multiple mzs appear in a single nominal mass bin
int nominalWindowWidthDaltons = (int)(Math.Round((scanRangeMaxMz - scanRangeMinMz) / 10d, 0));
FilteringParams secondFilter = new FilteringParams(null, minimumAllowedIntensityRatioToBasePeak, nominalWindowWidthDaltons, null, true, false, false);

WindowModeHelper.Run(ref genericIntensityArray, ref genericMzArray, secondFilter, genericMzArray.Min(), genericMzArray.Max(), true);
FilteringParams secondFilter = new FilteringParams(null,
minimumAllowedIntensityRatioToBasePeak, nominalWindowWidthDaltons, null,
true, false, false);

WindowModeHelper.Run(ref genericIntensityArray, ref genericMzArray, secondFilter,
genericMzArray.Min(), genericMzArray.Max(), true);

Array.Sort(genericMzArray, genericIntensityArray);

Expand Down
6 changes: 3 additions & 3 deletions mzLib/Readers/Bruker/BrukerFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public override MsDataFile LoadAllStaticData(FilteringParams? filteringParams =
// close the file connection. At this point, you don't need to be connected to the sqlite database anymore. You have all the data
// you need.
CloseFileConnection();
Scans = scans.OrderBy(x => x.OneBasedScanNumber).ToArray();
Scans = scans.OrderBy(x => x.OneBasedScanNumber).ToArray();
SourceFile = GetSourceFile();
return this;
return this;
}

private const string nativeIdFormat = "scan number only nativeID format";
Expand Down Expand Up @@ -450,7 +450,7 @@ private void OpenFileConnection(string path)
_connection = new SQLiteConnection();
_connection.ConnectionString = "DataSource=" + sqlite_fn;
_connection.Open();
}
}
private void CloseFileConnection()
{
baf2sql_array_close_storage(_handle!.Value);
Expand Down
1 change: 1 addition & 0 deletions mzLib/Readers/MsDataFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Readers
{
public static class MsDataFileReader
{

public static MsDataFile GetDataFile(string filePath)
{
return filePath.ParseFileType() switch
Expand Down
27 changes: 12 additions & 15 deletions mzLib/Readers/MzML/Mzml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@

using MassSpectrometry;
using MzLibUtil;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using UsefulProteomicsDatabases;

Expand Down Expand Up @@ -161,20 +156,20 @@ public override MsDataFile LoadAllStaticData(FilteringParams filterParams = null
}

Loaders.LoadElements();
SourceFile = GetSourceFile();

SourceFile = GetSourceFile();

var numSpecta = _mzMLConnection.run.spectrumList.spectrum.Length;
MsDataScan[] scans = new MsDataScan[numSpecta];

Parallel.ForEach(Partitioner.Create(0, numSpecta), new ParallelOptions
{ MaxDegreeOfParallelism = maxThreads }, fff =>
Parallel.ForEach(Partitioner.Create(0, numSpecta), new ParallelOptions
{ MaxDegreeOfParallelism = maxThreads }, fff =>
{
for (int i = fff.Item1; i < fff.Item2; i++)
{
for (int i = fff.Item1; i < fff.Item2; i++)
{
scans[i] = GetMsDataOneBasedScanFromConnection(_mzMLConnection, i + 1, filterParams);
}
});
scans[i] = GetMsDataOneBasedScanFromConnection(_mzMLConnection, i + 1, filterParams);
}
});

scans = scans.Where(s => s.MassSpectrum != null).ToArray();

Expand Down Expand Up @@ -236,7 +231,9 @@ public override MsDataFile LoadAllStaticData(FilteringParams filterParams = null
public override SourceFile GetSourceFile()
{
SourceFile sourceFile;
if (_mzMLConnection.fileDescription.sourceFileList != null && _mzMLConnection.fileDescription.sourceFileList.sourceFile != null && _mzMLConnection.fileDescription.sourceFileList.sourceFile[0] != null && _mzMLConnection.fileDescription.sourceFileList.sourceFile[0].cvParam != null)
if (_mzMLConnection.fileDescription.sourceFileList != null && _mzMLConnection.fileDescription.sourceFileList.sourceFile
!= null && _mzMLConnection.fileDescription.sourceFileList.sourceFile[0] != null
&& _mzMLConnection.fileDescription.sourceFileList.sourceFile[0].cvParam != null)
{
var simpler = _mzMLConnection.fileDescription.sourceFileList.sourceFile[0];
string nativeIdFormat = null;
Expand Down
8 changes: 2 additions & 6 deletions mzLib/Readers/MzML/ReverseLineReader.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text;
using System.Threading.Tasks;

namespace Readers
{
Expand Down Expand Up @@ -266,5 +262,5 @@ IEnumerator IEnumerable.GetEnumerator()
}
}


}
Loading

0 comments on commit 732c003

Please sign in to comment.