private void ReadFromFile(StreamReader file, IonTypeFactory ionTypeFactory) { var line = file.ReadLine(); while (line != null) { var parts = line.Split('\t').ToList(); var header = parts[0]; parts.RemoveAt(0); if (header == "MaxRanks") MaxRanks = Convert.ToInt32(parts[0]); else if (header == "Total") { if (MaxRanks < 0) throw new FormatException("Badly formatted rank data."); _rankTotals = new double[MaxRanks+1]; for (int i = 0; i < MaxRanks+1; i++) { _rankTotals[i] = Convert.ToInt32(parts[i]); } // reached end of rank table entry break; } else if (header == "Unexplained") {} else { if (MaxRanks < 0) throw new FormatException("Badly formatted rank data."); try { var ionType = ionTypeFactory.GetIonType(header); _rankTable.Add(ionType, new double[MaxRanks+1]); for (int i = 0; i < MaxRanks+1; i++) { _rankTable[ionType][i] = Convert.ToDouble(parts[i]); } } catch (KeyNotFoundException) { throw new FormatException("Invalid ion type: " + header); } } line = file.ReadLine(); } }
public void TestIonTypeGeneration() { var methodName = MethodBase.GetCurrentMethod().Name; ShowStarting(methodName); var ionTypeFactory = new IonTypeFactory(); int index = 0; foreach (var ionType in ionTypeFactory.GetAllKnownIonTypes()) { Console.WriteLine(++index + ": " + ionType); } var yIon = ionTypeFactory.GetIonType("y2-H2O"); Console.WriteLine(yIon.GetMz(0)); }
public void TestIonTypeFactory() { var methodName = MethodBase.GetCurrentMethod().Name; ShowStarting(methodName); const string sequenceStr = "PEPTIDE"; var aminoAcidSet = new AminoAcidSet(); var sequence = new Sequence(sequenceStr, aminoAcidSet); var compositionOfFirstPrefix = sequence.GetComposition(0, 2); Console.WriteLine("{0}\t{1}", compositionOfFirstPrefix, compositionOfFirstPrefix.Mass); var ionTypeFactory = new IonTypeFactory(new[] { BaseIonType.B, BaseIonType.Y }, new[] { NeutralLoss.NoLoss }, 10); var bIon = ionTypeFactory.GetIonType("b"); var yIon = ionTypeFactory.GetIonType("y2"); Console.WriteLine("{0}\t{1}\t{2}", bIon, bIon.OffsetComposition, bIon.OffsetComposition.Mass); Console.WriteLine("{0}\t{1}\t{2}", yIon, yIon.OffsetComposition, yIon.OffsetComposition.Mass); // Compute mass of y2 = AveragineMass(DE) + OffsetY var compositionOfSecondSuffix = sequence.GetComposition(sequenceStr.Length - 2, sequenceStr.Length); var y2Ion = yIon.GetIon(compositionOfSecondSuffix); Console.WriteLine("m/z of y++: {0}", y2Ion.GetMonoIsotopicMz()); }
private void ReadFromFile(string paramFile, Boolean verbose) { if (!File.Exists(paramFile)) return; var allIonTypes = new IonTypeFactory(); using (var reader = new JavaBinaryReader(File.Open(paramFile, FileMode.Open))) { // Version var version = reader.ReadInt32(); if(verbose) Console.WriteLine("Version: {0}", version); // Activation method var actMethod = new string(reader.ReadChars(reader.ReadByte())); // Instrument type var instType = new string(reader.ReadChars(reader.ReadByte())); // Enzyme var enzyme = new string(reader.ReadChars(reader.ReadByte())); // Protocol var len = reader.ReadByte(); var protocol = new string(reader.ReadChars(len)); if (verbose) Console.WriteLine("Spectral type: {0}_{1}_{2}{3}", actMethod, instType, enzyme, protocol.Length > 0 ? "_"+protocol : ""); // TODO: set up spectral type // Tolerance var isTolerancePpm = reader.ReadBoolean(); var mmeVal = reader.ReadSingle(); // Ignore mme //_mme = new Tolerance(mmeVal, isTolerancePpm ? ToleranceUnit.Ppm : ToleranceUnit.Da); // Deconvolution information _applyDeconvolution = reader.ReadBoolean(); _deconvolutionErrorToleranceDa = reader.ReadSingle(); if (verbose) Console.WriteLine("Apply deconvolution: {0}, Tolerance: {1}", _applyDeconvolution, _deconvolutionErrorToleranceDa); // Charge histogram if (verbose) Console.WriteLine("Charge Histogram"); var chargeHistSize = reader.ReadInt32(); _chargeHistogram = new Dictionary<int, int>(); for (var i = 0; i < chargeHistSize; i++) { var charge = reader.ReadInt32(); var numSpecs = reader.ReadInt32(); _chargeHistogram[charge] = numSpecs; if (verbose) Console.WriteLine(charge + "\t" + numSpecs); } // Partition information var numPartitions = reader.ReadInt32(); if (verbose) Console.WriteLine("Partition Information\t" + numPartitions); _numSegments = reader.ReadInt32(); _partitionSet = new SortedSet<Partition>(); for (var i = 0; i < numPartitions; i++) { var charge = reader.ReadInt32(); var neutralPeptideMass = reader.ReadSingle(); var segIndex = reader.ReadInt32(); _partitionSet.Add(new Partition(charge, neutralPeptideMass, segIndex)); if (verbose) Console.WriteLine("{0}\t{1}\t{2}", charge, neutralPeptideMass, segIndex); } // Precursor offset frequency function if (verbose) Console.WriteLine("Precursor Offset Frequency Function"); _precursorOffMap = new Dictionary<int, IList<PrecursorOffsetFrequency>>(); _numPrecursorOffsets = reader.ReadInt32(); for (var i = 0; i < _numPrecursorOffsets; i++) { var charge = reader.ReadInt32(); var reducedCharge = reader.ReadInt32(); var offset = reader.ReadSingle(); var isTolPpm = reader.ReadBoolean(); var tolVal = reader.ReadSingle(); var tolerance = new Tolerance(tolVal, isTolPpm ? ToleranceUnit.Ppm : ToleranceUnit.Da); var frequency = reader.ReadSingle(); IList<PrecursorOffsetFrequency> offList; if (!_precursorOffMap.TryGetValue(charge, out offList)) { offList = new List<PrecursorOffsetFrequency>(); _precursorOffMap[charge] = offList; } offList.Add(new PrecursorOffsetFrequency(reducedCharge, offset, frequency, tolerance)); if (verbose) Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", charge, reducedCharge, offset, tolerance, frequency); } // Fragment ion offset frequency function if (verbose) Console.WriteLine("Fragment Offset Frequency Function"); _fragmentOffMap = new Dictionary<Partition, IEnumerable<FragmentOffsetFrequency>>(); foreach (var partition in _partitionSet) { var numIons = reader.ReadInt32(); if (verbose) Console.WriteLine("Partition {0}\t{1}\t{2}\t{3}", partition.Charge, partition.SegmentIndex, partition.NeutralPeptideMass, numIons); var fragmentOff = new FragmentOffsetFrequency[numIons]; for (var ionIndex = 0; ionIndex < numIons; ionIndex++) { var isPrefix = reader.ReadBoolean(); var charge = reader.ReadInt32(); var offset = reader.ReadSingle(); var frequency = reader.ReadSingle(); fragmentOff[ionIndex] = new FragmentOffsetFrequency(allIonTypes.GetIonType(isPrefix, charge, offset), frequency); if(verbose) Console.WriteLine("{0}_{1}_{2}\t{3}\t{4}", isPrefix ? "P" : "S", charge, Math.Round(offset), frequency, offset); } _fragmentOffMap.Add(partition, fragmentOff); } //// TODO: determine fragment ions to be used for scoring //// Rank distributions //var maxRank = reader.ReadInt32(); //_rankDistTable = new Dictionary<Partition, Dictionary<IonType, float[]>>(); //for (var i = 0; i < numPartitions; i++) //{ // // repeat this for #Ions // for (var rank = 0; rank < maxRank + 1; rank++) // { // var frequency = reader.ReadSingle(); // } //} //// Error distributions //var errorScalingFactor = reader.ReadInt32(); //if (errorScalingFactor > 0) //{ // var numErrorBins = errorScalingFactor*2 + 1; // // Ion error distribution // for (var i = 0; i < numErrorBins; i++) // { // var ionErrorDist = reader.ReadSingle(); // } // // Noise error distribution // for (var i = 0; i < numErrorBins; i++) // { // var noiseErrorDist = reader.ReadSingle(); // } // // Ion existence table // for (var i = 0; i < 4; i++) // { // var ionExFreq = reader.ReadSingle(); // } //} //// Validation //var validationCode = reader.ReadInt32(); //if (validationCode != Int32.MaxValue) //{ // Console.WriteLine("Error while reading parameter file {0}", paramFile); // System.Environment.Exit(-1); // Error //} } }