File: SparkKMer.java

package info (click to toggle)
sra-sdk 3.2.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296,076 kB
  • sloc: ansic: 532,876; cpp: 243,000; perl: 9,649; python: 8,978; sh: 7,888; java: 6,253; makefile: 1,148; yacc: 703; xml: 310; lex: 236
file content (137 lines) | stat: -rw-r--r-- 4,295 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//package examples;
//
import scala.Tuple2;
import org.apache.commons.lang.math.LongRange;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.PairFunction;



import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.List;

import ngs.ErrorMsg;
import ngs.ReadCollection;
import ngs.ReadIterator;
import ngs.Read;


/** 
 * Computes an approximation to pi
 * Usage: SparkKMer [slices] [kmer-size]
 */



public final class SparkKMer {

  public static void main(String[] args) throws Exception {
    //Setup
    SparkConf sparkConf = new SparkConf().setAppName("SparkKMer");
    JavaSparkContext jsc = new JavaSparkContext(sparkConf);
    //Agrument parsing
	if(args.length < 2) {
	  System.err.println("Usage: SparkKMer <accession> <kmer-length>");
      System.exit(1);
    }
	final String acc =  args[0];
	final int KMER_LENGTH = Integer.parseInt(args[1]);
	
    //Check accession and split
	ReadCollection run = gov.nih.nlm.ncbi.ngs.NGS.openReadCollection ( acc );
	long numreads = run.getReadCount ();

	//Slice the job
	int chunk = 20000; /** amount of reads per 1 map operation **/
    int slices = (int)(numreads / chunk / 1);
	if(slices==0) slices=1;
	List<LongRange> sub = new ArrayList<LongRange>();
	for(long first=1;first <= numreads;){
		long last= first+chunk-1;
		if(last > numreads) last=numreads;
		sub.add(new LongRange(first,last));
		first=last+1;
	}
	System.err.println("Prepared ranges: \n"+sub);

	JavaRDD<LongRange> jobs= jsc.parallelize(sub, slices);
	//Map
	//
	JavaRDD<String> kmers = jobs.flatMap(new FlatMapFunction<LongRange, String>() {
	  ReadCollection  run=null;
	  @Override
      public Iterable<String> call(LongRange s) {
		//Executes on task nodes
		List<String>	ret = new ArrayList<String>();
		try {
			long	first = s.getMinimumLong();
			long	last  = s.getMaximumLong();
			if(run==null) {
				run = gov.nih.nlm.ncbi.ngs.NGS.openReadCollection ( acc ); 
			}
			ReadIterator it = run.getReadRange ( first, last-first+1, Read.all );
			while(it.nextRead ()){
				//iterate through fragments
				while ( it.nextFragment () ){
					String bases=it.getFragmentBases ();
					//iterate through kmers
					for(int i=0;i<bases.length()-KMER_LENGTH;i++){
						ret.add(bases.substring(i,i+KMER_LENGTH));
					}
				}
			}
		} catch ( ErrorMsg x ) {
            System.err.println ( x.toString () );
            x.printStackTrace ();
        }
		return ret;
	  }
    });
    //Initiate kmer counting;
    JavaPairRDD<String, Integer> kmer_ones = kmers.mapToPair(new PairFunction<String, String, Integer>() {
      @Override
      public Tuple2<String, Integer> call(String s) {
        return new Tuple2<String, Integer>(s, 1);
      }
    });
	//Reduce counts
	JavaPairRDD<String, Integer> counts = kmer_ones.reduceByKey(new Function2<Integer, Integer, Integer>() {
      @Override
      public Integer call(Integer i1, Integer i2) {
        return i1 + i2;
      }
    });
	//Collect the output
    List<Tuple2<String, Integer>> output = counts.collect();
    for (Tuple2<String,Integer> tuple : output) {
      System.out.println(tuple._1() + ": " + tuple._2());
    }
    jsc.stop();
  }
}