Hi All

I really need help in this. Im supposed to "extract" information from text files .

the text files looks somethings like this:

MODEL 1
ATOM 1 N SER A 253 -19.559 -25.512 -41.130 1.00 0.00 N
ATOM 2 CA SER A 253 -18.749 -26.500 -41.895 1.00 0.00 C
ATOM 3 C SER A 253 -17.587 -26.979 -41.032 1.00 0.00 C
ATOM 4 O SER A 253 -16.504 -27.273 -41.538 1.00 0.00 O
ATOM 5 CB SER A 253 -18.218 -25.843 -43.167 1.00 0.00 C
ATOM 6 OG SER A 253 -17.271 -24.841 -42.819 1.00 0.00 O
ATOM 7 H1 SER A 253 -18.928 -24.906 -40.567 1.00 0.00 H
ATOM 8 H2 SER A 253 -20.212 -26.016 -40.494 1.00 0.00 H
ENDMDL
MASTER 141 0 0 9 0 0 0 6 1137 1 0 12
END


Im supposed to extract the parts where the first word is ATOM and also when the last column is not H. Would be great if anyone could shed some light on this.Your help will be much appreciated!

hi stephen, so sorry, i forgot to tell you the 2nd part of the thing. I would need to put the output to a new txt file. Is it possible for me to do that if i use REGEX?

Using regular expressions you can check for patterns in your input.
Example:

String a = new String("1234");
if (a.matches("[0-9]+")) {
  System.out.println("String is a number");
}

In the above chunk of code the regex "[0-9]+" tests if the Given String is a number, you can construct a regular expression for your pattern along similar lines.
Example If I want to test for a String starting with "A" and ending with "N" the regex (regular expression) would be "[A][.]*[N]"

Use the regular expression to test if the input matches your pattern, if it does, then concatenate the data you wish to write to the file in another String variable (ex: dataToWrite).
Once you are done processing the input use File IO to dump the data in your dataToWrite variable into a File.

how do i go about checking for the last character of the line if I just want to discard the line that has H at the end of it and wants the other lines that contains C, O and N?

how do i go about checking for the last character of the line if I just want to discard the line that has H at the end of it and wants the other lines that contains C, O and N?

Thats the reason why I have added link to the regex tutorial, read it and construct your pattern according to what you wish to check, I already have given an example pattern which checks the Starting and ending character of a String.

Read your input file one line at a time into a String against which you check for the occurrence of your pattern (using the matches() method). If the match is successful concatenate this String, with a third string which contains the data you want to write to the new file.

ok, so what you mean is I use bufferedreader to read my file line by line then I use IF ELSE to check if it starts and ends with the words I want, then if it does I store them into an array and print them to the new file?

Yep, thats it.

As also said here. And, there was an old thread in which I had already said this, too. Stop starting new threads simply because you don't like that people are not telling you "yeah you're doing everything right, Java is just corked and so it's not working".

When i tried incorporating them, why is it that I dont get any output shown? sorry but I couldnt find the mistake. here's the code:

import java.io.*;

public class Extraction
{
	public static void main(String args [])
	{
		File dir = new File("1APB.pdb"); //set File
		String path = dir.getAbsolutePath();        //get file path
		int lala= path.lastIndexOf("\\");           //get index of '\'
        String howPath = path.substring(0,lala+1);  //extract directory name
        File usePath = new File (howPath);	        //assign directory for reading file
        String[] fileDir = usePath.list();          //store file in dir to array
        
        if(fileDir == null)                         //check if dir exists
        {
        	System.out.println("Directory Does Not Exist");
        }
        
        else
        {
        	for (int j=0; j<fileDir.length-2; j++)  //loop to run all files
            {
             String fileName = fileDir[j];
             readWriteTextFile(fileName);           //method call
            }
        
        }

	}

	public static void readWriteTextFile( String fileName )
	{
				
try {
	 BufferedReader in = new BufferedReader(new FileReader("fileName"));
	  
	  String str; 
	  while ((str = in.readLine()) != null) {
	  String a = new String("fileName");

      if (a.matches("^[ATOM][.]$[^H]")) 
      {

      System.out.println("String is a number");

      }	
	  	  }
	  	   in.close();
	  	    } catch (IOException e) { }
      }
   }

why is it that I dont get any output shown

Can you show your output?

String a = new String("fileName");
 
      if (a.matches("^[ATOM][.]$[^H]"))

What do these two statements do?

$ matches end of String so how can you have the H after the end of String. And, of course, you want to match to the read String ("str") not to some bogus string "filename". What was that suppossed to achieve?

Also, why not startsWith and endsWith as already suggested (in a few of your other threads on this)?

if (str.startsWith("ATOM") && !str.endsWith("H")) {

will give all lines that start with ATOM and do not end with H. Is that not what you wanted?

P.S. Actually posting the code you try (as you finally did) rather than simply whining that it doesn't work, or doggedly pursuing your original path (which you'd already been told won't work), allows us to finally help you fix, and allows you to finally advance.

NormR1 the line string(fileName) is to read the file that I would wana filter for the information. Correct right??

masijade, would like to ask you bcuz im not very familiar with the method that you suggested.if (str.startsWith("ATOM") && !str.endsWith("H")) so if that line matches the conditinos, i use readline() to show them as my output? or do i have to store them into the array?

What do you want to do? Do you simply want to display them, or do you wish to process the data in some way?

im supposed to take the lines that meet the criteria, show them and print them onto a new txt file that i need to create.im not very sure how to show the output using system.out.println

Simply show the lines that match. If you need to write them to a new file, then you also, of course, need a FileWriter.

hi masijade Im nearly there..when i tried to print the information I want into the new file...why is it that only the last line is printed? the following is my code:

import java.io.*;

public class Extraction
{
      public static void main( String[] args )
      {
     File dir = new File("1APB.pdb"); 					//set file
     String path = dir.getAbsolutePath();				//get file path
     int lala = path.lastIndexOf("\\");					//get index of '\'
     String howPath = path.substring(0,lala+1);			//extract directory name
     File usePath = new File(howPath);					//assign directory for reading file
     String[] fileDir = usePath.list();					//store file in dit to array
     if(fileDir == null) 								//check if dir exists
     {
     	System.out.println("Directory Does Not Exist");
     }
     else
     {
     	for(int j=0; j<fileDir.length-2;j++)
     	{
     		String fileName = fileDir[j];
      try 
      	{ 		
       	BufferedReader in = new BufferedReader(new FileReader(fileDir[j]));
      	String str;

      		while ((str = in.readLine()) != null) 
      		{
      		if (str.startsWith("ATOM") && !str.endsWith("H"))
      			{
      			System.out.print(str);
      			
      	FileWriter outStream = new FileWriter ("I:\\Proteins\\ATOM" +fileName);
      	outStream.write(str);
      	outStream.close();      			
      			}
      		}
		in.close();      		
   		} catch (IOException e) { }    
    
     	}

why is it that only the last line is printed?

Try some debugging. Print all the lines that are read with a prefix String and also print the lines the match with another prefix string so you see what is going on in the program.
System.out.println("read this" + str);
...
System.out.println("Matched this " + str);

Hi,

I wrote for you an entire function see below

public void extract(String str1)
{
String str2 = null;
File file1 = new File(str1);
FileReader fr = new FileReader(file1);  
BufferedReader br = new BufferedReader(fr); 

while(br.readLine() != null)
{

str2 = br.readLine();

if(((str2.startsWith("ATOM") == true)) && (str2.endsWith("H") == false))
{
System.out.println(str2);
}

else
{
//do something if you want
}

}

}

The argument is the file location as a string

Richard

When you create a FileWriter you are creating a new file or overwriting an existing file (unless you use another constructor, but we are not going to get into that as it is not applicable here), as a look at the API docs would have clearly spelled out. So, create and close the FileWriter in the same places as the FileReader and simly write to it in the loop.

You really need to learn how to read the documentation, and then actually do that. You also really need to learn some investigation skills rather than hoping someone else will solve your problems for you. All of this has been fairly (when not really) simple problems with a little thought and investigation and all these threads of yours have taken days (actually weeks).

Hi,

I wrote for you an entire function see below

public void extract(String str1)
{
String str2 = null;
File file1 = new File(str1);
FileReader fr = new FileReader(file1);  
BufferedReader br = new BufferedReader(fr); 

while(br.readLine() != null)
{

str2 = br.readLine();

if(((str2.startsWith("ATOM") == true)) && (str2.endsWith("H") == false))
{
System.out.println(str2);
}

else
{
//do something if you want
}

}

}

The argument is the file location as a string

Richard

And how does this help?

And how does this help?

Run it and find out

I don't need to as I know it doesn't help. First, the OP is already past the part of reading the file and secondly (among others) using == true and == false is just plain braindead coding.

I don't need to as I know it doesn't help. First, the OP is already past the part of reading the file and secondly (among others) using == true and == false is just plain braindead coding.

Yes, i know that but i tried to dumb it down as the OP is a newbie, its better for him to
understand the basics, instead of myself showing off. We are here to help him not to show off.

Here we help people out not to show off our skills

Richard

That "code" doesn't even fit that "explanation".

That "code" doesn't even fit that "explanation".

I seriously have no idea what you are talking about anymore.

Lets just close the topic.

Richard

Anymore?

I made a mistake, did not see the output clearly it just shows the same thing as many times as the lines meets the IF conditions..Do i use for loop to show it? or do i need to use an array??

I've fixed the problem. The codes are fully working now. However when i open to view the text files the alignment seems to have changed alittle, is there a way for me to fix that? below are my codes:

import java.io.*;

public class extracttion
{
      public static void main( String[] args )
      {
     File dir = new File("2KK2.pdb"); 					//set file
     String path = dir.getAbsolutePath();				//get file path
     int lala = path.lastIndexOf("\\");					//get index of '\'
     String howPath = path.substring(0,lala+1);			//extract directory name
     File usePath = new File(howPath);					//assign directory for reading file
     String[] fileDir = usePath.list();					//store file in dit to array
     if(fileDir == null) 								//check if dir exists
     {
     	System.out.println("Directory Does Not Exist");
     }
     else
     {
     	for(int j=0; j<fileDir.length-2;j++)
     	{
     		String fileName = fileDir[j];
      try 
      	{ 		
       	BufferedReader in = new BufferedReader(new FileReader(fileDir[j]));
FileWriter outStream = new FileWriter ("C:\\Users\\Royston\\Desktop\\New Folder\\Atom" +fileName); 
     	String str;

      		while ((str = in.readLine()) != null) 
      		{
      		if (str.startsWith("ATOM") && !str.endsWith("H"))
      			{
      			System.out.print(str);
      			
      	      	outStream.write(str);
      			
      			}
      		}

      	outStream.close();
   		in.close();      		
   		} catch (IOException e) { }    
    
     	}
     }
      }
}

he alignment seems to have changed

Can you describe what happens and what you want to change?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.