Skip to content

Commit

Permalink
Merge branch 'release/v1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
dbolotin committed Dec 29, 2015
2 parents 3d2db30 + 2951b86 commit 008c73a
Show file tree
Hide file tree
Showing 7 changed files with 451 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

MiTools 1.2 (29 Dec 2015)
========================

-- Handling of -v option in JCommanderBasedMain


MiTools 1.1.2 ( 6 Oct 2015)
========================

Expand Down
37 changes: 36 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<groupId>com.milaboratory</groupId>
<artifactId>mitools</artifactId>
<version>1.1.2</version>
<version>1.2</version>
<packaging>jar</packaging>
<name>MiTools</name>
<url>http://milaboratory.com/</url>
Expand Down Expand Up @@ -194,6 +194,41 @@

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>create-buildnumber</id>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
<execution>
<id>create-metadata</id>
<phase>generate-resources</phase>
<goals>
<goal>create-metadata</goal>
</goals>
<configuration>
<properties>
<branch>${scmBranch}</branch>
</properties>
</configuration>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<attach>true</attach>
<outputName>${project.artifactId}-build.properties</outputName>
<shortRevisionLength>7</shortRevisionLength>
<addOutputDirectoryToResources>true</addOutputDirectoryToResources>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public abstract class ActionParameters {
@Parameter(names = {"-h", "--help"}, help = true, description = "Displays help for this command.")
public Boolean help;
public Boolean help = false;

public boolean help() {
return help != null && help;
Expand Down
173 changes: 173 additions & 0 deletions src/main/java/com/milaboratory/mitools/cli/CheckAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* Copyright 2015 MiLaboratory.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.milaboratory.mitools.cli;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters;
import com.milaboratory.core.io.sequence.PairedRead;
import com.milaboratory.core.io.sequence.SingleRead;
import com.milaboratory.core.io.sequence.fastq.FastqRecordsReader;
import com.milaboratory.core.io.sequence.fastq.PairedFastqWriter;
import com.milaboratory.core.io.sequence.fastq.QualityFormat;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

public class CheckAction implements Action {
final AParameters params = new AParameters();

@Override
public void go(ActionHelper helper) throws Exception {
Pattern r1Pattern = params.getHeaderPatternR1();
Pattern r2Pattern = params.getHeaderPatternR2();

long missedR1 = 0, missedR2 = 0, skippedR1 = 0, skippedR2 = 0, mismatchedHeaders = 0, correct = 0;

try (FastqRecordsReader reader1 = new FastqRecordsReader(false,
getInputStream(params.getInputR1()), 524288, false, true);
FastqRecordsReader reader2 = new FastqRecordsReader(false,
getInputStream(params.getInputR2()), 524288, false, true);
PairedFastqWriter writer = params.doOutput() ?
new PairedFastqWriter(params.getOutputR1(), params.getOutputR2()) :
null
) {
OUTER:
while (true) {
boolean r1b = reader1.nextRecord(true);
boolean r2b = reader2.nextRecord(true);

if (!r1b && !r2b)
break;

if (!r1b) {
++missedR1;
continue;
}

if (!r2b) {
++missedR2;
continue;
}

SingleRead r1 = reader1.createRead(0, QualityFormat.Phred33);
SingleRead r2 = reader2.createRead(0, QualityFormat.Phred33);

Matcher matcherR1 = r1Pattern.matcher(r1.getDescription());
Matcher matcherR2 = r2Pattern.matcher(r2.getDescription());

if (!matcherR1.matches()) {
++skippedR1;
continue;
}

if (!matcherR2.matches()) {
++skippedR2;
continue;
}

for (int i = 0; i < matcherR1.groupCount(); i++) {
if (!matcherR1.group(i + 1).equals(matcherR2.group(i + 1))) {
mismatchedHeaders++;
continue OUTER;
}
}

++correct;

if (writer != null)
writer.write(new PairedRead(r1, r2));
}
}

System.out.println("Missed R1: " + missedR1);
System.out.println("Missed R2: " + missedR2);

System.out.println("Skipped R1: " + skippedR1);
System.out.println("Skipped R1: " + skippedR2);

System.out.println("Mismatched header: " + mismatchedHeaders);

System.out.println("Correct reads: " + correct);
}

public InputStream getInputStream(String fileName) throws IOException {
if (fileName.endsWith(".gz"))
return new GZIPInputStream(new BufferedInputStream(new FileInputStream(fileName), 524288));
else
return new BufferedInputStream(new FileInputStream(fileName), 524288);
}

@Override
public String command() {
return "check";
}

@Override
public ActionParameters params() {
return params;
}

@Parameters(commandDescription = "Checks paired-end read file for correctness and fixes it if possible.", optionPrefixes = "-")
public static final class AParameters extends ActionParameters {
@Parameter(description = "input_file_R1.fastq[.gz] input_file_R2.fastq[.gz] " +
"[fixed_output_file_R1.fastq[.gz] fixed_output_file_R2.fastq[.gz]]",
variableArity = true)
public List<String> parameters = new ArrayList<>();

@Parameter(description = "Header pattern.",
names = {"-d", "--header-pattern"})
String headerStructure = "^(.*)$";

public Pattern getHeaderPatternR1() {
return Pattern.compile(headerStructure);
}

public Pattern getHeaderPatternR2() {
return Pattern.compile(headerStructure);
}

public boolean doOutput() {
return parameters.size() == 4;
}

String getInputR1() {
return parameters.get(0);
}

String getInputR2() {
return parameters.get(1);
}

String getOutputR1() {
return parameters.get(2);
}

String getOutputR2() {
return parameters.get(3);
}

@Override
public void validate() {
if (parameters.size() != 2 && parameters.size() != 4)
throw new ParameterException("Wrong number of parameters.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class JCommanderBasedMain implements ActionHelper {
protected final String command;
protected boolean printHelpOnError = false;
protected boolean printStackTrace = false;
protected Runnable versionInfoCallback = null;
protected PrintStream outputStream = System.err;
protected String[] arguments;

Expand Down Expand Up @@ -81,7 +82,7 @@ public void main(String... args) throws Exception {
}

// Setting up JCommander
MainParameters mainParameters = new MainParameters();
MainParameters mainParameters = getMainParameters();
JCommander commander = new JCommander(mainParameters);
commander.setProgramName(command);
for (Action a : actions.values())
Expand All @@ -99,6 +100,13 @@ public void main(String... args) throws Exception {
} else {
commander.parse(args);

// Print Version information if requested and exit.
if (mainParameters instanceof MainParametersWithVersion &&
((MainParametersWithVersion) mainParameters).version()) {
versionInfoCallback.run();
return;
}

// Print complete help if requested
if (mainParameters.help()) {
// Creating new instance of jCommander to add only non-hidden actions
Expand Down Expand Up @@ -141,9 +149,15 @@ public void main(String... args) throws Exception {
}
}

private MainParameters getMainParameters() {
return versionInfoCallback != null ?
new MainParametersWithVersion() :
new MainParameters();
}

protected void printGlobalHelp() {
// Creating new instance of jCommander to add only non-hidden actions
JCommander tmpCommander = new JCommander(new MainParameters());
JCommander tmpCommander = new JCommander(getMainParameters());
tmpCommander.setProgramName(command);
for (Action a : actions.values())
if (!a.getClass().isAnnotationPresent(HiddenAction.class))
Expand Down Expand Up @@ -171,12 +185,35 @@ protected void printException(ParameterException e,
printActionHelp(commander, action);
}

public static final class MainParameters {
/**
* Enables -v / --version parameter.
*
* Sets callback that will be invoked if this option is specified by user.
*
* {@literal null} disables -v parameter.
*
* @param versionInfoCallback callback to be will be invoked if user specified -v option. {@literal null} disables
* -v parameter.
*/
public void setVersionInfoCallback(Runnable versionInfoCallback) {
this.versionInfoCallback = versionInfoCallback;
}

public static class MainParameters {
@Parameter(names = {"-h", "--help"}, help = true, description = "Displays this help message.")
public Boolean help;

public boolean help() {
return help != null && help;
}
}

public static class MainParametersWithVersion extends MainParameters {
@Parameter(names = {"-v", "--version"}, help = true, description = "Output version information.")
public Boolean version;

public boolean version() {
return version != null && version;
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/milaboratory/mitools/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public static void main(String[] args) throws Exception {
new MergeAction(),
new TrimAction(),
new RCAction(),
new RenameAction());
new RenameAction(),
new SplitAction(),
new CheckAction());
main.main(args);
}
}
Loading

0 comments on commit 008c73a

Please sign in to comment.