Showing posts with label Bio-Java. Show all posts
Showing posts with label Bio-Java. Show all posts

Escape analysis and lock coarsening in JAVA 6.0


The popularity of the Java programming language has made escape analysis a target of interest. Java's combination of heap-only object allocation, built-in threading, and the Sun HotSpot dynamic compiler creates a candidate platform for escape analysis related optimizations. Escape analysis is implemented in Java Standard Edition 6.


Example (Java)

class A {
  final int finalValue;
 
  public A( B b ) {
    super();
    b.doSomething( this ); // this escapes!
    finalValue = 23;
  }
 
  int getTheValue() {
    return finalValue;
  }
}
 
class B {
  void doSomething( A a ) {
    System.out.println( a.getTheValue() );
  }
}
In this example, the constructor for class A passes the new instance of A to B.doSomething. As a result, the instance of A—and all of its fields—escapes the scope of the constructor.

Java is able to manage multithreading at the language level. Multithreading is a technique that allows programs to operate faster on computer system that have multiple CPUs. Also, a multithreaded application has the ability to remain responsive to input, even when it is performing long running tasks.
However, programs that use multithreading need to take extra care of objects shared between threads, locking access to shared methods or blocks when they are used by one of the threads. Locking a block or an object is a time-consuming operation due to the nature of the underlying operating system-level operation involved .
As the Java library does not know which methods will be used by more than one thread, the standard library always locks blocks when necessary in a multithreaded environment.
Prior to Java 6, the virtual machine always locked objects and blocks when asked to by the program even if there was no risk of an object being modified by two different threads at the same time. For example, in this case, a local Vector was locked before each of the add operations to ensure that it would not be modified by other threads (Vector is synchronized), but because it is strictly local to the method this is not necessary:
public String getNames() {
     Vector v = new Vector();
     v.add("Me");
     v.add("You");
     v.add("Her");
     return v.toString();
}
Starting with Java 6, code blocks and objects are locked only when necessary , so in the above case, the virtual machine would not lock the Vector object at all

Share/Bookmark
How do I make a Sequence from a String or make a Sequence Object back into a String?

A lot of the time we see sequence represented as a String of characters eg "atgccgtggcatcgaggcatatagc". It's a convenient method for viewing and succinctly representing a more complex biological polymer. BioJava makes use of SymbolLists and Sequences to represent these biological polyners as Objects. Sequences extend SymbolLists and provide extra methods to store things like the name of the sequence and any features it might have but you can think of a Sequence as a SymbolList.
Within Sequence and SymbolList the polymer is not stored as a String. BioJava differentiates different polymer residues using Symbol objects that come from different Alphabets. In this way it is easy to tell if a sequence is DNA or RNA or something else and the 'A' symbol from DNA is not equal to the 'A' symbol from RNA. The details of Symbols, SymbolLists and Alphabets are covered here. The crucial part is there needs to be a way for a programmer to convert between the easily readable String and the BioJava Object and the reverse. To do this BioJava has Tokenizers that can read a String of text and parse it into a BioJava Sequence or SymbolList object. In the case of DNA, RNA and Protein you can do this with a single method call. The call is made to a static method from either DNATools, RNATools or ProteinTools.

String to SymbolList
import org.biojava.bio.seq.*;
import org.biojava.bio.symbol.*;

public class StringToSymbolList {
public static void main(String[] args) {

try {
//create a DNA SymbolList from a String
SymbolList dna = DNATools.createDNA("atcggtcggctta");

//create a RNA SymbolList from a String
SymbolList rna = RNATools.createRNA("auugccuacauaggc");

//create a Protein SymbolList from a String
SymbolList aa = ProteinTools.createProtein("AGFAVENDSA");
}
catch (IllegalSymbolException ex) {
//this will happen if you use a character in one of your strings that is
//not an accepted IUB Character for that Symbol.
ex.printStackTrace();
}

}
}

String to Sequence
import org.biojava.bio.seq.*;
import org.biojava.bio.symbol.*;

public class StringToSequence {
public static void main(String[] args) {

try {
//create a DNA sequence with the name dna_1
Sequence dna = DNATools.createDNASequence("atgctg", "dna_1");

//create an RNA sequence with the name rna_1
Sequence rna = RNATools.createRNASequence("augcug", "rna_1");

//create a Protein sequence with the name prot_1
Sequence prot = ProteinTools.createProteinSequence("AFHS", "prot_1");
}
catch (IllegalSymbolException ex) {
//an exception is thrown if you use a non IUB symbol
ex.printStackTrace();
}
}
}

SymbolList to String
You can call the seqString() method on either a SymbolList or a Sequence to get it's Stringified version.
import org.biojava.bio.symbol.*;

public class SymbolListToString {
public static void main(String[] args) {
SymbolList sl = null;
//code here to instantiate sl

//convert sl into a String
String s = sl.seqString();
}
}

Share/Bookmark
Writing your first Biojava program using NetBeans in 3 easy steps

If you are using Java for your bioinformatics work, you should consider using Biojava as it saves lot of time in writing codes.

Prerequisites:
NetBeans 6.5.1
Biojava library
Biojava support library

Step 1 Create a new Java Application in NetBeans.

Step 2 Open the projects windows, right-click Libraries and select "Add JAR/Folder..." and add the two jar files you have downloaded previously in the prerequisites section.

Step 3 Open Main.java and start writing your biojava code. A sample code for reading a PDB file and accessing its contents is shown below(Don't forget to click "Fix Imports" after copying this code to automatically add the required import statements).


String filename = "path/to/pdbfile.pdb" ;

PDBFileReader pdbreader = new PDBFileReader();

// the following parameters are optional:

//the parser can read the secondary structure
// assignment from the PDB file header and add it to the amino acids
pdbreader.setParseSecStruc(true);

// align the SEQRES and ATOM records, default = true
// slows the parsing speed slightly down, so if speed matters turn it off.
pdbreader.setAlignSeqRes(true);

// parse the C-alpha atoms only, default = false
pdbreader.setParseCAOnly(false);

// download missing PDB files automatically from EBI ftp server, default = false
pdbreader.setAutoFetch(false);

try{
Structure struc = pdbreader.getStructure(filename);

System.out.println(struc);

GroupIterator gi = new GroupIterator(struc);

while (gi.hasNext()){

Group g = (Group) gi.next();

if ( g instanceof AminoAcid ){
AminoAcid aa = (AminoAcid)g;
Map sec = aa.getSecStruc();
Chain c = g.getParent();
System.out.println(c.getName() + " " + g + " " + sec);
}
}

} catch (Exception e) {
e.printStackTrace();
}


From the structure object, you could get the atoms and do calculations on it.

The best place to start learning about the features of Biojava is the Biojava CookBook
Share/Bookmark

Share/Bookmark

Powered by  MyPagerank.Net

LinkWithin