8000 feat(ENGKNOW-2207) add EQUALLEN function by david-chambliss-gdx · Pull Request #40 · gorpipe/gor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(ENGKNOW-2207) add EQUALLEN function #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions documentation/src/command/FORMULAS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Type and String Conversion
+---------------------------------+----------------------------------------------------------------------------------+
| ``DECODE(str,str) : str`` | Decode (map) the string, given key-value mapping pairs. |
+---------------------------------+----------------------------------------------------------------------------------+
| ``MATCHLEN(str,str) : int`` | Count the length of matching substrings for the two strings |
+---------------------------------+----------------------------------------------------------------------------------+

Algebraic Functions
===================
Expand Down
24 changes: 24 additions & 0 deletions documentation/src/functions/matchlen.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.. _matchlen:

========
MATCHLEN
========

The **MATCHLEN** function is used to measure the length of the longest matching initial substrings between
the two supplied strings,
i.e., the number of characters from the start for which the two strings are identical.

Usage
=====

``MATCHLEN(string, string) : integer``

Example
=======
The following example creates columns called ``ref`` and ``alt`` with string values ``CCATGGA`` and ``CCAGC``,
and computes the length of the common prefix string, ``3``, the length of ``CCA``.

.. code-block:: gor

gorrow 1,1,1 | CALC ref 'CCATGGA' | CALC alt 'CCAGC' | calc prefixlen MATCHLEN(ref,alt)

14 changes: 14 additions & 0 deletions gortools/src/main/scala/gorsat/parser/StringFunctions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ object StringFunctions {
functions.register("CSLISTHASCOUNT", getSignatureStringString2Int(csListHasCount), csListHasCount _)
functions.register("REPLACE", getSignatureStringStringString2String(replace), replace _)
functions.register("LEN", getSignatureString2Int(len), len _)
functions.register("MATCHLEN", getSignatureStringString2Int(matchLen), matchLen _)
functions.register("LEFT", getSignatureStringInt2String(left), left _)
functions.register("RIGHT", getSignatureStringInt2String(right), right _)
functions.register("STR2LIST", getSignatureStringInt2String(str2List), str2List _)
Expand Down Expand Up @@ -159,6 +160,19 @@ object StringFunctions {
}
}

def matchLen(ex1: sFun, ex2: sFun): iFun = {
cvp => {
val s1 = ex1(cvp)
val s2 = ex2(cvp)
val minlen = math.min(s1.length, s2.length)
var pos = 0
while (pos < s1.length && pos < s2.length && s1(pos) == s2(pos)) {
pos += 1
}
pos
}
}

def csListHasCount(ex1: sFun, ex2: sFun): iFun = {
cvp => {
val tmp = ex1(cvp).split(",", -1)
Expand Down
20 changes: 15 additions & 5 deletions gortools/src/test/java/gorsat/parser/UTestBooleanFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,22 @@ public void testListHasCountWithStringLiterals() {
}

@Test
public void testCsListHasCountWithStringLiterals() {
TestUtils.assertCalculated("cslisthascount('a,b,c,d,e,f', 'a')", 1);
TestUtils.assertCalculated("cslisthascount('a,b,c,d,e,f', 'x')", 0);
TestUtils.assertCalculated("cslisthascount('a,b,c,d,e,f,a,b,c,a,b', 'a','b','c')", 3);
TestUtils.assertCalculated("cslisthascount('A,B,C,D,E,F,A,B,C,A,B', 'a','b','c')", 0);
public void testMatchLenWithStringLiterals() {
TestUtils.assertCalculated("matchlen('ACCTTG', 'A')", 1);
TestUtils.assertCalculated("matchlen('ACCTTG', 'ACCTA')", 4);
TestUtils.assertCalculated("matchlen('ACCTTG', 'ACCCTA')", 3);
TestUtils.assertCalculated("matchlen('ACCTTG', 'CCTTG')", 0);
TestUtils.assertCalculated("matchlen('A', 'ACCTTG')", 1);
TestUtils.assertCalculated("matchlen('ACCTA', 'ACCTTG')", 4);
TestUtils.assertCalculated("matchlen('ACCCTA', 'ACCTTG')", 3);
TestUtils.assertCalculated("matchlen('ACCCTA', 'ACCCTA')", 6);
TestUtils.assertCalculated("matchlen('xACCCTA', 'yACCCTA')", 0);
TestUtils.assertCalculated("matchlen('CCTTG', 'ACCTTG')", 0);
TestUtils.assertCalculated("matchlen('ACCTTG', '')", 0);
TestUtils.assertCalculated("matchlen('', 'A')", 0);
TestUtils.assertCalculated("matchlen('', '')", 0);
}

@Test
public void testCsListHasCount() {
// Need to work with rows here so we can do listhascount without using string literals
Expand Down
Loading
0