annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/clump/StreamToOutput.java @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 package clump;
jpayne@68 2
jpayne@68 3 import java.io.File;
jpayne@68 4 import java.io.IOException;
jpayne@68 5 import java.util.ArrayList;
jpayne@68 6
jpayne@68 7 import fileIO.FileFormat;
jpayne@68 8 import fileIO.ReadWrite;
jpayne@68 9 import sort.SortByName;
jpayne@68 10 import stream.ConcurrentReadInputStream;
jpayne@68 11 import stream.ConcurrentReadOutputStream;
jpayne@68 12 import stream.Read;
jpayne@68 13 import structures.ListNum;
jpayne@68 14
jpayne@68 15 public class StreamToOutput {
jpayne@68 16
jpayne@68 17 public StreamToOutput(FileFormat ffin1, FileFormat ffin2, ConcurrentReadOutputStream[] rosa_, KmerComparator old, boolean sortByName_, boolean incrementComparator){
jpayne@68 18 final ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(-1, false, ffin1, ffin2, null, null);
jpayne@68 19 cris.start();
jpayne@68 20 rosa=rosa_;
jpayne@68 21 kc=(incrementComparator ? new KmerComparator(old.k, old.seed+1, old.border-1, old.hashes, false, false) : old);
jpayne@68 22 sortByName=sortByName_;
jpayne@68 23 }
jpayne@68 24
jpayne@68 25 public StreamToOutput(ConcurrentReadInputStream cris_, ConcurrentReadOutputStream[] rosa_, KmerComparator old, boolean sortByName_, boolean incrementComparator){
jpayne@68 26 cris=cris_;
jpayne@68 27 rosa=rosa_;
jpayne@68 28 kc=(incrementComparator ? new KmerComparator(old.k, old.seed+1, old.border-1, old.hashes, false, false) : old);
jpayne@68 29 sortByName=sortByName_;
jpayne@68 30 }
jpayne@68 31
jpayne@68 32 public boolean process(){
jpayne@68 33 if(rosa==null || rosa.length==0){return errorState;}
jpayne@68 34
jpayne@68 35 File temp=null;
jpayne@68 36 if(sortByName){
jpayne@68 37 try {
jpayne@68 38 temp=File.createTempFile("temp_namesort_", ".fq.gz");
jpayne@68 39 } catch (IOException e) {
jpayne@68 40 // TODO Auto-generated catch block
jpayne@68 41 e.printStackTrace();
jpayne@68 42 }
jpayne@68 43 SortByName sbn=new SortByName(new String[] {"out="+temp.getAbsolutePath()});
jpayne@68 44 sbn.processInner(cris);
jpayne@68 45 FileFormat ff=FileFormat.testInput(temp.getAbsolutePath(), null, false);
jpayne@68 46 cris=ConcurrentReadInputStream.getReadInputStream(-1, false, ff, null, null, null);
jpayne@68 47 }
jpayne@68 48
jpayne@68 49 if(rosa.length==1){
jpayne@68 50 processSingle(cris);
jpayne@68 51 }else{
jpayne@68 52 processMulti(cris);
jpayne@68 53 }
jpayne@68 54
jpayne@68 55 errorState|=ReadWrite.closeStream(cris);
jpayne@68 56 if(temp!=null){
jpayne@68 57 temp.delete();
jpayne@68 58 }
jpayne@68 59 return errorState;
jpayne@68 60 }
jpayne@68 61
jpayne@68 62 public void processSingle(ConcurrentReadInputStream cris){
jpayne@68 63
jpayne@68 64 ListNum<Read> ln=cris.nextList();
jpayne@68 65 ArrayList<Read> reads=(ln!=null ? ln.list : null);
jpayne@68 66
jpayne@68 67 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
jpayne@68 68 if(rosa!=null){rosa[0].add(reads, ln.id);}
jpayne@68 69
jpayne@68 70 for(Read r : reads){
jpayne@68 71 readsIn+=r.pairCount();
jpayne@68 72 basesIn+=r.pairLength();
jpayne@68 73 }
jpayne@68 74
jpayne@68 75 cris.returnList(ln);
jpayne@68 76
jpayne@68 77 ln=cris.nextList();
jpayne@68 78 reads=(ln!=null ? ln.list : null);
jpayne@68 79 }
jpayne@68 80
jpayne@68 81 if(ln!=null){
jpayne@68 82 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
jpayne@68 83 }
jpayne@68 84 }
jpayne@68 85
jpayne@68 86 public void processMulti(ConcurrentReadInputStream cris){
jpayne@68 87 final int groups=rosa.length;
jpayne@68 88
jpayne@68 89 @SuppressWarnings("unchecked")
jpayne@68 90 final ArrayList<Read>[] out=new ArrayList[groups];
jpayne@68 91 for(int i=0; i<out.length; i++){
jpayne@68 92 out[i]=new ArrayList<Read>();
jpayne@68 93 }
jpayne@68 94
jpayne@68 95 ListNum<Read> ln=cris.nextList();
jpayne@68 96 ArrayList<Read> reads=(ln!=null ? ln.list : null);
jpayne@68 97
jpayne@68 98 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
jpayne@68 99 for(Read r : reads){
jpayne@68 100 long kmer=kc.hash(r, null, 0, false);
jpayne@68 101 int group=(int)(kmer%groups);
jpayne@68 102 out[group].add(r);
jpayne@68 103
jpayne@68 104 readsIn+=r.pairCount();
jpayne@68 105 basesIn+=r.pairLength();
jpayne@68 106 }
jpayne@68 107 for(int group=0; group<groups; group++){
jpayne@68 108 rosa[group].add(out[group], ln.id);
jpayne@68 109 out[group]=new ArrayList<Read>();
jpayne@68 110 }
jpayne@68 111
jpayne@68 112 cris.returnList(ln);
jpayne@68 113
jpayne@68 114 ln=cris.nextList();
jpayne@68 115 reads=(ln!=null ? ln.list : null);
jpayne@68 116 }
jpayne@68 117
jpayne@68 118 if(ln!=null){
jpayne@68 119 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
jpayne@68 120 }
jpayne@68 121 }
jpayne@68 122
jpayne@68 123 long readsIn=0;
jpayne@68 124 long basesIn=0;
jpayne@68 125
jpayne@68 126 // final FileFormat ffin1;
jpayne@68 127 // final FileFormat ffin2;
jpayne@68 128 ConcurrentReadInputStream cris;
jpayne@68 129 ConcurrentReadOutputStream[] rosa;
jpayne@68 130 final KmerComparator kc;
jpayne@68 131 final boolean sortByName;
jpayne@68 132
jpayne@68 133 boolean errorState=false;
jpayne@68 134
jpayne@68 135 }