Skip to content

Commit

Permalink
Fixed a bunch of bugs. Filters now are called once for all categories…
Browse files Browse the repository at this point in the history
…. Added more program options for collate sub-command.
  • Loading branch information
Maozi Chen committed Sep 26, 2020
1 parent 4d1bff1 commit dfe1577
Show file tree
Hide file tree
Showing 18 changed files with 501 additions and 238 deletions.
384 changes: 278 additions & 106 deletions Actions/CollateAction.cs

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions Actions/SearchAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public int Setup(SearchOptions options)

if (patternName == null)
{
Console.Error.WriteLine($"ERROR: Cannot find pattern name in {options.Pattern.FullName}");
Logger.Error($"Cannot find pattern name in {options.Pattern.FullName}");
return 1;
}

Console.WriteLine($"Found pattern name {patternName}");
Logger.Info($"Found pattern name {patternName}");

// Try to find the pattern CSV files in the pattern directory
var patternCsvs = new Dictionary<Category, FileInfo>();
Expand All @@ -54,12 +54,12 @@ public int Setup(SearchOptions options)

if (patternCsvs.Count == 0)
{
Console.Error.WriteLine($"ERROR: No pattern found in {options.Pattern.FullName}");
Logger.Error($"No pattern found in {options.Pattern.FullName}");
return 2;
}

if (errorList.Count > 0)
Console.Error.WriteLine($"WARN: Some patterns are missing: {string.Join(", ", errorList)}");
Logger.Warning($"Some patterns are missing: {string.Join(", ", errorList)}");

// Finally, set the model for running this action
Parameters = new SearchParameters(
Expand All @@ -81,12 +81,13 @@ public int Run()
// Must have been set in the Setup method
Debug.Assert(Parameters != null);

Console.WriteLine($"Using {Parameters.Similarity.Type} similarity measure");
// Print out key parameters
Logger.Info($"Using {Parameters.Similarity.Type} similarity measure");

Parallel.ForEach(Parameters.PatternCsvs, o =>
{
var (category, patternFile) = o;
Console.WriteLine($"Searching in category {category}");
Logger.Info($"Searching in category {category}");

// Load the pattern vector for matching
var patternResDict = File.ReadAllLines(patternFile.FullName)
Expand Down Expand Up @@ -142,7 +143,7 @@ public int Run()
count++;
}

Console.WriteLine($"Generating top {Parameters.ResultCount} matches out of {count} {category} vectors");
Logger.Info($"Generating top {Parameters.ResultCount} matches out of {count} {category} vectors");

// Prepare the category specific directory for storing output
string categoryDir = Path.Combine(Parameters.OutputDir.FullName, category.ToString());
Expand Down Expand Up @@ -202,7 +203,7 @@ private static void CopyBestConformation(string inputPdbqt, string outputPdbqt,
int startIndex = text.IndexOf($"MODEL {confId,8}");
if (startIndex == -1)
{
Console.Error.WriteLine($"ERROR: Failed to find conformation {confId} in {inputPdbqt}");
Logger.Error($"Failed to find conformation {confId} in {inputPdbqt}");
return;
}

Expand Down
8 changes: 8 additions & 0 deletions Enums/RowOrdering.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace mccsx
{
internal enum RowOrdering
{
score,
sequence,
}
}
20 changes: 0 additions & 20 deletions Exceptions/FilterColumnException.cs

This file was deleted.

20 changes: 20 additions & 0 deletions Exceptions/FilterException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Runtime.Serialization;

namespace mccsx
{
[Serializable]
public class FilterException : Exception
{
public FilterException() { }
public FilterException(string message) : base(message) { }
public FilterException(string message, Exception inner) : base(message, inner) { }
public FilterException(string message, string filterName) : base(message) => FilterName = filterName;
public FilterException(string message, string filterName, Exception inner) : base(message, inner) => FilterName = filterName;
protected FilterException(
SerializationInfo info,
StreamingContext context) : base(info, context) { }

public string? FilterName { get; }
}
}
4 changes: 2 additions & 2 deletions Extensions/ExcelFluentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace mccsx.Extensions
{
public static class ExcelFluentExtensions
{
public static SpreadsheetDocument OpenXlsxFile(this string filepath)
public static SpreadsheetDocument OpenXlsxFile(this string filepath, bool forceOverwrite)
{
if (!File.Exists(filepath))
if (forceOverwrite || !File.Exists(filepath))
{
// Create a spreadsheet document by supplying the filepath.
// By default, AutoSave = true, Editable = true, and Type = xlsx.
Expand Down
2 changes: 1 addition & 1 deletion Extensions/MeasureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static IVectorDistanceMeasure SimilarityMeasure(this Measure measure)

public static IVectorDistanceMeasure DistanceMeasure(this Measure measure)
{
var simClassType = EnumAnnotationHelper<Measure>.GetAttribute<MeasureImplAttribute>(measure).SimilarityClass;
var simClassType = EnumAnnotationHelper<Measure>.GetAttribute<MeasureImplAttribute>(measure).DistanceClass;
var obj = Activator.CreateInstance(simClassType);
Debug.Assert(obj != null && obj is IVectorDistanceMeasure);
return (obj as IVectorDistanceMeasure)!;
Expand Down
5 changes: 3 additions & 2 deletions Filters/IndexFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ internal class IndexFilter

public IndexFilter(string[] lines)
{
Debug.Assert(lines.Length > 0);
if (lines.Length < 2)
throw new FilterException($"Insufficient lines", "index");

string[][] fieldLines = lines
.Select(o => o.Split(" \t".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
Expand All @@ -30,7 +31,7 @@ public IndexFilter(string[] lines)
}
else
{
throw new FilterColumnException("no enough columns");
throw new FilterException("Insufficient columns");
}
}

Expand Down
9 changes: 6 additions & 3 deletions Filters/StateFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ internal class StateFilter

public StateFilter(string[] lines)
{
Debug.Assert(lines.Length > 0);
if (lines.Length < 2)
throw new FilterException($"Insufficient lines", "state");

string[]? headers = lines[0].Split(" \t".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

Expand All @@ -38,6 +39,8 @@ public StateFilter(string[] lines)
if (nameEnd == 0)
{
int idx = line.IndexOf(' ');
if (idx == -1)
idx = line.IndexOf('\t');
if (idx != -1)
{
nameBegin = 0;
Expand All @@ -47,7 +50,7 @@ public StateFilter(string[] lines)

if (nameEnd == 0)
{
throw new FilterColumnException($"no enough columns on line: {line}");
throw new FilterException($"Insufficient columns on line: {line}");
}

_data[line[nameBegin..nameEnd]] = line[nameEnd..].Trim();
Expand All @@ -56,7 +59,7 @@ public StateFilter(string[] lines)
}
else
{
throw new FilterColumnException("no enough columns");
throw new FilterException("Insufficient columns");
}
}

Expand Down
26 changes: 26 additions & 0 deletions Helpers/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace mccsx.Helpers
{
internal static class Logger
{
public static void Info(string message)
{
Console.WriteLine(message);
}

public static void Warning(string message)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Error.WriteLine($"WARN: {message}");
Console.ResetColor();
}

public static void Error(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine($"ERROR: {message}");
Console.ResetColor();
}
}
}
39 changes: 25 additions & 14 deletions Models/InputVectors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public static InputVectors FromRawRecvData(
// Verify consistency of index names
foreach (var recv in vecData)
{
if (recv.IndexName == null)
if (indexName == null)
{
indexName = recv.IndexName;
}
else if (indexName != recv.IndexName)
{
errorMessages.Add($"index name '{recv.IndexName}' different from previous ones '{indexName}' for {recv.VectorName}");
errorMessages.Add($"Index name '{recv.IndexName}' different from previous ones '{indexName}' for {recv.VectorName}");
}
}

Expand All @@ -114,7 +114,7 @@ public static InputVectors FromRawRecvData(
var summaryFields = vecData.ToDictionary(o => o.VectorName, o => o.SummaryFields);

// Create the instance
return new InputVectors
var obj = new InputVectors
(
vecData.Select // Column vectors
(
Expand All @@ -137,6 +137,10 @@ public static InputVectors FromRawRecvData(
summaryFields, // Summary fields
errorMessages // Error messages
);

Debug.Assert(obj.ColumnCount == vecData.Count);

return obj;
}

private static void SummarizeResidues(
Expand Down Expand Up @@ -200,7 +204,7 @@ private static void SummarizeResidues(
residueInfoByIndex = chainDictByIndex.Keys.ToDictionary(idx => idx, idx => new ResidueInfo
(
string.Join('/', chainDictByIndex[idx].OrderBy(p => p)),
string.Join('/', resNameDictByIndex[idx].Select(p => p.GetShortName()).OrderBy(p => p)),
string.Join('/', resNameDictByIndex[idx].Select(p => p.GetShortName().ToUpper()).OrderBy(p => p)),
string.Join('/', resSeqDictByIndex[idx].OrderBy(p => p)),
resSeqDictByIndex[idx].Count == 1 ? resSeqDictByIndex[idx].First() : (int?)null,
idx,
Expand All @@ -211,7 +215,7 @@ private static void SummarizeResidues(
residueInfoBySeq = chainDictBySeq.Keys.ToDictionary(seq => seq, seq => new ResidueInfo
(
string.Join('/', chainDictBySeq[seq].OrderBy(p => p)),
string.Join('/', resNameDictBySeq[seq].Select(p => p.GetShortName()).OrderBy(p => p)),
string.Join('/', resNameDictBySeq[seq].Select(p => p.GetShortName().ToUpper()).OrderBy(p => p)),
seq.ToString(),
seq,
$"{string.Join('/', resNameDictBySeq[seq].Select(p => p.GetCode()).OrderBy(p => p))}{seq}",
Expand Down Expand Up @@ -266,16 +270,23 @@ public Rect GetFormattedReport(
}

// Header row 1
var headerRow1 = RawRecvData.CsvColumnHeaders
.Append(StateName)
.Concat(columnKeys)
IEnumerable<string?> headerRow1 = RawRecvData.CsvColumnHeaders;

if (IndexName != null)
headerRow1 = headerRow1.Append(IndexName);

headerRow1 = headerRow1.Concat(columnKeys)
.Concat(stateCounts.Select(o => $"Average({o.Count})"))
.Append($"Average({ColumnCount})");

// Header row 2
var headerRow2 = RawRecvData.CsvColumnHeaders
.Select(o => (string?)null)
.Append($"{StateName}->")
IEnumerable<string?> headerRow2 = RawRecvData.CsvColumnHeaders.SkipLast(1)
.Select(o => (string?)null);

if (IndexName != null)
headerRow2 = headerRow2.Append(null);

headerRow2 = headerRow2.Append($"{StateName}->")
.Concat(columnKeys.Select(o => GetColumn(o).Tag))
.Concat(stateCounts.Select(o => o.State))
.Append("All");
Expand Down Expand Up @@ -306,9 +317,9 @@ public Rect GetFormattedReport(
int width = left + ColumnCount + (StateName != null ? stateCounts.Length : 0);
int height = top + RowCount + RawRecvData.SummaryRowHeaders.Count;

// Build the data rows, in the ascending lexical ordering
// Build the data rows, in the ascending ordinal ordering
dataRows = Rows
.OrderBy(vec => vec.Name)
.OrderBy(vec => ResidueInfo[vec.Name].ResidueSeq)
.Select((vec, i) =>
{
var resInfo = ResidueInfo[vec.Name];
Expand Down Expand Up @@ -371,7 +382,7 @@ public Rect GetFormattedReport(
/// <returns></returns>
private static string GetRowAverage(int rowNum, int colNum, int cols)
{
return $"=AVERAGE({ExcelColumnHelper.ToCellRef(colNum, rowNum)}:{ExcelColumnHelper.ToCellRef(cols, rowNum + cols - 1)})";
return $"=AVERAGE({ExcelColumnHelper.ToCellRef(colNum, rowNum)}:{ExcelColumnHelper.ToCellRef(colNum + cols - 1, rowNum)})";
}

public IReadOnlyDictionary<string, string>? GetRowTags()
Expand Down
Loading

0 comments on commit dfe1577

Please sign in to comment.