JAVA 多线程解决复杂度较高的算法问题

//再求基于GOTerm的基因相似度算法的时候 使用JAVA多线程开启20个线程

package com.txt;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.*;


public class genesSimilarityF {

	public static double genePairSimilarity(String gene1, String gene2,
		List<String> allgene, List<String> allgo,List<String> GO,List<String> GOTime) throws IOException {
		
		List<String> term1 = findgenesGo(gene1, allgene, allgo);
		List<String> term2 = findgenesGo(gene2, allgene, allgo);
		List<String> goterm = findSame(term1, term2);
		double result = genesSimilarity(goterm,allgene,allgo,GO,GOTime);
		return result;
	}

	public static List<String> findSame(List<String> term1, List<String> term2) {
		List<String> same = new ArrayList<String>();
		for (String s1 : term1) {
			for (String s2 : term2) {
				if (s1.equals(s2) || s1 == s2) {
					same.add(s2);
				}
			}
		}
		return same;
	}

	public static List<String> findgenesGo(String gene, List<String> allgene,
			List<String> allgo) {
		List<String> term1 = new ArrayList<String>();
		for (int i = 0; i < allgene.size(); i++) {
			String s = allgene.get(i);
			if (gene.equals(s) || gene == s) {
				term1.add(allgo.get(i));
			}
		}
		return term1;
	}


	public static int Count(String in, List<String> Go,List<String> GoTime) {
		int count = 0;
		for (int i =0;i<Go.size();i++) {
			if (in.equals(Go.get(i)) || in == Go.get(i)) {
				count = Integer.parseInt(GoTime.get(i)); 
			}
		}
		return count;
	}

	public static double genesSimilarity(List<String>Go, List<String> allgene,
			List<String> allgo,List<String> GO,List<String> GOTime) throws IOException {
		if (Go.size() == 0) {
			return 0.0;
		}
		
		int[] count = new int[Go.size()];
		int i = 0;
		for (String go : Go) {

			count[i++] = Count(go,GO,GOTime);
		}

		int min = count[0];

		for (int j = 1; j < count.length; j++) {
			if (count[j] < min) {
				min = count[j];
			}
		}

		double result = 0.0;

		DecimalFormat df = new DecimalFormat();
		df.setMaximumFractionDigits(5);
		result = 2.0 / (double) min;
		return result;
	}

	public static String[] cutString(String in) {

		return in.split("	");
	}

	public static HashSet<String> getAllGenes(String disease) {
		String realFile = "GeneSet\\" + disease;
		HashSet<String> genes = new HashSet<String>();
		try {
			BufferedReader br = new BufferedReader(new FileReader(new File(
					realFile)));

			String line = br.readLine();
			while (line != null) {
				genes.add(line.trim());
				line = br.readLine();
			}
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
		return genes;
	}

	public static void diseaseSimilarity(String s1, String s2,
			HashSet<String> genes1, HashSet<String> genes2,
			List<String> ALLGene, List<String> AllGo,List<String> GO,List<String> GOTime) throws IOException {

		double simsum = 0.0;
		for (String g1 : genes1) {
			for (String g2 : genes2) {
				simsum += genePairSimilarity(g1, g2, ALLGene, AllGo,GO,GOTime);
			}
		}
		System.out.println("Similarity:"
				+ s1
				+ ","
				+ s2
				+ ":"
				+ String.format("%1$.6f",
						simsum / (genes1.size() * genes2.size())));
		try {
			FileWriter writer = new FileWriter("D://genesSimilarityFFF1//"+s1+"--"+s2+".txt", true);
			writer.write(s1+ "	"+ s2+ "	"+ String.format("%1$.6f",
							simsum / (genes1.size() * genes2.size()))+"\n");
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static List<String> readAllGene(File f) throws IOException {
		List<String> r = new ArrayList<String>();
		if (f.isFile() && f.exists()) {
			InputStreamReader reader = new InputStreamReader(
					new FileInputStream(f));
			BufferedReader bufferR = new BufferedReader(reader);
			String lineTxt = "";
			while ((lineTxt = bufferR.readLine()) != null) {
				String[] out = cutString(lineTxt);
				String in = out[0];
				r.add(in);
			}
		}
		return r;
	}

	public static List<String> readAllGo(File f) throws IOException {
		List<String> r = new ArrayList<String>();
		if (f.isFile() && f.exists()) {
			InputStreamReader reader = new InputStreamReader(
					new FileInputStream(f));
			BufferedReader bufferR = new BufferedReader(reader);
			// String GoTermId = null;
			String lineTxt = "";
			while ((lineTxt = bufferR.readLine()) != null) {
				String[] out = cutString(lineTxt);
				String in = out[1];
				r.add(in);
			}
		}
		return r;
	}

	public static LinkedList<String> Disease1() throws IOException {
		LinkedList<String> r = new LinkedList<String>();
		File f = new File("GeneSet//HPOName.txt");
		if (f.isFile() && f.exists()) {
			InputStreamReader reader = new InputStreamReader(
					new FileInputStream(f));
			BufferedReader bufferR = new BufferedReader(reader);
			// String GoTermId = null;
			String lineTxt = "";
			while ((lineTxt = bufferR.readLine()) != null) {
				String[] out = cutString(lineTxt);
				String in = out[0];
				r.add(in);
			}
		}
		return r;
	}

	public static LinkedList<String> Disease2() throws IOException {
		LinkedList<String> r = new LinkedList<String>();
		File f = new File("GeneSet//HPOName.txt");
		if (f.isFile() && f.exists()) {
			InputStreamReader reader = new InputStreamReader(
					new FileInputStream(f));
			BufferedReader bufferR = new BufferedReader(reader);
			// String GoTermId = null;
			String lineTxt = "";
			while ((lineTxt = bufferR.readLine()) != null) {
				String[] out = cutString(lineTxt);
				String in = out[1];
				r.add(in);
			}
		}
		return r;
	}

	public static void main(String[] args) throws Exception{
		File file = new File("E:\\开题\\gene_association.goa_human\\FFF.txt");
		File file2 = new File("E:\\开题\\gene_association.goa_human\\fgo.txt");
		List<String> AllGene = readAllGene(file);
		List<String> AllGo = readAllGo(file);
		List<String> GO = readAllGene(file2);
		List<String> GOTime = readAllGo(file2);
		LinkedList<String> disease1 = Disease1();
		LinkedList<String> disease2 = Disease2();
//		List<String> dis1 = Collections.synchronizedList(disease1);
//		List<String> dis2 = Collections.synchronizedList(disease2);
//		for (int i = 0; i < disease1.size(); i++) {
//			HashSet<String> genes1 = getAllGenes(disease1.get(i) + ".txt");
//			HashSet<String> genes2 = getAllGenes(disease2.get(i) + ".txt");
//			diseaseSimilarity(disease1.get(i), disease2.get(i), genes1, genes2,
//					AllGene, AllGo,GO,GOTime);
//		}
//		for(int i =0;i<disease2.size();i++)
//			System.out.println(i+"###"+disease2.get(i));
		
		 for(int i =0;i<20;i++){
			 new Thread(new MultiThread(disease1,disease2, AllGene, AllGo, GO, GOTime)).start();
		 }
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值