//Throws DatabaseOpeningException public static IrtDb GetIrtDb(string path, IProgressMonitor loadMonitor) { var status = new ProgressStatus(string.Format(Resources.IrtDb_GetIrtDb_Loading_iRT_database__0_, path)); if (loadMonitor != null) loadMonitor.UpdateProgress(status); try { if (path == null) throw new DatabaseOpeningException(Resources.IrtDb_GetIrtDb_Database_path_cannot_be_null); if (!File.Exists(path)) throw new DatabaseOpeningException(String.Format(Resources.IrtDb_GetIrtDb_The_file__0__does_not_exist_, path)); string message; Exception xInner = null; try { //Check for a valid SQLite file and that it has our schema //Allow only one thread at a time to read from the same path using (var sessionFactory = GetSessionFactory(path)) { lock (sessionFactory) { return new IrtDb(path, sessionFactory).Load(loadMonitor, status); } } } catch (UnauthorizedAccessException x) { message = string.Format(Resources.IrtDb_GetIrtDb_You_do_not_have_privileges_to_access_the_file__0_, path); xInner = x; } catch (DirectoryNotFoundException x) { message = string.Format(Resources.IrtDb_GetIrtDb_The_path_containing__0__does_not_exist, path); xInner = x; } catch (FileNotFoundException x) { message = string.Format(Resources.IrtDb_GetIrtDb_The_file__0__could_not_be_created_Perhaps_you_do_not_have_sufficient_privileges, path); xInner = x; } catch (SQLiteException x) { message = string.Format(Resources.IrtDb_GetIrtDb_The_file__0__is_not_a_valid_iRT_database_file, path); xInner = x; } catch (Exception x) { message = string.Format(Resources.IrtDb_GetIrtDb_The_file__0__could_not_be_opened, path); xInner = x; } throw new DatabaseOpeningException(message, xInner); } catch (DatabaseOpeningException x) { if (loadMonitor == null) throw; loadMonitor.UpdateProgress(status.ChangeErrorException(x)); return null; } }
//Throws DatabaseOpeningException public static OptimizationDb GetOptimizationDb(string path, IProgressMonitor loadMonitor, SrmDocument document) { var status = new ProgressStatus(string.Format(Resources.OptimizationDb_GetOptimizationDb_Loading_optimization_library__0_, path)); if (loadMonitor != null) loadMonitor.UpdateProgress(status); try { if (path == null) throw new OptimizationsOpeningException(Resources.OptimizationDb_GetOptimizationDb_Library_path_cannot_be_null_); if (!File.Exists(path)) throw new OptimizationsOpeningException(String.Format(Resources.OptimizationDb_GetOptimizationDb_The_file__0__does_not_exist_, path)); string message; try { //Check for a valid SQLite file and that it has our schema //Allow only one thread at a time to read from the same path using (var sessionFactory = GetSessionFactory(path)) { lock (sessionFactory) { return new OptimizationDb(path, sessionFactory).Load(loadMonitor, status); } } } catch (UnauthorizedAccessException) { message = string.Format(Resources.OptimizationDb_GetOptimizationDb_You_do_not_have_privilieges_to_access_the_file__0__, path); } catch (DirectoryNotFoundException) { message = string.Format(Resources.OptimizationDb_GetOptimizationDb_The_path_containing__0__does_not_exist_, path); } catch (FileNotFoundException) { message = string.Format(Resources.OptimizationDb_GetOptimizationDb_The_file__0__could_not_be_created__Perhaps_you_do_not_have_sufficient_privileges_, path); } catch (SQLiteException) { message = string.Format(Resources.OptimizationDb_GetOptimizationDb_The_file__0__is_not_a_valid_optimization_library_file_, path); } catch (GenericADOException) { try { return ConvertFromOldFormat(path, loadMonitor, status, document); } catch (Exception e) { message = string.Format(Resources.OptimizationDb_GetOptimizationDb_The_file__0__could_not_be_opened__conversion_from_old_format_failed____1_, path, e.Message); } } catch (Exception e) { message = string.Format(Resources.OptimizationDb_GetOptimizationDb_The_file__0__could_not_be_opened___1_, path, e.Message); } throw new OptimizationsOpeningException(message); } catch (OptimizationsOpeningException x) { if (loadMonitor == null) throw; loadMonitor.UpdateProgress(status.ChangeErrorException(x)); return null; } }
// Throws DatabaseOpeningException public static IonMobilityDb GetIonMobilityDb(string path, IProgressMonitor loadMonitor) { var status = new ProgressStatus(string.Format(Resources.IonMobilityDb_GetIonMobilityDb_Loading_ion_mobility_library__0_, path)); if (loadMonitor != null) loadMonitor.UpdateProgress(status); try { if (String.IsNullOrEmpty(path)) throw new DatabaseOpeningException(Resources.IonMobilityDb_GetIonMobilityDb_Please_provide_a_path_to_an_existing_ion_mobility_library_); if (!File.Exists(path)) throw new DatabaseOpeningException( string.Format( Resources.IonMobilityDb_GetIonMobilityDb_The_ion_mobility_library_file__0__could_not_be_found__Perhaps_you_did_not_have_sufficient_privileges_to_create_it_, path)); string message; try { //Check for a valid SQLite file and that it has our schema //Allow only one thread at a time to read from the same path using (var sessionFactory = GetSessionFactory(path)) { lock (sessionFactory) { return new IonMobilityDb(path, sessionFactory).Load(loadMonitor, status); } } } catch (UnauthorizedAccessException) { message = string.Format(Resources.IonMobilityDb_GetIonMobilityDb_You_do_not_have_privileges_to_access_the_ion_mobility_library_file__0_, path); } catch (DirectoryNotFoundException) { message = string.Format(Resources.IonMobilityDb_GetIonMobilityDb_The_path_containing_ion_mobility_library__0__does_not_exist_, path); } catch (FileNotFoundException) { message = string.Format(Resources.IonMobilityDb_GetIonMobilityDb_The_ion_mobility_library_file__0__could_not_be_found__Perhaps_you_did_not_have_sufficient_privileges_to_create_it_, path); } catch (Exception) // SQLiteException is already something of a catch-all, just lump it with the others here { message = string.Format(Resources.IonMobilityDb_GetIonMobilityDb_The_file__0__is_not_a_valid_ion_mobility_library_file_, path); } throw new DatabaseOpeningException(message); } catch (DatabaseOpeningException x) { if (loadMonitor == null) throw; loadMonitor.UpdateProgress(status.ChangeErrorException(x)); return null; } }
protected override bool LoadBackground(IDocumentContainer container, SrmDocument document, SrmDocument docCurrent) { // Only allow one background proteome to load at a time. This can // get tricky, if the user performs an undo and then a redo across // a change in background proteome. // Our first priority is doing the digestions, the second is accessing web // services to add missing protein metadata. lock (_lockLoadBackgroundProteome) { BackgroundProteome originalBackgroundProteome = GetBackgroundProteome(docCurrent); // Check to see whether the Digestion already exists but has not been queried yet. BackgroundProteome backgroundProteomeWithDigestions = new BackgroundProteome(originalBackgroundProteome, true); if (IsNotLoadedExplained(docCurrent, backgroundProteomeWithDigestions, true) == null) { // digest is ready, and protein metdata is resolved CompleteProcessing(container, backgroundProteomeWithDigestions); return true; } // are we here to do the digest, or to resolve the protein metadata? bool getMetadata = (IsNotLoadedExplained(docCurrent, backgroundProteomeWithDigestions, false) == null) && backgroundProteomeWithDigestions.NeedsProteinMetadataSearch; string name = originalBackgroundProteome.Name; ProgressStatus progressStatus = new ProgressStatus(string.Format(getMetadata?Resources.BackgroundProteomeManager_LoadBackground_Resolving_protein_details_for__0__proteome:Resources.BackgroundProteomeManager_LoadBackground_Digesting__0__proteome, name)); try { using (FileSaver fs = new FileSaver(originalBackgroundProteome.DatabasePath, StreamManager)) { File.Copy(originalBackgroundProteome.DatabasePath, fs.SafeName, true); var digestHelper = new DigestHelper(this, container, docCurrent, name, fs.SafeName, true); bool success; if (getMetadata) success = digestHelper.LookupProteinMetadata(ref progressStatus); else success = (digestHelper.Digest(ref progressStatus) != null); if (!success) { // Processing was canceled EndProcessing(docCurrent); UpdateProgress(progressStatus.Cancel()); return false; } using (var proteomeDb = ProteomeDb.OpenProteomeDb(originalBackgroundProteome.DatabasePath)) { proteomeDb.DatabaseLock.AcquireWriterLock(int.MaxValue); try { if (!fs.Commit()) { EndProcessing(docCurrent); throw new IOException( string.Format( Resources .BackgroundProteomeManager_LoadBackground_Unable_to_rename_temporary_file_to__0__, fs.RealName)); } } finally { proteomeDb.DatabaseLock.ReleaseWriterLock(); } } CompleteProcessing(container, new BackgroundProteome(originalBackgroundProteome, true)); UpdateProgress(progressStatus.Complete()); return true; } } catch (Exception x) { var message = new StringBuilder(); message.AppendLine( string.Format(Resources.BackgroundProteomeManager_LoadBackground_Failed_updating_background_proteome__0__, name)); message.Append(x.Message); UpdateProgress(progressStatus.ChangeErrorException(new IOException(message.ToString(), x))); return false; } } }
// ReSharper restore UnusedMember.Local private bool Load(ILoadMonitor loader) { ProgressStatus status = new ProgressStatus(string.Format(Resources.BiblioSpecLibrary_Load_Loading__0__library, Path.GetFileName(FilePath))); loader.UpdateProgress(status); long lenRead = 0; // AdlerChecksum checksum = new AdlerChecksum(); try { // Use a buffered stream for initial read BufferedStream stream = new BufferedStream(CreateStream(loader), 32 * 1024); int countHeader = (int) LibHeaders.count*4; byte[] libHeader = new byte[countHeader]; if (stream.Read(libHeader, 0, countHeader) != countHeader) throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_library_header_File_may_be_corrupted); lenRead += countHeader; // Check the first byte of the primary version number to determine // whether the format is little- or big-endian. Little-endian will // have the version number in this byte, while big-endian will have zero. if (libHeader[(int) LibHeaders.version1 * 4] == 0) _bigEndian = true; int numSpectra = GetInt32(libHeader, (int) LibHeaders.num_spectra); var dictLibrary = new Dictionary<LibKey, BiblioSpectrumInfo>(numSpectra); var setSequences = new HashSet<LibSeqKey>(); string revStr = string.Format("{0}.{1}", // Not L10N GetInt32(libHeader, (int) LibHeaders.version1), GetInt32(libHeader, (int) LibHeaders.version2)); Revision = float.Parse(revStr, CultureInfo.InvariantCulture); // checksum.MakeForBuff(libHeader, AdlerChecksum.ADLER_START); countHeader = (int) SpectrumHeaders.count*4; byte[] specHeader = new byte[1024]; byte[] specSequence = new byte[1024]; for (int i = 0; i < numSpectra; i++) { int percent = i * 100 / numSpectra; if (status.PercentComplete != percent) { // Check for cancellation after each integer change in percent loaded. if (loader.IsCanceled) { loader.UpdateProgress(status.Cancel()); return false; } // If not cancelled, update progress. loader.UpdateProgress(status = status.ChangePercentComplete(percent)); } // Read spectrum header int bytesRead = stream.Read(specHeader, 0, countHeader); if (bytesRead != countHeader) throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_spectrum_header_File_may_be_corrupted); // If this is the first header, and the sequence length is zero, // then this is a Linux format library. Switch to linux format, // and start over. if (i == 0 && GetInt32(specHeader, (int)SpectrumHeaders.seq_len) == 0) { _linuxFormat = true; stream.Seek(lenRead, SeekOrigin.Begin); // Re-ead spectrum header countHeader = (int)SpectrumHeadersLinux.count * 4; bytesRead = stream.Read(specHeader, 0, countHeader); if (bytesRead != countHeader) throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_spectrum_header_File_may_be_corrupted); } lenRead += bytesRead; // checksum.MakeForBuff(specHeader, checksum.ChecksumValue); int charge = GetInt32(specHeader, (int)SpectrumHeaders.charge); if (charge > TransitionGroup.MAX_PRECURSOR_CHARGE) throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Invalid_precursor_charge_found_File_may_be_corrupted); int numPeaks = GetInt32(specHeader, (int)SpectrumHeaders.num_peaks); int seqLength = GetInt32(specHeader, (_linuxFormat ? (int)SpectrumHeadersLinux.seq_len : (int)SpectrumHeaders.seq_len)); int copies = GetInt32(specHeader, (_linuxFormat ? (int)SpectrumHeadersLinux.copies : (int)SpectrumHeaders.copies)); // Read sequence information int countSeq = (seqLength + 1)*2; if (stream.Read(specSequence, 0, countSeq) != countSeq) throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_spectrum_sequence_File_may_be_corrupted); lenRead += countSeq; // checksum.MakeForBuff(specSequence, checksum.ChecksumValue); // Store in dictionary if (IsUnmodified(specSequence, seqLength + 1, seqLength)) { // These libraries should not have duplicates, but just in case. // CONSIDER: Emit error about redundancy? // These legacy libraries assume [+57.0] modified Cysteine LibKey key = new LibKey(GetCModified(specSequence, ref seqLength), 0, seqLength, charge); if (!dictLibrary.ContainsKey(key)) dictLibrary.Add(key, new BiblioSpectrumInfo((short)copies, (short)numPeaks, lenRead)); setSequences.Add(new LibSeqKey(key)); } // Read over peaks int countPeaks = 2*sizeof(Single)*numPeaks; stream.Seek(countPeaks, SeekOrigin.Current); // Skip spectrum lenRead += countPeaks; // checksum.MakeForBuff(specPeaks, checksum.ChecksumValue); } // Checksum = checksum.ChecksumValue; _dictLibrary = dictLibrary; _setSequences = setSequences; loader.UpdateProgress(status.Complete()); return true; } catch (InvalidDataException x) { loader.UpdateProgress(status.ChangeErrorException(x)); return false; } catch (IOException x) { loader.UpdateProgress(status.ChangeErrorException(x)); return false; } catch (Exception x) { x = new Exception(string.Format(Resources.BiblioSpecLibrary_Load_Failed_loading_library__0__, FilePath), x); loader.UpdateProgress(status.ChangeErrorException(x)); return false; } finally { if (ReadStream != null) { // Close the read stream to ensure we never leak it. // This only costs on extra open, the first time the // active document tries to read. try { ReadStream.CloseStream(); } catch(IOException) {} } } }
public bool BuildLibrary(IProgressMonitor progress) { _ambiguousMatches = null; ProgressStatus status = new ProgressStatus(Resources.BiblioSpecLiteBuilder_BuildLibrary_Preparing_to_build_library); progress.UpdateProgress(status); if (InputFiles.Any(f => f.EndsWith(EXT_PILOT))) { try { InputFiles = VendorIssueHelper.ConvertPilotFiles(InputFiles, progress, status); if (progress.IsCanceled) return false; } catch (Exception x) { progress.UpdateProgress(status.ChangeErrorException(x)); return false; } } string message = string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Building__0__library, Path.GetFileName(OutputPath)); progress.UpdateProgress(status = status.ChangeMessage(message)); string redundantLibrary = BiblioSpecLiteSpec.GetRedundantName(OutputPath); var blibBuilder = new BlibBuild(redundantLibrary, InputFiles, TargetSequences) { Authority = Authority, IncludeAmbiguousMatches = IncludeAmbiguousMatches, CutOffScore = CutOffScore, Id = Id, }; try { if (!blibBuilder.BuildLibrary(Action, progress, ref status, out _ambiguousMatches)) { return false; } } catch (IOException x) { progress.UpdateProgress(status.ChangeErrorException(x)); return false; } catch (Exception x) { Console.WriteLine(x.Message); progress.UpdateProgress(status.ChangeErrorException( new Exception(string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Failed_trying_to_build_the_redundant_library__0__, redundantLibrary)))); return false; } var blibFilter = new BlibFilter(); status = new ProgressStatus(message); progress.UpdateProgress(status); // Write the non-redundant library to a temporary file first try { using (var saver = new FileSaver(OutputPath)) { if (!blibFilter.Filter(redundantLibrary, saver.SafeName, progress, ref status)) { return false; } saver.Commit(); } } catch (IOException x) { progress.UpdateProgress(status.ChangeErrorException(x)); return false; } catch { progress.UpdateProgress(status.ChangeErrorException( new Exception(string.Format(Resources.BiblioSpecLiteBuilder_BuildLibrary_Failed_trying_to_build_the_library__0__, OutputPath)))); return false; } finally { if (!KeepRedundant) FileEx.SafeDelete(redundantLibrary, true); } return true; }