Use BioJava with "Maven" Build Tool

Q

How to Use BioJava with "Maven" Build Tool?

✍: FYIcenter.com

A

If you want to install one or more BioJava Libraries with Maven, you can include BioJava libraries as dependencies in the Maven project file as shown in this tutorial.

1. Make sure that you have JDK installed.

fyicenter$ javac -version

javac 17.0.1

2. Make sure that you have Maven installed.

fyicenter$ mvn -v

Apache Maven 3.8.7 (b89d5959fcde851dcb1c8946a785a163f14e1e29)
Maven home: /home/center/apache-maven-3.8.7
Java version: 17.0.1, vendor: Red Hat, Inc., 
Default locale: en_CA, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-193.el8.x86_64", arch: "amd64", family: "unix"

3. Create a Maven project folder and the pom.xml file.

fyicenter$ mkdir bio
fyicenter$ cd bio

fyicenter$ vi pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.fyicenter</groupId>
  <artifactId>bio</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>BioJava Test</name>
  <url>http://jar.fyicenter.com</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.biojava</groupId>
      <artifactId>biojava-core</artifactId>
      <version>6.1.0</version>
    </dependency>
  </dependencies>
</project>

4. Create BioJava test program, FastaReaderTeat.java.

fyicenter$ vi src/main/java/com/fyicenter/FastaReaderTeat.java

package com.fyicenter;
import java.io.File;
import java.io.InputStream;
import java.util.LinkedHashMap;
import org.biojava.nbio.core.util.InputStreamProvider;
import org.biojava.nbio.core.sequence.io.FastaReader;
import org.biojava.nbio.core.sequence.io.GenericFastaHeaderParser;
import org.biojava.nbio.core.sequence.io.ProteinSequenceCreator;
import org.biojava.nbio.core.sequence.ProteinSequence;
import org.biojava.nbio.core.sequence.compound.AminoAcidCompound;
import org.biojava.nbio.core.sequence.compound.AminoAcidCompoundSet;

public class FastaReaderTeat { 
  public static void main(String[] args) {
    if ( args.length < 1) {
      System.err.println("First argument needs to be path to fasta file");
      return;
    }

    File f = new File(args[0]);
    if ( ! f.exists()) {
      System.err.println("File does not exist " + args[0]);
      return;  
    }

    try {
      // automatically uncompresses files using InputStreamProvider
      InputStreamProvider isp = new InputStreamProvider();
      InputStream inStream = isp.getInputStream(f);
      FastaReader<ProteinSequence, AminoAcidCompound> fastaReader 
        = new FastaReader<ProteinSequence, AminoAcidCompound>(
          inStream,
          new GenericFastaHeaderParser<ProteinSequence, AminoAcidCompound>(),
          new ProteinSequenceCreator(AminoAcidCompoundSet.getAminoAcidCompoundSet()));                
      LinkedHashMap<String, ProteinSequence> b;

      int nrSeq = 0;
      while ((b = fastaReader.process(10)) != null) {
        for (String key : b.keySet()) {
          nrSeq++;
          System.out.println(nrSeq + " : " + key + " " + b.get(key));
        }
      }

    } catch (Exception ex) {
      System.out.println(ex);
    }
  }
}

5. Build the Maven project.

fyicenter$ ../apache-maven-3.8.7/bin/mvn package

...
[INFO] Building jar: /home/fyicenter/packages/java/bio/target/bio-1.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

6. Run the test program.

fyicenter$ export Core_JAR=/home/fyicenter/.m2/repository/org/biojava/biojava-core/6.1.0/biojava-core-6.1.0.jar

fyicenter$ java -cp target/bio-1.0.jar:$Core_JAR com.fyicenter.FastaReaderTeat

First argument needs to be path to fasta file

As you can see, the FastaReaderTeat.java program is working. See the next tutorial on how to use it to read a FASTA file.

 

Read FASTA File with FastaReaderTeat.java

BioJava Library Installation Options

BioJava - Java Bioinformatics Toolkit

⇑⇑ OBF (Open Bioinformatics Foundation) Tools

2023-04-25, 308🔥, 0💬