練習問題の解答例_4

5'->3'側への伸長と、3'->5'側への伸長を試してみるバージョン

package train;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
 
public class Day4 {
 
	static int k=21;
 
	public static ArrayList<Map.Entry<String, Integer>> sortHash(HashMap<String, Integer> map){
		ArrayList<Map.Entry<String, Integer>> mapValuesList = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
 
		Collections.sort(mapValuesList, new Comparator<Map.Entry<String, Integer>>() {
			@Override
			public int compare(Map.Entry<String, Integer> entry1, Map.Entry<String, Integer> entry2) {
				return ((Integer) entry2.getValue()).compareTo((Integer) entry1.getValue());
			}
		});
		return mapValuesList;
	}
 
	public static String searchfor(HashMap<String, Integer> map, ArrayList<String> fin, String kstr){
 
    	String tempk2=kstr.substring(1,k);
    	String[] acgt={"A","C","G","T"};
    	int maxcov=0;
    	String maxbp="";
    	for(String a : acgt){
    		if(map.get(tempk2+a)!=null && map.get(tempk2+a)>maxcov){
    			maxbp=a;
    			maxcov=map.get(tempk2+a);
    		}
    	}
    	if(maxcov>0 && !fin.contains(tempk2+maxbp)){
			fin.add(tempk2+maxbp);
    		return maxbp+searchfor(map, fin, tempk2+maxbp);
    	}
		return "";
	}
 
	public static String searchback(HashMap<String, Integer> map, ArrayList<String> fin, String kstr){
 
    	String tempk2=kstr.substring(0,k-1);
    	String[] acgt={"A","C","G","T"};
    	int maxcov=0;
    	String maxbp="";
    	for(String a : acgt){
    		if(map.get(a+tempk2)!=null && map.get(a+tempk2)>maxcov){
    			maxbp=a;
    			maxcov=map.get(a+tempk2);
    		}
    	}
    	if(maxcov>0 && !fin.contains(maxbp+tempk2) ){
			fin.add(maxbp+tempk2);
    		return searchback(map, fin, maxbp+tempk2)+maxbp;
    	}
		return "";
	}
 
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
 
	HashMap<String, Integer> map = new HashMap<String, Integer>();
    	File file = new File("C:\\Users\\asakawa\\Desktop\\SRR987657.fastq");
    	BufferedReader br;
		int n=0;
		try {
			br = new BufferedReader(new FileReader(file));
	    	String str = br.readLine();
	    	while(str != null){
	        	n++;
	        	if(n%4==2){
	        		for(int i=0;i<=str.length()-k;i++){
	        			String tempk=str.substring(i, i+k);
	        			if(map.get(tempk)==null){
	        				map.put(tempk, 1);
	        			}else{
		        			map.put(tempk, map.get(tempk)+1);
	        			}
	        		}
	        	}
 
	        	if(n%400000==0){
	            	System.out.println(n/4+"リード読み込み完了");
	        	}
	        	str = br.readLine();
	    	}
	    	br.close();
	    	System.out.println(n/4+"リード読み込み完了");
 
	    	ArrayList<Map.Entry<String, Integer>> sortedlist=sortHash(map);
	    	ArrayList<String> fin=new ArrayList<String>();
 
	    	for(int i=0;i<sortedlist.size();i++){
		    	String tempk=sortedlist.get(i).getKey();
		    	if(!fin.contains(tempk)){
		    		fin.add(tempk);
		    		String tempstr = searchback(map,fin,tempk)+tempk+searchfor(map,fin,tempk);
		    		if(tempstr.length()>1000){
		    			System.out.println(">"+sortedlist.get(i).toString()+"_"+tempstr.length());
		    			System.out.println(tempstr);
		    		}
		    	}
	    	}
 
		} catch (Exception e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}
	}
}

さらに工夫すべき点としては、伸長するときに相補鎖を調べるとか、分岐しているような箇所は伸長せずに止めるとか、一定の出現頻度以下のk-merは無視するとか、色々と必要。


Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /home/webpark1634/www/yosh/lib/plugins/authplain/auth.php on line 441

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /home/webpark1634/www/yosh/inc/ErrorHandler.php on line 76