public IList<PeakData> ImportFile(TextReader peakViewReader) { var peakDatas = new List<PeakData>(); var msFileNames = new List<string>(); var fileReader = new DsvFileReader(peakViewReader, TextUtil.SEPARATOR_TSV); Assert.AreEqual(fileReader.GetFieldIndex(PROTEIN), 0); Assert.AreEqual(fileReader.GetFieldIndex(PEPTIDE), 1); Assert.AreEqual(fileReader.GetFieldIndex(LABEL), 2); Assert.AreEqual(fileReader.GetFieldIndex(PRECURSOR), 3); Assert.AreEqual(fileReader.GetFieldIndex(CHARGE), 4); Assert.AreEqual(fileReader.GetFieldIndex(RT), 5); Assert.AreEqual(fileReader.GetFieldIndex(DECOY), 6); for (int i = 7; i < fileReader.NumberOfFields; ++i) { msFileNames.Add(fileReader.FieldNames[i]); } while (fileReader.ReadLine() != null) { string modifiedSequence = fileReader.GetFieldByName(PEPTIDE); string decoyString = fileReader.GetFieldByName(DECOY); bool decoy; Assert.IsTrue(bool.TryParse(decoyString, out decoy)); foreach (var msFileName in msFileNames) { string dataFieldString = fileReader.GetFieldByName(msFileName); double dataField; Assert.IsTrue(double.TryParse(dataFieldString, out dataField)); var peakData = new PeakData(dataField, modifiedSequence, msFileName, decoy); peakDatas.Add(peakData); } } return peakDatas; }
public static IList<Dictionary<string, string>> ReadCsvFile(DsvFileReader fileReader) { var result = new List<Dictionary<string, string>>(); while (null != fileReader.ReadLine()) { Dictionary<string, string> row = new Dictionary<string, string>(); for (int i = 0; i < fileReader.NumberOfFields; i++) { var value = fileReader.GetFieldByIndex(i); if (null != value) { row.Add(fileReader.FieldNames[i], value); } } result.Add(row); } return result; }
public static IDictionary<string, LinearFitResult> ReadExpectedResults(Type type, string resourceName) { var result = new Dictionary<string, LinearFitResult>(); using (var reader = GetTextReaderForManifestResource(type, resourceName)) { var csvReader = new DsvFileReader(reader, ','); while (null != csvReader.ReadLine()) { string protein = csvReader.GetFieldByName("Protein"); var linearFitResult = new LinearFitResult(Convert.ToDouble(csvReader.GetFieldByName("log2FC"), CultureInfo.InvariantCulture)) .SetStandardError(Convert.ToDouble(csvReader.GetFieldByName("SE"), CultureInfo.InvariantCulture)) .SetTValue(Convert.ToDouble(csvReader.GetFieldByName("Tvalue"), CultureInfo.InvariantCulture)) .SetDegreesOfFreedom(Convert.ToInt32(csvReader.GetFieldByName("DF"), CultureInfo.InvariantCulture)) .SetPValue(Convert.ToDouble(csvReader.GetFieldByName("pvalue"), CultureInfo.InvariantCulture)); result.Add(protein, linearFitResult); } } return result; }
public void DsvHeadersTest() { // test reading DSV files with and without headers string[] lines = {"dog,1.1,cat,2.2", "pony,3.3,fish,4.4"}; // Not L10N const char csvSeperator = ','; var withheaders = new DsvFileReader(new StringReader("0,1,2,3\n" + String.Join("\n", lines)), csvSeperator); // Not L10N var withoutheaders = new DsvFileReader(new StringReader(String.Join("\n", lines)), csvSeperator, hasHeaders: false); // Not L10N Assert.AreEqual(withheaders.NumberOfFields, withoutheaders.NumberOfFields); for (int h = 0; h < withoutheaders.NumberOfFields; h++) { var f = String.Format("{0}", h); // verify that a headerless CSV file will pretend to have a header of form "0,1,2,..." // Not L10N Assert.AreEqual(withheaders.GetFieldIndex(f), withoutheaders.GetFieldIndex(f)); } int linenumber = 0; while (true) { var fields = withoutheaders.ReadLine(); var fieldsTest = withheaders.ReadLine(); Assert.IsTrue((fields == null) == (fieldsTest == null)); if (fields == null) break; for (int f = 0; f < withoutheaders.NumberOfFields; f++) { Assert.AreEqual(fields[f], fieldsTest[f]); // quick string compare Assert.AreEqual(lines[linenumber].Split(csvSeperator)[f],fields[f]); } linenumber++; } Assert.AreEqual(2,linenumber); // let's also check the precision handling and field exception in AssertEx.FieldsEqual var A = String.Join("\n", lines).Replace("cat","hamster"); // change col 2, then ignore it // Not L10N var B = String.Join("\n", lines).Replace("1.1","1.09"); // add error at limits of precision, then allow for it // Not L10N AssertEx.FieldsEqual(A, B, 4, 2, true); }
public List<string> TranscribeAndModifyFile(StreamWriter writer, TextReader reader, List<string> fields, bool first, int currentFileCount) { var fileReader = new DsvFileReader(reader, SEPARATOR); if (first) { fields = fileReader.FieldNames; for (int i = 0; i < fields.Count; ++i) { if (i > 0) writer.Write(SEPARATOR); writer.WriteDsvField(fileReader.FieldNames[i], SEPARATOR); } writer.WriteLine(); } Assert.AreEqual(fileReader.NumberOfFields, fields.Count); for (int i = 0; i < fields.Count; ++i) { Assert.AreEqual(fileReader.FieldNames[i], fields[i]); } while (fileReader.ReadLine() != null) { for (int i = 0; i < fields.Count; ++i) { string modifiedField = fileReader.GetFieldByIndex(i); switch (fileReader.FieldNames[i]) { case FILE_NAME: modifiedField = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(modifiedField)) + MS_FILE_TYPE; break; case TRANSITION_GROUP: modifiedField = modifiedField + currentFileCount; break; case RUN_ID: modifiedField = currentFileCount.ToString(CultureInfo.CurrentCulture); break; } if (i > 0) writer.Write(SEPARATOR); writer.WriteDsvField(modifiedField, SEPARATOR); } writer.WriteLine(); } return fields; }