0

I am using this code to download a fasta sequence file from the pdb website. The pdb id is the string protid.

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

public class Trialoffile
{

    public static void main(String[] args){

        InputStream url;

        String protID="2ly4";


        try{


                url = new URL("http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=FASTA&compression=NO&structureId="+protID).openStream();
                Scanner fasta = new Scanner(url);

                BufferedWriter bw= new BufferedWriter(new FileWriter(protID+".txt", true));




                 //output file is prepared.

                while(fasta.hasNextLine()){

                bw.write(fasta.nextLine()+"\n");

                }
                }





        catch (Exception e)
        {
            System.err.println("File input error on:"+protID);
        }
    }

}

I am not getting error but the file written is of 0 bytes. I tried downloading another file of a different format from the same site and had no issues.

user1954680
  • 23
  • 1
  • 5

3 Answers3

0

Don't forget to flush and close the writer after you are done!

bw.flush();
bw.close();
Katalyst
  • 471
  • 4
  • 10
0

Try closing the file at the end.

 while(fasta.hasNextLine()){

      bw.write(fasta.nextLine()+"\n");

}
bw.close();
MoveFast
  • 3,011
  • 2
  • 27
  • 53
0

After many years I got my answer for python users (which will probably help to Java too...):

protein_id = "6T1T"
url = 'https://www.rcsb.org/pdb/download/downloadFastaFiles.do?structureIdList=' + protein_id + '&compressionType=uncompressed'
response = requests.get(url)
fasta_text = response.text
Izik
  • 746
  • 1
  • 9
  • 25