MapReduce之简单K-mer计数

MapReduce之简单K-mer计数

什么是K-mer计数

K-mer是一个长度为 K ( K > 0 ) K(K>0) K(K>0)的子串,K-mer计数是指整个序列中K-mer出现的频度

K-mer计数应用

DNA序列中的K-mer计数在和多生物信息中都是一个非常重要的步骤,主要如下:

  • 确定序列读取之间的偏差是测序错误还是序列的基因差异
  • 修正短读组装错误
  • 计算亲缘度和特异度等参数
MapReduce解决方案
输入数据
@EAS54_6_R1_2_1_413_324
CCCTTCTTGTCCCCAGCGTTTCTCC
+
;;3;;;;;;;;;;;;7;;;;;;;88
@EAS54_6_R1_2_1_540_792
TTGGCAGGCCAAGGCCGATGGATCA
+
;;;;;;;;;;;7;;;;;-;;;3;83
@EAS54_6_R1_2_1_443_348
GTTGCTTCTGGCGTGGGTGGGGGGG
+EAS54_6_R1_2_1_443_348
;;;;;;;;;;;9;;;;.7;393333
@EAS54_6_R1_2_1_413_324
CCCCCCTTGTCTTCAGCCCTTCTCC
+
;;3;;;;;;;;;;;;7;;;;;;;88
@EAS54_6_R1_2_1_540_792
TTTTCAGGCCAAGGCCGATGGATCA
+
;;;;;;;;;;;7;;;;;-;;;3;83
@EAS54_6_R1_2_1_443_348
GTTGTTTCTGGCGTGGGTGGGGGGG
+EAS54_6_R1_2_1_443_348
;;;;;;;;;;;9;7;;.7;393333
@EAS54_6_R1_2_1_443_348
GTTGTTTCTGGCGTGGGTGGCCCCC
+EAS54_6_R1_2_1_443_348
;;;;;;;;;;;9;7;;.7;393333
map阶段任务

假如输入序列为CACACACAGT,且K=3,要统计3-mers个数,则会生成如下键-值对:
CAC 1
ACA 1
CAC 1
ACA 1
CAC 1
ACA 1
CAG 1
AGT 1

map阶段编码
package com.deng.Kmer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class KmerCountMapper extends Mapper<LongWritable,Text, Text, IntWritable> {
    private final static IntWritable ONE=new IntWritable(1);
    private int k;
    private final Text kmerKey=new Text();

    public void setup(Context context){
        Configuration conf=context.getConfiguration();
        this.k=conf.getInt("k.mer",3);
    }

    public void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException {
        String sequence=value.toString();
        for(int i=0;i<sequence.length()-k+1;i++){
            //如果这一行不存在DNA,跳过
            if(!Character.isLetter(sequence.charAt(0))) continue;
            String kmer=sequence.substring(i,k+i);
            kmerKey.set(kmer);
            context.write(kmerKey,ONE);
        }
    }
}

reducer阶段任务

对相同的键求和,如下
reducer输入
CAC 1
ACA 1
CAC 1
ACA 1
CAC 1
ACA 1
CAG 1
AGT 1
reducer输出
ACA 3
CAC 3
CAG 1
AGT 1

reducer阶段编码
package com.deng.Kmer;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class KmerCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum = 0;
        for(IntWritable value:values){
            sum+=value.get();
        }
        context.write(key,new IntWritable(sum));
    }
}

驱动程序如下
package com.deng.Kmer;

import com.deng.util.FileUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class KmerCountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        FileUtil.deleteDirs("output");
        Configuration conf=new Configuration();
        String[] otherArgs=new String[]{"input/k_mer.txt","output"};
        Job job=new Job(conf,"Kmer");
        job.setJarByClass(KmerCountDriver.class);
        job.setJobName("KmerCount");
        job.setMapperClass(KmerCountMapper.class);
        job.setReducerClass(KmerCountReducer.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setMapOutputKeyClass(Text.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job,new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));
        System.exit(job.waitForCompletion(true)?0:1);
    }
}

运行结果如下

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值