Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55820e0
commit d764983
Showing
7 changed files
with
1,703 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Reads a multiple FASTA file containing DNA sequences in FASTA format | ||
* and stores the entries as FASTAEntry objects. | ||
* | ||
* @author Alexander Herbig | ||
* | ||
*/ | ||
public class FASTAParser | ||
{ | ||
/** | ||
* Reads a multiple FASTA file containing DNA sequences in FASTA format. | ||
* @param br the BufferedReader which reads from the multiple FASTA file | ||
* @return a list containing the resulting FASTAEntry objects | ||
* @throws Exception | ||
*/ | ||
public static Map<String,String> parseDNA(String filename) throws Exception | ||
{ | ||
BufferedReader br = new BufferedReader(new FileReader(filename)); | ||
|
||
Map<String,String> fastaEntries = new HashMap<String, String>(); | ||
|
||
String tmpID; | ||
StringBuffer tmpSeqString = new StringBuffer(); | ||
String tmpLine = br.readLine(); | ||
|
||
//jump to first header line | ||
while(tmpLine!=null&&(tmpLine.charAt(0)!='>' || tmpLine.length()==0)) | ||
{ | ||
tmpLine=br.readLine(); | ||
} | ||
tmpID=tmpLine; | ||
|
||
tmpLine=br.readLine(); | ||
while(tmpLine!=null) | ||
{ | ||
if(tmpLine.length()!=0) | ||
{ | ||
if(tmpLine.charAt(0)=='>') | ||
{ | ||
fastaEntries.put(toID(tmpID), tmpSeqString.toString()); | ||
|
||
tmpSeqString = new StringBuffer(); | ||
|
||
tmpID = tmpLine; | ||
} | ||
else | ||
{ | ||
tmpSeqString.append(tmpLine); | ||
} | ||
} | ||
tmpLine = br.readLine(); | ||
} | ||
fastaEntries.put(toID(tmpID), tmpSeqString.toString()); | ||
|
||
br.close(); | ||
|
||
return fastaEntries; | ||
} | ||
|
||
private static String toID(String fastaID) | ||
{ | ||
return(fastaID.substring(1)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
import java.io.BufferedWriter; | ||
|
||
/** | ||
* Writes a list of sequences to a file in FASTA format. | ||
* | ||
* @author Alexander Herbig | ||
* | ||
*/ | ||
public class FASTAWriter | ||
{ | ||
/** | ||
* Writes a list of sequences to a file in FASTA format. | ||
* | ||
* @param bw the BufferedWriter writing the FASTA file | ||
* @param fastaEntries list of FASTAEntry objects holding the sequences | ||
* which are written to the FASTA file | ||
* @throws Exception | ||
*/ | ||
public static void write(BufferedWriter bw, String genomeID, String sequence) throws Exception | ||
{ | ||
int charsInLine; | ||
String tmpSeqString; | ||
|
||
tmpSeqString=sequence; | ||
bw.write(">"+genomeID); | ||
bw.newLine(); | ||
|
||
charsInLine=0; | ||
for(int i=0; i<tmpSeqString.length();i++) | ||
{ | ||
bw.write(tmpSeqString.charAt(i)); | ||
charsInLine++; | ||
|
||
if(charsInLine==60) | ||
{ | ||
bw.newLine(); | ||
charsInLine=0; | ||
} | ||
} | ||
if(charsInLine!=0) | ||
{ | ||
bw.newLine(); | ||
} | ||
|
||
bw.flush(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* | ||
* @author Alexander Herbig | ||
* | ||
*/ | ||
public class Gene | ||
{ | ||
public String name; | ||
public String anno; | ||
|
||
public int start; | ||
public int end; | ||
public char strand; | ||
public String idAsParent; | ||
|
||
|
||
public Gene(String name, String anno, int start, int end, char strand, String idAsParent) { | ||
super(); | ||
this.name = name; | ||
this.anno = anno; | ||
this.start = start; | ||
this.end = end; | ||
this.strand = strand; | ||
this.idAsParent = idAsParent; | ||
} | ||
|
||
public Gene(String name, String anno) | ||
{ | ||
super(); | ||
this.name = name; | ||
this.anno = anno; | ||
} | ||
|
||
public Gene(String name, String anno, int length) | ||
{ | ||
this(name,anno); | ||
} | ||
|
||
public int length() | ||
{ | ||
return Math.abs(end-start)+1; | ||
} | ||
} |
Oops, something went wrong.