NextFlow-操作者(operator)

Nextflow 运算符是使您可以使用一些用户提供的规则将通道彼此连接或转换通道发出的值的方法。

运算符可以分为七个组:

  • 过滤算子
  • 变换算子
  • 分裂算子
  • 组合算子
  • 分叉算子
  • 数学运算符
  • 其他操作员

1 过滤算子(运算符)

给定一个通道,过滤运算符允许您仅选择符合给定规则的项目。

可用的过滤器运算符为:

  • distinct
  • filter
  • first
  • last
  • randomSample
  • take
  • unique
  • until

1.1 filter

该filter运算符允许你只得到通过满足条件并丢弃所有其他的通道发出的项。可以通过使用正则表达式,文字值,类型限定符(即Java类)或任何布尔谓词来指定过滤条件。

下面的示例显示如何使用正则表达式过滤通道,该正则表达式仅返回以开头的字符串a

Channel
    .from( 'a', 'b', 'aa', 'bc', 3, 4.5 )
    .filter( ~/^a.*/ )
    .subscribe { println it }
a
aa

下面的示例演示如何通过指定类型限定符来过滤通道,Number以便仅返回数字:

Channel
    .from( 'a', 'b', 'aa', 'bc', 3, 4.5 )
    .filter( Number )
    .subscribe { println it }
3
4.5

最后,可以使用任何布尔谓词来定义过滤条件。谓词由返回布尔值的闭包表示。例如,以下片段显示了如何过滤通道以发出数字,以便返回奇数:

Channel
    .from( 1, 2, 3, 4, 5 )
    .filter { it % 2 == 1 }
    .subscribe { println it }
1
3
5

1.2 unique

这个unique运算符允许您从通道中删除重复项,并且只发出单个项而不重复。

例如:

Channel
    .from( 1,1,1,5,7,7,7,3,3 )
    .unique()
    .subscribe { println it }
1
5
7
3

您还可以指定一个可选的闭包,以定制其区分唯一项目的方式。例如:

Channel
    .from(1,3,4,5)
    .unique { it % 2 }
    .subscribe { println it }
1
4

1.3 distinct

distinct操作符可用于删除连续从一个通道重复的项,以便每个发出的项与前面的项不同。例如:

Channel
    .from( 1,1,2,2,2,3,1,1,2,2,3 )
    .distinct()
    .subscribe onNext: { println it }, onComplete: { println 'Done' }
1
2
3
1
2
3
Done

您还可以指定一个可选的闭包,以定制其区分不同项目的方式。例如:

Channel
    .from( 1,1,2,2,2,3,1,1,2,4,6 )
    .distinct { it % 2 }
    .subscribe onNext: { println it }, onComplete: { println 'Done' }
1
2
3
2
Done

1.4 first

这个first运算符创建一个通道,该通道返回源通道发出的第一个项,或者最终返回与可选条件匹配的第一个项。可以使用正则表达式、Java类型或任何布尔值谓词。例如:

// no condition is specified, emits the very first item: 1
Channel
    .from( 1, 2, 3 )
    .first()
    .subscribe { println it }


// emits the first String value: 'a'
Channel
    .from( 1, 2, 'a', 'b', 3 )
    .first( String )
    .subscribe { println it }

// emits the first item matching the regular expression: 'aa'
Channel
    .from( 'a', 'aa', 'aaa' )
    .first( ~/aa.*/ )
    .subscribe { println it }

// emits the first item for which the predicate evaluates to true: 4
Channel
    .from( 1,2,3,4,5 )
    .first { it > 3 }
    .subscribe { println it }

1.5 randomSample(随机样本)

randomSample运算符允许您创建一个通道,该通道从指定的通道中随机抽取指定数量的项目。例如:

Channel
      .from( 1..100 )
      .randomSample( 10 )
      .println()

上面的代码片段将打印1至100范围内的10个数字。

运算符支持第二个参数,该参数允许设置随机数生成器的初始种子(seed)。通过设置它,randomSample运算符将始终返回相同的伪随机序列。例如:

Channel
      .from( 1..100 )
      .randomSample( 10, 234 )
      .println()

上面的示例将在10到100之间的范围内打印10个随机数。在每次运行脚本时,将返回相同的序列。

1.6 take

take运算符允许您仅过滤通道发出的前n个项。例如:

Channel
    .from( 1,2,3,4,5,6 )
    .take( 3 )
    .subscribe onNext: { println it }, onComplete: { println 'Done' }
1
2
3
Done

1.7 last

所述last操作符将创建一个通道,只有返回由源通道的最后一个项。例如:

Channel
    .from( 1,2,3,4,5,6 )
    .last()
    .subscribe { println it }
6

1.8 until

这个until运算符创建一个通道,该通道返回源通道发出的项,并在验证指定条件时停止。例如:

Channel
    .from( 3,2,1,5,1,5 )
    .until{ it==5 }
    .println()
3
2
1

由此可以看出,算子是数据具体转换的模块!

参考资料:https://www.nextflow.io/docs/latest/operator.html#filtering-operators

发布了115 篇原创文章 · 获赞 4 · 访问量 4612

猜你喜欢

转载自blog.csdn.net/weixin_43999327/article/details/104211055