From ebf9a72ce28b208fd512e7efb5a3851e8bf8bc76 Mon Sep 17 00:00:00 2001
From: arq5x <arq5x@virginia.edu>
Date: Sat, 1 Feb 2014 16:44:14 -0500
Subject: [PATCH] add a Context for Map tool.

---
 src/utils/Contexts/ContextMap.cpp | 121 ++++++++++++++++++++++++++++++
 src/utils/Contexts/ContextMap.h   |  39 ++++++++++
 2 files changed, 160 insertions(+)
 create mode 100644 src/utils/Contexts/ContextMap.cpp
 create mode 100644 src/utils/Contexts/ContextMap.h

diff --git a/src/utils/Contexts/ContextMap.cpp b/src/utils/Contexts/ContextMap.cpp
new file mode 100644
index 00000000..80fd2b7b
--- /dev/null
+++ b/src/utils/Contexts/ContextMap.cpp
@@ -0,0 +1,121 @@
+/*
+ * ContextMap.cpp
+ *
+ *  Created on: Jan 6, 2014
+ *      Author: arq5x
+ */
+
+#include "ContextMap.h"
+
+ContextMap::ContextMap()
+{
+	// map requires sorted input
+	setSortedInput(true);
+	setLeftJoin(true);
+
+	// default to BED score column
+	setColumn(5);
+	// default to "sum"
+	setColumnOperation("sum");
+	// default to "." as a NULL value
+	setNullValue('.');
+}
+
+ContextMap::~ContextMap()
+{
+
+}
+
+
+bool ContextMap::parseCmdArgs(int argc, char **argv, int skipFirstArgs) {
+	_argc = argc;
+	_argv = argv;
+	_skipFirstArgs = skipFirstArgs;
+	if (_argc < 2) {
+		setShowHelp(true);
+		return false;
+	}
+
+	setProgram(_programNames[argv[0]]);
+
+	_argsProcessed.resize(_argc - _skipFirstArgs, false);
+
+	for (_i=_skipFirstArgs; _i < argc; _i++) {
+		if (isUsed(_i - _skipFirstArgs)) {
+			continue;
+		}
+        else if (strcmp(_argv[_i], "-o") == 0) {
+			if (!handle_o()) return false;
+        }
+        else if (strcmp(_argv[_i], "-c") == 0) {
+			if (!handle_c()) return false;
+        }
+        else if (strcmp(_argv[_i], "-null") == 0) {
+			if (!handle_null()) return false;
+        }
+	}
+	return ContextIntersect::parseCmdArgs(argc, argv, _skipFirstArgs);
+}
+
+
+bool ContextMap::isValidState()
+{
+	if (!cmdArgsValid()) {
+		return false;
+	}
+
+	ContextIntersect::isValidState();
+
+    if (getDatabaseFileType() == FileRecordTypeChecker::BAM_FILE_TYPE) {
+         //throw Error
+        cerr << endl << "*****" 
+             << endl 
+             << "***** ERROR: BAM database file not currently supported for the map tool." 
+             << endl;
+        exit(1);
+    }
+	// TODO 
+	// enforce any specific checks for Map.
+
+	return ContextIntersect::isValidState();
+}
+
+
+// for map, -c is the column upon which to operate
+bool ContextMap::handle_c()
+{
+    if ((_i+1) < _argc) {
+        setColumn(atoi(_argv[_i + 1]));
+        markUsed(_i - _skipFirstArgs);
+        _i++;
+        markUsed(_i - _skipFirstArgs);
+    }
+    return true;
+}
+
+
+// for map, -o is the operation to apply to the column (-c)
+bool ContextMap::handle_o()
+{
+    if ((_i+1) < _argc) {
+        setColumnOperation(_argv[_i + 1]);
+        markUsed(_i - _skipFirstArgs);
+        _i++;
+        markUsed(_i - _skipFirstArgs);
+    }
+    return true;
+}
+
+
+// for map, -null is a NULL vakue assigned
+// when no overlaps are detected.
+bool ContextMap::handle_null()
+{
+    if ((_i+1) < _argc) {
+        setNullValue(_argv[_i + 1]);
+        markUsed(_i - _skipFirstArgs);
+        _i++;
+        markUsed(_i - _skipFirstArgs);
+    }
+    return true;
+}
diff --git a/src/utils/Contexts/ContextMap.h b/src/utils/Contexts/ContextMap.h
new file mode 100644
index 00000000..b8ee57fd
--- /dev/null
+++ b/src/utils/Contexts/ContextMap.h
@@ -0,0 +1,39 @@
+/*
+ * ContextMap.h
+ *
+ *  Created on: Jan 7, 2014
+ *      Author: arq5x
+ */
+
+#ifndef CONTEXTMAP_H_
+#define CONTEXTMAP_H_
+
+#include "ContextIntersect.h"
+
+class ContextMap : public ContextIntersect {
+public:
+	ContextMap();
+	virtual ~ContextMap();
+	virtual bool isValidState();
+
+	virtual bool parseCmdArgs(int argc, char **argv, int skipFirstArgs);
+
+	int getColumn() const { return _column; }
+	void setColumn(int column) { _column = column; }
+
+	const QuickString & getColumnOperation() const { return _columnOperation; }
+	void setColumnOperation(const QuickString & operation) { _columnOperation = operation; }
+
+	const QuickString & getNullValue() const { return _nullValue; }
+	void setNullValue(const QuickString & nullValue) { _nullValue = nullValue; }
+
+    virtual bool hasIntersectMethods() const { return true; }
+
+private:
+	virtual bool handle_c();
+	virtual bool handle_o();
+	virtual bool handle_null();
+
+};
+
+#endif /* CONTEXTMAP_H_ */
-- 
GitLab