Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
alexherbig committed Jul 26, 2017
1 parent 55820e0 commit d764983
Show file tree
Hide file tree
Showing 7 changed files with 1,703 additions and 0 deletions.
72 changes: 72 additions & 0 deletions FASTAParser.java
@@ -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));
}

}
48 changes: 48 additions & 0 deletions FASTAWriter.java
@@ -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();
}
}
43 changes: 43 additions & 0 deletions Gene.java
@@ -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;
}
}

0 comments on commit d764983

Please sign in to comment.