Skip to content

Commit

Permalink
Version 0.32
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyBolger committed Oct 12, 2020
1 parent 5ca4f14 commit 95ee2ba
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 27 deletions.
1 change: 1 addition & 0 deletions MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Main-Class: org.usadellab.trimmomatic.Trimmomatic
12 changes: 12 additions & 0 deletions adapters/NexteraPE-PE.fa
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
>PrefixNX/1
AGATGTGTATAAGAGACAG
>PrefixNX/2
AGATGTGTATAAGAGACAG
>Trans1
TCGTCGGCAGCGTCAGATGTGTATAAGAGACAG
>Trans1_rc
CTGTCTCTTATACACATCTGACGCTGCCGACGA
>Trans2
GTCTCGTGGGCTCGGAGATGTGTATAAGAGACAG
>Trans2_rc
CTGTCTCTTATACACATCTCCGAGCCCACGAGAC
12 changes: 12 additions & 0 deletions adapters/TruSeq3-PE-2.fa
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
>PrefixPE/1
TACACTCTTTCCCTACACGACGCTCTTCCGATCT
>PrefixPE/2
GTGACTGGAGTTCAGACGTGTGCTCTTCCGATCT
>PE1
TACACTCTTTCCCTACACGACGCTCTTCCGATCT
>PE1_rc
AGATCGGAAGAGCGTCGTGTAGGGAAAGAGTGTA
>PE2
GTGACTGGAGTTCAGACGTGTGCTCTTCCGATCT
>PE2_rc
AGATCGGAAGAGCACACGTCTGAACTCCAGTCAC
4 changes: 2 additions & 2 deletions build.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project name="Trimmomatic" default="dist" basedir=".">
<property name="version" value="0.30"/>
<property name="version" value="0.32"/>

<property name="src" location="src" />
<property name="lib" location="lib" />
Expand Down Expand Up @@ -72,7 +72,7 @@
</zip>

<zip destfile="${dist}/Trimmomatic-Src-${version}.zip">
<zipfileset dir="." includes="src/**/*,adapters/**/*,distSrc/**/*,lib/**/*,build.xml,versionHistory.txt" prefix="trimmomatic-${version}/"/>
<zipfileset dir="." includes="src/**/*,adapters/**/*,distSrc/**/*,lib/**/*,build.xml,versionHistory.txt,MANIFEST.MF" prefix="trimmomatic-${version}/"/>
</zip>

</target>
Expand Down
14 changes: 13 additions & 1 deletion src/org/usadellab/trimmomatic/Trimmomatic.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
public class Trimmomatic
{

private static final int MAX_AUTO_THREADS=16;

public static int calcAutoThreadCount()
{
int cpus=Runtime.getRuntime().availableProcessors();

if(cpus>MAX_AUTO_THREADS)
return MAX_AUTO_THREADS;

return cpus;
}

/**
* @param args
*/
Expand Down Expand Up @@ -33,7 +45,7 @@ else if(mode.equals("SE"))
if(showUsage)
{
System.err.println("Usage: ");
System.err.println(" PE [-threads <threads>] [-phred33|-phred64] [-trimlog <trimLogFile>] <inputFile1> <inputFile2> <outputFile1P> <outputFile1U> <outputFile2P> <outputFile2U> <trimmer1>...");
System.err.println(" PE [-threads <threads>] [-phred33|-phred64] [-trimlog <trimLogFile>] [-basein <inputBase> | <inputFile1> <inputFile2>] [-baseout <outputBase> | <outputFile1P> <outputFile1U> <outputFile2P> <outputFile2U>] <trimmer1>...");
System.err.println(" or: ");
System.err.println(" SE [-threads <threads>] [-phred33|-phred64] [-trimlog <trimLogFile>] <inputFile> <outputFile> <trimmer1>...");
System.exit(1);
Expand Down
179 changes: 165 additions & 14 deletions src/org/usadellab/trimmomatic/TrimmomaticPE.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.usadellab.trimmomatic.trim.Trimmer;
import org.usadellab.trimmomatic.trim.TrimmerFactory;

public class TrimmomaticPE
public class TrimmomaticPE extends Trimmomatic
{

/**
Expand Down Expand Up @@ -253,6 +253,24 @@ public void process(File input1, File input2, File output1P, File output1U, File
FastqParser parser2 = new FastqParser(phredOffset);
parser2.parse(input2);

if(phredOffset==0)
{
int phred1=parser1.determinePhredOffset();
int phred2=parser2.determinePhredOffset();

if(phred1==phred2 && phred1!=0)
{
System.err.println("Quality encoding detected as phred"+phred1);
parser1.setPhredOffset(phred1);
parser2.setPhredOffset(phred1);
}
else
{
System.err.println("Error: Unable to detect quality encoding");
System.exit(1);
}
}

FastqSerializer serializer1P = new FastqSerializer();
serializer1P.open(output1P);

Expand Down Expand Up @@ -286,13 +304,92 @@ public void process(File input1, File input2, File output1P, File output1U, File
if (trimLogStream != null)
trimLogStream.close();
}

private static int getFileExtensionIndex(String str)
{
String extensions[]={".fq",".fastq",".txt",".gz",".bz2",".zip"};

String tmp=str;
boolean done=false;

while(!done)
{
done=true;
for(String ext: extensions)
{
if(tmp.endsWith(ext))
{
tmp=tmp.substring(0,tmp.length()-ext.length());
done=false;
}
}
}

return tmp.length();
}

private static String replaceLast(String str, String out, String in)
{
int idx1=str.lastIndexOf(out);
if(idx1==-1)
return null;

int idx2=idx1+out.length();

return str.substring(0,idx1)+in+str.substring(idx2);
}



private static File[] calculateTemplatedInput(String baseStr)
{
String translation[][]={{"_R1_","_R2_"},{"_f","_r"},{".f",".r"},{"_1","_2"},{".1",".2"}};

File fileBase=new File(baseStr);
File baseDir=fileBase.getParentFile();

String baseName=fileBase.getName();
int extSplit=getFileExtensionIndex(baseName);

String core=baseName.substring(0,extSplit);
String exts=baseName.substring(extSplit);

for(String pair[]: translation)
{
String tmp=replaceLast(core, pair[0], pair[1]);
if(tmp!=null)
return new File[] {fileBase, new File(baseDir, tmp+exts)};
}

return null;
}


private static File[] calculateTemplatedOutput(String baseStr)
{
File fileBase=new File(baseStr);
File baseDir=fileBase.getParentFile();

String baseName=fileBase.getName();
int extSplit=getFileExtensionIndex(baseName);

String core=baseName.substring(0,extSplit);
String exts=baseName.substring(extSplit);

return new File[] {new File(baseDir,core+"_1P"+exts),new File(baseDir,core+"_1U"+exts),new File(baseDir,core+"_2P"+exts),new File(baseDir,core+"_2U"+exts)};
}



public static boolean run(String[] args) throws IOException
{
int argIndex = 0;
int phredOffset = 64;
int threads = 1;
int phredOffset = 0;
int threads = 0;

String templateInput=null;
String templateOutput=null;

boolean badOption = false;

File trimLog = null;
Expand All @@ -313,29 +410,83 @@ else if (arg.equals("-trimlog"))
else
badOption = true;
}
else if (arg.equals("-basein"))
{
if (argIndex < args.length)
templateInput = args[argIndex++];
else
badOption = true;
}
else if (arg.equals("-baseout"))
{
if (argIndex < args.length)
templateOutput = args[argIndex++];
else
badOption = true;
}
else
{
System.err.println("Unknown option " + arg);
badOption = true;
}
}

if (args.length - argIndex < 7 || badOption)
int additionalArgs=1+(templateInput==null?2:0)+(templateOutput==null?4:0);

if (args.length - argIndex < additionalArgs || badOption)
return false;

System.err.print("TrimmomaticPE: Started with arguments:");
for (String arg : args)
System.err.print(" " + arg);
System.err.println();

if(threads==0)
{
threads=calcAutoThreadCount();
if(threads>1)
System.err.println("Multiple cores found: Using "+threads+" threads");
}

File input1 = new File(args[argIndex++]);
File input2 = new File(args[argIndex++]);

File output1P = new File(args[argIndex++]);
File output1U = new File(args[argIndex++]);

File output2P = new File(args[argIndex++]);
File output2U = new File(args[argIndex++]);
File inputs[],outputs[];

if(templateInput!=null)
{
inputs=calculateTemplatedInput(templateInput);
if(inputs==null)
{
System.err.println("Unable to determine input files from: "+templateInput);
System.exit(1);
}

System.out.println("Using templated Input files: "+inputs[0]+" "+inputs[1]);
}
else
{
inputs=new File[2];
inputs[0]=new File(args[argIndex++]);
inputs[1]=new File(args[argIndex++]);
}

if(templateOutput!=null)
{
outputs=calculateTemplatedOutput(templateOutput);
if(outputs==null)
{
System.err.println("Unable to determine output files from: "+templateInput);
System.exit(1);
}

System.out.println("Using templated Output files: "+outputs[0]+" "+outputs[1]+" "+outputs[2]+" "+outputs[3]);
}
else
{
outputs=new File[4];
outputs[0]=new File(args[argIndex++]);
outputs[1]=new File(args[argIndex++]);
outputs[2]=new File(args[argIndex++]);
outputs[3]=new File(args[argIndex++]);
}

TrimmerFactory fac = new TrimmerFactory();
Trimmer trimmers[] = new Trimmer[args.length - argIndex];
Expand All @@ -344,7 +495,7 @@ else if (arg.equals("-trimlog"))
trimmers[i] = fac.makeTrimmer(args[i + argIndex]);

TrimmomaticPE tm = new TrimmomaticPE();
tm.process(input1, input2, output1P, output1U, output2P, output2U, trimmers, phredOffset, trimLog, threads);
tm.process(inputs[0], inputs[1], outputs[0], outputs[1], outputs[2], outputs[3], trimmers, phredOffset, trimLog, threads);

System.err.println("TrimmomaticPE: Completed successfully");
return true;
Expand All @@ -355,7 +506,7 @@ public static void main(String[] args) throws IOException
if (!run(args))
{
System.err
.println("Usage: TrimmomaticPE [-threads <threads>] [-phred33|-phred64] [-trimlog <trimLogFile>] <inputFile1> <inputFile2> <outputFile1P> <outputFile1U> <outputFile2P> <outputFile2U> <trimmer1>...");
.println("Usage: TrimmomaticPE [-threads <threads>] [-phred33|-phred64] [-trimlog <trimLogFile>] [-basein <inputBase> | <inputFile1> <inputFile2>] [-baseout <outputBase> | <outputFile1P> <outputFile1U> <outputFile2P> <outputFile2U>] <trimmer1>...");
System.exit(1);
}
}
Expand Down
28 changes: 25 additions & 3 deletions src/org/usadellab/trimmomatic/TrimmomaticSE.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.usadellab.trimmomatic.trim.Trimmer;
import org.usadellab.trimmomatic.trim.TrimmerFactory;

public class TrimmomaticSE
public class TrimmomaticSE extends Trimmomatic
{

/**
Expand Down Expand Up @@ -191,6 +191,21 @@ public void process(File input, File output, Trimmer trimmers[], int phredOffset
FastqParser parser = new FastqParser(phredOffset);
parser.parse(input);

if(phredOffset==0)
{
int phred=parser.determinePhredOffset();
if(phred!=0)
{
System.err.println("Quality encoding detected as phred"+phred);
parser.setPhredOffset(phred);
}
else
{
System.err.println("Error: Unable to detect quality encoding");
System.exit(1);
}
}

FastqSerializer serializer = new FastqSerializer();
serializer.open(output);

Expand All @@ -212,8 +227,8 @@ public void process(File input, File output, Trimmer trimmers[], int phredOffset
public static boolean run(String[] args) throws IOException
{
int argIndex = 0;
int phredOffset = 64;
int threads = 1;
int phredOffset = 0;
int threads = 0;

boolean badOption = false;

Expand Down Expand Up @@ -250,6 +265,13 @@ else if (arg.equals("-trimlog"))
System.err.print(" " + arg);
System.err.println();

if(threads==0)
{
threads=calcAutoThreadCount();
System.err.println("Automatically using "+threads+" threads");
}


File input = new File(args[argIndex++]);
File output = new File(args[argIndex++]);

Expand Down
Loading

0 comments on commit 95ee2ba

Please sign in to comment.