2018-11-20
2018-11-20 本文已影响0人
少年英雄小猪熊
今天继续看pipeline,单独试blastn那个部分,发现需要把sampling_random.sh里的fastq-sample前面的./去掉,这样就生成了有内容的sample.fa。
运行起来独立出来的blastn代码是下面结果,见2018-11-20(附)
代码是这样
import java.io.*;
import java.util.*;
import java.text.*;
/* Running BLAST for genome sequences*/
public class BInterface
{
static String file;
static HashMap organisms;
static HashMap dico;
static int number;
static int [] tab_num;
static String [] tab_gi;
static String [] tab_name;
static String path;
static String res="";
public static void main(String [] args) throws IOException {
file="sample.fa"; // file = sample.fa
number=5; // m1=5
path="results/"; // path = results_11_07_2018_03:17:26/
tab_num=new int[number];
tab_gi=new String[number];
tab_name=new String[number];
organisms = new HashMap();
dico=new HashMap();
System.out.println("before try in main");
try{
System.out.println("before init() in try of main");
init();
System.out.println("after init() in try of main and enter run()");
run();
System.out.println("after run() in try of main");
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
public static void init() throws IOException{
// BufferedReader in = new BufferedReader(new FileReader("external_scripts/dict_genomes.txt"));
System.out.println("before./external_scripts/dict_genomes.txt");
BufferedReader in = new BufferedReader(new FileReader(".//external_scripts/dict_genomes.txt"));
System.out.println("after./external_scripts/dict_genomes.txt");
String l="";
while((l = in.readLine()) != null) {
String [] seq=l.split(";");
dico.put(seq[0],seq[1]);
System.out.println("seq[0]="+seq[0]+";seq[1]="+seq[1]);
}
in.close();
}
public static void run(){
try {
Runtime runtime = Runtime.getRuntime();
final Process process;
System.out.println("before runtime.exec in run()");
// process= runtime.exec( "blastn -query "+file+" -db BLAST_db/ncbi.fa -num_threads 40 -outfmt 5 -evalue 1e-5 -num_alignments 10");
process= runtime.exec( "blastn -query "+file+" -db nt -outfmt 5 -evalue 1e-5 -num_alignments 10 -remote");
System.out.println("after runtime.exec in run()");
Thread thread=new Thread() {
public void run() {
try {
System.out.println("new reader");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
System.out.println("new genomeList.txt and genomeList.html");
PrintWriter out=new PrintWriter(new FileWriter(path+"genomeList.txt"));
PrintWriter out2=new PrintWriter(new FileWriter(path+"genomeList.html"));
System.out.println("");
out2.println("<!DOCTYPE html>");
out2.println("<html>");
out2.println("<head>");
out2.println(" <meta charset=\"utf-8\" />");
out2.println(" <title>Genome List</title>");
out2.println(" </head>");
out2.println(" <body>");
out2.println("<h1> Genome list</h1>");
out2.println("<ul>");
System.out.println("before try in run()");
String l = "";
try {
while((l = reader.readLine()) != null) {
System.out.println("*******************");
System.out.println("l="+l);
if((l.indexOf("<Hit_def>")!=-1)&&(!l.equals(""))){
String temp=l.substring(l.indexOf("|")+1,l.lastIndexOf("|")+1);
String gi=temp.substring(0,temp.indexOf("|"));
String name="";
if(l.lastIndexOf("chromosome")!=-1){
name=l.substring(l.lastIndexOf("|")+2,l.lastIndexOf("chromosome")-1);
System.out.println("1name="+name);
}else{
if(l.lastIndexOf(",")!=-1){
name=l.substring(l.lastIndexOf("|")+2,l.lastIndexOf(","));
}else{
name=l.substring(l.lastIndexOf("|")+2,l.length()-10);
}
System.out.println("2name="+name);
}
name=name.replaceAll(" ","-");
name=name.replaceAll("/","-");
System.out.println("3name="+name);
if(organisms.get(gi)==null){
organisms.put(gi,new Couple2(name));
}else{
Couple2 c=(Couple2)organisms.get(gi);
c.addNb();
organisms.put(gi,c);
}
}
}
int nb_reads=organisms.size();
Set cles = organisms.keySet();
Iterator it = cles.iterator();
while (it.hasNext()){
String cle = (String)it.next();
update(cle,(Couple2)organisms.get(cle));
}
for(int i=tab_num.length-1;i>=0;i--){
if(tab_gi[i]!=null){
String acc=(String)dico.get(tab_gi[i]);
res=res+acc+",";
String name=tab_name[i]+":"+acc;
out.println(acc+";"+name+";genome");
out2.println("<li>accession:<a target=_blank href=\"http://www.ncbi.nlm.nih.gov/nuccore/"+acc+"\"> "+acc+"</a> - "+name+" - "+tab_num[i]+" read matches </li>");
}
}
} finally {
process.getInputStream().close();
reader.close();
out.close();
out2.println("</ul>");
out2.println(" </body> ");
out2.println("</html>");
out2.close();
System.out.println("before res in run()");
System.out.println("1res="+res);
res=res.substring(0,res.length()-1);
System.out.println("2res="+res);
}
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
};
System.out.println("before thread.start()");
thread.start();
System.out.println("after thread.start()");
System.out.println("before thread.join()");
thread.join();
System.out.println("after thread.join()");
Thread t2=new Thread() {
public void run() {
try {
BufferedReader reader2 = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = "";
try {
while((line = reader2.readLine()) != null) {
System.out.println(line);
}
} finally {
reader2.close();
}
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
};
System.out.println("before t2.start()");
t2.start();
System.out.println("after t2.start()");
System.out.println("before t2.join()");
t2.join();
System.out.println("after t2.join()");
System.out.println("before process.waitFor()");
process.waitFor();
System.out.println("after process.waitFor()");
System.out.println("before process.getOutputStream().close();process.getErrorStream().close();");
process.getOutputStream().close();
process.getErrorStream().close();
System.out.println("after process.getOutputStream().close();process.getErrorStream().close();");
}catch (InterruptedException e) {
System.out.println("Thread was interrupted");
}
catch (IOException e) {
System.out.println("ERREUR BInterface");
}
}
static void update(String gi,Couple2 c){
int n=c.nb;
System.out.println("# update gi="+gi+";c.nb="+n);
int i=0;
while(i<number){
if(n>tab_num[i]){
/* permutation */
int tmp_n=tab_num[i];
String tmp_gi=tab_gi[i];
String tmp_name=tab_name[i];
tab_num[i]=n;
tab_gi[i]=gi;
tab_name[i]=c.name;
if(i!=0){
tab_num[i-1]=tmp_n;
tab_gi[i-1]=tmp_gi;
tab_name[i-1]=tmp_name;
}
}else{return;}
i++;
}
}
}
String gi=temp.substring(0,temp.indexOf("|"));
这句会报错字符串参数错误 java.lang.StringIndexOutOfBoundsException: String index out of range: -1
但是终端不显示,该try就在这结束了,然后终端看见的是finally里的res的错误。
因为readline的时候根本没取到要取的值,也就是temp是"",是空,所以temp.substring(0,-1)报错,temp要取的值根本不在if语句里那一行。