Click here to Skip to main content
16,004,977 members
Home / Discussions / Java
   

Java

 
General[Message Deleted] Pin
s_a_2002639-Oct-09 3:22
s_a_2002639-Oct-09 3:22 
GeneralRe: Array Subsequence Pin
Nagy Vilmos9-Oct-09 3:52
professionalNagy Vilmos9-Oct-09 3:52 
QuestionJava Intenet Jobs.... Pin
TGeist8-Oct-09 18:33
TGeist8-Oct-09 18:33 
AnswerRe: Java Intenet Jobs.... Pin
Richard MacCutchan8-Oct-09 22:49
mveRichard MacCutchan8-Oct-09 22:49 
AnswerRe: Java Intenet Jobs.... Pin
42774809-Oct-09 3:12
42774809-Oct-09 3:12 
GeneralRe: Java Intenet Jobs.... Pin
Richard MacCutchan9-Oct-09 4:31
mveRichard MacCutchan9-Oct-09 4:31 
GeneralRe: Java Intenet Jobs.... Pin
42774809-Oct-09 6:29
42774809-Oct-09 6:29 
QuestionParsing Text Files and Spliting on '>' Pin
meixiang68-Oct-09 14:09
meixiang68-Oct-09 14:09 
Hi can some kind person help with this JAVA Q on parsing text files:



INPUT TEXT (FASTA) FILE:
>AB485992 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
>AB485993 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
>AB485994 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
>AB485922 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
>AB485912 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
>AB485942 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT


I need JAVA Code that parses the input file and generates 6 files each file contains file is labelled after the '>' and before the space ie
file 1 will be named AB485992 and that file will contain the text between the '>' and the following '>' ie:
>AB485992 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT

file 2 will be labelled: AB485993 and it contents
>AB485993 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT

file 3 will be labelled: AB485993 and it contents
>AB485994 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT

and so on for all six files

So far I have code that parses out the word after '>' ie the file label and prints those to args file like below, can some kind person PLEASE show me how to parse the INPUT file and generate the 6 seperate files as described above each file labeled after the '>'
eg file1, labelled AB485992 and so on for the 6 files
>AB485992 some text
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT
ATTGGGAATGGAGGGAAATAAATGACTGGATGGTCGCTGCT


THANKS SO MUCH!


import java.io.*;
import java.util.Scanner;

public final class ReadWithScanner {

public static void main(String... aArgs) throws FileNotFoundException {
File f = new File("args");
f.delete();
ReadWithScanner parser = new ReadWithScanner("file.text");
parser.processLineByLine();

log("Done.");
}

public ReadWithScanner(String aFileName){
fFile = new File(aFileName);
}


public final void processLineByLine() throws FileNotFoundException {
Scanner scanner = new Scanner(fFile);
try {
while ( scanner.hasNextLine() ){
processLine( scanner.nextLine() );
}
}
finally {
scanner.close();
}
}

protected void processLine(String aLine){

Scanner scanner = new Scanner(aLine);
scanner.useDelimiter(" ");
String[] fields = aLine.split(" ");
String start = ">";

if ( scanner.hasNext() ){
String name = scanner.next();
if (name.startsWith(start)){
String valueNeeded = fields[0];
String d = valueNeeded.substring(1,valueNeeded.length());
writeToLog(d);
}
}
else {
log("Empty or invalid line. Unable to process.");
}
scanner.close();
}



private void writeToLog (String fileName) {
File f = new File("args");
FileOutputStream out;
PrintStream p;

try {
out = new FileOutputStream(f, true);
p = new PrintStream( out );
p.println(fileName);
System.out.println(fileName);
}
catch (Exception e) {
System.err.println ("Error writing to file");
}
}

// PRIVATE /
private final File fFile;

private static void log(Object aObject){
System.out.println(String.valueOf(aObject));
}

private String quote(String aText){
String QUOTE = "'";
return QUOTE + aText + QUOTE;
}
}
AnswerRe: Parsing Text Files and Spliting on '>' Pin
42774809-Oct-09 3:13
42774809-Oct-09 3:13 
GeneralRe: Parsing Text Files and Spliting on '>' Pin
meixiang69-Oct-09 4:22
meixiang69-Oct-09 4:22 
GeneralRe: Parsing Text Files and Spliting on '>' Pin
42774809-Oct-09 6:20
42774809-Oct-09 6:20 
GeneralRe: Parsing Text Files and Spliting on '>' Pin
meixiang69-Oct-09 8:56
meixiang69-Oct-09 8:56 
GeneralRe: Parsing Text Files and Spliting on '>' Pin
42774809-Oct-09 9:42
42774809-Oct-09 9:42 
GeneralRe: Parsing Text Files and Spliting on '>' Pin
meixiang69-Oct-09 9:56
meixiang69-Oct-09 9:56 
Question12 days of christmas Pin
Shah Ravi8-Oct-09 9:05
Shah Ravi8-Oct-09 9:05 
AnswerRe: 12 days of christmas Pin
Wes Aday8-Oct-09 9:51
professionalWes Aday8-Oct-09 9:51 
GeneralRe: 12 days of christmas Pin
Shah Ravi8-Oct-09 10:19
Shah Ravi8-Oct-09 10:19 
GeneralRe: 12 days of christmas Pin
David Skelly8-Oct-09 22:13
David Skelly8-Oct-09 22:13 
AnswerRe: 12 days of christmas Pin
42774809-Oct-09 3:16
42774809-Oct-09 3:16 
QuestionJ2METext Field Problem Pin
DarkSorrow388-Oct-09 4:56
DarkSorrow388-Oct-09 4:56 
AnswerRe: J2METext Field Problem Pin
42774809-Oct-09 3:20
42774809-Oct-09 3:20 
QuestionEpub-LRFTools [modified] Pin
sharkbc7-Oct-09 21:54
sharkbc7-Oct-09 21:54 
AnswerRe: Epub-LRFTools Pin
Nagy Vilmos7-Oct-09 22:04
professionalNagy Vilmos7-Oct-09 22:04 
GeneralRe: Epub-LRFTools Pin
sharkbc7-Oct-09 23:30
sharkbc7-Oct-09 23:30 
AnswerRe: Epub-LRFTools Pin
42774808-Oct-09 1:04
42774808-Oct-09 1:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.